Skip to content

Let's use AI to Earn!

feat: 国际化及国际化demo完成

gaoSir2db636b0c189
diff --git a/@types/next.d.ts b/@types/next.d.ts
deleted file mode 100644
index 2911c9eb..00000000
--- a/@types/next.d.ts
+++ /dev/null
@@ -1 +0,0 @@
-export type PageParams = Promise<{ lng: string }>;
diff --git a/@types/resources.d.ts b/@types/resources.d.ts
index 32437015..50221aa6 100644
--- a/@types/resources.d.ts
+++ b/@types/resources.d.ts
@@ -1,38 +1,10 @@
interface Resources {
- "client-page": {
- "back-to-home": "Back to home";
- counter_one: "one selected";
- counter_other: "{{count}} selected";
- counter_zero: "none selected";
- h1: "A client page, to demonstrate client side i18n";
- title: "Client page";
- "to-second-client-page": "to second client page";
- };
- footer: {
- description: "This is a non-page component that requires its own namespace";
- helpLocize: "With using <1>locize</1> you directly support the future of <3>i18next</3>.";
- languageSwitcher: "Switch from <1>{{lng}}</1> to: ";
- };
- "second-client-page": {
- "back-to-home": "Back to home";
- h1: "A second client page, to demonstrate client side i18n";
- title: "Second client page";
- };
- "second-page": {
- "back-to-home": "Back to home";
- h1: "A second page, to demonstrate routing";
- title: "Second page";
- };
translation: {
- h1: "A simple example";
- title: "Home";
- "to-client-page": "To client page";
- "to-second-page": "To second page";
- welcome: "Welcome to Next.js 13 <1>with the new app directory features</1> and i18next";
- blog: {
- text: "Check out the corresponding <1>blog post</1> describing this example.";
- link: "https://locize.com/blog/next-app-dir-i18n/";
- };
+ title: "项目标题";
+ content: "项目 content";
+ };
+ demo: {
+ demoText: "测试文字";
};
}
diff --git a/eslint.config.mjs b/eslint.config.mjs
index f3d8bed1..da6e1b3c 100644
--- a/eslint.config.mjs
+++ b/eslint.config.mjs
@@ -32,6 +32,7 @@ const eslintConfig = [
"@typescript-eslint/no-unused-vars": "warn",
"prettier/prettier": ["error", { "endOfLine": "auto" }],
"@typescript-eslint/ban-ts-comment": "off",
+ "react-hooks/rules-of-hooks": "off",
},
}
];
diff --git a/src/app/[lng]/components/Header.tsx b/src/app/[lng]/components/Header.tsx
deleted file mode 100644
index 201c176a..00000000
--- a/src/app/[lng]/components/Header.tsx
+++ /dev/null
@@ -1,33 +0,0 @@
-import { i18n } from "i18next";
-import { Trans } from "react-i18next/TransWithoutContext";
-
-export const FooterBase = ({ i18n, lng }: { i18n: i18n; lng: string }) => {
- const t = i18n.getFixedT(lng, "footer");
- return (
- <footer>
- <Trans i18nKey="languageSwitcher" t={t}>
- {/* @ts-expect-error Trans interpolation */}
- Switch from <strong>{{ lng }}</strong> to:{" "}
- </Trans>
- <p
- style={{
- fontSize: "smaller",
- fontStyle: "italic",
- marginTop: 20,
- }}
- >
- <Trans i18nKey="helpLocize" t={t}>
- With using
- <a href="https://locize.com" target="_new">
- locize
- </a>
- you directly support the future of
- <a href="https://www.i18next.com" target="_new">
- i18next
- </a>
- .
- </Trans>
- </p>
- </footer>
- );
-};
diff --git a/src/app/[lng]/demo/demoPageCore.tsx b/src/app/[lng]/demo/demoPageCore.tsx
index a83f8299..7cea927e 100644
--- a/src/app/[lng]/demo/demoPageCore.tsx
+++ b/src/app/[lng]/demo/demoPageCore.tsx
@@ -1,6 +1,9 @@
"use client";
-export const DemoPageCore = () => {
- console.log("111");
- return <div>demoCore</div>;
+import { useTranslation } from "@/app/i18n/client";
+
+export const DemoPageCore = ({ lng }: { lng: string }) => {
+ const { t } = useTranslation(lng, "demo");
+
+ return <div>{t("demoText")}</div>;
};
diff --git a/src/app/[lng]/demo/page.tsx b/src/app/[lng]/demo/page.tsx
index 7d34fb4d..df3d1746 100644
--- a/src/app/[lng]/demo/page.tsx
+++ b/src/app/[lng]/demo/page.tsx
@@ -1,18 +1,28 @@
+import { useTranslation } from "@/app/i18n";
+import { PageParams } from "@/app/globals";
+import * as React from "react";
import { getMetadata } from "@/utils/general";
import { DemoPageCore } from "@/app/[lng]/demo/demoPageCore";
-export const metadata = getMetadata({
- title: "Demo",
- keywords: "Demo",
- description: `Demo`,
-});
+export async function generateMetadata({ params }: PageParams) {
+ const { lng } = await params;
+ const { t } = await useTranslation(lng, "demo");
+ return await getMetadata(
+ {
+ title: t("title"),
+ },
+ lng,
+ );
+}
+
+export default async function Page({ params }: PageParams) {
+ const { lng } = await params;
+ const { t } = await useTranslation(lng, "demo");
-export default function Page() {
return (
<>
- <div>11111</div>
- <DemoPageCore />
+ <div>{t("demoText")}</div>
+ <DemoPageCore lng={lng} />
</>
);
}
-
\ No newline at end of file
diff --git a/src/app/[lng]/layout.tsx b/src/app/[lng]/layout.tsx
index c595eb93..91390bd5 100644
--- a/src/app/[lng]/layout.tsx
+++ b/src/app/[lng]/layout.tsx
@@ -1,16 +1,35 @@
import "../globals.css";
-import { metadata } from "../metadata";
import { Providers } from "./providers";
-export { metadata };
+import { dir } from "i18next";
+import { languages, fallbackLng } from "@/app/i18n/settings";
+import { useTranslation } from "@/app/i18n";
-export default function RootLayout({
+export async function generateMetadata({
+ params,
+}: {
+ params: Promise<{ lng: string }>;
+}) {
+ let { lng } = await params;
+ if (languages.indexOf(lng) < 0) lng = fallbackLng;
+ const { t } = await useTranslation(lng);
+ return {
+ title: t("title"),
+ // @ts-ignore
+ content: t("content"),
+ };
+}
+
+export default async function RootLayout({
children,
+ params,
}: Readonly<{
children: React.ReactNode;
+ params: Promise<{ lng: string }>;
}>) {
+ const { lng } = await params;
return (
- <html lang="zh">
+ <html lang={lng} dir={dir(lng)}>
<body>
<Providers>{children}</Providers>
</body>
diff --git a/src/app/globals.ts b/src/app/globals.ts
new file mode 100644
index 00000000..b99ef751
--- /dev/null
+++ b/src/app/globals.ts
@@ -0,0 +1,3 @@
+export type PageParams = {
+ params: Promise<{ lng: string }>;
+};
diff --git a/src/app/i18n/locales/de/client-page.json b/src/app/i18n/locales/de/client-page.json
deleted file mode 100644
index 3d152a2f..00000000
--- a/src/app/i18n/locales/de/client-page.json
+++ /dev/null
@@ -1,9 +0,0 @@
-{
- "back-to-home": "Zurück zur Hauptseite",
- "counter_one": "eines ausgewählt",
- "counter_other": "{{count}} ausgewählt",
- "counter_zero": "keines ausgewählt",
- "h1": "Eine client Seite, um das client-seitige i18n zu demonstrieren",
- "title": "Client Seite",
- "to-second-client-page": "zur zweiten client Seite"
-}
\ No newline at end of file
diff --git a/src/app/i18n/locales/de/footer.json b/src/app/i18n/locales/de/footer.json
deleted file mode 100644
index 3df6269c..00000000
--- a/src/app/i18n/locales/de/footer.json
+++ /dev/null
@@ -1,5 +0,0 @@
-{
- "description": "Dies ist eine Nicht-Seitenkomponente, die einen eigenen Namespace erfordert",
- "helpLocize": "Wenn Sie <1>locize</1> einsetzen, unterstützen Sie direkt die Zukunft von <3>i18next</3>.",
- "languageSwitcher": "Wechseln von <1>{{lng}}</1> nach: "
-}
\ No newline at end of file
diff --git a/src/app/i18n/locales/de/second-client-page.json b/src/app/i18n/locales/de/second-client-page.json
deleted file mode 100644
index 0f3639fa..00000000
--- a/src/app/i18n/locales/de/second-client-page.json
+++ /dev/null
@@ -1,5 +0,0 @@
-{
- "back-to-home": "Zurück zur Hauptseite",
- "h1": "Eine zweite client Seite, um das client-seitige i18n zu demonstrieren",
- "title": "Zweite client Seite"
-}
\ No newline at end of file
diff --git a/src/app/i18n/locales/de/second-page.json b/src/app/i18n/locales/de/second-page.json
deleted file mode 100644
index f66083da..00000000
--- a/src/app/i18n/locales/de/second-page.json
+++ /dev/null
@@ -1,5 +0,0 @@
-{
- "back-to-home": "Zurück zur Hauptseite",
- "h1": "Eine zweite Seite, um das Routing zu demonstrieren",
- "title": "Zweite Seite"
-}
\ No newline at end of file
diff --git a/src/app/i18n/locales/de/translation.json b/src/app/i18n/locales/de/translation.json
deleted file mode 100644
index 13b4da98..00000000
--- a/src/app/i18n/locales/de/translation.json
+++ /dev/null
@@ -1,11 +0,0 @@
-{
- "h1": "Ein einfaches Beispiel",
- "title": "Hauptseite",
- "to-client-page": "Zur client seitigen Seite",
- "to-second-page": "Zur zweiten Seite",
- "welcome": "Willkommen ZU Next.js 13/14/15 <1>mit den neuen App-Verzeichnisfunktionen</1> und i18next",
- "blog": {
- "text": "Schau dir den entsprechenden <1>Blogeintrag</1> dieses Beispiels an.",
- "link": "https://locize.com/blog/next-app-dir-i18n/"
- }
-}
\ No newline at end of file
diff --git a/src/app/i18n/locales/en/client-page.json b/src/app/i18n/locales/en/client-page.json
deleted file mode 100644
index 1c032196..00000000
--- a/src/app/i18n/locales/en/client-page.json
+++ /dev/null
@@ -1,9 +0,0 @@
-{
- "back-to-home": "Back to home",
- "counter_one": "one selected",
- "counter_other": "{{count}} selected",
- "counter_zero": "none selected",
- "h1": "A client page, to demonstrate client side i18n",
- "title": "Client page",
- "to-second-client-page": "to second client page"
-}
\ No newline at end of file
diff --git a/src/app/i18n/locales/en/demo.json b/src/app/i18n/locales/en/demo.json
new file mode 100644
index 00000000..a0e5daf4
--- /dev/null
+++ b/src/app/i18n/locales/en/demo.json
@@ -0,0 +1,4 @@
+{
+ "demoText": "demo Text",
+ "title": "Demo Title"
+}
\ No newline at end of file
diff --git a/src/app/i18n/locales/en/footer.json b/src/app/i18n/locales/en/footer.json
deleted file mode 100644
index c81955ba..00000000
--- a/src/app/i18n/locales/en/footer.json
+++ /dev/null
@@ -1,5 +0,0 @@
-{
- "description": "This is a non-page component that requires its own namespace",
- "helpLocize": "With using <1>locize</1> you directly support the future of <3>i18next</3>.",
- "languageSwitcher": "Switch from <1>{{lng}}</1> to: "
-}
\ No newline at end of file
diff --git a/src/app/i18n/locales/en/second-client-page.json b/src/app/i18n/locales/en/second-client-page.json
deleted file mode 100644
index 7a02e85e..00000000
--- a/src/app/i18n/locales/en/second-client-page.json
+++ /dev/null
@@ -1,5 +0,0 @@
-{
- "back-to-home": "Back to home",
- "h1": "A second client page, to demonstrate client side i18n",
- "title": "Second client page"
-}
\ No newline at end of file
diff --git a/src/app/i18n/locales/en/second-page.json b/src/app/i18n/locales/en/second-page.json
deleted file mode 100644
index fdbb27a3..00000000
--- a/src/app/i18n/locales/en/second-page.json
+++ /dev/null
@@ -1,5 +0,0 @@
-{
- "back-to-home": "Back to home",
- "h1": "A second page, to demonstrate routing",
- "title": "Second page"
-}
\ No newline at end of file
diff --git a/src/app/i18n/locales/en/translation.json b/src/app/i18n/locales/en/translation.json
index 56865a20..dce94e62 100644
--- a/src/app/i18n/locales/en/translation.json
+++ b/src/app/i18n/locales/en/translation.json
@@ -1,11 +1,4 @@
{
- "h1": "A simple example",
- "title": "Home",
- "to-client-page": "To client page",
- "to-second-page": "To second page",
- "welcome": "Welcome to Next.js 13/14/15 <1>with the new app directory features</1> and i18next",
- "blog": {
- "text": "Check out the corresponding <1>blog post</1> describing this example.",
- "link": "https://locize.com/blog/next-app-dir-i18n/"
- }
+ "title": "AIToEarn Official Website",
+ "content": "The best open-source matrix tool"
}
\ No newline at end of file
diff --git a/src/app/i18n/locales/it/client-page.json b/src/app/i18n/locales/it/client-page.json
deleted file mode 100644
index a7ce6f29..00000000
--- a/src/app/i18n/locales/it/client-page.json
+++ /dev/null
@@ -1,10 +0,0 @@
-{
- "back-to-home": "Tornare alla pagina principale",
- "counter_many": "selezionati {{count}}",
- "counter_one": "uno selezionato",
- "counter_other": "{{count}} selezionati",
- "counter_zero": "nessuno selezionato",
- "h1": "Una pagina client, per dimostrare il i18n nel client",
- "title": "Pagina client",
- "to-second-client-page": "alla seconda pagina client"
-}
\ No newline at end of file
diff --git a/src/app/i18n/locales/it/footer.json b/src/app/i18n/locales/it/footer.json
deleted file mode 100644
index dd0efea4..00000000
--- a/src/app/i18n/locales/it/footer.json
+++ /dev/null
@@ -1,5 +0,0 @@
-{
- "description": "Questo è un componente non di pagina che richiede il proprio spazio dei nomi",
- "helpLocize": "Con l'utilizzare <1>locize</1> sostieni direttamente il futuro di <3>i18next</3>.",
- "languageSwitcher": "Passa da <1>{{lng}}</1> a: "
-}
\ No newline at end of file
diff --git a/src/app/i18n/locales/it/second-client-page.json b/src/app/i18n/locales/it/second-client-page.json
deleted file mode 100644
index 02c49ada..00000000
--- a/src/app/i18n/locales/it/second-client-page.json
+++ /dev/null
@@ -1,5 +0,0 @@
-{
- "back-to-home": "Tornare alla pagina principale",
- "h1": "Una seconda pagina client, per dimostrare il i18n nel client",
- "title": "Seconda pagina client"
-}
\ No newline at end of file
diff --git a/src/app/i18n/locales/it/second-page.json b/src/app/i18n/locales/it/second-page.json
deleted file mode 100644
index 99f1d73b..00000000
--- a/src/app/i18n/locales/it/second-page.json
+++ /dev/null
@@ -1,5 +0,0 @@
-{
- "back-to-home": "Tornare alla pagina principale",
- "h1": "Una seconda pagina, per dimostrare il routing",
- "title": "Seconda pagina"
-}
\ No newline at end of file
diff --git a/src/app/i18n/locales/it/translation.json b/src/app/i18n/locales/it/translation.json
deleted file mode 100644
index ccbc691a..00000000
--- a/src/app/i18n/locales/it/translation.json
+++ /dev/null
@@ -1,11 +0,0 @@
-{
- "h1": "Un semplice esempio",
- "title": "Casa",
- "to-client-page": "Alla pagina client",
- "to-second-page": "Alla seconda pagina",
- "welcome": "Benvenuti a Next.js 13/14/15 <1>con le nuove funzionalità di app directory</1> e i18next",
- "blog": {
- "text": "Scopri il corrispondente <1>articolo</1> che descrivere questo esempio.",
- "link": "https://locize.com/blog/next-app-dir-i18n/"
- }
-}
\ No newline at end of file
diff --git a/src/app/i18n/locales/zh-CN/client-page.json b/src/app/i18n/locales/zh-CN/client-page.json
deleted file mode 100644
index a7ce6f29..00000000
--- a/src/app/i18n/locales/zh-CN/client-page.json
+++ /dev/null
@@ -1,10 +0,0 @@
-{
- "back-to-home": "Tornare alla pagina principale",
- "counter_many": "selezionati {{count}}",
- "counter_one": "uno selezionato",
- "counter_other": "{{count}} selezionati",
- "counter_zero": "nessuno selezionato",
- "h1": "Una pagina client, per dimostrare il i18n nel client",
- "title": "Pagina client",
- "to-second-client-page": "alla seconda pagina client"
-}
\ No newline at end of file
diff --git a/src/app/i18n/locales/zh-CN/demo.json b/src/app/i18n/locales/zh-CN/demo.json
new file mode 100644
index 00000000..b14e97c2
--- /dev/null
+++ b/src/app/i18n/locales/zh-CN/demo.json
@@ -0,0 +1,4 @@
+{
+ "demoText": "测试文字",
+ "title": "测试页面标题"
+}
\ No newline at end of file
diff --git a/src/app/i18n/locales/zh-CN/footer.json b/src/app/i18n/locales/zh-CN/footer.json
deleted file mode 100644
index dd0efea4..00000000
--- a/src/app/i18n/locales/zh-CN/footer.json
+++ /dev/null
@@ -1,5 +0,0 @@
-{
- "description": "Questo è un componente non di pagina che richiede il proprio spazio dei nomi",
- "helpLocize": "Con l'utilizzare <1>locize</1> sostieni direttamente il futuro di <3>i18next</3>.",
- "languageSwitcher": "Passa da <1>{{lng}}</1> a: "
-}
\ No newline at end of file
diff --git a/src/app/i18n/locales/zh-CN/second-client-page.json b/src/app/i18n/locales/zh-CN/second-client-page.json
deleted file mode 100644
index 02c49ada..00000000
--- a/src/app/i18n/locales/zh-CN/second-client-page.json
+++ /dev/null
@@ -1,5 +0,0 @@
-{
- "back-to-home": "Tornare alla pagina principale",
- "h1": "Una seconda pagina client, per dimostrare il i18n nel client",
- "title": "Seconda pagina client"
-}
\ No newline at end of file
diff --git a/src/app/i18n/locales/zh-CN/second-page.json b/src/app/i18n/locales/zh-CN/second-page.json
deleted file mode 100644
index 99f1d73b..00000000
--- a/src/app/i18n/locales/zh-CN/second-page.json
+++ /dev/null
@@ -1,5 +0,0 @@
-{
- "back-to-home": "Tornare alla pagina principale",
- "h1": "Una seconda pagina, per dimostrare il routing",
- "title": "Seconda pagina"
-}
\ No newline at end of file
diff --git a/src/app/i18n/locales/zh-CN/translation.json b/src/app/i18n/locales/zh-CN/translation.json
index ccbc691a..947951f2 100644
--- a/src/app/i18n/locales/zh-CN/translation.json
+++ b/src/app/i18n/locales/zh-CN/translation.json
@@ -1,11 +1,4 @@
{
- "h1": "Un semplice esempio",
- "title": "Casa",
- "to-client-page": "Alla pagina client",
- "to-second-page": "Alla seconda pagina",
- "welcome": "Benvenuti a Next.js 13/14/15 <1>con le nuove funzionalità di app directory</1> e i18next",
- "blog": {
- "text": "Scopri il corrispondente <1>articolo</1> che descrivere questo esempio.",
- "link": "https://locize.com/blog/next-app-dir-i18n/"
- }
+ "title": "AIToEarn官方网站",
+ "content": "最好用的开源矩阵工具"
}
\ No newline at end of file
diff --git a/src/constant.ts b/src/constant.ts
index 8f186025..751c8f01 100644
--- a/src/constant.ts
+++ b/src/constant.ts
@@ -1,6 +1,3 @@
-// 项目总标题
-export const APP_TITLE = "哎呦赚Web端";
-
// api url
// export const API_BASE_URL = "https://api.yikart.cn/api/";
export const API_BASE_URL = "http://localhost:3000/api/";
diff --git a/src/utils/general.ts b/src/utils/general.ts
index aaf020f9..6e454173 100644
--- a/src/utils/general.ts
+++ b/src/utils/general.ts
@@ -1,17 +1,26 @@
-import { APP_TITLE } from "@/constant";
import { Metadata } from "next";
+import { useTranslation } from "@/app/i18n";
-export function getPageTitle(name: string) {
- return `${name} —— ${APP_TITLE}`;
+export async function getPageTitle(name: string, lng: string) {
+ const { t } = await useTranslation(lng);
+ return `${name} —— ${t("title")}`;
}
/**
* 拦截 Metadata
* @param props
+ * @param lng
*/
-export function getMetadata(props: Metadata): Metadata {
+export async function getMetadata(
+ props: Metadata,
+ lng: string,
+): Promise<Metadata> {
+ const title = await getPageTitle(
+ typeof props.title === "string" ? props.title : "",
+ lng,
+ );
return {
...props,
- title: getPageTitle(typeof props.title === "string" ? props.title : ""),
+ title,
};
}