-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathgenerate-locale-data.js
81 lines (76 loc) · 2.49 KB
/
generate-locale-data.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
const path = require('path');
const fs = require('fs-extra-promise');
const matchAll = require('string.prototype.matchall');
const baseDir = path.join(__dirname, 'src');
const selectedFiles = [];
const processFile = async function(filePath) {
const stats = await fs.statAsync(filePath);
if(stats.isDirectory()) {
const files = await fs.readdirAsync(filePath);
for(const file of files) {
await processFile(path.join(filePath, file));
}
} else if(['.js', '.ts', '.html'].includes(path.extname(filePath).toLowerCase())) {
selectedFiles.push(filePath);
}
};
const functionPatt = /Localize\.text\('(.+?)',\s*?'(.+?)'/g;
const componentPatt0 = /<Localize\s+context="(.+?)"\s+key=["'](.+?)["']>/g;
const componentPatt1 = /<Localize\s+key=["'](.+?)["']\s+context="(.+?)">/g;
(async function() {
try {
const locale = 'en';
const matches = [];
await processFile(baseDir);
for(const file of selectedFiles) {
let contents = await fs.readFileAsync(file, 'utf8');
contents = contents.replace(/\\'/g, `'`);
[...matchAll(contents, functionPatt)]
.filter(arr => arr.length > 1)
.reduce((arr, a) => {
return [...arr, a.slice(1, 3)];
}, [])
.forEach(([ key, context ]) => matches.push({ key, context }));
[...matchAll(contents, componentPatt0)]
.filter(arr => arr.length > 1)
.reduce((arr, a) => {
return [...arr, a.slice(1, 3)];
}, [])
.forEach(([ context, key ]) => matches.push({ key: key.replace(/\\/g, ''), context}));
[...matchAll(contents, componentPatt1)]
.filter(arr => arr.length > 1)
.reduce((arr, a) => {
return [...arr, a.slice(1, 3)];
}, [])
.forEach(([ key, context ]) => matches.push({ key: key.replace(/\\/g, ''), context}));
}
const localeData = {
locale
};
for(const { key, context } of matches) {
if(localeData[key]) {
localeData[key] = {
...localeData[key],
[context]: {
val: key,
note: ''
}
};
} else {
localeData[key] = {
[context]: {
val: key,
note: ''
}
};
}
}
const json = JSON.stringify(localeData, null, ' ');
console.log(json);
const localesDir = path.join(__dirname, 'locales');
await fs.ensureDirAsync(localesDir);
await fs.writeFileAsync(path.join(localesDir, locale + '.json'), json);
} catch(err) {
console.error(err);
}
})();