'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();