diff --git a/client/package.json b/client/package.json index 0770dd1df..6a2aea297 100644 --- a/client/package.json +++ b/client/package.json @@ -11,7 +11,7 @@ "post-build": "cp src/404.html dist/client/browser/404.html", "watch": "ng build --watch --configuration development", "extract-i18n": "ng extract-i18n --format json --output-path src/locales/ --out-file messages.fr.json", - "postextract-i18n": "node src/locales/sort-messages-fr-translations.js", + "postextract-i18n": "node src/locales/check-messages-translations.js", "test": "jest", "lint": "ng lint --fix", "lint:ci": "ng lint", diff --git a/client/src/locales/check-messages-translations.js b/client/src/locales/check-messages-translations.js new file mode 100644 index 000000000..a08a3d9d2 --- /dev/null +++ b/client/src/locales/check-messages-translations.js @@ -0,0 +1,57 @@ +const { readFile, writeFile } = require('node:fs/promises'); +const { join } = require('node:path'); + +const consolidateMessagesTranslations = async (fileName, addKeys = [], removeKeys = []) => { + const messagesPath = join(__dirname, fileName); + const messages = JSON.parse(await readFile(messagesPath, 'utf8')); + + addKeys.forEach((key) => (messages.translations[key] = '')); + removeKeys.forEach((key) => delete messages.translations[key]); + + const sortedTranslationEntries = Object.entries(messages.translations).sort(([a], [b]) => + a > b ? 1 : a < b ? -1 : 0, + ); + messages.translations = sortedTranslationEntries.reduce((translations, [sortedKey, value]) => { + translations[sortedKey] = value; + return translations; + }, {}); + + await writeFile(messagesPath, JSON.stringify(messages, undefined, 2) + '\n', 'utf8'); +}; + +const compareMessagesTranslations = async () => { + const frenchMessages = new Set( + Object.keys(JSON.parse(await readFile(join(__dirname, 'messages.fr.json'), 'utf8')).translations), + ); + const englishMessages = new Set( + Object.keys(JSON.parse(await readFile(join(__dirname, 'messages.en.json'), 'utf8')).translations), + ); + + const englishMissingKeys = []; + frenchMessages.forEach((key) => englishMessages.has(key) || englishMissingKeys.push(key)); + + const englishUselessKeys = []; + englishMessages.forEach((key) => frenchMessages.has(key) || englishUselessKeys.push(key)); + + if (englishMissingKeys.length || englishUselessKeys.length) { + console.warn( + '\nWarning:\n\tEnglish messages (messages.en.json) not aligned with French messages (messages.fr.json):', + ); + if (englishMissingKeys.length) { + console.warn('Missing keys:', englishMissingKeys); + } + if (englishUselessKeys.length) { + console.warn('Useless keys:', englishUselessKeys); + } + } + + await consolidateMessagesTranslations('messages.en.json', englishMissingKeys, englishUselessKeys); +}; + +const run = async () => { + await consolidateMessagesTranslations('messages.fr.json'); + + await compareMessagesTranslations(); +}; + +run(); diff --git a/client/src/locales/sort-messages-fr-translations.js b/client/src/locales/sort-messages-fr-translations.js deleted file mode 100644 index e0a6a1bb0..000000000 --- a/client/src/locales/sort-messages-fr-translations.js +++ /dev/null @@ -1,19 +0,0 @@ -const { readFile, writeFile } = require('node:fs/promises'); -const { join } = require('node:path'); - -const sortMessagesTranslations = async () => { - const messagesPath = join(__dirname, 'messages.fr.json'); - const messages = JSON.parse(await readFile(messagesPath, 'utf8')); - - const sortedTranslationEntries = Object.entries(messages.translations).sort(([a], [b]) => - a > b ? 1 : a < b ? -1 : 0, - ); - messages.translations = sortedTranslationEntries.reduce((translations, [sortedKey, value]) => { - translations[sortedKey] = value; - return translations; - }, {}); - - await writeFile(messagesPath, JSON.stringify(messages, undefined, 2) + '\n', 'utf8'); -}; - -sortMessagesTranslations();