From d701195ae56b09916e9b459fe1148e06a113de9f Mon Sep 17 00:00:00 2001 From: Anthony LC Date: Mon, 7 Oct 2024 15:31:30 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=92=BB(i18n)=20rebui?= =?UTF-8?q?ld=20translations=20for=20crowdin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/frontend/packages/i18n/package.json | 1 + .../packages/i18n/rebuild-translations.mjs | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/frontend/packages/i18n/rebuild-translations.mjs diff --git a/src/frontend/packages/i18n/package.json b/src/frontend/packages/i18n/package.json index 250469b4..86d1deb0 100644 --- a/src/frontend/packages/i18n/package.json +++ b/src/frontend/packages/i18n/package.json @@ -7,6 +7,7 @@ "extract-translation:impress": "yarn i18next ../../apps/impress/**/*.{ts,tsx} -c ./i18next-parser.config.mjs -o ./locales/impress/translations-crowdin.json", "format-deploy": "yarn format-deploy:impress", "format-deploy:impress": "node ./format-deploy.mjs --app=impress --output=../../apps/impress/src/i18n/translations.json", + "format-rebuild-fr:impress": "node ./rebuild-translations.mjs --language=fr --app=impress --output=../../apps/impress/src/i18n/translations.json", "lint": "eslint --ext .js,.ts,.mjs .", "test": "jest" }, diff --git a/src/frontend/packages/i18n/rebuild-translations.mjs b/src/frontend/packages/i18n/rebuild-translations.mjs new file mode 100644 index 00000000..80ff117e --- /dev/null +++ b/src/frontend/packages/i18n/rebuild-translations.mjs @@ -0,0 +1,50 @@ +/** + * 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. + * + * This script is used to rebuild the translations file from the empty skeleton file generated by Crowdin. + */ + +import fs from 'fs'; +import path from 'path'; + +import { hideBin } from 'yargs/helpers'; +import yargs from 'yargs/yargs'; + +// Get our args +const argv = yargs(hideBin(process.argv)).argv; +const { app, output, language } = argv; + +const folderPath = './locales/' + app; +const namefile = 'translations-skeleton.json'; +const nameRebuildfile = 'translations-rebuild.json'; + +const pathRebuildFile = path.join(folderPath, path.sep, nameRebuildfile); + +// Get the skeleton generated from crowdin +const pathSkeletonFile = path.join(folderPath, path.sep, namefile); + +if (!fs.existsSync(pathSkeletonFile)) { + throw new Error(`File ${pathSkeletonFile} not found!`); +} + +// Get the translated file +if (!fs.existsSync(output)) { + throw new Error(`File ${output} not found!`); +} + +const jsonSkel = JSON.parse(fs.readFileSync(pathSkeletonFile, 'utf8')); +const jsonTrans = JSON.parse(fs.readFileSync(output, 'utf8')); + +// Transform the json file to the format expected by i18next +const jsonRebuild = jsonSkel; +Object.keys(jsonSkel) + .sort() + .forEach((key) => { + jsonRebuild[key]['message'] = jsonTrans[language]['translation'][key] || ''; + }); + +// Write the file to the output +fs.writeFileSync(pathRebuildFile, JSON.stringify(jsonRebuild), 'utf8'); + +console.log(`${app} translations rebuild!`);