Skip to content

Let's use AI to Earn!

部分翻译

daoyian9deab7ecd627
diff --git a/src/app/i18n/client.ts b/src/app/i18n/client.ts
index b9cdd473..ef83cd66 100644
--- a/src/app/i18n/client.ts
+++ b/src/app/i18n/client.ts
@@ -68,3 +68,5 @@ export function useTransClient<
}
return useTranslation(ns, options);
}
+
+export default i18next;
diff --git a/src/app/i18n/locales/en/common.json b/src/app/i18n/locales/en/common.json
new file mode 100644
index 00000000..83dc253c
--- /dev/null
+++ b/src/app/i18n/locales/en/common.json
@@ -0,0 +1,6 @@
+{
+ "profile": "Profile",
+ "logout": "Logout",
+ "unknownUser": "Unknown User",
+ "login": "Login"
+}
\ No newline at end of file
diff --git a/src/app/i18n/locales/zh-CN/common.json b/src/app/i18n/locales/zh-CN/common.json
new file mode 100644
index 00000000..82a5acae
--- /dev/null
+++ b/src/app/i18n/locales/zh-CN/common.json
@@ -0,0 +1,6 @@
+{
+ "profile": "个人中心",
+ "logout": "退出登录",
+ "unknownUser": "未知用户",
+ "login": "登录"
+}
\ No newline at end of file
diff --git a/src/app/i18n/settings.ts b/src/app/i18n/settings.ts
index 251d6b21..c4b7a6fc 100644
--- a/src/app/i18n/settings.ts
+++ b/src/app/i18n/settings.ts
@@ -1,6 +1,8 @@
-export const fallbackLng = "en";
+import { FallbackLng } from "i18next";
+
+export const fallbackLng: FallbackLng = "en";
export const languages = [fallbackLng, "zh-CN"];
-export const defaultNS = "translation";
+export const defaultNS = "common";
export const cookieName = "i18next";
export function getOptions(
diff --git a/src/app/layout/LyaoutHeader.tsx b/src/app/layout/LyaoutHeader.tsx
index 9b66d92d..12cdbf5b 100644
--- a/src/app/layout/LyaoutHeader.tsx
+++ b/src/app/layout/LyaoutHeader.tsx
@@ -13,7 +13,6 @@ import defaultAvatar from "./images/defaultAvatar.jpg";
import { CaretDownOutlined, GlobalOutlined } from "@ant-design/icons";
import { useRouter } from "next/navigation";
import { useTransClient } from "@/app/i18n/client";
-import { useGetClientLng } from "@/hooks/useSystem";
export interface ILyaoutHeaderRef {}
@@ -22,6 +21,7 @@ export interface ILyaoutHeaderProps {}
function UserInfo() {
const userInfo = useUserStore((state) => state.userInfo)!;
const router = useRouter();
+ const { t } = useTransClient("common");
const items: MenuProps["items"] = [
{
@@ -32,7 +32,7 @@ function UserInfo() {
router.push("/profile");
}}
>
- 个人中心
+ {t('profile')}
</div>
),
},
@@ -45,7 +45,7 @@ function UserInfo() {
router.push("/");
}}
>
- 退出登录
+ {t('logout')}
</div>
),
},
@@ -63,7 +63,7 @@ function UserInfo() {
height={35}
/>
<div className={styles["layoutHeader-userinfo-name"]}>
- {userInfo.name || "未知用户"}
+ {userInfo.name || t('unknownUser')}
</div>
<CaretDownOutlined />
</div>
@@ -77,12 +77,12 @@ const LyaoutHeader = memo(
const userStore = useUserStore();
const layoutHeader = useRef<HTMLDivElement>(null);
const router = useRouter();
- const { t } = useTransClient();
- const lng = useGetClientLng();
+ const { t } = useTransClient("common");
const toggleLanguage = () => {
- const newLng = lng === "zh-CN" ? "en" : "zh-CN";
- location.href = `/${newLng}` + location.pathname.replace(`/${lng}`, "");
+ const newLng = userStore.lang === "zh-CN" ? "en" : "zh-CN";
+ userStore.setLang(newLng);
+ location.href = `/${newLng}` + location.pathname.replace(`/${userStore.lang}`, "");
};
useEffect(() => {
@@ -121,7 +121,7 @@ const LyaoutHeader = memo(
onClick={toggleLanguage}
className={styles.languageButton}
>
- {lng === "zh-CN" ? "EN" : "中文"}
+ {userStore.lang === "zh-CN" ? "EN" : "中文"}
</Button>
<NoSSR>
{userStore.token ? (
@@ -132,7 +132,7 @@ const LyaoutHeader = memo(
router.push("/login");
}}
>
- 登录
+ {t('login')}
</Button>
)}
</NoSSR>
diff --git a/src/utils/request.ts b/src/utils/request.ts
index 0e120467..298371f6 100644
--- a/src/utils/request.ts
+++ b/src/utils/request.ts
@@ -20,6 +20,15 @@ const fetchService = new FetchService({
Authorization: token ? `Bearer ${token}` : "",
};
+ // 添加语言头
+ if (typeof window !== "undefined") {
+ const lng = useUserStore.getState().lang;
+ requestParams.headers = {
+ ...requestParams.headers,
+ 'Accept-Language': lng
+ };
+ }
+
return requestParams;
},
responseInterceptor(response) {
@@ -32,6 +41,7 @@ export async function request<T>(params: RequestParams) {
const res = await fetchService.request(params);
const data: ResponseType<T> = await res.json();
+
// 语言拦截
if (typeof window !== "undefined") {
const lng = useUserStore.getState().lang;