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

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