Skip to content

Commit

Permalink
🪟 🔧 Compile i18n messages at build time (#6481)
Browse files Browse the repository at this point in the history
  • Loading branch information
timroes committed May 10, 2023
1 parent a6ffc51 commit 3574723
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 39 deletions.
1 change: 1 addition & 0 deletions airbyte-webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"@babel/preset-env": "^7.20.2",
"@babel/preset-react": "^7.18.6",
"@babel/preset-typescript": "^7.21.0",
"@formatjs/icu-messageformat-parser": "^2.4.0",
"@modyfi/vite-plugin-yaml": "^1.0.4",
"@storybook/addon-actions": "^7.0.2",
"@storybook/addon-docs": "^7.0.2",
Expand Down
25 changes: 25 additions & 0 deletions airbyte-webapp/packages/vite-plugins/compile-formatjs-msgs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { Plugin } from "vite";

import { parse } from "@formatjs/icu-messageformat-parser";

export function compileFormatJsMessages(): Plugin {
return {
name: "airbyte/compile-formatjs-messages",
transform(code: string, id: string) {
// Transform all JSON files under /locales/
if (/\/locales\/.+\.json$/.test(id)) {
if (!code.startsWith("export default {") || !code.endsWith("};\n")) {
throw new Error(`Tried to compile JSON locale file, but got an unexpected format. (${id})`);
}
// Remove JS export, to extract raw JSON from that file
const rawJson = code.replace(/^export default {/, "{").replace(/\};\n$/, "}");
const messages = JSON.parse(rawJson);
// Parse every message using the formatjs utils into an AST
const compiledMsgs = Object.fromEntries(
Object.entries(messages).map(([key, i18nmsg]) => [key, parse(i18nmsg as string)])
);
return `export default ${JSON.stringify(compiledMsgs)};`;
}
},
};
}
3 changes: 3 additions & 0 deletions airbyte-webapp/packages/vite-plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export { docMiddleware } from "./doc-middleware";
export { buildInfo } from "./build-info";
export { environmentVariables } from "./environment-variables";
export { experimentOverwrites } from "./experiment-overwrites";
export { compileFormatJsMessages } from "./compile-formatjs-msgs";
Loading

0 comments on commit 3574723

Please sign in to comment.