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. constructor() {
  22. jqueryI18next.init(i18next, $, { useOptionsAttr: true });
  23. if (i18next.isInitialized) {
  24. _onI18nInitialized();
  25. } else {
  26. i18next.on('initialized', _onI18nInitialized);
  27. }
  28. i18next.on('languageChanged', _onI18nInitialized);
  29. }
  30. /**
  31. *
  32. */
  33. generateTranslationHTML(key: string, options: Object) {
  34. const optAttr
  35. = options ? ` data-i18n-options='${JSON.stringify(options)}'` : '';
  36. // XXX i18next expects undefined if options are missing.
  37. const text = i18next.t(key, options ? options : undefined);
  38. return `<span data-i18n="${key}"${optAttr}>${text}</span>`;
  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();