Improve ftl localization scripts

This commit is contained in:
Tom Najdek 2024-06-21 14:54:16 +02:00
parent 3c6625f3cf
commit b9f0d26cee
No known key found for this signature in database
GPG key ID: EEC61A7B4C667D77
3 changed files with 17 additions and 13 deletions

View file

@ -49,21 +49,25 @@ async function getFTL() {
catch (e) { catch (e) {
// no local .ftl file // no local .ftl file
} }
let baseFTL;
let jsonFromEnUSFTL = {}; let jsonFromEnUSFTL = {};
try { try {
const enUSFtlPath = join(getLocaleDir('en-US'), sourceFileBaseName + '.ftl'); const enUSFtlPath = join(getLocaleDir('en-US'), sourceFileBaseName + '.ftl');
const ftl = await fs.readFile(enUSFtlPath, 'utf8'); baseFTL = await fs.readFile(enUSFtlPath, 'utf8');
jsonFromEnUSFTL = ftlToJSON(ftl); jsonFromEnUSFTL = ftlToJSON(baseFTL);
} }
catch (e) { catch (e) {
console.warn(`No en-US .ftl file for ${sourceFileBaseName}.`); throw new Error(`No en-US .ftl file for ${sourceFileBaseName}.ftl`);
} }
const mergedSourceJSON = { ...jsonFromEnUSFTL, ...jsonFromLocalFTL }; const mergedSourceJSON = { ...jsonFromEnUSFTL, ...jsonFromLocalFTL };
const sourceKeys = Object.keys(mergedSourceJSON); const sourceKeys = Object.keys(mergedSourceJSON);
const translated = new Map(); const translated = new Map();
for (let key of sourceKeys) { for (let key of sourceKeys) {
if (key in jsonFromTransifex) { // ignore empty messages from transifex
if (key in jsonFromTransifex && jsonFromTransifex[key]?.string) {
translated.set(key, jsonFromTransifex[key]); translated.set(key, jsonFromTransifex[key]);
} }
else { else {
@ -71,7 +75,7 @@ async function getFTL() {
} }
} }
const ftl = JSONToFtl(Object.fromEntries(translated)); const ftl = JSONToFtl(Object.fromEntries(translated), baseFTL);
const outFtlPath = join(getLocaleDir(locale), sourceFileBaseName + '.ftl'); const outFtlPath = join(getLocaleDir(locale), sourceFileBaseName + '.ftl');
await fs.outputFile(outFtlPath, ftl); await fs.outputFile(outFtlPath, ftl);
onProgress(`${locale}/${sourceFileBaseName}.ftl`, null, 'localize'); onProgress(`${locale}/${sourceFileBaseName}.ftl`, null, 'localize');

14
package-lock.json generated
View file

@ -40,7 +40,7 @@
"eslint-plugin-react": "^7.28.0", "eslint-plugin-react": "^7.28.0",
"eslint-plugin-react-hooks": "^4.0.4", "eslint-plugin-react-hooks": "^4.0.4",
"fs-extra": "^3.0.1", "fs-extra": "^3.0.1",
"ftl-tx": "^0.10.0", "ftl-tx": "^0.11.0",
"globby": "^6.1.0", "globby": "^6.1.0",
"jspath": "^0.4.0", "jspath": "^0.4.0",
"mocha": "^10.4.0", "mocha": "^10.4.0",
@ -3667,9 +3667,9 @@
} }
}, },
"node_modules/ftl-tx": { "node_modules/ftl-tx": {
"version": "0.10.0", "version": "0.11.0",
"resolved": "https://registry.npmjs.org/ftl-tx/-/ftl-tx-0.10.0.tgz", "resolved": "https://registry.npmjs.org/ftl-tx/-/ftl-tx-0.11.0.tgz",
"integrity": "sha512-MtUChEGb2vQzVtQvEkNnEPCLbYDWdGkt0ytcXL71wN/4HIBHAeaRO5Y1+MnnkFKxfm0aVHBbwONHyRmS9ewJ9g==", "integrity": "sha512-tkE5Im6Pr2FzHQT/ZLYfK4Rt48Zok/4WY2ZXc/xvmjbpQ+Wvslgvy9KjrgTJbv8295NmZbp8NMTQeijBpejabQ==",
"dev": true, "dev": true,
"dependencies": { "dependencies": {
"@fluent/syntax": "^0.19.0" "@fluent/syntax": "^0.19.0"
@ -10588,9 +10588,9 @@
} }
}, },
"ftl-tx": { "ftl-tx": {
"version": "0.10.0", "version": "0.11.0",
"resolved": "https://registry.npmjs.org/ftl-tx/-/ftl-tx-0.10.0.tgz", "resolved": "https://registry.npmjs.org/ftl-tx/-/ftl-tx-0.11.0.tgz",
"integrity": "sha512-MtUChEGb2vQzVtQvEkNnEPCLbYDWdGkt0ytcXL71wN/4HIBHAeaRO5Y1+MnnkFKxfm0aVHBbwONHyRmS9ewJ9g==", "integrity": "sha512-tkE5Im6Pr2FzHQT/ZLYfK4Rt48Zok/4WY2ZXc/xvmjbpQ+Wvslgvy9KjrgTJbv8295NmZbp8NMTQeijBpejabQ==",
"dev": true, "dev": true,
"requires": { "requires": {
"@fluent/syntax": "^0.19.0" "@fluent/syntax": "^0.19.0"

View file

@ -49,7 +49,7 @@
"eslint-plugin-react": "^7.28.0", "eslint-plugin-react": "^7.28.0",
"eslint-plugin-react-hooks": "^4.0.4", "eslint-plugin-react-hooks": "^4.0.4",
"fs-extra": "^3.0.1", "fs-extra": "^3.0.1",
"ftl-tx": "^0.10.0", "ftl-tx": "^0.11.0",
"globby": "^6.1.0", "globby": "^6.1.0",
"jspath": "^0.4.0", "jspath": "^0.4.0",
"mocha": "^10.4.0", "mocha": "^10.4.0",