mirror of
https://github.com/qlik-oss/nebula.js.git
synced 2025-12-19 17:58:43 -05:00
chore: validate string translations (#214)
This commit is contained in:
56
tools/locale-string-validator.js
Normal file
56
tools/locale-string-validator.js
Normal 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;
|
||||
32
tools/verify-translations.js
Normal file
32
tools/verify-translations.js
Normal 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]}'`);
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user