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

i18next.ts 3.5KB

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