2020-07-08 14:53:07 +00:00
|
|
|
const convert = require('xml-js');
|
|
|
|
const fs = require('fs');
|
|
|
|
const {getAllFiles} = require('./utils');
|
|
|
|
|
|
|
|
console.log('Scanning TS files...');
|
|
|
|
const tsFiles = getAllFiles('../../ui/i18n', 'ts');
|
|
|
|
|
|
|
|
const options = {compact: true, spaces: 4};
|
|
|
|
|
|
|
|
tsFiles.forEach(file => {
|
|
|
|
if (file.endsWith('base.ts')) {
|
|
|
|
// We skip the base file
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const fileContent = fs.readFileSync(file).toString();
|
|
|
|
const json = convert.xml2js(fileContent, options);
|
|
|
|
|
|
|
|
const doctype = json["_doctype"];
|
2020-07-08 16:56:51 +00:00
|
|
|
let language = json[doctype]._attributes.language;
|
2020-07-08 15:37:27 +00:00
|
|
|
const isEn = language === 'en_US'
|
2020-07-08 14:53:07 +00:00
|
|
|
|
|
|
|
let translations;
|
|
|
|
try {
|
|
|
|
translations = require(`./status-react-translations/${language}.json`)
|
|
|
|
} catch (e) {
|
|
|
|
// No translation file for the exact match, let's use the file name instead
|
2020-07-08 16:56:51 +00:00
|
|
|
const match = /qml_([a-zA-Z0-9_]+)\.ts/.exec(file)
|
|
|
|
language = language || match[1];
|
2020-07-08 14:53:07 +00:00
|
|
|
try {
|
2020-07-08 16:56:51 +00:00
|
|
|
translations = require(`./status-react-translations/${match[1]}.json`)
|
2020-07-08 14:53:07 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(`No translation file found for ${language}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:10:08 +00:00
|
|
|
let messages = []
|
|
|
|
if (json[doctype].context.length) {
|
|
|
|
messages = json[doctype].context.flatMap(c => c.message)
|
|
|
|
} else {
|
|
|
|
messages = json[doctype].context.message;
|
|
|
|
}
|
2020-07-08 14:53:07 +00:00
|
|
|
|
2020-07-08 15:37:27 +00:00
|
|
|
console.log(`Modying ${language}...`)
|
2020-07-08 14:53:07 +00:00
|
|
|
messages.forEach(message => {
|
|
|
|
if (!message._attributes || !message._attributes.id) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-08 15:37:27 +00:00
|
|
|
if (isEn) {
|
|
|
|
// We just put the source string in the tranlsation
|
|
|
|
message.translation = {
|
|
|
|
"_text": message.source._text
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2020-07-08 14:53:07 +00:00
|
|
|
const messageId = message._attributes.id;
|
|
|
|
if (!translations[messageId]) {
|
|
|
|
// Skip this message, as we have no translation
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
message.translation = {
|
|
|
|
"_text": translations[messageId]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const xml = convert.js2xml(json, options);
|
|
|
|
|
|
|
|
fs.writeFileSync(file, xml);
|
|
|
|
});
|
2020-07-08 15:37:27 +00:00
|
|
|
|
|
|
|
console.log('All done!')
|