mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 21:13:50 +00:00
Perf: i18n change and captcha code. (#2625)
* perf: send captcha check * perf: back router * perf: i18n init * perf: ui * i18n * perf: ui duration
This commit is contained in:
45
packages/web/hooks/useI18n.ts
Normal file
45
packages/web/hooks/useI18n.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
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
|
||||
};
|
||||
};
|
Reference in New Issue
Block a user