-
Notifications
You must be signed in to change notification settings - Fork 27
/
generate-intl.js
39 lines (32 loc) · 1.04 KB
/
generate-intl.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
/* eslint-disable import/no-dynamic-require */
const { resolve } = require('path');
const fs = require('fs');
const glob = require('glob');
const path = require('path');
const fileMappings = {};
const supportedLocales = [];
const defaultLang = 'en';
const localeFiles = glob.sync('./locale/*.json');
localeFiles.forEach((file) => {
const lang = path.basename(file).split('.json')[0];
supportedLocales.push(lang);
fileMappings[lang] = require(resolve(file));
});
// JSON is strucutred with first key being the block name
const blockNames = Object.keys(fileMappings[defaultLang]);
blockNames.forEach((key) => {
const blockOutputMap = {};
supportedLocales.forEach((l) => {
let keys = [];
if (fileMappings[l][key]) {
keys = Object.keys(fileMappings[l][key]);
}
keys.forEach((item) => {
if (!blockOutputMap[item]) {
blockOutputMap[item] = {};
}
blockOutputMap[item][l] = fileMappings[l][key][item];
});
});
fs.writeFileSync(`./blocks/${key}/intl.json`, JSON.stringify(blockOutputMap, null, '\t'));
});