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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* global interfaceConfig */
  2. import i18n from 'i18next';
  3. import XHR from 'i18next-xhr-backend';
  4. import { DEFAULT_LANG, languages } from './constants';
  5. import languagesR from '../../../../lang/languages.json';
  6. import mainR from '../../../../lang/main.json';
  7. import Browser from 'i18next-browser-languagedetector';
  8. import ConfigLanguageDetector from './ConfigLanguageDetector';
  9. /**
  10. * Default options to initialize i18next.
  11. *
  12. * @enum {string}
  13. */
  14. const defaultOptions = {
  15. compatibilityAPI: 'v1',
  16. compatibilityJSON: 'v1',
  17. fallbackLng: DEFAULT_LANG,
  18. load: 'unspecific',
  19. resGetPath: 'lang/__ns__-__lng__.json',
  20. ns: {
  21. namespaces: [ 'main', 'languages' ],
  22. defaultNs: 'main'
  23. },
  24. lngWhitelist: languages.getLanguages(),
  25. fallbackOnNull: true,
  26. fallbackOnEmpty: true,
  27. useDataAttrOptions: true,
  28. app: interfaceConfig.APP_NAME
  29. };
  30. /**
  31. * List of detectors to use in their order.
  32. *
  33. * @type {[*]}
  34. */
  35. const detectors = [ 'querystring', 'localStorage', 'configLanguageDetector' ];
  36. /**
  37. * Allow i18n to detect the system language from the browser.
  38. */
  39. if (interfaceConfig.LANG_DETECTION) {
  40. detectors.push('navigator');
  41. }
  42. /**
  43. * The language detectors.
  44. */
  45. const browser = new Browser(null, {
  46. order: detectors,
  47. lookupQuerystring: 'lang',
  48. lookupLocalStorage: 'language',
  49. caches: [ 'localStorage' ]
  50. });
  51. // adds a language detector that just checks the config
  52. browser.addDetector(ConfigLanguageDetector);
  53. i18n.use(XHR)
  54. .use(browser)
  55. .use({
  56. type: 'postProcessor',
  57. name: 'resolveAppName',
  58. process: (res, key) => i18n.t(key, { app: defaultOptions.app })
  59. })
  60. .init(defaultOptions);
  61. // adds default language which is preloaded from code
  62. i18n.addResourceBundle(DEFAULT_LANG, 'main', mainR, true, true);
  63. i18n.addResourceBundle(DEFAULT_LANG, 'languages', languagesR, true, true);
  64. export default i18n;