This repository has been archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
/
translation-keys.js
82 lines (65 loc) · 2.38 KB
/
translation-keys.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
73
74
75
76
77
78
79
80
81
82
const fs = require('fs').promises;
const path = require('path');
const asyncForEach = async (arr, cb) => {
for (let index = 0; index < arr.length; index++) {
await cb(arr[index], index, arr);
}
};
const getRegionContent = async (activeLanguages, contentDirectory) => {
const data = {};
await asyncForEach(activeLanguages, async lang => {
const filePath = path.join(__dirname, contentDirectory, `${lang}.json`);
data[lang] = await fs.readFile(filePath, 'utf8');
});
return data;
};
const resolveObjectPath = (targetPath, obj) => {
return targetPath.split('.').reduce((prev, curr) => {
return prev ? prev[curr] : '';
}, obj);
};
/* eslint-disable */
// original https://codegolf.stackexchange.com/questions/195476/extract-all-keys-from-an-object-json
const parseKeys = (obj, str, cb) => {
!obj | ([obj] == obj) ||
Object.keys(obj).map(key => {
parseKeys(obj[key], (key = str ? str + [, key] : key), cb, cb(key));
});
};
/* eslint-enable */
const parseFiles = (targetPath, languages, regionContent, ignore) => {
const en = JSON.parse(regionContent[languages[0]]);
const fr = JSON.parse(regionContent[languages[1]]);
let missing = '';
parseKeys(en, undefined, arr => {
const strPath = arr.split(',').join('.');
if (!ignore.includes(strPath)) {
const enStr = resolveObjectPath(strPath, en);
const frStr = resolveObjectPath(strPath, fr);
if (typeof enStr === 'string' && enStr && !frStr) {
missing += ` - Missing FR string: "${strPath}" ${'\n'}`;
}
if (typeof frStr === 'string' && frStr && !enStr) {
missing += ` - Missing EN string: "${strPath}" ${'\n'}`;
}
}
});
if (!missing) {
console.log(`${targetPath} ${languages}`, '✅'); // eslint-disable-line no-console
return;
}
console.log(`${targetPath} ${languages}`, '❌'); // eslint-disable-line no-console
console.log(missing); // eslint-disable-line no-console
process.exit(1);
};
const contentPaths = ['src/locale/translations', 'src/locale/translations/regional'];
const ingoredKeys = [
'RegionPicker.ExposedHelpLinkNote.AB',
'RegionPicker.ExposedHelpLinkNote.BC',
'RegionPicker.ExposedHelpLinkNote.SK',
];
contentPaths.forEach(async targetPath => {
const languages = ['en', 'fr'];
const regionContent = await getRegionContent(languages, targetPath);
parseFiles(targetPath, languages, regionContent, ingoredKeys);
});