Skip to content

Commit d701195

Browse files
committed
πŸ§‘β€πŸ’»(i18n) rebuild translations for crowdin
For some unexpected reasons it can happen that the translations in Crowdin are lost. If that happens, we can rebuild the Crowdin translations file from our translated json file. "translations-skeleton.json" is the downloaded source file from Crowdin. It will generate "translations-rebuild.json", which can be uploaded directly to Crowdin.
1 parent ac18d23 commit d701195

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

β€Žsrc/frontend/packages/i18n/package.jsonβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"extract-translation:impress": "yarn i18next ../../apps/impress/**/*.{ts,tsx} -c ./i18next-parser.config.mjs -o ./locales/impress/translations-crowdin.json",
88
"format-deploy": "yarn format-deploy:impress",
99
"format-deploy:impress": "node ./format-deploy.mjs --app=impress --output=../../apps/impress/src/i18n/translations.json",
10+
"format-rebuild-fr:impress": "node ./rebuild-translations.mjs --language=fr --app=impress --output=../../apps/impress/src/i18n/translations.json",
1011
"lint": "eslint --ext .js,.ts,.mjs .",
1112
"test": "jest"
1213
},
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* For some unexpected reasons it can happen that the translations in Crowdin are lost.
3+
* If that happens, we can rebuild the Crowdin translations file from our translated json file.
4+
*
5+
* This script is used to rebuild the translations file from the empty skeleton file generated by Crowdin.
6+
*/
7+
8+
import fs from 'fs';
9+
import path from 'path';
10+
11+
import { hideBin } from 'yargs/helpers';
12+
import yargs from 'yargs/yargs';
13+
14+
// Get our args
15+
const argv = yargs(hideBin(process.argv)).argv;
16+
const { app, output, language } = argv;
17+
18+
const folderPath = './locales/' + app;
19+
const namefile = 'translations-skeleton.json';
20+
const nameRebuildfile = 'translations-rebuild.json';
21+
22+
const pathRebuildFile = path.join(folderPath, path.sep, nameRebuildfile);
23+
24+
// Get the skeleton generated from crowdin
25+
const pathSkeletonFile = path.join(folderPath, path.sep, namefile);
26+
27+
if (!fs.existsSync(pathSkeletonFile)) {
28+
throw new Error(`File ${pathSkeletonFile} not found!`);
29+
}
30+
31+
// Get the translated file
32+
if (!fs.existsSync(output)) {
33+
throw new Error(`File ${output} not found!`);
34+
}
35+
36+
const jsonSkel = JSON.parse(fs.readFileSync(pathSkeletonFile, 'utf8'));
37+
const jsonTrans = JSON.parse(fs.readFileSync(output, 'utf8'));
38+
39+
// Transform the json file to the format expected by i18next
40+
const jsonRebuild = jsonSkel;
41+
Object.keys(jsonSkel)
42+
.sort()
43+
.forEach((key) => {
44+
jsonRebuild[key]['message'] = jsonTrans[language]['translation'][key] || '';
45+
});
46+
47+
// Write the file to the output
48+
fs.writeFileSync(pathRebuildFile, JSON.stringify(jsonRebuild), 'utf8');
49+
50+
console.log(`${app} translations rebuild!`);

0 commit comments

Comments
Β (0)