-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextract.js
More file actions
68 lines (55 loc) · 1.79 KB
/
extract.js
File metadata and controls
68 lines (55 loc) · 1.79 KB
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
/*
* Reads in `en.default.schema.json` and all EFIGS dictionaries (de.dictionary.json, es.dictionary.json etc.)
* Matches EN strings to dictionary keys
* Outputs *.schema.json files for DE, ES, IT, FR and missing.en.dictionary.json containing any strings not found
* in existing dictionaries
*
*/
const glob = require('glob');
const fs = require('fs');
const path = require('path');
const filePatterns = [
'./sections/*/*/locales/en.default.schema.json',
'./components/*/*/locales/en.default.schema.json',
'./blocks/*/*/locales/en.default.schema.json',
'./source/locales/en.default.schema.json',
]
const dictionaryPatterns = [
'./de.dictionary.json',
'./es.dictionary.json',
'./it.dictionary.json',
'./fr.dictionary.json',
]
let dictionary;
let missing = {};
let locale;
function extractDict(obj) {
Object.keys(obj).forEach(key => {
if (typeof(obj[key]) === 'string') {
if (Object.keys(dictionary).includes(obj[key])) {
obj[key] = dictionary[obj[key]];
} else {
missing[obj[key]] = obj[key];
obj[key] = '';
}
} else {
extractDict(obj[key]);
}
});
}
dictionaryPatterns.forEach(dictionaryFile => {
dictionary = JSON.parse(fs.readFileSync(dictionaryFile));
filePatterns.forEach(pattern => {
const files = glob.sync(pattern);
files.forEach(file => {
locale = JSON.parse(fs.readFileSync(file));
extractDict(locale);
const localeString = JSON.stringify(locale, null, 2);
const localeFilePrefix = dictionaryFile.substring(2, 4);
const localeFileDir = path.dirname(file);
fs.writeFileSync(`${localeFileDir}/${localeFilePrefix}.schema.json`, localeString);
});
});
});
const missingString = JSON.stringify(missing, null, 2);
fs.writeFileSync(`./missing.en.dictionary.json`, missingString);