Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

1.1 KB
Raw Blame
import type { ResourceKey } from 'i18next'
import type { Locale } from './locale'
import type { Namespace, NamespaceInFileName } from './resources'
import { kebabCase } from 'es-toolkit/string'
import { defaultLocale, normalizeLocale } from './locale'

type LocaleResourceModule = {
  loadResource: (fileNamespace: string) => Promise<{ default: ResourceKey }>
}

const contactsLocalizedLocales = new Set<Locale>(['en-US', 'zh-Hans'])

const loadLocaleResources = (locale: string): Promise<LocaleResourceModule> => {
  const normalized = normalizeLocale(locale)
  return import(`./locale-resources/${normalized}.ts`)
}

export const loadI18nResource = async (
  locale: string,
  namespace: Namespace | NamespaceInFileName,
) => {
  const normalizedLocale = normalizeLocale(locale)
  const fileNamespace = kebabCase(namespace)
  const resourceLocale =
    fileNamespace === 'contacts' && !contactsLocalizedLocales.has(normalizedLocale)
      ? defaultLocale
      : normalizedLocale
  const { loadResource } = await loadLocaleResources(resourceLocale)
  return loadResource(fileNamespace)
}