gui/modules/desktop/scripts/update-translations.cjs
Neil a7a63ad030
Crowdin dynamic update (#268)
* #264 i18n dropdown ui

* #264 improve i18n configuration to use hardcoded english as default

* #264 sync language english to crowdin when merged to main

---------

Co-authored-by: neil <neil@neils-MacBook-Pro.local>
2023-03-07 11:12:11 +08:00

55 lines
1.3 KiB
JavaScript

const otaClient = require("@crowdin/ota-client");
const _ = require("lodash");
const fs = require("fs");
const path = require("path");
const defaultEnTranslation = require("../src/libs/translations/translations.json");
// this read only hash is from crowdin
const hash = "cf849610ca66250f0954379ct4t";
const client = new otaClient.default(hash);
async function main() {
console.log("getting latest translation!");
if (!process.env.SYNC_I18N) return;
const [languagesList, translationsRaw] = await Promise.all([
client.getLanguageObjects(),
client.getStrings()
]);
const lang = languagesList.reduce((map, lang) => {
map[lang.id] = lang.name;
return map;
}, {});
const translations = languagesList.reduce((map, langRaw) => {
map[langRaw.id] = {
lang
};
const translation = translationsRaw[langRaw.id];
for (const k in translation) {
const key = [langRaw.id, k].join(".");
_.set(map, key, translation[k]);
}
return map;
}, {});
const translationsPath = path.join(__dirname, "../src/libs/translations/translations.json");
defaultEnTranslation.en.lang = translations.en.lang;
await fs.writeFileSync(
translationsPath,
JSON.stringify(
{
...translations,
...defaultEnTranslation
},
null,
2
),
"utf-8"
);
}
main();