2025-12-30 06:38:23 +00:00
|
|
|
import type { Locale } from '@/i18n-config/language'
|
2026-01-21 08:31:48 +00:00
|
|
|
import type { DocPathWithoutLang } from '@/types/doc-paths'
|
2026-01-07 05:20:09 +00:00
|
|
|
import { useTranslation } from '#i18n'
|
2026-01-30 09:30:24 +00:00
|
|
|
import { useCallback } from 'react'
|
2025-07-25 07:01:28 +00:00
|
|
|
import { getDocLanguage, getLanguage, getPricingPageLanguage } from '@/i18n-config/language'
|
2026-01-21 08:31:48 +00:00
|
|
|
import { apiReferencePathTranslations } from '@/types/doc-paths'
|
2023-05-15 00:51:32 +00:00
|
|
|
|
2025-12-30 06:38:23 +00:00
|
|
|
export const useLocale = () => {
|
2026-01-07 05:20:09 +00:00
|
|
|
const { i18n } = useTranslation()
|
|
|
|
|
return i18n.language as Locale
|
2023-05-15 00:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
2024-04-08 10:51:46 +00:00
|
|
|
export const useGetLanguage = () => {
|
2025-12-30 06:38:23 +00:00
|
|
|
const locale = useLocale()
|
2024-04-08 10:51:46 +00:00
|
|
|
|
|
|
|
|
return getLanguage(locale)
|
|
|
|
|
}
|
2025-04-30 06:58:49 +00:00
|
|
|
export const useGetPricingPageLanguage = () => {
|
2025-12-30 06:38:23 +00:00
|
|
|
const locale = useLocale()
|
2025-04-30 06:58:49 +00:00
|
|
|
|
|
|
|
|
return getPricingPageLanguage(locale)
|
|
|
|
|
}
|
2024-04-08 10:51:46 +00:00
|
|
|
|
2025-08-28 13:13:18 +00:00
|
|
|
export const defaultDocBaseUrl = 'https://docs.dify.ai'
|
2026-01-21 08:31:48 +00:00
|
|
|
export type DocPathMap = Partial<Record<Locale, DocPathWithoutLang>>
|
|
|
|
|
|
|
|
|
|
export const useDocLink = (baseUrl?: string): ((path?: DocPathWithoutLang, pathMap?: DocPathMap) => string) => {
|
2025-06-16 06:13:04 +00:00
|
|
|
let baseDocUrl = baseUrl || defaultDocBaseUrl
|
|
|
|
|
baseDocUrl = (baseDocUrl.endsWith('/')) ? baseDocUrl.slice(0, -1) : baseDocUrl
|
2025-12-30 06:38:23 +00:00
|
|
|
const locale = useLocale()
|
2026-01-30 09:30:24 +00:00
|
|
|
return useCallback(
|
|
|
|
|
(path?: DocPathWithoutLang, pathMap?: DocPathMap): string => {
|
|
|
|
|
const docLanguage = getDocLanguage(locale)
|
|
|
|
|
const pathUrl = path || ''
|
|
|
|
|
let targetPath = (pathMap) ? pathMap[locale] || pathUrl : pathUrl
|
|
|
|
|
let languagePrefix = `/${docLanguage}`
|
|
|
|
|
|
2026-02-04 06:58:26 +00:00
|
|
|
if (targetPath.startsWith('/api-reference/')) {
|
|
|
|
|
languagePrefix = ''
|
|
|
|
|
if (docLanguage !== 'en') {
|
|
|
|
|
const translatedPath = apiReferencePathTranslations[targetPath]?.[docLanguage]
|
|
|
|
|
if (translatedPath) {
|
|
|
|
|
targetPath = translatedPath
|
|
|
|
|
}
|
2026-01-30 09:30:24 +00:00
|
|
|
}
|
2026-01-22 07:02:37 +00:00
|
|
|
}
|
2026-01-21 08:31:48 +00:00
|
|
|
|
2026-01-30 09:30:24 +00:00
|
|
|
return `${baseDocUrl}${languagePrefix}${targetPath}`
|
|
|
|
|
},
|
|
|
|
|
[baseDocUrl, locale],
|
|
|
|
|
)
|
2025-06-13 08:58:43 +00:00
|
|
|
}
|