Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

i18next-config.ts 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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(`../i18n/${lang}/${namespace}`)).default
  9. }
  10. catch {
  11. res = (await import(`../i18n/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 resources
  50. }
  51. /**
  52. * !Need to load en-US and zh-Hans resources for initial rendering, which are used in both marketplace and dify
  53. * !Other languages will be loaded on demand
  54. * !This is to avoid loading all languages at once which can be slow
  55. */
  56. const getInitialTranslations = () => {
  57. const en_USResources = NAMESPACES.reduce((acc, ns, index) => {
  58. acc[camelCase(NAMESPACES[index])] = require(`../i18n/en-US/${ns}`).default
  59. return acc
  60. }, {} as Record<string, any>)
  61. const zh_HansResources = NAMESPACES.reduce((acc, ns, index) => {
  62. acc[camelCase(NAMESPACES[index])] = require(`../i18n/zh-Hans/${ns}`).default
  63. return acc
  64. }, {} as Record<string, any>)
  65. return {
  66. 'en-US': {
  67. translation: en_USResources,
  68. },
  69. 'zh-Hans': {
  70. translation: zh_HansResources,
  71. },
  72. }
  73. }
  74. if (!i18n.isInitialized) {
  75. i18n.use(initReactI18next)
  76. .init({
  77. lng: undefined,
  78. fallbackLng: 'en-US',
  79. resources: getInitialTranslations(),
  80. })
  81. }
  82. export const changeLanguage = async (lng?: string) => {
  83. if (!lng) return
  84. const resource = await loadLangResources(lng)
  85. if (!i18n.hasResourceBundle(lng, 'translation'))
  86. i18n.addResourceBundle(lng, 'translation', resource, true, true)
  87. await i18n.changeLanguage(lng)
  88. }
  89. export default i18n