2017-02-22 17:26:33 +00:00
|
|
|
/* global $ */
|
|
|
|
import { i18n, DEFAULT_LANG } from '../../react/features/base/translation';
|
2016-10-26 16:39:51 +00:00
|
|
|
import jqueryI18next from 'jquery-i18next';
|
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
|
|
|
}
|
|
|
|
|
2016-10-31 22:16:30 +00:00
|
|
|
class Translation {
|
2017-02-22 17:26:33 +00:00
|
|
|
init () {
|
|
|
|
if (i18n.isInitialized)
|
|
|
|
initCompleted();
|
|
|
|
else
|
|
|
|
i18n.on('initialized', initCompleted);
|
2015-02-24 10:49:46 +00:00
|
|
|
|
2016-10-26 16:39:51 +00:00
|
|
|
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;
|
2017-02-22 17:26:33 +00:00
|
|
|
i18n.setLng(lang, {}, 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();
|