check that all translation messages are commit

This commit is contained in:
konstantin krivlenia
2020-03-12 15:45:35 -05:00
committed by Umputun
parent fe4b46f924
commit 156f8e446f
6 changed files with 52 additions and 11 deletions
+3
View File
@@ -29,3 +29,6 @@ jobs:
- run: npm run size
working-directory: ./frontend
- run: npm run check:translation
working-directory: ./frontend
+1
View File
@@ -5,6 +5,7 @@
"build": "webpack --mode production && es-check es5 './public/*.js'",
"start": "webpack-dev-server --mode development",
"check": "tsc -p tsconfig.json --noEmit --skipLibCheck",
"check:translation": "npm run extract-messages && node ./tasks/checkTranslation.js",
"lint": "eslint --max-warnings=0 --ext=.ts,.tsx,.js,.jsx .",
"lint:style": "stylelint '**/*.scss' '**/*.pcss' '**/*.css' 'iframe.html'",
"test": "jest",
+24
View File
@@ -0,0 +1,24 @@
const process = require('process');
const locales = require('./supportedLocales');
const { getLocalePath } = require('./getLocalePath');
const { keys } = require('./getTranslationKeys');
const errors = [];
locales.forEach(locale => {
const dict = require(getLocalePath({ locale }));
const keysFromDict = Object.keys(dict);
keysFromDict.forEach(key => {
if (!keys.includes(key)) {
errors.push(
`"${key}" key not found in "${locale}" locale dict. Please run "npm run generate-langs" and commit changes.`
);
}
return null;
});
});
if (errors.length) {
// eslint-disable-next-line no-console
console.error(errors.join(`\n`));
process.exit(1);
}
+5 -11
View File
@@ -1,20 +1,14 @@
const fs = require('fs');
const path = require('path');
const defaultMessages = require('../extracted-messages/messages');
const { keyMessagePairs, keys } = require('./getTranslationKeys');
const { getLocalePath } = require('./getLocalePath');
const { renderLoadLocale } = require('./localeLoadTemplate');
const { getSupportedLocales } = require('./getSupportedLocales');
const locales = getSupportedLocales();
const keyMessagePairs = [];
const keysSet = new Set();
defaultMessages.forEach(({ id, defaultMessage }) => {
keyMessagePairs.push([id, defaultMessage]);
keysSet.add(id);
});
function removeAbandonedKeys(existKeys, dictionary) {
return Object.fromEntries(Object.entries(dictionary).filter(([key]) => existKeys.has(key)));
return Object.fromEntries(Object.entries(dictionary).filter(([key]) => existKeys.includes(key)));
}
function sortDict(dict) {
@@ -27,7 +21,7 @@ function sortDict(dict) {
locales.forEach(locale => {
let currentDict = {};
const pathToDict = path.resolve(__dirname, `../app/locales/${locale}.json`);
const pathToDict = getLocalePath({ locale });
if (fs.existsSync(pathToDict)) {
currentDict = require(pathToDict);
}
@@ -36,7 +30,7 @@ locales.forEach(locale => {
currentDict[key] = defaultMessage;
}
});
currentDict = removeAbandonedKeys(keysSet, currentDict);
currentDict = removeAbandonedKeys(keys, currentDict);
currentDict = sortDict(currentDict);
fs.writeFileSync(pathToDict, JSON.stringify(currentDict, null, 2) + '\n');
fs.writeFileSync(
+6
View File
@@ -0,0 +1,6 @@
const path = require('path');
module.exports = {
getLocalePath({ locale }) {
return path.resolve(__dirname, `../app/locales/${locale}.json`);
},
};
+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,
};