Files
FastGPT/packages/web/hooks/useI18n.ts
Archer 9b0706ed92 HTTP support jsonPath; System plugin support save file. (#2969)
* perf: system plugin auto save file

* feat: http support jsonPath

* fix: assistant response

* reset milvus version

* fix: textarea register

* fix: global variable

* delete tip

* doc
2024-10-23 00:40:54 +08:00

45 lines
1017 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: Record<string, string> = {
zh: 'zh',
'zh-CN': 'zh'
};
const lang = languageMap[navigator.language] || 'en';
// currentLng not in userLang
return onChangeLng(lang);
};
return {
onChangeLng,
setUserDefaultLng
};
};