chore: validate string translations (#214)

This commit is contained in:
Miralem Drek
2019-12-04 17:21:20 +01:00
committed by GitHub
parent 1205f3201c
commit ed7e9df75f
11 changed files with 508 additions and 41 deletions

View File

@@ -0,0 +1,56 @@
const { declare } = require('@babel/helper-plugin-utils');
const vars = require('../apis/nucleus/src/locale/translations/all.json');
const ids = {};
Object.keys(vars).forEach(key => {
ids[vars[key].id] = key;
});
const used = [];
const warnings = {};
const warn = s => console.warn(`\x1b[43m\x1b[30m WARN \x1b[0m \x1b[1m\x1b[33m ${s}\x1b[0m`);
const find = declare((/* api, options */) => {
function useString(id) {
if (used.indexOf(id) !== -1) {
return;
}
used.push(id);
if (typeof ids[id] === 'undefined') {
warn(`String '${id}' does not exist in locale registry`);
}
}
return {
name: 'find',
visitor: {
CallExpression(path) {
if (!path.get('callee').isMemberExpression()) {
return;
}
if (
path.node.callee.object &&
path.node.callee.object.name === 'translator' &&
path.node.callee.property &&
path.node.callee.property.name === 'get'
) {
const { type, value } = path.node.arguments[0];
if (type === 'StringLiteral') {
useString(value, path);
} else {
const s = `${this.file.opts.filename}:${path.node.loc.start.line}:${path.node.loc.start.column}`;
if (!warnings[s]) {
warnings[s] = true;
warn(`Could not verify used string at ${s}`);
}
}
}
},
},
};
});
module.exports = find;

View File

@@ -0,0 +1,32 @@
const vars = require('../apis/nucleus/src/locale/translations/all.json');
const languages = [
'en-US',
'it-IT',
'zh-CN',
'zh-TW',
'ko-KR',
'de-DE',
'sv-SE',
'es-ES',
'pt-BR',
'ja-JP',
'fr-FR',
'nl-NL',
'tr-TR',
'pl-PL',
'ru-RU',
];
Object.keys(vars).forEach(key => {
const supportLanguagesForString = Object.keys(vars[key].locale);
if (supportLanguagesForString.indexOf('en-US') === -1) {
// en-US must exist
throw new Error(`String '${vars[key].id}' is missing value for 'en-US'`);
}
for (let i = 0; i < languages.length; i++) {
if (supportLanguagesForString.indexOf(languages[i]) === -1) {
console.warn(`String '${vars[key].id}' is missing value for '${languages[i]}'`);
}
}
});