-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractTranslations.js
More file actions
91 lines (84 loc) · 2.95 KB
/
extractTranslations.js
File metadata and controls
91 lines (84 loc) · 2.95 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
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
82
83
84
85
86
87
88
89
90
91
const fs = require('fs');
const { convertPropsToFlatJson } = require('propson');
const englishLang = getlangFileAsJson('en-US');
const rawTranslations = require('./rawTranslations.json');
const supportedLocales = {
Vietnam: { locale: 'vi', messages: {} },
Thailand: { locale: 'th', messages: {} },
Japan: { locale: 'ja', messages: {} },
Korean: { locale: 'ko', messages: {} },
Turkish: { locale: 'tr', messages: {} },
Russia: { locale: 'ru', messages: {} },
};
const notFoundList = [];
const foundList = [];
function main() {
// console.log(rawTranslations);
// console.log(englishLang);
const modifiedRawTranslations = rawTranslations.map(block => {
const id = Object.keys(englishLang).find(id => {
const taggedMessage = englishLang[id];
const nonTaggedMessage = block['English text'];
// if (
// taggedMessage == "OK" &&
// taggedMessage.toLowerCase() === nonTaggedMessage.toLowerCase()
// ) {
// console.warn(id);
// console.log(nonTaggedMessage);
// }
return taggedMessage.toLowerCase() === nonTaggedMessage.toLowerCase();
});
const newBlock = { ...block, id: id || block.id };
if (id) {
foundList.push(newBlock);
} else {
notFoundList.push(newBlock);
}
return newBlock;
});
foundList.forEach(foundBlock => {
Object.keys(supportedLocales).forEach(locale => {
supportedLocales[locale].messages[foundBlock.id] = foundBlock[locale];
});
});
Object.keys(supportedLocales).forEach(locale => {
const localeCode = supportedLocales[locale].locale;
const extractedTranslations = supportedLocales[locale].messages;
const currentLangFile = getlangFileAsJson(localeCode);
const newLangFile = {};
Object.keys(currentLangFile).forEach(key => {
const currentTranslation = currentLangFile[key];
if (currentTranslation || extractedTranslations[key]) {
if (currentTranslation) {
newLangFile[key] = currentTranslation;
}
if (extractedTranslations[key]) {
newLangFile[key] = extractedTranslations[key];
}
}
});
const propertiesContent = Object.keys(newLangFile)
.map(id => `${id}=${newLangFile[id] ? newLangFile[id] : ''}`)
.join('\n');
fs.writeFileSync(
`messages/${localeCode}/strings.properties`,
propertiesContent
);
});
console.log('found: ', foundList.length);
console.log('notFound: ', notFoundList.length);
// console.log("notFoundList: ", notFoundList);
}
function getlangFileAsJson(locale) {
function returnMessagesFromPropertiesFile(rawPropertiesString) {
const translationsContent = rawPropertiesString.split('\n');
const parsedTranslations = convertPropsToFlatJson(translationsContent);
return parsedTranslations;
}
const englishPropertiesRaw = fs.readFileSync(
`./messages/${locale}/strings.properties`,
{ encoding: 'utf-8' }
);
return returnMessagesFromPropertiesFile(englishPropertiesRaw);
}
main();