-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompile-translations.mjs
74 lines (62 loc) · 1.81 KB
/
compile-translations.mjs
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
/// <reference types="node" />
import path from 'path';
import fs from 'fs';
import chalk from 'chalk';
/**
*
* @param {string} startDir
* @param {RegExp} regexFilter
* @param {function} callback
*/
function findTranslations(startDir, regexFilter, callback) {
console.log(
chalk.blue('- Finding translation files in directory:'),
startDir
);
if (!fs.existsSync(startDir)) {
console.log(chalk.red('- Directory does not exist:'), startDir);
return;
}
const files = fs.readdirSync(startDir);
for (var i = 0; i < files.length; i++) {
var filename = path.join(startDir, files[i]);
var stat = fs.lstatSync(filename);
if (stat.isDirectory()) {
findTranslations(filename, regexFilter, callback);
} else if (regexFilter.test(filename)) {
callback(filename);
}
}
}
/**
*
* @param {string} filePath
*/
function appendTranslationFile(filePath) {
console.log(chalk.green('- Translation file found:'), chalk.yellow(filePath));
const translationFile = JSON.parse(fs.readFileSync(filePath));
const language = filePath.split('/').pop().replace('.json', '');
const translationsFolder = 'public/translations';
if (!fs.existsSync(translationsFolder)) {
fs.mkdirSync(translationsFolder, { recursive: true });
}
const resultTranslationFilePath = `${translationsFolder}/${language}.json`;
if (!fs.existsSync(resultTranslationFilePath)) {
fs.writeFileSync(resultTranslationFilePath, JSON.stringify({}), {
flag: 'w',
});
}
const resultTranslationFile = JSON.parse(
fs.readFileSync(resultTranslationFilePath)
);
fs.writeFileSync(
resultTranslationFilePath,
JSON.stringify({
...resultTranslationFile,
...translationFile,
})
);
}
['modules', 'ui'].forEach(dir =>
findTranslations(dir, /\.json$/, appendTranslationFile)
);