選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

i18next.ts 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import COUNTRIES_RESOURCES from 'i18n-iso-countries/langs/en.json';
  2. import i18next from 'i18next';
  3. import I18nextXHRBackend from 'i18next-xhr-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 {Object}
  56. */
  57. const options = {
  58. backend: {
  59. loadPath: 'lang/{{ns}}-{{lng}}.json'
  60. },
  61. defaultNS: 'main',
  62. fallbackLng: DEFAULT_LANGUAGE,
  63. interpolation: {
  64. escapeValue: false // not needed for react as it escapes by default
  65. },
  66. load: 'languageOnly',
  67. ns: [ 'main', 'languages', 'countries', 'translation-languages' ],
  68. react: {
  69. // re-render when a new resource bundle is added
  70. bindI18nStore: 'added',
  71. useSuspense: false
  72. },
  73. returnEmptyString: false,
  74. returnNull: false,
  75. // XXX i18next modifies the array lngWhitelist so make sure to clone
  76. // LANGUAGES.
  77. whitelist: LANGUAGES.slice()
  78. };
  79. i18next
  80. .use(navigator.product === 'ReactNative' ? {} : I18nextXHRBackend)
  81. .use(languageDetector) // @ts-ignore
  82. .init(options);
  83. // Add default language which is preloaded from the source code.
  84. i18next.addResourceBundle(
  85. DEFAULT_LANGUAGE,
  86. 'countries',
  87. COUNTRIES,
  88. /* deep */ true,
  89. /* overwrite */ true);
  90. i18next.addResourceBundle(
  91. DEFAULT_LANGUAGE,
  92. 'languages',
  93. LANGUAGES_RESOURCES,
  94. /* deep */ true,
  95. /* overwrite */ true);
  96. i18next.addResourceBundle(
  97. DEFAULT_LANGUAGE,
  98. 'translation-languages',
  99. TRANSLATION_LANGUAGES_RESOURCES,
  100. /* deep */ true,
  101. /* overwrite */ true);
  102. i18next.addResourceBundle(
  103. DEFAULT_LANGUAGE,
  104. 'main',
  105. MAIN_RESOURCES,
  106. /* deep */ true,
  107. /* overwrite */ true);
  108. // Add builtin languages.
  109. // XXX: Note we are using require here, because we want the side-effects of the
  110. // import, but imports can only be placed at the top, and it would be too early,
  111. // since i18next is not yet initialized at that point.
  112. require('./BuiltinLanguages');
  113. // Label change through dynamic branding is available only for web
  114. if (typeof APP !== 'undefined') {
  115. i18next.on('initialized', () => {
  116. APP.store.dispatch({ type: I18NEXT_INITIALIZED });
  117. });
  118. i18next.on('languageChanged', () => {
  119. APP.store.dispatch({ type: LANGUAGE_CHANGED });
  120. });
  121. }
  122. export default i18next;