You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

i18next.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // @flow
  2. declare var APP: Object;
  3. import COUNTRIES_RESOURCES from 'i18n-iso-countries/langs/en.json';
  4. import i18next from 'i18next';
  5. import I18nextXHRBackend from 'i18next-xhr-backend';
  6. import _ from 'lodash';
  7. import LANGUAGES_RESOURCES from '../../../../lang/languages.json';
  8. import MAIN_RESOURCES from '../../../../lang/main.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 default language.
  32. *
  33. * English is the default language.
  34. *
  35. * @public
  36. * @type {string} The default language.
  37. */
  38. export const DEFAULT_LANGUAGE = 'en';
  39. /**
  40. * The options to initialize i18next with.
  41. *
  42. * @type {Object}
  43. */
  44. const options = {
  45. backend: {
  46. loadPath: 'lang/{{ns}}-{{lng}}.json'
  47. },
  48. defaultNS: 'main',
  49. fallbackLng: DEFAULT_LANGUAGE,
  50. interpolation: {
  51. escapeValue: false // not needed for react as it escapes by default
  52. },
  53. load: 'languageOnly',
  54. ns: [ 'main', 'languages', 'countries' ],
  55. react: {
  56. // re-render when a new resource bundle is added
  57. bindI18nStore: 'added',
  58. useSuspense: false
  59. },
  60. returnEmptyString: false,
  61. returnNull: false,
  62. // XXX i18next modifies the array lngWhitelist so make sure to clone
  63. // LANGUAGES.
  64. whitelist: LANGUAGES.slice()
  65. };
  66. i18next
  67. .use(navigator.product === 'ReactNative' ? {} : I18nextXHRBackend)
  68. .use(languageDetector)
  69. .init(options);
  70. // Add default language which is preloaded from the source code.
  71. i18next.addResourceBundle(
  72. DEFAULT_LANGUAGE,
  73. 'countries',
  74. COUNTRIES,
  75. /* deep */ true,
  76. /* overwrite */ true);
  77. i18next.addResourceBundle(
  78. DEFAULT_LANGUAGE,
  79. 'languages',
  80. LANGUAGES_RESOURCES,
  81. /* deep */ true,
  82. /* overwrite */ true);
  83. i18next.addResourceBundle(
  84. DEFAULT_LANGUAGE,
  85. 'main',
  86. MAIN_RESOURCES,
  87. /* deep */ true,
  88. /* overwrite */ true);
  89. // Add builtin languages.
  90. // XXX: Note we are using require here, because we want the side-effects of the
  91. // import, but imports can only be placed at the top, and it would be too early,
  92. // since i18next is not yet initialized at that point.
  93. require('./BuiltinLanguages');
  94. // Label change through dynamic branding is available only for web
  95. if (typeof APP !== 'undefined') {
  96. i18next.on('initialized', () => {
  97. APP.store.dispatch({ type: I18NEXT_INITIALIZED });
  98. });
  99. i18next.on('languageChanged', () => {
  100. APP.store.dispatch({ type: LANGUAGE_CHANGED });
  101. });
  102. }
  103. export default i18next;