dify/web/i18n-config/index.ts
Stephen Zhou 1e3823e605
chore: fix type check for i18n (#30058)
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: yyh <yuanyouhuilyz@gmail.com>
2025-12-24 16:31:16 +08:00

35 lines
935 B
TypeScript

import type { Locale } from '@/i18n-config/language'
import Cookies from 'js-cookie'
import { LOCALE_COOKIE_NAME } from '@/config'
import { changeLanguage } from '@/i18n-config/i18next-config'
import { LanguagesSupported } from '@/i18n-config/language'
export const i18n = {
defaultLocale: 'en-US',
locales: LanguagesSupported,
} as const
export { Locale }
export const setLocaleOnClient = async (locale: Locale, reloadPage = true) => {
Cookies.set(LOCALE_COOKIE_NAME, locale, { expires: 365 })
await changeLanguage(locale)
if (reloadPage)
location.reload()
}
export const getLocaleOnClient = (): Locale => {
return Cookies.get(LOCALE_COOKIE_NAME) as Locale || i18n.defaultLocale
}
export const renderI18nObject = (obj: Record<string, string>, language: string) => {
if (!obj)
return ''
if (obj?.[language])
return obj[language]
if (obj?.en_US)
return obj.en_US
return Object.values(obj)[0]
}