forked from dhis2/who-data-quality-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpot-extractor.js
More file actions
45 lines (38 loc) · 1.38 KB
/
pot-extractor.js
File metadata and controls
45 lines (38 loc) · 1.38 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
const { readFileSync, writeFileSync } = require('fs');
const { i18nextToPot } = require('i18next-conv');
const recursive = require("recursive-readdir");
const argv = require('minimist')(process.argv.slice(2));
const filename = argv['o'] || 'en.pot';
const HTML_FUNCTION_REGEX = new RegExp("{{ *'([^']*)' *| *i18next *}}", 'g');
const JS_FUNCTION_REGEX = new RegExp("\\$i18next\\.t\\('([^']*)'\\)", 'g');
const getFileExtension = (filename) => {
return filename.split('.').pop();
};
// save file to disk
const save = (target) => {
return result => {
writeFileSync(target, result);
};
};
let translations = {};
const addKeysFromFileContent = (fileContent, functionRegex) => {
let matches;
while (( matches = functionRegex.exec(fileContent))) {
if (matches[1]) {
translations[matches[1]] = '';
}
}
};
recursive('src', function (err, files) {
for (let file of files) {
const fileExtension = getFileExtension(file);
if (fileExtension === 'html') {
addKeysFromFileContent(readFileSync(file, 'utf-8'), HTML_FUNCTION_REGEX);
} else if (fileExtension === 'js') {
addKeysFromFileContent(readFileSync(file, 'utf-8'), JS_FUNCTION_REGEX);
} else if (fileExtension === 'ejs') {
addKeysFromFileContent(readFileSync(file, 'utf-8'), HTML_FUNCTION_REGEX);
}
}
i18nextToPot('en', JSON.stringify(translations)).then(save('./i18n/' + filename));
});