i18n.ts 2.0 KB

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