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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* @flow */
  2. import jqueryI18next from 'jquery-i18next';
  3. import { i18next } from '../../react/features/base/i18n';
  4. declare var $: Function;
  5. /**
  6. * Notifies that the {@link i18next} instance has finished its initialization.
  7. *
  8. * @returns {void}
  9. * @private
  10. */
  11. function _onI18nInitialized() {
  12. $('[data-i18n]').localize();
  13. }
  14. /**
  15. *
  16. */
  17. class Translation {
  18. /**
  19. *
  20. */
  21. generateTranslationHTML(key: string, options: Object) {
  22. const optAttr
  23. = options ? ` data-i18n-options='${JSON.stringify(options)}'` : '';
  24. // XXX i18next expects undefined if options are missing.
  25. const text = i18next.t(key, options ? options : undefined);
  26. return `<span data-i18n="${key}"${optAttr}>${text}</span>`;
  27. }
  28. /**
  29. *
  30. */
  31. init() {
  32. jqueryI18next.init(i18next, $, { useOptionsAttr: true });
  33. if (i18next.isInitialized) {
  34. _onI18nInitialized();
  35. } else {
  36. i18next.on('initialized', _onI18nInitialized);
  37. }
  38. i18next.on('languageChanged', _onI18nInitialized);
  39. }
  40. /**
  41. *
  42. */
  43. translateElement(selector: Object, options: Object) {
  44. // XXX i18next expects undefined if options are missing.
  45. selector.localize(options ? options : undefined);
  46. }
  47. }
  48. export default new Translation();