2024-04-08 18:51:46 +08:00
|
|
|
import {
|
|
|
|
createContext,
|
|
|
|
useContext,
|
|
|
|
} from 'use-context-selector'
|
2023-05-15 08:51:32 +08:00
|
|
|
import type { Locale } from '@/i18n'
|
2025-04-30 14:58:49 +08:00
|
|
|
import { getDocLanguage, getLanguage, getPricingPageLanguage } from '@/i18n/language'
|
2025-04-09 12:10:17 +08:00
|
|
|
import { noop } from 'lodash-es'
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
type II18NContext = {
|
|
|
|
locale: Locale
|
2023-08-23 13:54:40 +08:00
|
|
|
i18n: Record<string, any>
|
2024-04-29 17:55:52 +08:00
|
|
|
setLocaleOnClient: (_lang: Locale, _reloadPage?: boolean) => void
|
2023-05-15 08:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const I18NContext = createContext<II18NContext>({
|
2024-02-23 14:31:06 +08:00
|
|
|
locale: 'en-US',
|
2023-05-15 08:51:32 +08:00
|
|
|
i18n: {},
|
2025-04-09 12:10:17 +08:00
|
|
|
setLocaleOnClient: noop,
|
2023-05-15 08:51:32 +08:00
|
|
|
})
|
|
|
|
|
2024-04-08 18:51:46 +08:00
|
|
|
export const useI18N = () => useContext(I18NContext)
|
|
|
|
export const useGetLanguage = () => {
|
|
|
|
const { locale } = useI18N()
|
|
|
|
|
|
|
|
return getLanguage(locale)
|
|
|
|
}
|
2025-04-30 14:58:49 +08:00
|
|
|
export const useGetPricingPageLanguage = () => {
|
|
|
|
const { locale } = useI18N()
|
|
|
|
|
|
|
|
return getPricingPageLanguage(locale)
|
|
|
|
}
|
2024-04-08 18:51:46 +08:00
|
|
|
|
2025-06-13 16:58:43 +08:00
|
|
|
const defaultDocBaseUrl = 'https://docs.dify.ai'
|
|
|
|
export const useDocLink = (baseUrl?: string): ((path?: string, pathMap?: { [index: string]: string }) => string) => {
|
2025-06-16 14:13:04 +08:00
|
|
|
let baseDocUrl = baseUrl || defaultDocBaseUrl
|
|
|
|
baseDocUrl = (baseDocUrl.endsWith('/')) ? baseDocUrl.slice(0, -1) : baseDocUrl
|
|
|
|
const { locale } = useI18N()
|
|
|
|
const docLanguage = getDocLanguage(locale)
|
|
|
|
return (path?: string, pathMap?: { [index: string]: string }): string => {
|
|
|
|
const pathUrl = path || ''
|
|
|
|
let targetPath = (pathMap) ? pathMap[locale] || pathUrl : pathUrl
|
|
|
|
targetPath = (targetPath.startsWith('/')) ? targetPath.slice(1) : targetPath
|
|
|
|
return `${baseDocUrl}/${docLanguage}/${targetPath}`
|
|
|
|
}
|
2025-06-13 16:58:43 +08:00
|
|
|
}
|
2023-05-15 08:51:32 +08:00
|
|
|
export default I18NContext
|