Skip to content

Commit 036f6ef

Browse files
hyobanRiskeyL
andauthored
fix: following docs link fix (langgenius#31390)
Co-authored-by: Riskey <[email protected]>
1 parent 811e43d commit 036f6ef

File tree

4 files changed

+260
-5
lines changed

4 files changed

+260
-5
lines changed

web/app/components/header/account-setting/api-based-extension-page/modal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const ApiBasedExtensionModal: FC<ApiBasedExtensionModalProps> = ({
3030
onSave,
3131
}) => {
3232
const { t } = useTranslation()
33-
const docLink = useDocLink('https://docs.dify.ai/versions/3-0-x')
33+
const docLink = useDocLink()
3434
const [localeData, setLocaleData] = useState(data)
3535
const [loading, setLoading] = useState(false)
3636
const { notify } = useToastContext()

web/app/components/workflow-app/components/workflow-onboarding-modal/index.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ const WorkflowOnboardingModal: FC<WorkflowOnboardingModalProps> = ({
6060
</h3>
6161
<div className="body-xs-regular leading-4 text-text-tertiary">
6262
{t('onboarding.description', { ns: 'workflow' })}
63-
{' '}
64-
{t('onboarding.aboutStartNode', { ns: 'workflow' })}
6563
</div>
6664
</div>
6765

web/context/i18n.spec.ts

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
import type { DocPathMap } from './i18n'
2+
import type { DocPathWithoutLang } from '@/types/doc-paths'
3+
import { useTranslation } from '#i18n'
4+
import { renderHook } from '@testing-library/react'
5+
import { getDocLanguage } from '@/i18n-config/language'
6+
import { defaultDocBaseUrl, useDocLink } from './i18n'
7+
8+
// Mock dependencies
9+
vi.mock('#i18n', () => ({
10+
useTranslation: vi.fn(() => ({
11+
i18n: { language: 'en-US' },
12+
})),
13+
}))
14+
15+
vi.mock('@/i18n-config/language', () => ({
16+
getDocLanguage: vi.fn((locale: string) => {
17+
const map: Record<string, string> = {
18+
'zh-Hans': 'zh',
19+
'ja-JP': 'ja',
20+
'en-US': 'en',
21+
}
22+
return map[locale] || 'en'
23+
}),
24+
getLanguage: vi.fn(),
25+
getPricingPageLanguage: vi.fn(),
26+
}))
27+
28+
describe('useDocLink', () => {
29+
beforeEach(() => {
30+
vi.clearAllMocks()
31+
vi.mocked(useTranslation).mockReturnValue({
32+
i18n: { language: 'en-US' },
33+
} as ReturnType<typeof useTranslation>)
34+
vi.mocked(getDocLanguage).mockReturnValue('en')
35+
})
36+
37+
describe('Rendering', () => {
38+
it('should return a function', () => {
39+
const { result } = renderHook(() => useDocLink())
40+
expect(typeof result.current).toBe('function')
41+
})
42+
})
43+
44+
describe('Base URL handling', () => {
45+
it('should use default base URL when no baseUrl provided', () => {
46+
const { result } = renderHook(() => useDocLink())
47+
const url = result.current()
48+
expect(url).toBe(`${defaultDocBaseUrl}/en`)
49+
})
50+
51+
it('should use custom base URL when provided', () => {
52+
const customBaseUrl = 'https://custom.docs.com'
53+
const { result } = renderHook(() => useDocLink(customBaseUrl))
54+
const url = result.current()
55+
expect(url).toBe(`${customBaseUrl}/en`)
56+
})
57+
58+
it('should remove trailing slash from base URL', () => {
59+
const baseUrlWithSlash = 'https://docs.dify.ai/'
60+
const { result } = renderHook(() => useDocLink(baseUrlWithSlash))
61+
const url = result.current('/use-dify/getting-started/introduction')
62+
expect(url).toBe('https://docs.dify.ai/en/use-dify/getting-started/introduction')
63+
})
64+
65+
it('should handle base URL without trailing slash', () => {
66+
const baseUrlWithoutSlash = 'https://docs.dify.ai'
67+
const { result } = renderHook(() => useDocLink(baseUrlWithoutSlash))
68+
const url = result.current('/use-dify/getting-started/introduction')
69+
expect(url).toBe('https://docs.dify.ai/en/use-dify/getting-started/introduction')
70+
})
71+
})
72+
73+
describe('Path handling', () => {
74+
it('should handle path parameter', () => {
75+
const { result } = renderHook(() => useDocLink())
76+
const url = result.current('/use-dify/getting-started/introduction')
77+
expect(url).toBe(`${defaultDocBaseUrl}/en/use-dify/getting-started/introduction`)
78+
})
79+
80+
it('should handle empty path', () => {
81+
const { result } = renderHook(() => useDocLink())
82+
const url = result.current()
83+
expect(url).toBe(`${defaultDocBaseUrl}/en`)
84+
})
85+
86+
it('should handle undefined path', () => {
87+
const { result } = renderHook(() => useDocLink())
88+
const url = result.current(undefined)
89+
expect(url).toBe(`${defaultDocBaseUrl}/en`)
90+
})
91+
})
92+
93+
describe('PathMap handling', () => {
94+
it('should use path from pathMap when locale matches', () => {
95+
vi.mocked(useTranslation).mockReturnValue({
96+
i18n: { language: 'zh-Hans' },
97+
} as ReturnType<typeof useTranslation>)
98+
vi.mocked(getDocLanguage).mockReturnValue('zh')
99+
100+
const pathMap: DocPathMap = {
101+
'zh-Hans': '/use-dify/getting-started/introduction',
102+
'en-US': '/use-dify/getting-started/quick-start',
103+
}
104+
105+
const { result } = renderHook(() => useDocLink())
106+
const url = result.current('/use-dify/getting-started/quick-start' as DocPathWithoutLang, pathMap)
107+
expect(url).toBe(`${defaultDocBaseUrl}/zh/use-dify/getting-started/introduction`)
108+
})
109+
110+
it('should use default path when locale not in pathMap', () => {
111+
vi.mocked(useTranslation).mockReturnValue({
112+
i18n: { language: 'ja-JP' },
113+
} as ReturnType<typeof useTranslation>)
114+
vi.mocked(getDocLanguage).mockReturnValue('ja')
115+
116+
const pathMap: DocPathMap = {
117+
'zh-Hans': '/use-dify/getting-started/introduction',
118+
'en-US': '/use-dify/getting-started/quick-start',
119+
}
120+
121+
const { result } = renderHook(() => useDocLink())
122+
const url = result.current('/use-dify/getting-started/quick-start' as DocPathWithoutLang, pathMap)
123+
expect(url).toBe(`${defaultDocBaseUrl}/ja/use-dify/getting-started/quick-start`)
124+
})
125+
126+
it('should handle undefined pathMap', () => {
127+
const { result } = renderHook(() => useDocLink())
128+
const url = result.current('/use-dify/getting-started/introduction', undefined)
129+
expect(url).toBe(`${defaultDocBaseUrl}/en/use-dify/getting-started/introduction`)
130+
})
131+
})
132+
133+
describe('Language prefix handling', () => {
134+
it('should add /en prefix for English locale', () => {
135+
vi.mocked(useTranslation).mockReturnValue({
136+
i18n: { language: 'en-US' },
137+
} as ReturnType<typeof useTranslation>)
138+
vi.mocked(getDocLanguage).mockReturnValue('en')
139+
140+
const { result } = renderHook(() => useDocLink())
141+
const url = result.current('/use-dify/getting-started/introduction')
142+
expect(url).toContain('/en/')
143+
})
144+
145+
it('should add /zh prefix for Chinese locale', () => {
146+
vi.mocked(useTranslation).mockReturnValue({
147+
i18n: { language: 'zh-Hans' },
148+
} as ReturnType<typeof useTranslation>)
149+
vi.mocked(getDocLanguage).mockReturnValue('zh')
150+
151+
const { result } = renderHook(() => useDocLink())
152+
const url = result.current('/use-dify/getting-started/introduction')
153+
expect(url).toContain('/zh/')
154+
})
155+
156+
it('should add /ja prefix for Japanese locale', () => {
157+
vi.mocked(useTranslation).mockReturnValue({
158+
i18n: { language: 'ja-JP' },
159+
} as ReturnType<typeof useTranslation>)
160+
vi.mocked(getDocLanguage).mockReturnValue('ja')
161+
162+
const { result } = renderHook(() => useDocLink())
163+
const url = result.current('/use-dify/getting-started/introduction')
164+
expect(url).toContain('/ja/')
165+
})
166+
})
167+
168+
describe('API reference path translations', () => {
169+
it('should translate API reference path for Chinese locale', () => {
170+
vi.mocked(useTranslation).mockReturnValue({
171+
i18n: { language: 'zh-Hans' },
172+
} as ReturnType<typeof useTranslation>)
173+
vi.mocked(getDocLanguage).mockReturnValue('zh')
174+
175+
const { result } = renderHook(() => useDocLink())
176+
const url = result.current('/api-reference/annotations/create-annotation')
177+
expect(url).toBe(`${defaultDocBaseUrl}/api-reference/标注管理/创建标注`)
178+
})
179+
180+
it('should translate API reference path for Japanese locale when translation exists', () => {
181+
vi.mocked(useTranslation).mockReturnValue({
182+
i18n: { language: 'ja-JP' },
183+
} as ReturnType<typeof useTranslation>)
184+
vi.mocked(getDocLanguage).mockReturnValue('ja')
185+
186+
const { result } = renderHook(() => useDocLink())
187+
const url = result.current('/api-reference/application/get-application-basic-information')
188+
expect(url).toBe(`${defaultDocBaseUrl}/api-reference/アプリケーション情報/アプリケーションの基本情報を取得`)
189+
})
190+
191+
it('should not translate API reference path for English locale', () => {
192+
vi.mocked(useTranslation).mockReturnValue({
193+
i18n: { language: 'en-US' },
194+
} as ReturnType<typeof useTranslation>)
195+
vi.mocked(getDocLanguage).mockReturnValue('en')
196+
197+
const { result } = renderHook(() => useDocLink())
198+
const url = result.current('/api-reference/annotations/create-annotation')
199+
expect(url).toBe(`${defaultDocBaseUrl}/en/api-reference/annotations/create-annotation`)
200+
})
201+
202+
it('should keep original path when no translation exists for non-English locale', () => {
203+
vi.mocked(useTranslation).mockReturnValue({
204+
i18n: { language: 'ja-JP' },
205+
} as ReturnType<typeof useTranslation>)
206+
vi.mocked(getDocLanguage).mockReturnValue('ja')
207+
208+
const { result } = renderHook(() => useDocLink())
209+
// This path has no Japanese translation
210+
const url = result.current('/api-reference/annotations/create-annotation')
211+
expect(url).toBe(`${defaultDocBaseUrl}/ja/api-reference/annotations/create-annotation`)
212+
})
213+
214+
it('should remove language prefix when translation is applied', () => {
215+
vi.mocked(useTranslation).mockReturnValue({
216+
i18n: { language: 'zh-Hans' },
217+
} as ReturnType<typeof useTranslation>)
218+
vi.mocked(getDocLanguage).mockReturnValue('zh')
219+
220+
const { result } = renderHook(() => useDocLink())
221+
const url = result.current('/api-reference/annotations/create-annotation')
222+
// Should NOT have /zh/ prefix when translated
223+
expect(url).not.toContain('/zh/')
224+
expect(url).toBe(`${defaultDocBaseUrl}/api-reference/标注管理/创建标注`)
225+
})
226+
227+
it('should not translate non-API-reference paths', () => {
228+
vi.mocked(useTranslation).mockReturnValue({
229+
i18n: { language: 'zh-Hans' },
230+
} as ReturnType<typeof useTranslation>)
231+
vi.mocked(getDocLanguage).mockReturnValue('zh')
232+
233+
const { result } = renderHook(() => useDocLink())
234+
const url = result.current('/use-dify/getting-started/introduction')
235+
expect(url).toBe(`${defaultDocBaseUrl}/zh/use-dify/getting-started/introduction`)
236+
})
237+
})
238+
239+
describe('Edge Cases', () => {
240+
it('should handle path with anchor', () => {
241+
const { result } = renderHook(() => useDocLink())
242+
const url = result.current('/use-dify/getting-started/introduction#overview' as DocPathWithoutLang)
243+
expect(url).toBe(`${defaultDocBaseUrl}/en/use-dify/getting-started/introduction#overview`)
244+
})
245+
246+
it('should handle multiple calls with same hook instance', () => {
247+
const { result } = renderHook(() => useDocLink())
248+
const url1 = result.current('/use-dify/getting-started/introduction')
249+
const url2 = result.current('/use-dify/getting-started/quick-start')
250+
expect(url1).toBe(`${defaultDocBaseUrl}/en/use-dify/getting-started/introduction`)
251+
expect(url2).toBe(`${defaultDocBaseUrl}/en/use-dify/getting-started/quick-start`)
252+
})
253+
})
254+
})

web/context/i18n.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,17 @@ export const useDocLink = (baseUrl?: string): ((path?: DocPathWithoutLang, pathM
3131
return (path?: DocPathWithoutLang, pathMap?: DocPathMap): string => {
3232
const pathUrl = path || ''
3333
let targetPath = (pathMap) ? pathMap[locale] || pathUrl : pathUrl
34+
let languagePrefix = `/${docLanguage}`
3435

3536
// Translate API reference paths for non-English locales
3637
if (targetPath.startsWith('/api-reference/') && docLanguage !== 'en') {
3738
const translatedPath = apiReferencePathTranslations[targetPath]?.[docLanguage as 'zh' | 'ja']
38-
if (translatedPath)
39+
if (translatedPath) {
3940
targetPath = translatedPath
41+
languagePrefix = ''
42+
}
4043
}
4144

42-
return `${baseDocUrl}/${docLanguage}${targetPath}`
45+
return `${baseDocUrl}${languagePrefix}${targetPath}`
4346
}
4447
}

0 commit comments

Comments
 (0)