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 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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-pipeline',
  29. 'dataset-settings',
  30. 'dataset',
  31. 'education',
  32. 'explore',
  33. 'layout',
  34. 'login',
  35. 'oauth',
  36. 'pipeline',
  37. 'plugin-tags',
  38. 'plugin',
  39. 'register',
  40. 'run-log',
  41. 'share',
  42. 'time',
  43. 'tools',
  44. 'workflow',
  45. ]
  46. export const loadLangResources = async (lang: string) => {
  47. const modules = await Promise.all(
  48. NAMESPACES.map(ns => requireSilent(lang, ns)),
  49. )
  50. const resources = modules.reduce((acc, mod, index) => {
  51. acc[camelCase(NAMESPACES[index])] = mod
  52. return acc
  53. }, {} as Record<string, any>)
  54. return resources
  55. }
  56. // Load en-US resources first to make sure fallback works
  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. return {
  63. 'en-US': {
  64. translation: en_USResources,
  65. },
  66. }
  67. }
  68. if (!i18n.isInitialized) {
  69. i18n.use(initReactI18next).init({
  70. lng: undefined,
  71. fallbackLng: 'en-US',
  72. resources: getInitialTranslations(),
  73. })
  74. }
  75. export const changeLanguage = async (lng?: string) => {
  76. if (!lng) return
  77. if (!i18n.hasResourceBundle(lng, 'translation')) {
  78. const resource = await loadLangResources(lng)
  79. i18n.addResourceBundle(lng, 'translation', resource, true, true)
  80. }
  81. await i18n.changeLanguage(lng)
  82. }
  83. export default i18n