-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
trim-locales.js
45 lines (40 loc) · 1.33 KB
/
trim-locales.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
var diff = require('deep-diff');
const _ = require('lodash');
const fs = require('fs');
let isObject = function (a) {
return !!a && a.constructor === Object;
};
function removeEmpty(obj) {
return Object.entries(obj)
.filter(([_, v]) => v != null && v != '')
.reduce((acc, [k, v]) => ({ ...acc, [k]: isObject(v) ? removeEmpty(v) : v }), {});
}
const enTranslationFile = './src/i18n/locales/en.json';
const enTranslations = require(enTranslationFile);
const ignoredFiles = ['localeNames.json', 'en.json'];
const localesDir = './src/i18n/locales';
const files = [];
const dir = fs.opendirSync(localesDir);
let dirent;
while ((dirent = dir.readSync()) !== null) {
if (!ignoredFiles.includes(dirent.name)) {
files.push(dirent.name);
}
}
dir.closeSync();
for (const file of files) {
const localeFileName = `./src/i18n/locales/${file}`;
const translations = require(localeFileName);
const diffR = diff(enTranslations, translations);
let newTranslation = {};
diffR.forEach((d) => {
switch (d.kind) {
case 'E':
_.set(newTranslation, d.path, d.rhs);
break;
}
});
newTranslation = removeEmpty(newTranslation);
console.log(`Trimmed ${localeFileName}`);
fs.writeFileSync(localeFileName, JSON.stringify(newTranslation, null, 4));
}