refactor(i18n): about locales (#30336)

Co-authored-by: yyh <yuanyouhuilyz@gmail.com>
This commit is contained in:
Stephen Zhou
2025-12-30 14:38:23 +08:00
committed by GitHub
parent 3505516e8e
commit 2399d00d86
70 changed files with 273 additions and 320 deletions

View File

@@ -1,33 +1,19 @@
import type { Locale } from '@/i18n-config'
import { noop } from 'es-toolkit/compat'
import {
createContext,
useContext,
} from 'use-context-selector'
import type { Locale } from '@/i18n-config/language'
import { atom, useAtomValue } from 'jotai'
import { getDocLanguage, getLanguage, getPricingPageLanguage } from '@/i18n-config/language'
type II18NContext = {
locale: Locale
i18n: Record<string, any>
setLocaleOnClient: (_lang: Locale, _reloadPage?: boolean) => Promise<void>
export const localeAtom = atom<Locale>('en-US')
export const useLocale = () => {
return useAtomValue(localeAtom)
}
const I18NContext = createContext<II18NContext>({
locale: 'en-US',
i18n: {},
setLocaleOnClient: async (_lang: Locale, _reloadPage?: boolean) => {
noop()
},
})
export const useI18N = () => useContext(I18NContext)
export const useGetLanguage = () => {
const { locale } = useI18N()
const locale = useLocale()
return getLanguage(locale)
}
export const useGetPricingPageLanguage = () => {
const { locale } = useI18N()
const locale = useLocale()
return getPricingPageLanguage(locale)
}
@@ -36,7 +22,7 @@ export const defaultDocBaseUrl = 'https://docs.dify.ai'
export const useDocLink = (baseUrl?: string): ((path?: string, pathMap?: { [index: string]: string }) => string) => {
let baseDocUrl = baseUrl || defaultDocBaseUrl
baseDocUrl = (baseDocUrl.endsWith('/')) ? baseDocUrl.slice(0, -1) : baseDocUrl
const { locale } = useI18N()
const locale = useLocale()
const docLanguage = getDocLanguage(locale)
return (path?: string, pathMap?: { [index: string]: string }): string => {
const pathUrl = path || ''
@@ -45,4 +31,3 @@ export const useDocLink = (baseUrl?: string): ((path?: string, pathMap?: { [inde
return `${baseDocUrl}/${docLanguage}/${targetPath}`
}
}
export default I18NContext