i18n.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. },
  21. interpolation: {
  22. // We use the legacy v1 JSON format, so configure interpolator to use
  23. // underscores instead of curly braces
  24. prefix: '__',
  25. suffix: '__',
  26. unescapeSuffix: 'HTML',
  27. // Disable nesting in interpolated values, preventing user input
  28. // injection via another nested value
  29. skipOnVariables: true,
  30. // Do not escape values, as `t` + React will already escape them
  31. // (`escapeValue: true` and `shouldUnescape` must be set on each use of `Trans`)
  32. escapeValue: false,
  33. defaultVariables: {
  34. appName: window.ExposedSettings.appName,
  35. },
  36. },
  37. })
  38. // The webpackChunkName here will name this chunk (and thus the requested
  39. // script) according to the file name. See https://webpack.js.org/api/module-methods/#magic-comments
  40. // for details
  41. const localesPromise = import(
  42. /* webpackChunkName: "[request]" */ `../../locales/${LANG}.json`
  43. ).then(lang => {
  44. i18n.addResourceBundle(LANG, 'translation', lang)
  45. })
  46. export default localesPromise