2015-07-28 17:49:58 +00:00
|
|
|
/* global $, require, config, interfaceConfig */
|
2016-10-26 16:39:51 +00:00
|
|
|
import i18n from 'i18next';
|
|
|
|
import XHR from 'i18next-xhr-backend';
|
|
|
|
import jqueryI18next from 'jquery-i18next';
|
2016-10-31 22:16:30 +00:00
|
|
|
import languagesR from "../../lang/languages.json";
|
|
|
|
import mainR from "../../lang/main.json";
|
|
|
|
import languages from "../../service/translation/languages";
|
2015-02-06 15:46:50 +00:00
|
|
|
|
2016-10-31 22:16:30 +00:00
|
|
|
const DEFAULT_LANG = languages.EN;
|
|
|
|
|
|
|
|
const defaultOptions = {
|
2016-10-26 16:39:51 +00:00
|
|
|
compatibilityAPI: 'v1',
|
|
|
|
compatibilityJSON: 'v1',
|
2015-02-06 15:46:50 +00:00
|
|
|
fallbackLng: DEFAULT_LANG,
|
|
|
|
load: "unspecific",
|
|
|
|
resGetPath: 'lang/__ns__-__lng__.json',
|
|
|
|
ns: {
|
|
|
|
namespaces: ['main', 'languages'],
|
|
|
|
defaultNs: 'main'
|
|
|
|
},
|
|
|
|
lngWhitelist : languages.getLanguages(),
|
|
|
|
fallbackOnNull: true,
|
2015-02-24 10:49:46 +00:00
|
|
|
fallbackOnEmpty: true,
|
2015-02-06 15:46:50 +00:00
|
|
|
useDataAttrOptions: true,
|
2016-10-26 16:39:51 +00:00
|
|
|
app: interfaceConfig.APP_NAME
|
2015-02-06 15:46:50 +00:00
|
|
|
};
|
|
|
|
|
2016-10-03 16:12:04 +00:00
|
|
|
function initCompleted() {
|
2016-10-26 16:39:51 +00:00
|
|
|
$("[data-i18n]").localize();
|
2015-02-06 15:46:50 +00:00
|
|
|
}
|
|
|
|
|
2015-12-29 12:41:43 +00:00
|
|
|
function getLangFromQuery() {
|
2015-02-24 10:49:46 +00:00
|
|
|
var query = window.location.search.substring(1);
|
|
|
|
var vars = query.split("&");
|
|
|
|
for (var i=0;i<vars.length;i++) {
|
|
|
|
var pair = vars[i].split("=");
|
|
|
|
if(pair[0] == "lang")
|
|
|
|
{
|
|
|
|
return pair[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-10-31 22:16:30 +00:00
|
|
|
class Translation {
|
|
|
|
init (settingsLang) {
|
2015-12-29 12:41:43 +00:00
|
|
|
let options = defaultOptions;
|
2015-02-24 10:49:46 +00:00
|
|
|
|
2015-12-29 12:41:43 +00:00
|
|
|
let lang = getLangFromQuery() || settingsLang || config.defaultLanguage;
|
2016-10-28 15:54:35 +00:00
|
|
|
// XXX If none of the above has been set then the 'lang' will be
|
|
|
|
// 'undefined' and the i18n lib will try to auto detect user's
|
|
|
|
// preferred language based on browser's locale.
|
|
|
|
// The interface config option allows to disable this auto detection
|
|
|
|
// by specifying the fallback language in that case.
|
2016-10-28 13:13:29 +00:00
|
|
|
let langDetection = interfaceConfig.LANG_DETECTION;
|
|
|
|
|
|
|
|
if (!langDetection && !lang) {
|
|
|
|
lang = DEFAULT_LANG;
|
|
|
|
}
|
|
|
|
|
2015-12-29 12:41:43 +00:00
|
|
|
if (lang) {
|
2015-02-06 15:46:50 +00:00
|
|
|
options.lng = lang;
|
2015-02-24 10:49:46 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 16:39:51 +00:00
|
|
|
i18n.use(XHR)
|
|
|
|
.use({
|
|
|
|
type: 'postProcessor',
|
|
|
|
name: "resolveAppName",
|
2016-10-31 22:16:30 +00:00
|
|
|
process: (res, key) => {
|
|
|
|
return i18n.t(key, {app: options.app});
|
|
|
|
}
|
2016-10-26 16:39:51 +00:00
|
|
|
})
|
|
|
|
.init(options, initCompleted);
|
|
|
|
// adds default language which is preloaded from code
|
|
|
|
i18n.addResourceBundle(DEFAULT_LANG, 'main', mainR, true, true);
|
|
|
|
i18n.addResourceBundle(
|
|
|
|
DEFAULT_LANG, 'languages', languagesR, true, true);
|
|
|
|
jqueryI18next.init(i18n, $, {useOptionsAttr: true});
|
2016-10-31 22:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setLanguage (lang) {
|
2015-02-06 15:46:50 +00:00
|
|
|
if(!lang)
|
|
|
|
lang = DEFAULT_LANG;
|
|
|
|
i18n.setLng(lang, defaultOptions, initCompleted);
|
2016-10-31 22:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentLanguage () {
|
2015-02-06 15:46:50 +00:00
|
|
|
return i18n.lng();
|
2016-10-31 22:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
translateElement (selector, options) {
|
2016-10-25 20:18:56 +00:00
|
|
|
// i18next expects undefined if options are missing, check if its null
|
2016-10-26 16:39:51 +00:00
|
|
|
selector.localize(
|
2016-10-25 20:18:56 +00:00
|
|
|
options === null ? undefined : options);
|
2016-10-31 22:16:30 +00:00
|
|
|
}
|
2015-02-20 16:17:46 +00:00
|
|
|
|
2016-10-31 22:16:30 +00:00
|
|
|
generateTranslationHTML (key, options) {
|
|
|
|
let optAttr = options
|
|
|
|
? ` data-i18n-options='${JSON.stringify(options)}'` : "";
|
|
|
|
let text = i18n.t(key, options === null ? undefined : options);
|
|
|
|
return `<span data-i18n="${key}"${optAttr}>${text}</span>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
addLanguageChangedListener(listener) {
|
2016-10-26 16:39:51 +00:00
|
|
|
i18n.on('languageChanged', listener);
|
2015-02-06 15:46:50 +00:00
|
|
|
}
|
2016-10-31 22:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default new Translation();
|