forked from oskariorg/oskari-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract-locale.js
73 lines (59 loc) · 2.17 KB
/
extract-locale.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
var path = require('path');
var fs = require('fs');
var glob = require('glob');
var merge = require('merge');
function deconstructPath(filePath, chain) {
chain = chain || [];
if (chain[0] === 'bundles') {
return chain.slice(1, -3);
}
chain.unshift(path.basename(filePath));
return deconstructPath(path.dirname(filePath), chain);
}
function ensureDirectoryExists(filePath) {
var dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return true;
}
ensureDirectoryExists(dirname);
fs.mkdirSync(dirname);
}
var locale = process.argv[2];
if (!locale) {
throw new Error('Must give locale as parameter, for example "fi"');
}
var localeFilePaths = glob.sync('/bundles/**/resources/locale/@(en|' + locale + ').js', {root: path.join(__dirname, '..')});
var collection = {};
localeFilePaths.forEach(function(filePath){
var dir = deconstructPath(filePath).join('__');
var Oskari = {
registerLocalization: function(loc, isOverride) {
var lang = loc.lang;
if (!lang) {
throw new Error('Localization file has no "lang"!');
}
if (!collection[dir]) {
collection[dir] = {content: {}};
}
if (collection[dir].content[lang]) {
collection[dir].isMulti = true; // multiple calls to registerLocalization for language. Lang-overrides bundle does this.
}
collection[dir].content[lang] = loc.value;
}
};
var source = fs.readFileSync(filePath, 'utf8');
eval(source);
});
Object.keys(collection)
.filter(function(dir){
return !collection[dir].isMulti;
})
.forEach(function(dir){
var enContent = collection[dir].content.en || {};
var localeContent = collection[dir].content[locale] || {};
var output = merge.recursive(true, enContent, localeContent);
var outputPath = path.join(__dirname, '..', 'locale', dir + '__' + locale + '.json');
ensureDirectoryExists(outputPath);
fs.writeFileSync(outputPath, JSON.stringify(output, null, 2));
});
console.log('Extracted files succesfully to "locale" directory in repo root');