123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
"use strict";
modulum('I18NService', ['InitService'],
function(context, cls) {
context.I18NService = context.oo.StaticClass( {
__name: 'I18NService',
_init: false,
_fallBackLng: 'en-US',
_eventListener: new cls.EventListener(),
_i18nReady: 'i18nReady',
change: 'change',
isReady: false,
init: function() {
window.i18next
.use(window.i18nextBrowserLanguageDetector)
.init({
fallbackLng: this._getNormalizedLanguage(),
resources: window.gbcLocales,
detection: {
lookupQuerystring: 'setLng',
lookupCookie: 'lang',
caches: ['cookie']
}
}, this._onReady.bind(this));
},
setLng: function(locale) {
window.i18next.changeLanguage(this._getNormalizedLanguage(locale)[0]);
this._eventListener.emit(this.change);
},
whenLangChange: function(hook) {
return this._eventListener.when(this.change, hook);
},
translate: function(widget) {
if (this.isReady) {
widget.translate();
} else {
return this._eventListener.when(this._i18nReady, widget.translate.bind(widget));
}
return null;
},
getAllLng: function() {
return Object.keys(window.gbcLocales).map(function(key) {
if (key !== 'undefined') {
return {
'locale': key,
'language': window.i18next.exists('gwc.lngName', {
lng: key,
fallbackLng: 'undef'
}) ? window.i18next.t('gwc.lngName', {
lng: key
}) : key
};
}
});
},
_onReady: function() {
this._checkLanguageCompatibility();
var storedLng = gbc.StoredSettingsService.getLanguage();
var cookieLng = gbc.StoredSettingsService._getCookie('lang');
var language = cookieLng || storedLng || this._fallBackLng;
document.querySelector("html").setAttribute("lang", language.substring(0, 2));
this._eventListener.emit(this._i18nReady);
this.isReady = true;
},
_checkLanguageCompatibility: function() {
var storedLng = gbc.StoredSettingsService.getLanguage();
var cookieLng = gbc.StoredSettingsService._getCookie('lang');
var language = cookieLng || storedLng || this._fallBackLng;
if (language && !window.gbcLocales[language]) {
var allLngKeys = Object.keys(window.gbcLocales);
for (var i = 0; i < allLngKeys.length; i++) {
if (language.startsWith(allLngKeys[i].substring(0, 2))) {
this.setLng(allLngKeys[i]);
gbc.StoredSettingsService.setSettings('gwc.app.locale', allLngKeys[i]);
break;
}
}
}
},
getBrowserLanguage: function() {
return navigator.language || navigator.userLanguage;
},
_getNormalizedLanguage: function(locale) {
locale = typeof locale === "undefined" ? false : locale;
var fallback = locale || this.getBrowserLanguage() || this._fallBackLng;
var availableLC = Object.keys(window.gbcLocales);
var fallbackLanguageList = [];
var localeRegex = /^([a-z]{2})[-_]?([A-Z0-9]*)/;
var r = null;
var fallbackR = localeRegex.exec(fallback);
if (fallbackR) {
for (var i = 0; i < availableLC.length; i++) {
r = localeRegex.exec(availableLC[i]);
if (availableLC[i].indexOf(fallback) === 0 || r[1] === fallbackR[1]) {
if (availableLC[i] === locale) {
fallbackLanguageList.unshift(availableLC[i]);
} else {
fallbackLanguageList.push(availableLC[i]);
}
}
}
}
if (fallbackLanguageList.indexOf(this._fallBackLng) === -1) {
fallbackLanguageList.push(this._fallBackLng);
}
return fallbackLanguageList;
},
});
context.InitService.register(context.I18NService);
});