-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
client: improve "postextract-i18n" script (#624)
- Loading branch information
Showing
3 changed files
with
58 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file was deleted.
Oops, something went wrong.