-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
find-missing-translations.js
72 lines (66 loc) · 2.51 KB
/
find-missing-translations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* eslint-env node*/
var {parse} = require('path')
var jsonDiff = require('json-diff');
const prototype = "./translations/en.json";
const data = require(prototype);
const langs = ["./translations/en-us.json",
"./translations/cn.json",
"./translations/es.json",
"./translations/ja.json",
"./translations/ko.json",
]
var debug = false
if (process.argv.length > 2) {
debug = process.argv[2] === "-d"
}
for (const lang of langs) {
const dataLang = require(lang)
const diffData = jsonDiff.diff(data, dataLang);
if (typeof(diffData) === 'undefined') {
console.log(`${prototype} and ${lang} are identical`)
console.log('===============================')
continue;
}
const diffKeys = Object.keys(diffData);
const missingKey = /__deleted$/;
const missingKeys = diffKeys.filter(k => missingKey.test(k));
const friendlyMissingKeys = missingKeys.map(k => k.replace(missingKey, ''));
const parsedLang = parse(lang);
const changedKeys = diffKeys.filter(k => missingKeys.indexOf(k) === -1)
var addedKeys= []
var modifiedKeys = []
for (const key of changedKeys) {
if (key.endsWith("__added")){
addedKeys.push(key.replace("__added", ""));
continue;
}
const jsonObject = diffData[key];
const jsonType = typeof(jsonObject);
switch (jsonType) {
case "object":
// object contains recursively embedded __new and __old fields within
modifiedKeys.push(key)
break;
case "string":
// this "object" doesn't exist in the main translation file
throw new Error(`Expected to filter "${key}" earlier!`);
default:
console.error(key);
throw Error(`Unrecognized type encountered "${jsonType}`)
}
}
console.log(`${parsedLang.base} audit details:`);
console.log(`${friendlyMissingKeys.length} missing keys`);
if (debug) printKeys(friendlyMissingKeys);
console.log(`${addedKeys.length} added keys`)
if (debug) printKeys(addedKeys)
console.log(`${modifiedKeys.length} modified keys`)
if (debug) printKeys(modifiedKeys)
console.log('===============================')
if (!debug & (friendlyMissingKeys.length > 0 || addedKeys.length > 0 || modifiedKeys.length > 0)) console.log("to view keys, add '-- -d' at the end of this script (npm run audit-translate -- -d)`")
}
function printKeys(keys) {
for (const key of keys) {
console.log(`\t${key}`)
}
}