var AcceptLngs = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pt-BR'],
_getI18N = {
'wordToTranslate' : {
'pt' : `translatedWord`,
[...]
},
'2ndWordToTranslate' : {
'pt-BR' : `translatedWord`,
[...]
}
},
nav = window.navigator,
navLng = nav.language || nav.browserLanguage || nav.userLanguage,
prefs = nav.languages,
navPrefLng = prefs[0],
defaultLng = 'en',
lng = navLng, //It must be set for translation
// CHECK THE LANGUAGE OF THE NAVIGATOR.
checkLng = function checkLng() {
// isL10nAvailable:
if (AcceptLngs.indexOf(navPrefLng) < -1) {
lng = navPrefLng;
return lng;
}
},
// SEARCH IF LANGUAGE OF THE APP IS AVAILABLE.
isLngDispo = function isLngDispo() {
// isSubstrL10nAvailable:
checkLng();
if (AcceptLngs.indexOf(navPrefLng) === -1) {
lng = navPrefLng.substring(0,2);
return lng;
}
},
// SEARCH IF LANGUAGE OF THE APP IS AVAILABLE.
setLng = function setLng() {
// isL10nNoAvailable:
isLngDispo();
if (AcceptLngs.indexOf(lng) === -1) {
lng = defaultLng;
return lng;
}
};
// APPLYING LANGUAGE FOR THE APP.
setLng();
I am beginner in JavaScript. My script works. The problem is its execution time. I go from 102 ms (without the translation) to 448 ms (with).
I have try with loop, but I didn't manage to make it work properly. He didn't detect the language in AcceptedLng
and automatically put me the nav.language.
Perhaps, more simply, I should search directly in '_getI18N', but I have not yet gotten familiar with the search in the arrays, and I can not get any results either.
Info: To translate, I use _getI18N[wordToTranslate
][lng] into my script.