Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

translation.js 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. class Translation {
  15. addLanguageChangedListener(listener: Function) {
  16. i18next.on('languageChanged', listener);
  17. }
  18. generateTranslationHTML(key: string, options: Object) {
  19. const optAttr
  20. = options ? ` data-i18n-options='${JSON.stringify(options)}'` : '';
  21. // XXX i18next expects undefined if options are missing.
  22. const text = i18next.t(key, options ? options : undefined);
  23. return `<span data-i18n="${key}"${optAttr}>${text}</span>`;
  24. }
  25. getCurrentLanguage() {
  26. return i18next.lng();
  27. }
  28. init() {
  29. if (i18next.isInitialized)
  30. _onI18nInitialized();
  31. else
  32. i18next.on('initialized', _onI18nInitialized);
  33. jqueryI18next.init(i18next, $, { useOptionsAttr: true });
  34. }
  35. setLanguage(language: string = DEFAULT_LANGUAGE) {
  36. i18next.setLng(language, {}, _onI18nInitialized);
  37. }
  38. translateElement(selector: Object, options: Object) {
  39. // XXX i18next expects undefined if options are missing.
  40. selector.localize(options ? options : undefined);
  41. }
  42. }
  43. export default new Translation();