Skip to content

build(t9n): generate json file containing t9n values #7214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
49 changes: 49 additions & 0 deletions packages/calcite-components/support/generateT9nDocsJSON.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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" }));

for (const [key, value] of Object.entries(messagesFileMain)) {
data[component][key] = { main: value };
}

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);
}
})();