2024-02-23 14:31:06 +08:00
|
|
|
import Cookies from 'js-cookie'
|
|
|
|
|
|
|
|
import { changeLanguage } from '@/i18n/i18next-config'
|
|
|
|
import { LOCALE_COOKIE_NAME } from '@/config'
|
|
|
|
import { LanguagesSupported } from '@/i18n/language'
|
2024-01-23 21:14:53 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
export const i18n = {
|
2024-02-23 14:31:06 +08:00
|
|
|
defaultLocale: 'en-US',
|
2024-01-23 21:14:53 +08:00
|
|
|
locales: LanguagesSupported,
|
2023-05-15 08:51:32 +08:00
|
|
|
} as const
|
|
|
|
|
|
|
|
export type Locale = typeof i18n['locales'][number]
|
2024-02-23 14:31:06 +08:00
|
|
|
|
|
|
|
export const setLocaleOnClient = (locale: Locale, reloadPage = true) => {
|
2025-05-14 15:00:28 +08:00
|
|
|
Cookies.set(LOCALE_COOKIE_NAME, locale, { expires: 365 })
|
2024-02-23 14:31:06 +08:00
|
|
|
changeLanguage(locale)
|
|
|
|
reloadPage && location.reload()
|
|
|
|
}
|
2024-04-29 18:59:37 +08:00
|
|
|
|
|
|
|
export const getLocaleOnClient = (): Locale => {
|
|
|
|
return Cookies.get(LOCALE_COOKIE_NAME) as Locale || i18n.defaultLocale
|
|
|
|
}
|
2025-04-10 10:03:19 +08:00
|
|
|
|
|
|
|
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]
|
|
|
|
}
|