Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

translation.js 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* @flow */
  2. import jqueryI18next from 'jquery-i18next';
  3. import { DEFAULT_LANGUAGE, 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. addLanguageChangedListener(listener: Function) {
  22. i18next.on('languageChanged', listener);
  23. }
  24. /**
  25. *
  26. */
  27. generateTranslationHTML(key: string, options: Object) {
  28. const optAttr
  29. = options ? ` data-i18n-options='${JSON.stringify(options)}'` : '';
  30. // XXX i18next expects undefined if options are missing.
  31. const text = i18next.t(key, options ? options : undefined);
  32. return `<span data-i18n="${key}"${optAttr}>${text}</span>`;
  33. }
  34. /**
  35. *
  36. */
  37. getCurrentLanguage() {
  38. return i18next.lng();
  39. }
  40. /**
  41. *
  42. */
  43. init() {
  44. jqueryI18next.init(i18next, $, { useOptionsAttr: true });
  45. if (i18next.isInitialized) {
  46. _onI18nInitialized();
  47. } else {
  48. i18next.on('initialized', _onI18nInitialized);
  49. }
  50. }
  51. /**
  52. *
  53. */
  54. setLanguage(language: string = DEFAULT_LANGUAGE) {
  55. i18next.setLng(language, {}, _onI18nInitialized);
  56. }
  57. /**
  58. *
  59. */
  60. translateElement(selector: Object, options: Object) {
  61. // XXX i18next expects undefined if options are missing.
  62. selector.localize(options ? options : undefined);
  63. }
  64. }
  65. export default new Translation();