Add i18n completion tool.

This commit is contained in:
Florian THIERRY
2024-09-21 12:20:22 +02:00
parent 3935f6ad21
commit b546a0cf01
8 changed files with 665 additions and 10 deletions

83
ci/i18n-completer/app.ts Normal file
View File

@@ -0,0 +1,83 @@
'use strict';
import fs from 'fs';
const englishI18nFilePath='../../frontend/src/locale/messages.json';
const frenchI18nFilePath='../../frontend/src/locale/messages-fr.json';
interface Translations {
[key: string]: string;
}
interface I18nFileContent {
locale: string;
translations: Translations;
};
interface Translation {
key: string;
value: string;
}
interface I18nTranslations {
locale: string;
translations: Translation[];
}
function readAndParseFile(filePath): I18nTranslations {
const fileContent = fs.readFileSync(filePath);
const parsedFileContent: I18nFileContent = JSON.parse(fileContent.toString());
return {
locale: parsedFileContent.locale,
translations: Object.keys(parsedFileContent.translations)
.map(translationKey => {
return {
key: translationKey,
value: parsedFileContent.translations[translationKey]
};
})
};
}
function findMatchingFrenchTranslation(englishTranslation: Translation, frenchI18nFileContent: I18nTranslations): Translation | undefined {
return frenchI18nFileContent.translations.find(frenchTranslation => frenchTranslation.key === englishTranslation.key);
}
function main(): void {
const englishI18nTranslations = readAndParseFile(englishI18nFilePath);
const frenchI18nTranslations = readAndParseFile(frenchI18nFilePath);
const frenchTranslations = englishI18nTranslations.translations
.map(englishTranslation => {
let result: Translation;
const matchingFrenchTranslation = findMatchingFrenchTranslation(englishTranslation, frenchI18nTranslations);
if (matchingFrenchTranslation?.value?.length) {
result = matchingFrenchTranslation;
} else {
result = {
key: englishTranslation.key,
value: `<À TRADUIRE> - ${englishTranslation.value}`
}
}
return result;
});
const newFrenchI18nTranslations: I18nTranslations = {
locale: 'fr-FR',
translations: frenchTranslations
};
const newFrenchTranslationFileContent: I18nFileContent = {
locale: newFrenchI18nTranslations.locale,
translations: newFrenchI18nTranslations.translations.reduce(
(result, translation) => Object.assign(result, { [translation.key]: translation.value }),
{}
)
}
fs.writeFileSync(frenchI18nFilePath, JSON.stringify(newFrenchTranslationFileContent));
}
main();