2017-03-01 02:55:12 +00:00
|
|
|
/* @flow */
|
|
|
|
|
2016-10-26 16:39:51 +00:00
|
|
|
import jqueryI18next from 'jquery-i18next';
|
2015-02-06 15:46:50 +00:00
|
|
|
|
2017-11-21 02:21:35 +00:00
|
|
|
import { i18next } from '../../react/features/base/i18n';
|
2017-03-01 02:55:12 +00:00
|
|
|
|
|
|
|
declare var $: Function;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifies that the {@link i18next} instance has finished its initialization.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
function _onI18nInitialized() {
|
|
|
|
$('[data-i18n]').localize();
|
2015-02-06 15:46:50 +00:00
|
|
|
}
|
|
|
|
|
2017-10-12 23:02:29 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2016-10-31 22:16:30 +00:00
|
|
|
class Translation {
|
2017-10-12 23:02:29 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2018-05-17 21:09:32 +00:00
|
|
|
constructor() {
|
2017-03-07 22:46:37 +00:00
|
|
|
jqueryI18next.init(i18next, $, { useOptionsAttr: true });
|
|
|
|
|
2017-10-12 23:02:29 +00:00
|
|
|
if (i18next.isInitialized) {
|
2017-03-01 02:55:12 +00:00
|
|
|
_onI18nInitialized();
|
2017-10-12 23:02:29 +00:00
|
|
|
} else {
|
2017-03-01 02:55:12 +00:00
|
|
|
i18next.on('initialized', _onI18nInitialized);
|
2017-10-12 23:02:29 +00:00
|
|
|
}
|
2015-02-20 16:17:46 +00:00
|
|
|
|
2017-11-21 02:21:35 +00:00
|
|
|
i18next.on('languageChanged', _onI18nInitialized);
|
2016-10-31 22:16:30 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 21:09:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
generateTranslationHTML(key: string, options: Object) {
|
|
|
|
const optAttr
|
|
|
|
= options ? ` data-i18n-options='${JSON.stringify(options)}'` : '';
|
|
|
|
|
|
|
|
// XXX i18next expects undefined if options are missing.
|
|
|
|
const text = i18next.t(key, options ? options : undefined);
|
|
|
|
|
|
|
|
return `<span data-i18n="${key}"${optAttr}>${text}</span>`;
|
|
|
|
}
|
|
|
|
|
2017-10-12 23:02:29 +00:00
|
|
|
/**
|
2017-11-16 18:26:14 +00:00
|
|
|
*
|
2017-10-12 23:02:29 +00:00
|
|
|
*/
|
2017-03-01 02:55:12 +00:00
|
|
|
translateElement(selector: Object, options: Object) {
|
|
|
|
// XXX i18next expects undefined if options are missing.
|
|
|
|
selector.localize(options ? options : undefined);
|
2015-02-06 15:46:50 +00:00
|
|
|
}
|
2016-10-31 22:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default new Translation();
|