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

i18next.ts 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import COUNTRIES_RESOURCES from 'i18n-iso-countries/langs/en.json';
  2. import i18next from 'i18next';
  3. import I18nextXHRBackend, { HttpBackendOptions } from 'i18next-http-backend';
  4. import _ from 'lodash';
  5. import LANGUAGES_RESOURCES from '../../../../lang/languages.json';
  6. import MAIN_RESOURCES from '../../../../lang/main.json';
  7. import TRANSLATION_LANGUAGES_RESOURCES from '../../../../lang/translation-languages.json';
  8. import { I18NEXT_INITIALIZED, LANGUAGE_CHANGED } from './actionTypes';
  9. import languageDetector from './languageDetector';
  10. /**
  11. * Override certain country names.
  12. */
  13. const COUNTRIES_RESOURCES_OVERRIDES = {
  14. countries: {
  15. TW: 'Taiwan'
  16. }
  17. };
  18. /**
  19. * Merged country names.
  20. */
  21. const COUNTRIES = _.merge({}, COUNTRIES_RESOURCES, COUNTRIES_RESOURCES_OVERRIDES);
  22. /**
  23. * The available/supported languages.
  24. *
  25. * @public
  26. * @type {Array<string>}
  27. */
  28. export const LANGUAGES: Array<string> = Object.keys(LANGUAGES_RESOURCES);
  29. /**
  30. * The available/supported translation languages.
  31. *
  32. * @public
  33. * @type {Array<string>}
  34. */
  35. export const TRANSLATION_LANGUAGES: Array<string> = Object.keys(TRANSLATION_LANGUAGES_RESOURCES);
  36. /**
  37. * The available/supported translation languages head. (Languages displayed on the top ).
  38. *
  39. * @public
  40. * @type {Array<string>}
  41. */
  42. export const TRANSLATION_LANGUAGES_HEAD: Array<string> = [ 'en' ];
  43. /**
  44. * The default language.
  45. *
  46. * English is the default language.
  47. *
  48. * @public
  49. * @type {string} The default language.
  50. */
  51. export const DEFAULT_LANGUAGE = 'en';
  52. /**
  53. * The options to initialize i18next with.
  54. *
  55. * @type {i18next.InitOptions}
  56. */
  57. const options: i18next.InitOptions = {
  58. backend: <HttpBackendOptions>{
  59. loadPath: (lng: string[], ns: string[]) =>
  60. // eslint-disable-next-line no-extra-parens
  61. (ns[0] === 'main' ? 'lang/{{ns}}-{{lng}}.json' : 'lang/{{ns}}.json')
  62. },
  63. defaultNS: 'main',
  64. fallbackLng: DEFAULT_LANGUAGE,
  65. interpolation: {
  66. escapeValue: false // not needed for react as it escapes by default
  67. },
  68. load: 'languageOnly',
  69. ns: [ 'main', 'languages', 'countries', 'translation-languages' ],
  70. react: {
  71. // re-render when a new resource bundle is added
  72. // @ts-expect-error. Fixed in i18next 19.6.1.
  73. bindI18nStore: 'added',
  74. useSuspense: false
  75. },
  76. returnEmptyString: false,
  77. returnNull: false,
  78. // XXX i18next modifies the array lngWhitelist so make sure to clone
  79. // LANGUAGES.
  80. whitelist: LANGUAGES.slice()
  81. };
  82. i18next
  83. .use(navigator.product === 'ReactNative' ? {} : I18nextXHRBackend)
  84. .use(languageDetector)
  85. .init(options);
  86. // Add default language which is preloaded from the source code.
  87. i18next.addResourceBundle(
  88. DEFAULT_LANGUAGE,
  89. 'countries',
  90. COUNTRIES,
  91. /* deep */ true,
  92. /* overwrite */ true);
  93. i18next.addResourceBundle(
  94. DEFAULT_LANGUAGE,
  95. 'languages',
  96. LANGUAGES_RESOURCES,
  97. /* deep */ true,
  98. /* overwrite */ true);
  99. i18next.addResourceBundle(
  100. DEFAULT_LANGUAGE,
  101. 'translation-languages',
  102. TRANSLATION_LANGUAGES_RESOURCES,
  103. /* deep */ true,
  104. /* overwrite */ true);
  105. i18next.addResourceBundle(
  106. DEFAULT_LANGUAGE,
  107. 'main',
  108. MAIN_RESOURCES,
  109. /* deep */ true,
  110. /* overwrite */ true);
  111. // Add builtin languages.
  112. // XXX: Note we are using require here, because we want the side-effects of the
  113. // import, but imports can only be placed at the top, and it would be too early,
  114. // since i18next is not yet initialized at that point.
  115. require('./BuiltinLanguages');
  116. // Label change through dynamic branding is available only for web
  117. if (typeof APP !== 'undefined') {
  118. i18next.on('initialized', () => {
  119. APP.store.dispatch({ type: I18NEXT_INITIALIZED });
  120. });
  121. i18next.on('languageChanged', () => {
  122. APP.store.dispatch({ type: LANGUAGE_CHANGED });
  123. });
  124. }
  125. export default i18next;