Skip to content

Commit

Permalink
client: improve "postextract-i18n" script (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
avine authored Jul 19, 2024
1 parent 5fbf50b commit 3538a88
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 20 deletions.
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
57 changes: 57 additions & 0 deletions client/src/locales/check-messages-translations.js
Original file line number Diff line number Diff line change
@@ -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();
19 changes: 0 additions & 19 deletions client/src/locales/sort-messages-fr-translations.js

This file was deleted.

0 comments on commit 3538a88

Please sign in to comment.