Files
gsfdc/demo-dify/dify/web/context/i18n.ts
T
wuzilongandCursor c2cb845a32 feat(dify): 把本地控制台的中文与去外链改动入库
这些界面改动和专家服务草稿停在工作区,没有随上一笔主分支提交进入仓库。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-23 09:44:09 +08:00

114 lines
3.9 KiB
TypeScript

import type { DeploymentEdition } from '@dify/contracts/api/console/system-features/types.gen'
import type { Locale } from '@/i18n/locale'
import type { DocLanguage, DocPathWithoutLang, DocsProduct } from '@/types/doc-paths'
import { useAtomValue } from 'jotai'
import { useCallback } from 'react'
import { useLocale } from '#i18n'
import { deploymentEditionAtom } from '@/features/system-features/state'
import { getDocLanguage } from '@/i18n/language'
import { getPluginLanguage } from '@/i18n/metadata'
import { docPathProductAvailability, isProductlessDocPath } from '@/types/doc-paths'
export const useGetLanguage = () => {
const locale = useLocale()
return getPluginLanguage(locale)
}
export const defaultDocBaseUrl = 'https://docs.dify.ai'
export const enterpriseDocBaseUrl = 'https://enterprise-docs.dify.ai'
export type DocPathMap = Partial<Record<Locale, DocPathWithoutLang>>
const getDocHomePath = () => '/home'
const getCurrentDocsProduct = (deploymentEdition: DeploymentEdition): DocsProduct => {
if (deploymentEdition === 'CLOUD') return 'cloud'
return 'self-host'
}
const splitPathHash = (path: string) => {
const hashIndex = path.indexOf('#')
if (hashIndex === -1) {
return {
pathname: path,
hash: '',
}
}
return {
pathname: path.slice(0, hashIndex),
hash: path.slice(hashIndex),
}
}
const getProductAwarePath = (path: string, deploymentEdition: DeploymentEdition): string => {
const { pathname, hash } = splitPathHash(path)
if (!isProductlessDocPath(pathname)) return path
const availableProducts: readonly DocsProduct[] = docPathProductAvailability[pathname]
const currentProduct = getCurrentDocsProduct(deploymentEdition)
const targetProduct = availableProducts.includes(currentProduct)
? currentProduct
: availableProducts[0]
if (!targetProduct) return path
return `/${targetProduct}${pathname}${hash}`
}
const replacePathPrefix = (path: string, sourcePrefix: string, targetPrefix: string): string => {
if (path === sourcePrefix) return targetPrefix
if (!path.startsWith(`${sourcePrefix}/`)) return path
return `${targetPrefix}${path.slice(sourcePrefix.length)}`
}
const enterpriseDocPathOverrides: Readonly<Record<string, string>> = {
'/use-dify/getting-started/introduction': '/use/build/workflow-chatflow',
'/cli/overview': '/develop/cli/introduction',
'/cli/authenticate': '/develop/cli/account-users/authenticate',
'/cli/common-tasks': '/develop/cli/account-users/common-tasks',
'/cli/quick-start': '/develop/cli/account-users/quick-start',
}
const unavailableEnterpriseDocPaths: ReadonlySet<string> = new Set([
'/use/knowledge/knowledge-request-rate-limit',
'/use/knowledge/knowledge-storage-limit',
'/use/workspace/subscription-management',
])
const getEnterpriseDocPath = (path: string): string => {
const { pathname, hash } = splitPathHash(path)
let targetPath = replacePathPrefix(pathname, '/cloud', '')
targetPath = replacePathPrefix(targetPath, '/self-host', '')
if (!targetPath) return '/'
const overriddenPath = enterpriseDocPathOverrides[targetPath]
if (overriddenPath) return `${overriddenPath}${hash}`
targetPath = replacePathPrefix(targetPath, '/use-dify', '/use')
targetPath = replacePathPrefix(targetPath, '/api-reference', '/develop/api')
targetPath = replacePathPrefix(targetPath, '/develop-plugin', '/develop/plugins')
targetPath = replacePathPrefix(targetPath, '/cli', '/develop/cli')
if (unavailableEnterpriseDocPaths.has(targetPath)) return '/'
return `${targetPath}${hash}`
}
export const getEnterpriseDocUrl = (path: string, docLanguage: DocLanguage): string => {
const targetPath = path ? getEnterpriseDocPath(path) : '/'
return `${enterpriseDocBaseUrl}/${docLanguage}${targetPath}`
}
export const useDocLink = (
_baseUrl?: string,
): ((path?: DocPathWithoutLang, pathMap?: DocPathMap) => string) => {
useLocale()
useAtomValue(deploymentEditionAtom)
return useCallback((_path?: DocPathWithoutLang, _pathMap?: DocPathMap) => '', [])
}