i18n.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. defaultVariables: {
  29. appName: window.ExposedSettings.appName,
  30. },
  31. },
  32. })
  33. // The webpackChunkName here will name this chunk (and thus the requested
  34. // script) according to the file name. See https://webpack.js.org/api/module-methods/#magic-comments
  35. // for details
  36. const localesPromise = import(
  37. /* webpackChunkName: "[request]" */ `../../locales/${LANG}.json`
  38. ).then(lang => {
  39. i18n.addResourceBundle(LANG, 'translation', lang)
  40. })
  41. export default localesPromise