i18n.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import i18n from 'i18next'
  2. import { initReactI18next } from 'react-i18next'
  3. const LANG = window.i18n.currentLangCode
  4. // Since we are rendering React from Angular, the initialisation is
  5. // synchronous on page load (but hidden behind the loading screen). This
  6. // means that translations must be initialised without any actual
  7. // translation strings, and load those manually ourselves later
  8. i18n.use(initReactI18next).init({
  9. lng: LANG,
  10. // still using the v3 plural suffixes
  11. compatibilityJSON: 'v3',
  12. react: {
  13. // Since we are manually waiting on the translations data to
  14. // load, we don't need to use Suspense
  15. useSuspense: false,
  16. // Trigger a re-render when a language is added. Since we load the
  17. // translation strings asynchronously, we need to trigger a re-render once
  18. // they've loaded
  19. bindI18nStore: 'added',
  20. // Disable automatic conversion of basic markup to React components
  21. transSupportBasicHtmlNodes: false,
  22. },
  23. interpolation: {
  24. // We use the legacy v1 JSON format, so configure interpolator to use
  25. // underscores instead of curly braces
  26. prefix: '__',
  27. suffix: '__',
  28. unescapeSuffix: 'HTML',
  29. // Disable nesting in interpolated values, preventing user input
  30. // injection via another nested value
  31. skipOnVariables: true,
  32. // Do not escape values, as `t` + React will already escape them
  33. // (`escapeValue: true` and `shouldUnescape` must be set on each use of `Trans`)
  34. escapeValue: false,
  35. defaultVariables: {
  36. appName: window.ExposedSettings.appName,
  37. },
  38. },
  39. })
  40. // The webpackChunkName here will name this chunk (and thus the requested
  41. // script) according to the file name. See https://webpack.js.org/api/module-methods/#magic-comments
  42. // for details
  43. const localesPromise = import(
  44. /* webpackChunkName: "[request]" */ `../../locales/${LANG}.json`
  45. ).then(lang => {
  46. i18n.addResourceBundle(LANG, 'translation', lang)
  47. })
  48. export default localesPromise