turn back translaton scripts

This commit is contained in:
Pavel Mineev
2021-01-12 13:39:26 -06:00
committed by Umputun
parent 42cc226b16
commit f332ab194d
9 changed files with 73 additions and 80 deletions
+2 -2
View File
@@ -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",
@@ -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`));
-8
View File
@@ -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;
+40
View File
@@ -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'))
);
});
-43
View File
@@ -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'))
);
});
+4 -7
View File
@@ -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`);
},
};
+6
View File
@@ -0,0 +1,6 @@
const getSupportedLocales = require('./supportedLocales');
module.exports = {
getSupportedLocales() {
return getSupportedLocales;
},
};
+13
View File
@@ -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,
};
+4 -5
View File
@@ -6,10 +6,9 @@ const enMessages = {};
export async function loadLocale(locale: string): Promise<Record<string, string>> {
${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 };