32 lines
725 B
JavaScript
32 lines
725 B
JavaScript
function I18n(modules) {
|
|
var translations = modules.translations;
|
|
var language = chooseLanguage();
|
|
return {
|
|
get: get
|
|
};
|
|
|
|
function chooseLanguage() {
|
|
var userPreference = navigator.language || navigator.userLanguage;
|
|
if(userPreference != undefined) {
|
|
if(translations[userPreference] != undefined) {
|
|
return userPreference;
|
|
}
|
|
var lang = userPreference.replace(/-.*/, '');
|
|
for(var key in translations) {
|
|
if(key.replace(/-.*/, '') == lang) {
|
|
return key;
|
|
}
|
|
}
|
|
}
|
|
if(translations['en-US'] != undefined) {
|
|
return 'en-US';
|
|
}
|
|
for(var key in translations) {
|
|
return key;
|
|
}
|
|
}
|
|
|
|
function get(textId) {
|
|
return translations[language][textId] || ('TRANSLATE "'+textId+'" !!');
|
|
}
|
|
}
|