i18n.ts 2.1 KB

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