From f332ab194d835c5e50310d20e6587d3fdb43a17e Mon Sep 17 00:00:00 2001 From: Pavel Mineev Date: Sun, 10 Jan 2021 17:56:52 +0300 Subject: [PATCH] turn back translaton scripts --- frontend/package.json | 4 +- ...eckTranslations.js => checkTranslation.js} | 19 ++------ frontend/tasks/defaultMessages.js | 8 ---- frontend/tasks/generateDictionary.js | 40 +++++++++++++++++ frontend/tasks/generateTranslations.js | 43 ------------------- frontend/tasks/getLocalePath.js | 11 ++--- frontend/tasks/getSupportedLocales.js | 6 +++ frontend/tasks/getTranslationKeys.js | 13 ++++++ frontend/tasks/localeLoadTemplate.js | 9 ++-- 9 files changed, 73 insertions(+), 80 deletions(-) rename frontend/tasks/{checkTranslations.js => checkTranslation.js} (54%) delete mode 100644 frontend/tasks/defaultMessages.js create mode 100644 frontend/tasks/generateDictionary.js delete mode 100644 frontend/tasks/generateTranslations.js create mode 100644 frontend/tasks/getSupportedLocales.js create mode 100644 frontend/tasks/getTranslationKeys.js diff --git a/frontend/package.json b/frontend/package.json index 270aa4c9..e12af59b 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -19,8 +19,8 @@ "size": "cross-env NODE_ENV=production run-s build check:size", "prettier": "prettier --write './**/*.{js,ts,tsx,css,html}'", "translation:extract": "formatjs extract --out-file=./extracted-messages/messages.json '**/!(*.d).ts?(x)'", - "translation:generate": "node ./tasks/generateTranslations.js", - "translation:check": "node ./tasks/checkTranslations.js" + "translation:generate": "node ./tasks/generateDictionary.js", + "translation:check": "node ./tasks/checkTranslation.js" }, "engines": { "node": ">=12.11", diff --git a/frontend/tasks/checkTranslations.js b/frontend/tasks/checkTranslation.js similarity index 54% rename from frontend/tasks/checkTranslations.js rename to frontend/tasks/checkTranslation.js index 460787f1..7642d6e2 100644 --- a/frontend/tasks/checkTranslations.js +++ b/frontend/tasks/checkTranslation.js @@ -1,33 +1,22 @@ -const fs = require('fs'); const process = require('process'); const locales = require('./supportedLocales'); -const getLocalePath = require('./getLocalePath'); -const defaultMessages = require('./defaultMessages'); +const { getLocalePath } = require('./getLocalePath'); +const { keys } = require('./getTranslationKeys'); const errors = []; locales.forEach(locale => { - const pathToDict = getLocalePath(locale); - - if (!fs.existsSync(pathToDict)) { - throw new Error( - `"${locale}" translation file not found. Please run "npm run translation:generate" and commit changes.` - ); - } - - const dict = require(pathToDict); + const dict = require(getLocalePath({ locale })); const keysFromDict = Object.keys(dict); - const keys = Object.keys(defaultMessages); - keysFromDict.forEach(key => { if (!keys.includes(key)) { errors.push( `"${key}" key not found in "${locale}" locale dict. Please run "npm run translation:generate" and commit changes.` ); } + return null; }); }); - if (errors.length) { // eslint-disable-next-line no-console console.error(errors.join(`\n`)); diff --git a/frontend/tasks/defaultMessages.js b/frontend/tasks/defaultMessages.js deleted file mode 100644 index faa8c7f1..00000000 --- a/frontend/tasks/defaultMessages.js +++ /dev/null @@ -1,8 +0,0 @@ -const defaultMessages = require('../extracted-messages/messages.json'); - -const formattedMessages = Object.entries(defaultMessages).reduce( - (accum, [key, { defaultMessage }]) => ({ ...accum, [key]: defaultMessage }), - {} -); - -module.exports = formattedMessages; diff --git a/frontend/tasks/generateDictionary.js b/frontend/tasks/generateDictionary.js new file mode 100644 index 00000000..82e4a6b2 --- /dev/null +++ b/frontend/tasks/generateDictionary.js @@ -0,0 +1,40 @@ +const fs = require('fs'); +const path = require('path'); +const { keyMessagePairs, keys } = require('./getTranslationKeys'); +const { getLocalePath } = require('./getLocalePath'); +const { renderLoadLocale } = require('./localeLoadTemplate'); +const { getSupportedLocales } = require('./getSupportedLocales'); + +const locales = getSupportedLocales(); + +function removeAbandonedKeys(existKeys, dictionary) { + return Object.fromEntries(Object.entries(dictionary).filter(([key]) => existKeys.includes(key))); +} + +function sortDict(dict) { + return Object.fromEntries( + Object.entries(dict).sort(([a], [b]) => { + return a.localeCompare(b); + }) + ); +} + +locales.forEach(locale => { + let currentDict = {}; + const pathToDict = getLocalePath({ locale }); + if (fs.existsSync(pathToDict)) { + currentDict = require(pathToDict); + } + keyMessagePairs.forEach(([key, defaultMessage]) => { + if (!currentDict[key] || locale === `en`) { + currentDict[key] = defaultMessage; + } + }); + currentDict = removeAbandonedKeys(keys, currentDict); + currentDict = sortDict(currentDict); + fs.writeFileSync(pathToDict, `${JSON.stringify(currentDict, null, 2)}\n`); + fs.writeFileSync( + path.resolve(__dirname, `../app/utils/loadLocale.ts`), + renderLoadLocale(locales.filter(locale => locale !== 'en')) + ); +}); diff --git a/frontend/tasks/generateTranslations.js b/frontend/tasks/generateTranslations.js deleted file mode 100644 index 53fd16a8..00000000 --- a/frontend/tasks/generateTranslations.js +++ /dev/null @@ -1,43 +0,0 @@ -const fs = require('fs'); -const path = require('path'); - -const defaultMessages = require('./defaultMessages'); -const getLocalePath = require('./getLocalePath'); -const renderLoadLocale = require('./localeLoadTemplate'); -const locales = require('./supportedLocales.json'); - -function removeAbandonedKeys(existKeys, dictionary) { - return Object.fromEntries(Object.entries(dictionary).filter(([key]) => existKeys.includes(key))); -} - -function sortDict(dict) { - return Object.fromEntries(Object.entries(dict).sort(([a], [b]) => a.localeCompare(b))); -} - -locales.forEach(locale => { - let currentDict = {}; - const pathToDict = getLocalePath(locale); - - if (fs.existsSync(pathToDict)) { - currentDict = require(pathToDict); - } else { - throw Error(`Unable to read translation dictionary for "${locale}" locale`); - } - - Object.entries(defaultMessages).forEach(([key, defaultMessage]) => { - if (currentDict[key]) { - return; - } - - currentDict[key] = defaultMessage; - }); - - currentDict = removeAbandonedKeys(Object.keys(defaultMessages), currentDict); - currentDict = sortDict(currentDict); - - fs.writeFileSync(pathToDict, `${JSON.stringify(currentDict, null, 2)}\n`); - fs.writeFileSync( - path.resolve(__dirname, '../app/utils/loadLocale.ts'), - renderLoadLocale(locales.filter(locale => locale !== 'en')) - ); -}); diff --git a/frontend/tasks/getLocalePath.js b/frontend/tasks/getLocalePath.js index be7b7c13..a9f56ace 100644 --- a/frontend/tasks/getLocalePath.js +++ b/frontend/tasks/getLocalePath.js @@ -1,9 +1,6 @@ const path = require('path'); - -module.exports = function getLocalePath(locale) { - if (locale === 'en') { - return path.resolve(__dirname, '../extracted-messages/messages.json'); - } - - return path.resolve(__dirname, `../app/locales/${locale}.json`); +module.exports = { + getLocalePath({ locale }) { + return path.resolve(__dirname, `../app/locales/${locale}.json`); + }, }; diff --git a/frontend/tasks/getSupportedLocales.js b/frontend/tasks/getSupportedLocales.js new file mode 100644 index 00000000..ec3b0acb --- /dev/null +++ b/frontend/tasks/getSupportedLocales.js @@ -0,0 +1,6 @@ +const getSupportedLocales = require('./supportedLocales'); +module.exports = { + getSupportedLocales() { + return getSupportedLocales; + }, +}; diff --git a/frontend/tasks/getTranslationKeys.js b/frontend/tasks/getTranslationKeys.js new file mode 100644 index 00000000..870a0d70 --- /dev/null +++ b/frontend/tasks/getTranslationKeys.js @@ -0,0 +1,13 @@ +const defaultMessages = require('../extracted-messages/messages'); + +const keyMessagePairs = []; +const keys = []; +defaultMessages.forEach(({ id, defaultMessage }) => { + keyMessagePairs.push([id, defaultMessage]); + keys.push(id); +}); + +module.exports = { + keyMessagePairs, + keys, +}; diff --git a/frontend/tasks/localeLoadTemplate.js b/frontend/tasks/localeLoadTemplate.js index 1f673149..0aeb63dc 100644 --- a/frontend/tasks/localeLoadTemplate.js +++ b/frontend/tasks/localeLoadTemplate.js @@ -6,10 +6,9 @@ const enMessages = {}; export async function loadLocale(locale: string): Promise> { ${locales .map( - locale => - ` if (locale === '${locale}') { - return import(/* webpackChunkName: "${locale}.locale" */ '../locales/${locale}.json') - .then(m => m.default) + locale => ` if (locale === '${locale}') { + return import(/* webpackChunkName: "${locale}" */ '../locales/${locale}.json') + .then(res => res.default) .catch(() => enMessages); } ` @@ -19,4 +18,4 @@ ${locales }\n`; } -module.exports = renderLoadLocale; +module.exports = { renderLoadLocale };