gctGenerator/scripts/compare_translations.js

61 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

2020-07-10 07:57:26 +09:00
const path = require('path');
const locales = require(path.join(__dirname, '../site/.vuepress/i18n/locales.json'));
/**
2020-07-10 12:29:07 +09:00
* Gets all leaf nodes from the specified object
2020-07-10 07:57:26 +09:00
* @param {*} node The root node
* @param {*} path The already visited path
*/
const getLeaves = (node, path = null) => {
2021-01-06 19:29:40 +09:00
if (typeof node === 'string') return leafPaths.push(path);
2020-07-10 07:57:26 +09:00
if (typeof node != 'object') throw new Error(typeof node);
const currentPath = path ? `${path}.` : '';
Object.keys(node).forEach((key) => getLeaves(node[key], `${currentPath}${key}`));
};
/**
* Gets a nested property of the specified node
* @param {*} node The root node
* @param {*} path The property path
*/
const getNestedProperty = (node, path) => {
const props = path.split('.');
let currentValue = node;
while (currentValue && props.length > 0) {
currentValue = currentValue[props.shift()];
}
return currentValue;
};
// Load the translation files
const translations = Object.keys(locales).map((locale) => ({
lang: locales[locale].lang,
values: require(path.join(__dirname, `../site/.vuepress/i18n/${locales[locale].lang}.json`)),
}));
// Set the default translation
const defaultTranslation = translations.find((t) => t.lang === locales['/'].lang);
console.log(`Default translation set to ${defaultTranslation.lang}`);
2020-07-10 12:29:07 +09:00
// Holds the paths to all leaf nodes of the default translation
2021-01-06 19:29:40 +09:00
const leafPaths = [];
2020-07-10 07:57:26 +09:00
getLeaves(defaultTranslation.values);
2021-01-06 19:29:40 +09:00
console.log('Detected translations: ', leafPaths);
2020-07-10 07:57:26 +09:00
// Compare all translations to the default translation
translations.forEach((t) => {
console.log(`Comparing ${t.lang} to ${defaultTranslation.lang}`);
2021-01-06 19:29:40 +09:00
leafPaths.forEach((p) => {
2020-07-10 07:57:26 +09:00
const value = getNestedProperty(t.values, p);
2020-08-16 14:22:48 +09:00
if (value == null || typeof value !== 'string') {
2020-07-10 07:57:26 +09:00
console.warn(`${t.lang} is missing a translation at '${p}'`);
2020-08-16 14:42:37 +09:00
console.log(
`::warning file=compare_translations.js::${t.lang} is missing a translation at '${p}'`,
2020-08-16 14:22:48 +09:00
);
}
2020-07-10 07:57:26 +09:00
});
});