您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import {
  2. createContext,
  3. useContext,
  4. } from 'use-context-selector'
  5. import type { Locale } from '@/i18n-config'
  6. import { getDocLanguage, getLanguage, getPricingPageLanguage } from '@/i18n-config/language'
  7. import { noop } from 'lodash-es'
  8. type II18NContext = {
  9. locale: Locale
  10. i18n: Record<string, any>
  11. setLocaleOnClient: (_lang: Locale, _reloadPage?: boolean) => Promise<void>
  12. }
  13. const I18NContext = createContext<II18NContext>({
  14. locale: 'en-US',
  15. i18n: {},
  16. setLocaleOnClient: async (_lang: Locale, _reloadPage?: boolean) => {
  17. noop()
  18. },
  19. })
  20. export const useI18N = () => useContext(I18NContext)
  21. export const useGetLanguage = () => {
  22. const { locale } = useI18N()
  23. return getLanguage(locale)
  24. }
  25. export const useGetPricingPageLanguage = () => {
  26. const { locale } = useI18N()
  27. return getPricingPageLanguage(locale)
  28. }
  29. const defaultDocBaseUrl = 'https://docs.dify.ai'
  30. export const useDocLink = (baseUrl?: string): ((path?: string, pathMap?: { [index: string]: string }) => string) => {
  31. let baseDocUrl = baseUrl || defaultDocBaseUrl
  32. baseDocUrl = (baseDocUrl.endsWith('/')) ? baseDocUrl.slice(0, -1) : baseDocUrl
  33. const { locale } = useI18N()
  34. const docLanguage = getDocLanguage(locale)
  35. return (path?: string, pathMap?: { [index: string]: string }): string => {
  36. const pathUrl = path || ''
  37. let targetPath = (pathMap) ? pathMap[locale] || pathUrl : pathUrl
  38. targetPath = (targetPath.startsWith('/')) ? targetPath.slice(1) : targetPath
  39. return `${baseDocUrl}/${docLanguage}/${targetPath}`
  40. }
  41. }
  42. export default I18NContext