Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

i18next-config.ts 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. 'oauth',
  35. 'plugin-tags',
  36. 'plugin',
  37. 'register',
  38. 'run-log',
  39. 'share',
  40. 'time',
  41. 'tools',
  42. 'workflow',
  43. ]
  44. export const loadLangResources = async (lang: string) => {
  45. const modules = await Promise.all(NAMESPACES.map(ns => requireSilent(lang, ns)))
  46. const resources = modules.reduce((acc, mod, index) => {
  47. acc[camelCase(NAMESPACES[index])] = mod
  48. return acc
  49. }, {} as Record<string, any>)
  50. return resources
  51. }
  52. /**
  53. * !Need to load en-US and zh-Hans resources for initial rendering, which are used in both marketplace and dify
  54. * !Other languages will be loaded on demand
  55. * !This is to avoid loading all languages at once which can be slow
  56. */
  57. const getInitialTranslations = () => {
  58. const en_USResources = NAMESPACES.reduce((acc, ns, index) => {
  59. acc[camelCase(NAMESPACES[index])] = require(`../i18n/en-US/${ns}`).default
  60. return acc
  61. }, {} as Record<string, any>)
  62. const zh_HansResources = NAMESPACES.reduce((acc, ns, index) => {
  63. acc[camelCase(NAMESPACES[index])] = require(`../i18n/zh-Hans/${ns}`).default
  64. return acc
  65. }, {} as Record<string, any>)
  66. return {
  67. 'en-US': {
  68. translation: en_USResources,
  69. },
  70. 'zh-Hans': {
  71. translation: zh_HansResources,
  72. },
  73. }
  74. }
  75. if (!i18n.isInitialized) {
  76. i18n.use(initReactI18next)
  77. .init({
  78. lng: undefined,
  79. fallbackLng: 'en-US',
  80. resources: getInitialTranslations(),
  81. })
  82. }
  83. export const changeLanguage = async (lng?: string) => {
  84. if (!lng) return
  85. const resource = await loadLangResources(lng)
  86. if (!i18n.hasResourceBundle(lng, 'translation'))
  87. i18n.addResourceBundle(lng, 'translation', resource, true, true)
  88. await i18n.changeLanguage(lng)
  89. }
  90. export default i18n