選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

i18n.ts 1.4KB

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