i18n.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. react: {
  11. // Since we are manually waiting on the translations data to
  12. // load, we don't need to use Suspense
  13. useSuspense: false,
  14. // Trigger a re-render when a language is added. Since we load the
  15. // translation strings asynchronously, we need to trigger a re-render once
  16. // they've loaded
  17. bindI18nStore: 'added',
  18. },
  19. interpolation: {
  20. // We use the legacy v1 JSON format, so configure interpolator to use
  21. // underscores instead of curly braces
  22. prefix: '__',
  23. suffix: '__',
  24. unescapeSuffix: 'HTML',
  25. // Disable nesting in interpolated values, preventing user input
  26. // injection via another nested value
  27. skipOnVariables: true,
  28. // Do not escape values, as `t` + React will already escape them
  29. // (`escapeValue: true` and `shouldUnescape` must be set on each use of `Trans`)
  30. escapeValue: false,
  31. defaultVariables: {
  32. appName: window.ExposedSettings.appName,
  33. },
  34. },
  35. })
  36. // The webpackChunkName here will name this chunk (and thus the requested
  37. // script) according to the file name. See https://webpack.js.org/api/module-methods/#magic-comments
  38. // for details
  39. const localesPromise = import(
  40. /* webpackChunkName: "[request]" */ `../../locales/${LANG}.json`
  41. ).then(lang => {
  42. i18n.addResourceBundle(LANG, 'translation', lang)
  43. })
  44. export default localesPromise