-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build(t9n): generate json file containing t9n values (#7214)
**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
Showing
2 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/calcite-components/support/generateT9nDocsJSON.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
})(); |