an-anime-game-launcher/src/ts/i18n.ts

39 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-10-23 02:53:33 +03:00
const path = require("path");
const fs = require('fs');
let loadedLanguage: any;
function i18n(): any {
2021-10-23 16:37:15 +03:00
if(fs.existsSync(path.join(path.dirname(__dirname), 'locales', navigator.language.toLowerCase() + '.json'))) {
loadedLanguage = JSON.parse(fs.readFileSync(path.join(path.dirname(__dirname), 'locales', navigator.language.toLowerCase() + '.json'), 'utf8'));
}
else {
loadedLanguage = JSON.parse(fs.readFileSync(path.join(path.dirname(__dirname), 'locales', 'en.json'), 'utf8'));
}
2021-10-23 02:53:33 +03:00
};
i18n.prototype.translate = function(phrase: any) {
let translation = loadedLanguage[phrase];
if(translation === undefined) {
translation = phrase;
}
return translation
}
2021-10-23 16:37:15 +03:00
i18n.prototype.updatelang = function(newlang: string) {
2021-10-23 16:38:29 +03:00
// Test if the locale is the same string so if it's de-de or id-id remove -de or -id like navigator.language does.
2021-10-23 16:37:15 +03:00
let samecode = new RegExp(`(${newlang.toLowerCase().replace(/-.*$/, '')}.*){2}`, 'g');
samecode.test(newlang.toLowerCase()) ? newlang = newlang.toLowerCase().replace(/-.*$/, '') : newlang = newlang.toLowerCase();
2021-10-23 17:37:31 +03:00
if (newlang == 'ja-jp') newlang = 'ja';
if (newlang == 'vi-vn') newlang = 'vi';
2021-10-23 16:37:15 +03:00
if(fs.existsSync(path.join(path.dirname(__dirname), 'locales', newlang + '.json'))) {
loadedLanguage = JSON.parse(fs.readFileSync(path.join(path.dirname(__dirname), 'locales', newlang + '.json'), 'utf8'));
}
else {
loadedLanguage = JSON.parse(fs.readFileSync(path.join(path.dirname(__dirname), 'locales', 'en.json'), 'utf8'));
}
}
export default new (i18n as any);