Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

i18next-config.ts 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 'use client'
  2. import i18n from 'i18next'
  3. import { camelCase } from 'lodash-es'
  4. import { initReactI18next } from 'react-i18next'
  5. const requireSilent = async (lang: string, namespace: string) => {
  6. let res
  7. try {
  8. res = (await import(`./${lang}/${namespace}`)).default
  9. }
  10. catch {
  11. res = (await import(`./en-US/${namespace}`)).default
  12. }
  13. return res
  14. }
  15. const NAMESPACES = [
  16. 'app-annotation',
  17. 'app-api',
  18. 'app-debug',
  19. 'app-log',
  20. 'app-overview',
  21. 'app',
  22. 'billing',
  23. 'common',
  24. 'custom',
  25. 'dataset-creation',
  26. 'dataset-documents',
  27. 'dataset-hit-testing',
  28. 'dataset-settings',
  29. 'dataset',
  30. 'education',
  31. 'explore',
  32. 'layout',
  33. 'login',
  34. 'plugin-tags',
  35. 'plugin',
  36. 'register',
  37. 'run-log',
  38. 'share',
  39. 'time',
  40. 'tools',
  41. 'workflow',
  42. ]
  43. export const loadLangResources = async (lang: string) => {
  44. const modules = await Promise.all(NAMESPACES.map(ns => requireSilent(lang, ns)))
  45. const resources = modules.reduce((acc, mod, index) => {
  46. acc[camelCase(NAMESPACES[index])] = mod
  47. return acc
  48. }, {} as Record<string, any>)
  49. return {
  50. translation: resources,
  51. }
  52. }
  53. i18n.use(initReactI18next)
  54. .init({
  55. lng: undefined,
  56. fallbackLng: 'en-US',
  57. })
  58. export const changeLanguage = async (lng?: string) => {
  59. const resolvedLng = lng ?? 'en-US'
  60. const resources = {
  61. [resolvedLng]: await loadLangResources(resolvedLng),
  62. }
  63. if (!i18n.hasResourceBundle(resolvedLng, 'translation'))
  64. i18n.addResourceBundle(resolvedLng, 'translation', resources[resolvedLng].translation, true, true)
  65. await i18n.changeLanguage(resolvedLng)
  66. }
  67. export default i18n