You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

translation.js 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* global $ */
  2. import { i18n, DEFAULT_LANG } from '../../react/features/base/translation';
  3. import jqueryI18next from 'jquery-i18next';
  4. function initCompleted() {
  5. $("[data-i18n]").localize();
  6. }
  7. class Translation {
  8. init () {
  9. if (i18n.isInitialized)
  10. initCompleted();
  11. else
  12. i18n.on('initialized', initCompleted);
  13. jqueryI18next.init(i18n, $, {useOptionsAttr: true});
  14. }
  15. setLanguage (lang) {
  16. if(!lang)
  17. lang = DEFAULT_LANG;
  18. i18n.setLng(lang, {}, initCompleted);
  19. }
  20. getCurrentLanguage () {
  21. return i18n.lng();
  22. }
  23. translateElement (selector, options) {
  24. // i18next expects undefined if options are missing, check if its null
  25. selector.localize(
  26. options === null ? undefined : options);
  27. }
  28. generateTranslationHTML (key, options) {
  29. let optAttr = options
  30. ? ` data-i18n-options='${JSON.stringify(options)}'` : "";
  31. let text = i18n.t(key, options === null ? undefined : options);
  32. return `<span data-i18n="${key}"${optAttr}>${text}</span>`;
  33. }
  34. addLanguageChangedListener(listener) {
  35. i18n.on('languageChanged', listener);
  36. }
  37. }
  38. export default new Translation();