fix: remove api reference doc link en prefix (#31910)

This commit is contained in:
Stephen Zhou 2026-02-04 14:58:26 +08:00 committed by GitHub
parent 64e769f96e
commit 468990cc39
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 10 deletions

View File

@ -196,19 +196,19 @@ describe('useDocLink', () => {
const { result } = renderHook(() => useDocLink()) const { result } = renderHook(() => useDocLink())
const url = result.current('/api-reference/annotations/create-annotation') const url = result.current('/api-reference/annotations/create-annotation')
expect(url).toBe(`${defaultDocBaseUrl}/en/api-reference/annotations/create-annotation`) expect(url).toBe(`${defaultDocBaseUrl}/api-reference/annotations/create-annotation`)
}) })
it('should keep original path when no translation exists for non-English locale', () => { it('should keep original path when no translation exists for non-English locale', () => {
vi.mocked(useTranslation).mockReturnValue({ vi.mocked(useTranslation).mockReturnValue({
i18n: { language: 'ja-JP' }, i18n: { language: 'zh-Hans' },
} as ReturnType<typeof useTranslation>) } as ReturnType<typeof useTranslation>)
vi.mocked(getDocLanguage).mockReturnValue('ja') vi.mocked(getDocLanguage).mockReturnValue('zh')
const { result } = renderHook(() => useDocLink()) const { result } = renderHook(() => useDocLink())
// This path has no Japanese translation // This path has no Japanese translation
const url = result.current('/api-reference/annotations/create-annotation') const url = result.current('/api-reference/annotations/create-annotation')
expect(url).toBe(`${defaultDocBaseUrl}/ja/api-reference/annotations/create-annotation`) expect(url).toBe(`${defaultDocBaseUrl}/api-reference/标注管理/创建标注`)
}) })
it('should remove language prefix when translation is applied', () => { it('should remove language prefix when translation is applied', () => {

View File

@ -35,12 +35,13 @@ export const useDocLink = (baseUrl?: string): ((path?: DocPathWithoutLang, pathM
let targetPath = (pathMap) ? pathMap[locale] || pathUrl : pathUrl let targetPath = (pathMap) ? pathMap[locale] || pathUrl : pathUrl
let languagePrefix = `/${docLanguage}` let languagePrefix = `/${docLanguage}`
// Translate API reference paths for non-English locales if (targetPath.startsWith('/api-reference/')) {
if (targetPath.startsWith('/api-reference/') && docLanguage !== 'en') { languagePrefix = ''
const translatedPath = apiReferencePathTranslations[targetPath]?.[docLanguage as 'zh' | 'ja'] if (docLanguage !== 'en') {
if (translatedPath) { const translatedPath = apiReferencePathTranslations[targetPath]?.[docLanguage]
targetPath = translatedPath if (translatedPath) {
languagePrefix = '' targetPath = translatedPath
}
} }
} }