Skip to content

Commit

Permalink
build(t9n): generate json file containing t9n values (#7214)
Browse files Browse the repository at this point in the history
**Related Issue:** #6143

## Summary

Generates `dist/extras/translations-json.json` which contains the t9n
values per component in the following format:

```jsonc
{
    "components": {
        "calcite-action": {
            "loading": {
                "en": "Close",
                "es": "Cerrar"
                // ...
            },
            "disabled": {
                "en": "Close",
                "es": "Cerrar"
                // ...
            }
        },
        "calcite-alert": {
            "loading": {
                "en": "Close",
                "es": "Cerrar"
               // ...
            },
            "disabled": {
                "en": "Close",
                "es": "Cerrar"
                // ...
            }
        }
    }
}
```
  • Loading branch information
benelan authored Jun 25, 2023
1 parent 3ec84a6 commit 11d93b2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/calcite-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"scripts": {
"build": "npm run util:prep-build-reqs && stencil build",
"postbuild": "npm run util:patch && git restore src/components/*/readme.md",
"postbuild": "npm run util:patch && npm run util:generate-t9n-docs-json && git restore src/components/*/readme.md",
"build:watch": "npm run util:prep-build-reqs && stencil build --no-docs --watch",
"build:watch-dev": "npm run util:prep-build-reqs && stencil build --no-docs --dev --watch",
"build-storybook": "npm run util:build-docs && build-storybook --output-dir ./docs --quiet",
Expand All @@ -43,6 +43,7 @@
"util:clean-tested-build": "npm ci && npm test && npm run build",
"util:copy-assets": "npm run util:copy-icons",
"util:copy-icons": "cpy \"../../node_modules/@esri/calcite-ui-icons/js/*.json\" \"./src/components/icon/assets/icon/\" --flat",
"util:generate-t9n-docs-json": "ts-node --esm support/generateT9nDocsJSON.ts",
"util:generate-t9n-types": "ts-node --esm support/generateT9nTypes.ts",
"util:hydration-styles": "ts-node --esm support/hydrationStyles.ts",
"util:patch": "npm run util:patch-esm-resolution && npm run util:patch-tree-shaking",
Expand Down
46 changes: 46 additions & 0 deletions packages/calcite-components/support/generateT9nDocsJSON.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// generates a JSON file containing the per component t9n translation values
(async () => {
const { dirname, resolve } = await import("path");
const { fileURLToPath } = await import("url");
const {
existsSync,
promises: { readFile, readdir, writeFile }
} = await import("fs");
try {
const __dirname = dirname(fileURLToPath(import.meta.url));

const outfile = resolve(__dirname, "..", "dist", "extras", "translations-json.json");
const assetsPaths = resolve(__dirname, "..", "dist", "calcite", "assets");
const components = await readdir(assetsPaths);

const data = {};
const messagesFilenameRegex = /messages_(.*)\.json/;

for (const component of components) {
const t9nPath = resolve(assetsPaths, component, "t9n");
if (existsSync(t9nPath)) {
data[component] = {};
const messagesFileMain = JSON.parse(await readFile(resolve(t9nPath, "messages.json"), { encoding: "utf-8" }));
Object.keys(messagesFileMain).forEach((key) => (data[component][key] = {}));

const messagesFilenames = (await readdir(t9nPath, { withFileTypes: true })).map((dirent) => dirent.name);
for (const messagesFilename of messagesFilenames) {
const messagesFilenameMatch = messagesFilename.match(messagesFilenameRegex);

if (messagesFilenameMatch && messagesFilenameMatch.length > 1) {
const lang = messagesFilenameMatch[1];
const messagesFile = JSON.parse(await readFile(resolve(t9nPath, messagesFilename), { encoding: "utf-8" }));

for (const [key, value] of Object.entries(messagesFile)) {
data[component][key][lang] = value;
}
}
}
}
}
await writeFile(outfile, JSON.stringify(data), "utf-8");
} catch (err) {
console.error(err);
process.exit(1);
}
})();

0 comments on commit 11d93b2

Please sign in to comment.