Files
FastGPT/packages/web/hooks/useI18n.ts
Archer c614f8b9ca Perf: i18n change and captcha code. (#2625)
* perf: send captcha check

* perf: back router

* perf: i18n init

* perf: ui

* i18n

* perf: ui duration
2024-09-05 23:01:12 +08:00

46 lines
1011 B
TypeScript

import Cookies, { CookieAttributes } from 'js-cookie';
import { useTranslation } from 'next-i18next';
const setCookie = (key: string, value: string, options?: CookieAttributes) => {
Cookies.set(key, value, options);
};
const getCookie = (key: string) => {
return Cookies.get(key);
};
const LANG_KEY = 'NEXT_LOCALE';
export const useI18nLng = () => {
const { i18n } = useTranslation();
const onChangeLng = (lng: string) => {
setCookie(LANG_KEY, lng, {
expires: 30,
sameSite: 'None',
secure: true
});
i18n?.changeLanguage(lng);
};
const setUserDefaultLng = () => {
if (!navigator || !localStorage) return;
if (getCookie(LANG_KEY)) return onChangeLng(getCookie(LANG_KEY) as string);
const languageMap = {
zh: 'zh',
'zh-CN': 'zh'
};
// @ts-ignore
const lang = languageMap[navigator.language] || 'en';
// currentLng not in userLang
return onChangeLng(lang);
};
return {
onChangeLng,
setUserDefaultLng
};
};