-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract-all-translations.js
75 lines (65 loc) · 2.17 KB
/
extract-all-translations.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
const recursive = require("recursive-readdir");
const fs = require('fs');
const config = require('./config');
const {
stringifyTranslationFile,
stringifyTranslationContent
} = require('./helpers/handle-different-file-types');
const BASE_PATH = config.translationsPath;
function readFiles(filePaths) {
const translations = {};
filePaths.forEach((filePath, idx) => {
const file = fs.readFileSync(filePath, "UTF-8");
const stringifiedObject = stringifyTranslationFile(file, filePath);
const parsedObject = JSON.parse(stringifiedObject);
translations[filePath] = parsedObject;
});
return translations;
}
function findDefaultTranslation(translations) {
const localePaths = Object.keys(translations);
let idx = localePaths.length - 1;
for (; idx >= 0; idx--) {
if (localePaths[idx].includes(config.mainTranslationFileName))
return translations[localePaths[idx]];
}
}
function checkTranslationAgainstDefault(translation, enTranslation) {
const transKeys = Object.keys(translation);
transKeys.forEach(key => {
if (!enTranslation[key])
delete translation[key];
});
return translation;
}
function fillTranslation(translation, enTranslation) {
const transKeys = Object.keys(enTranslation);
translation = checkTranslationAgainstDefault(translation, enTranslation);
transKeys.forEach(key => {
if (!translation[key])
translation[key] = 'ERR! No translation!';
});
return translation;
}
function saveTranslation(translation, path) {
const stringifiedTranslation = JSON.stringify(translation, null, 2);
const fileContent = stringifyTranslationContent(stringifiedTranslation, path);
fs.writeFileSync(path, fileContent);
}
function fillTranslations(translations) {
let defaultTranslation = findDefaultTranslation(translations);
const transPaths = Object.keys(translations);
transPaths.forEach(path => {
translations[path] = fillTranslation(translations[path], defaultTranslation);
saveTranslation(translations[path], path);
});
}
function extractAllTranslations() {
return recursive(BASE_PATH)
.then(readFiles)
.then(fillTranslations)
.catch(console.log);
}
module.exports = {
extractAllTranslations,
};