Generate Locale Files using Google Translate

Many thanks to born2net for sharing this. (#261)

The script will read the en.json translation file and will generate new localized files based on what's defined in the script.

Steps#

  1. Uncomment createLocalFiles() one time to create all the files: he.json, ca.json etc...
  2. Comment/Delete createLocalFiles() as you no longer need it
  3. Provide your google key via process.env.GOOGLE_KEY
  4. Run the script to populate the translations, re-run it whenever you make changes to your en.json

Note: this script requires google-translate

#!/usr/bin/env node
const fs = require('fs');
const googleTranslate = require('google-translate')(process.env.GOOGLE_KEY);
const i18Dir = './src/assets/i18n';
const defaultSourceLang = 'en';
const codes = {
Afrikaans: 'af',
Irish: 'ga',
Albanian: 'sq',
Italian: 'it',
Arabic: 'ar',
Japanese: 'ja',
Azerbaijani: 'az',
Kannada: 'kn',
Basque: 'eu',
Korean: 'ko',
Bengali: 'bn',
Latin: 'la',
Belarusian: 'be',
Latvian: 'lv',
Bulgarian: 'bg',
Lithuanian: 'lt',
Catalan: 'ca',
Macedonian: 'mk',
ChineseSimplified: 'zh-CN',
Malay: 'ms',
ChineseTraditional: 'zh-TW',
Maltese: 'mt',
Croatian: 'hr',
Norwegian: 'no',
Czech: 'cs',
Persian: 'fa',
Danish: 'da',
Polish: 'pl',
Dutch: 'nl',
Portuguese: 'pt',
English: 'en',
Romanian: 'ro',
Esperanto: 'eo',
Russian: 'ru',
Estonian: 'et',
Serbian: 'sr',
Filipino: 'tl',
Slovak: 'sk',
Finnish: 'fi',
Slovenian: 'sl',
French: 'fr',
Spanish: 'es',
Galician: 'gl',
Swahili: 'sw',
Georgian: 'ka',
Swedish: 'sv',
German: 'de',
Tamil: 'ta',
Greek: 'el',
Telugu: 'te',
Gujarati: 'gu',
Thai: 'th',
Haitian: 'ht',
Turkish: 'tr',
Hebrew: 'he',
Ukrainian: 'uk',
Hindi: 'hi',
Urdu: 'ur',
Hungarian: 'hu',
Vietnamese: 'vi',
Icelandic: 'is',
Welsh: 'cy',
Indonesian: 'id',
Yiddish: 'yi'
};
let createLocalFiles = () => {
Object.values(codes).forEach((local) => {
if (local === defaultSourceLang) return;
const p = `./src/assets/i18n/${local}.json`;
if (fs.existsSync(p)) {
console.log('exists ' + p);
} else {
fs.writeFileSync(p, '', {encoding: 'utf8', flag: 'w'});
}
});
};
// createLocalFiles();
let sourceFile = (local) => {
try {
return JSON.parse(fs.readFileSync(`./src/assets/i18n/${local}.json`, 'utf8'));
} catch (e) {
return null;
}
};
let getLocals = () => {
return new Promise((resolve, reject) => {
const locals = [];
fs.readdir(i18Dir, (err, files) => {
files.forEach(file => {
if (file === `${defaultSourceLang}.json`) return;
file = file.replace(/\.json/, '');
locals.push(file);
});
resolve(locals);
});
});
};
let trasnlate = (word, local) => {
return new Promise((resolve, reject) => {
googleTranslate.translate(word, local, function (err, translation) {
if (translation === undefined) {
console.log('>> google error ' + err + ' ' + word + ' ' + local);
} else {
var translated = cleanProbCharactersV2(translation.translatedText);
}
resolve(translated);
});
});
};
const cleanProbCharactersV2 = (i_string) => {
i_string = i_string.replace(/'/ig, "");
i_string = i_string.replace(/"/ig, "");
i_string = i_string.replace(/}/ig, "");
i_string = i_string.replace(/{/ig, "");
i_string = i_string.replace(/\)/ig, "");
i_string = i_string.replace(/\r/ig, "");
i_string = i_string.replace(/\n/ig, "");
i_string = i_string.replace(/()/ig, "");
return i_string;
};
const localSource = sourceFile(defaultSourceLang);
(async function asyncConnect() {
try {
const languages = await getLocals(3000);
for (let i = 0; i < languages.length; i++) {
let final = {};
const local = languages[i];
console.log('processing local ' + local + ' >>>');
const destlSource = sourceFile(local);
if (destlSource) {
final = destlSource;
}
for (section in localSource) {
if (!final[section])
final[section] = {};
const words = localSource[section];
for (word in words) {
if (destlSource && destlSource[section] && destlSource[section][word]) {
final[section][word] = destlSource[section][word]
} else {
console.log(' >>> ' + section + ' ' + words[word]);
const newWord = await trasnlate(words[word], local);
console.log(' ### translated to ' + newWord);
final[section][word] = newWord;
}
}
}
const f = i18Dir + '/' + local + '.json';
try {
fs.writeFileSync(f, JSON.stringify(final, null, '\t'), {encoding: 'utf8', flag: 'w'});
} catch (err) {
console.error(err);
}
}
} catch (err) {
console.log('problem encountered ' + err);
client.end()
}
})();

Expected output:#