You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

i18next-config.ts 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. } catch {
  10. res = (await import(`../i18n/en-US/${namespace}`)).default;
  11. }
  12. return res;
  13. };
  14. const NAMESPACES = [
  15. "app-annotation",
  16. "app-api",
  17. "app-debug",
  18. "app-log",
  19. "app-overview",
  20. "app",
  21. "billing",
  22. "common",
  23. "custom",
  24. "dataset-creation",
  25. "dataset-documents",
  26. "dataset-hit-testing",
  27. "dataset-pipeline",
  28. "dataset-settings",
  29. "dataset",
  30. "education",
  31. "explore",
  32. "layout",
  33. "login",
  34. "oauth",
  35. "pipeline",
  36. "plugin-tags",
  37. "plugin",
  38. "register",
  39. "run-log",
  40. "share",
  41. "time",
  42. "tools",
  43. "workflow",
  44. ];
  45. export const loadLangResources = async (lang: string) => {
  46. const modules = await Promise.all(
  47. NAMESPACES.map((ns) => requireSilent(lang, ns))
  48. );
  49. const resources = modules.reduce((acc, mod, index) => {
  50. acc[camelCase(NAMESPACES[index])] = mod;
  51. return acc;
  52. }, {} as Record<string, any>);
  53. return resources;
  54. };
  55. /**
  56. * !Need to load en-US and zh-Hans resources for initial rendering, which are used in both marketplace and dify
  57. * !Other languages will be loaded on demand
  58. * !This is to avoid loading all languages at once which can be slow
  59. */
  60. const getInitialTranslations = () => {
  61. const en_USResources = NAMESPACES.reduce((acc, ns, index) => {
  62. acc[camelCase(NAMESPACES[index])] = require(`../i18n/en-US/${ns}`).default;
  63. return acc;
  64. }, {} as Record<string, any>);
  65. const zh_HansResources = NAMESPACES.reduce((acc, ns, index) => {
  66. acc[camelCase(NAMESPACES[index])] =
  67. require(`../i18n/zh-Hans/${ns}`).default;
  68. return acc;
  69. }, {} as Record<string, any>);
  70. return {
  71. "en-US": {
  72. translation: en_USResources,
  73. },
  74. "zh-Hans": {
  75. translation: zh_HansResources,
  76. },
  77. };
  78. };
  79. if (!i18n.isInitialized) {
  80. i18n.use(initReactI18next).init({
  81. lng: undefined,
  82. fallbackLng: "en-US",
  83. resources: getInitialTranslations(),
  84. });
  85. }
  86. export const changeLanguage = async (lng?: string) => {
  87. if (!lng) return;
  88. const resource = await loadLangResources(lng);
  89. if (!i18n.hasResourceBundle(lng, "translation"))
  90. i18n.addResourceBundle(lng, "translation", resource, true, true);
  91. await i18n.changeLanguage(lng);
  92. };
  93. export default i18n;