Added unused translations counter
This commit is contained in:
@@ -3,7 +3,9 @@
|
||||
### Usage
|
||||
|
||||
- `npm install` Install the dependencies in the local node_modules folder
|
||||
- `npm run locales:download` Download and save all locales
|
||||
- `npm run locales:upload` Upload base `en` locale
|
||||
- `npm run locales:download` - Download and save all translations
|
||||
- `npm run locales:upload` - Upload base `en` locale
|
||||
- `npm run locales:summary` - Shows the current locales summary
|
||||
- 'npm run locales:unused' - Shows a list of unused strings
|
||||
|
||||
After download you'll find the output locales in the `client/src/__locales/` folder.
|
||||
|
||||
41
scripts/translations/count.js
Normal file
41
scripts/translations/count.js
Normal file
@@ -0,0 +1,41 @@
|
||||
const path = require('path');
|
||||
const twoskyConfig = require('../../.twosky.json')[0];
|
||||
|
||||
const {languages} = twoskyConfig;
|
||||
const LOCALES_DIR = '../../client/src/__locales';
|
||||
const LOCALES_LIST = Object.keys(languages);
|
||||
const BASE_FILE = 'en.json';
|
||||
|
||||
const main = () => {
|
||||
const pathToBaseFile = path.join(LOCALES_DIR, BASE_FILE);
|
||||
const baseLanguageJson = require(pathToBaseFile);
|
||||
|
||||
const summary = {};
|
||||
|
||||
LOCALES_LIST.forEach((locale) => {
|
||||
const pathToFile = path.join(LOCALES_DIR, `${locale}.json`);
|
||||
if (pathToFile === pathToBaseFile) {
|
||||
return;
|
||||
}
|
||||
|
||||
let total = 0;
|
||||
let translated = 0;
|
||||
|
||||
const languageJson = require(pathToFile);
|
||||
for (let key in baseLanguageJson) {
|
||||
total += 1;
|
||||
if (key in languageJson) {
|
||||
translated += 1;
|
||||
}
|
||||
}
|
||||
|
||||
summary[locale] = Math.round(translated / total * 10000) / 100;
|
||||
});
|
||||
|
||||
console.log('Translations summary:');
|
||||
for (let key in summary) {
|
||||
console.log(`${key}, translated ${summary[key]}%`);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
@@ -1,13 +1,14 @@
|
||||
{
|
||||
"name": "translations",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"locales:download": "TWOSKY_URI=https://twosky.adtidy.org/api/v1 TWOSKY_PROJECT_ID=home node download.js",
|
||||
"locales:upload": "TWOSKY_URI=https://twosky.adtidy.org/api/v1 TWOSKY_PROJECT_ID=home node upload.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"request": "^2.88.0",
|
||||
"request-promise": "^4.2.2"
|
||||
}
|
||||
"name": "translations",
|
||||
"version": "0.2.0",
|
||||
"scripts": {
|
||||
"locales:download": "TWOSKY_URI=https://twosky.adtidy.org/api/v1 TWOSKY_PROJECT_ID=home node download.js ; node count.js",
|
||||
"locales:upload": "TWOSKY_URI=https://twosky.adtidy.org/api/v1 TWOSKY_PROJECT_ID=home node upload.js",
|
||||
"locales:summary": "node count.js",
|
||||
"locales:unused": "node unused.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"request": "^2.88.0",
|
||||
"request-promise": "^4.2.2"
|
||||
}
|
||||
}
|
||||
|
||||
63
scripts/translations/unused.js
Normal file
63
scripts/translations/unused.js
Normal file
@@ -0,0 +1,63 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const SRC_DIR = '../../client/src/'
|
||||
const LOCALES_DIR = '../../client/src/__locales';
|
||||
const BASE_FILE = path.join(LOCALES_DIR, 'en.json');
|
||||
|
||||
// Strings that may be found by the algorithm,
|
||||
// but in fact they are used.
|
||||
const KNOWN_USED_STRINGS = {
|
||||
'blocking_mode_refused': true,
|
||||
'blocking_mode_nxdomain': true,
|
||||
'blocking_mode_custom_ip': true,
|
||||
}
|
||||
|
||||
function traverseDir(dir, callback) {
|
||||
fs.readdirSync(dir).forEach(file => {
|
||||
let fullPath = path.join(dir, file);
|
||||
if (fs.lstatSync(fullPath).isDirectory()) {
|
||||
traverseDir(fullPath, callback);
|
||||
} else {
|
||||
callback(fullPath);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const contains = (key, files) => {
|
||||
for (let file of files) {
|
||||
if (file.includes(key)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
const main = () => {
|
||||
const baseLanguage = require(BASE_FILE);
|
||||
const files = [];
|
||||
|
||||
traverseDir(SRC_DIR, (path) => {
|
||||
const canContain = (path.endsWith('.js') || path.endsWith('.json')) &&
|
||||
!path.includes(LOCALES_DIR);
|
||||
|
||||
if (canContain) {
|
||||
files.push(fs.readFileSync(path).toString());
|
||||
}
|
||||
});
|
||||
|
||||
const unused = [];
|
||||
for (let key in baseLanguage) {
|
||||
if (!contains(key, files) && !KNOWN_USED_STRINGS[key]) {
|
||||
unused.push(key);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Unused keys:');
|
||||
for (let key of unused) {
|
||||
console.log(key);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user