diff --git a/app/[locale]/(main)/ClientComponents/NetworkChart.tsx b/app/(main)/ClientComponents/NetworkChart.tsx similarity index 96% rename from app/[locale]/(main)/ClientComponents/NetworkChart.tsx rename to app/(main)/ClientComponents/NetworkChart.tsx index bbbda4f..45243e1 100644 --- a/app/[locale]/(main)/ClientComponents/NetworkChart.tsx +++ b/app/(main)/ClientComponents/NetworkChart.tsx @@ -1,10 +1,7 @@ "use client"; -import NetworkChartLoading from "@/app/[locale]/(main)/ClientComponents/NetworkChartLoading"; -import { - NezhaAPIMonitor, - ServerMonitorChart, -} from "@/app/[locale]/types/nezha-api"; +import NetworkChartLoading from "@/app/(main)/ClientComponents/NetworkChartLoading"; +import { NezhaAPIMonitor, ServerMonitorChart } from "@/app/types/nezha-api"; import { BackIcon } from "@/components/Icon"; import { Card, @@ -108,8 +105,6 @@ export const NetworkChart = React.memo(function NetworkChart({ formattedData: ResultItem[]; }) { const t = useTranslations("NetworkChart"); - const router = useRouter(); - const locale = useLocale(); const defaultChart = "All"; diff --git a/app/[locale]/(main)/ClientComponents/NetworkChartLoading.tsx b/app/(main)/ClientComponents/NetworkChartLoading.tsx similarity index 82% rename from app/[locale]/(main)/ClientComponents/NetworkChartLoading.tsx rename to app/(main)/ClientComponents/NetworkChartLoading.tsx index 6c3d383..c9c58c7 100644 --- a/app/[locale]/(main)/ClientComponents/NetworkChartLoading.tsx +++ b/app/(main)/ClientComponents/NetworkChartLoading.tsx @@ -1,13 +1,7 @@ -import { BackIcon } from "@/components/Icon"; import { Loader } from "@/components/loading/Loader"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; -import { useLocale } from "next-intl"; -import { useRouter } from "next/navigation"; export default function NetworkChartLoading() { - const router = useRouter(); - const locale = useLocale(); - return ( diff --git a/app/[locale]/(main)/ClientComponents/ServerDetailChartClient.tsx b/app/(main)/ClientComponents/ServerDetailChartClient.tsx similarity index 99% rename from app/[locale]/(main)/ClientComponents/ServerDetailChartClient.tsx rename to app/(main)/ClientComponents/ServerDetailChartClient.tsx index ba04a7e..c2fb955 100644 --- a/app/[locale]/(main)/ClientComponents/ServerDetailChartClient.tsx +++ b/app/(main)/ClientComponents/ServerDetailChartClient.tsx @@ -1,7 +1,7 @@ "use client"; -import { ServerDetailChartLoading } from "@/app/[locale]/(main)/ClientComponents/ServerDetailLoading"; -import { NezhaAPISafe, ServerApi } from "@/app/[locale]/types/nezha-api"; +import { ServerDetailChartLoading } from "@/app/(main)/ClientComponents/ServerDetailLoading"; +import { NezhaAPISafe, ServerApi } from "@/app/types/nezha-api"; import AnimatedCircularProgressBar from "@/components/ui/animated-circular-progress-bar"; import { Card, CardContent } from "@/components/ui/card"; import { ChartConfig, ChartContainer } from "@/components/ui/chart"; diff --git a/app/[locale]/(main)/ClientComponents/ServerDetailClient.tsx b/app/(main)/ClientComponents/ServerDetailClient.tsx similarity index 96% rename from app/[locale]/(main)/ClientComponents/ServerDetailClient.tsx rename to app/(main)/ClientComponents/ServerDetailClient.tsx index a3236d7..ad695bb 100644 --- a/app/[locale]/(main)/ClientComponents/ServerDetailClient.tsx +++ b/app/(main)/ClientComponents/ServerDetailClient.tsx @@ -1,7 +1,7 @@ "use client"; -import { ServerDetailLoading } from "@/app/[locale]/(main)/ClientComponents/ServerDetailLoading"; -import { NezhaAPISafe, ServerApi } from "@/app/[locale]/types/nezha-api"; +import { ServerDetailLoading } from "@/app/(main)/ClientComponents/ServerDetailLoading"; +import { NezhaAPISafe, ServerApi } from "@/app/types/nezha-api"; import { BackIcon } from "@/components/Icon"; import ServerFlag from "@/components/ServerFlag"; import { Badge } from "@/components/ui/badge"; @@ -21,7 +21,6 @@ export default function ServerDetailClient({ }) { const t = useTranslations("ServerDetailClient"); const router = useRouter(); - const locale = useLocale(); const [hasHistory, setHasHistory] = useState(false); @@ -43,7 +42,7 @@ export default function ServerDetailClient({ if (hasHistory) { router.back(); } else { - router.push(`/${locale}/`); + router.push(`/`); } }; @@ -52,6 +51,7 @@ export default function ServerDetailClient({ nezhaFetcher, ); const fallbackData = allFallbackData?.result?.find( + // @ts-ignore (item) => item.id === server_id, ); diff --git a/app/[locale]/(main)/ClientComponents/ServerDetailLoading.tsx b/app/(main)/ClientComponents/ServerDetailLoading.tsx similarity index 95% rename from app/[locale]/(main)/ClientComponents/ServerDetailLoading.tsx rename to app/(main)/ClientComponents/ServerDetailLoading.tsx index 1036e3e..1f26be8 100644 --- a/app/[locale]/(main)/ClientComponents/ServerDetailLoading.tsx +++ b/app/(main)/ClientComponents/ServerDetailLoading.tsx @@ -20,13 +20,12 @@ export function ServerDetailChartLoading() { export function ServerDetailLoading() { const router = useRouter(); - const locale = useLocale(); return ( <>
{ - router.push(`/${locale}/`); + router.push(`/`); }} className="flex flex-none cursor-pointer font-semibold leading-none items-center break-all tracking-tight gap-0.5 text-xl" > diff --git a/app/[locale]/(main)/ClientComponents/ServerListClient.tsx b/app/(main)/ClientComponents/ServerListClient.tsx similarity index 96% rename from app/[locale]/(main)/ClientComponents/ServerListClient.tsx rename to app/(main)/ClientComponents/ServerListClient.tsx index 707047f..ff5345c 100644 --- a/app/[locale]/(main)/ClientComponents/ServerListClient.tsx +++ b/app/(main)/ClientComponents/ServerListClient.tsx @@ -1,6 +1,6 @@ "use client"; -import { ServerApi } from "@/app/[locale]/types/nezha-api"; +import { ServerApi } from "@/app/types/nezha-api"; import ServerCard from "@/components/ServerCard"; import Switch from "@/components/Switch"; import getEnv from "@/lib/env-entry"; @@ -12,7 +12,7 @@ import useSWR from "swr"; export default function ServerListClient() { const t = useTranslations("ServerListClient"); const containerRef = useRef(null); - const defaultTag = t("defaultTag"); + const defaultTag = "defaultTag"; const [tag, setTag] = useState(defaultTag); diff --git a/app/[locale]/(main)/ClientComponents/ServerOverviewClient.tsx b/app/(main)/ClientComponents/ServerOverviewClient.tsx similarity index 99% rename from app/[locale]/(main)/ClientComponents/ServerOverviewClient.tsx rename to app/(main)/ClientComponents/ServerOverviewClient.tsx index 764ca38..44407a3 100644 --- a/app/[locale]/(main)/ClientComponents/ServerOverviewClient.tsx +++ b/app/(main)/ClientComponents/ServerOverviewClient.tsx @@ -1,6 +1,6 @@ "use client"; -import { ServerApi } from "@/app/[locale]/types/nezha-api"; +import { ServerApi } from "@/app/types/nezha-api"; import { Loader } from "@/components/loading/Loader"; import { Card, CardContent } from "@/components/ui/card"; import getEnv from "@/lib/env-entry"; diff --git a/app/[locale]/(main)/[id]/page.tsx b/app/(main)/[id]/page.tsx similarity index 82% rename from app/[locale]/(main)/[id]/page.tsx rename to app/(main)/[id]/page.tsx index 9309b7b..3646322 100644 --- a/app/[locale]/(main)/[id]/page.tsx +++ b/app/(main)/[id]/page.tsx @@ -1,8 +1,8 @@ "use client"; -import { NetworkChartClient } from "@/app/[locale]/(main)/ClientComponents/NetworkChart"; -import ServerDetailChartClient from "@/app/[locale]/(main)/ClientComponents/ServerDetailChartClient"; -import ServerDetailClient from "@/app/[locale]/(main)/ClientComponents/ServerDetailClient"; +import { NetworkChartClient } from "@/app/(main)/ClientComponents/NetworkChart"; +import ServerDetailChartClient from "@/app/(main)/ClientComponents/ServerDetailChartClient"; +import ServerDetailClient from "@/app/(main)/ClientComponents/ServerDetailClient"; import TabSwitch from "@/components/TabSwitch"; import { Separator } from "@/components/ui/separator"; import { useTranslations } from "next-intl"; diff --git a/app/[locale]/(main)/footer.tsx b/app/(main)/footer.tsx similarity index 100% rename from app/[locale]/(main)/footer.tsx rename to app/(main)/footer.tsx diff --git a/app/[locale]/(main)/header.tsx b/app/(main)/header.tsx similarity index 98% rename from app/[locale]/(main)/header.tsx rename to app/(main)/header.tsx index f3a6e9e..2a1c1a7 100644 --- a/app/[locale]/(main)/header.tsx +++ b/app/(main)/header.tsx @@ -19,7 +19,6 @@ function Header() { const customDescription = getEnv("NEXT_PUBLIC_CustomDescription"); const router = useRouter(); - const locale = useLocale(); return (
@@ -27,7 +26,7 @@ function Header() {
{ sessionStorage.removeItem("selectedTag"); - router.push(`/${locale}/`); + router.push(`/`); }} className="flex cursor-pointer items-center text-base font-medium" > diff --git a/app/[locale]/(main)/layout.tsx b/app/(main)/layout.tsx similarity index 86% rename from app/[locale]/(main)/layout.tsx rename to app/(main)/layout.tsx index c237dc7..c8ba248 100644 --- a/app/[locale]/(main)/layout.tsx +++ b/app/(main)/layout.tsx @@ -1,5 +1,5 @@ -import Footer from "@/app/[locale]/(main)/footer"; -import Header from "@/app/[locale]/(main)/header"; +import Footer from "@/app/(main)/footer"; +import Header from "@/app/(main)/header"; import { auth } from "@/auth"; import { SignIn } from "@/components/SignIn"; import getEnv from "@/lib/env-entry"; diff --git a/app/[locale]/(main)/page.tsx b/app/(main)/page.tsx similarity index 100% rename from app/[locale]/(main)/page.tsx rename to app/(main)/page.tsx diff --git a/app/[locale]/android-chrome-192x192.png b/app/android-chrome-192x192.png similarity index 100% rename from app/[locale]/android-chrome-192x192.png rename to app/android-chrome-192x192.png diff --git a/app/[locale]/android-chrome-512x512.png b/app/android-chrome-512x512.png similarity index 100% rename from app/[locale]/android-chrome-512x512.png rename to app/android-chrome-512x512.png diff --git a/app/api/detail/route.ts b/app/api/detail/route.ts index eddf954..a500bce 100644 --- a/app/api/detail/route.ts +++ b/app/api/detail/route.ts @@ -1,4 +1,3 @@ -import { NezhaAPISafe } from "@/app/[locale]/types/nezha-api"; import { auth } from "@/auth"; import getEnv from "@/lib/env-entry"; import { GetServerDetail } from "@/lib/serverFetch"; diff --git a/app/[locale]/apple-touch-icon.png b/app/apple-touch-icon.png similarity index 100% rename from app/[locale]/apple-touch-icon.png rename to app/apple-touch-icon.png diff --git a/app/[locale]/favicon-16x16.png b/app/favicon-16x16.png similarity index 100% rename from app/[locale]/favicon-16x16.png rename to app/favicon-16x16.png diff --git a/app/[locale]/favicon-32x32.png b/app/favicon-32x32.png similarity index 100% rename from app/[locale]/favicon-32x32.png rename to app/favicon-32x32.png diff --git a/app/[locale]/layout.tsx b/app/layout.tsx similarity index 77% rename from app/[locale]/layout.tsx rename to app/layout.tsx index 089d03a..a4deb23 100644 --- a/app/[locale]/layout.tsx +++ b/app/layout.tsx @@ -1,12 +1,11 @@ // @auto-i18n-check. Please do not delete the line. -import { locales } from "@/i18n-metadata"; import getEnv from "@/lib/env-entry"; import { cn } from "@/lib/utils"; import "@/styles/globals.css"; import type { Metadata } from "next"; import { Viewport } from "next"; -import { NextIntlClientProvider, useMessages } from "next-intl"; -import { unstable_setRequestLocale } from "next-intl/server"; +import { NextIntlClientProvider } from "next-intl"; +import { getLocale, getMessages } from "next-intl/server"; import { PublicEnvScript } from "next-runtime-env"; import { ThemeProvider } from "next-themes"; import { Inter as FontSans } from "next/font/google"; @@ -38,20 +37,13 @@ export const viewport: Viewport = { userScalable: false, }; -export async function generateStaticParams() { - return locales.map((locale) => ({ locale })); -} - -export default function LocaleLayout({ +export default async function LocaleLayout({ children, - params: { locale }, }: { children: React.ReactNode; - params: { locale: string }; }) { - unstable_setRequestLocale(locale); - - const messages = useMessages(); + const locale = await getLocale(); + const messages = await getMessages(); return ( @@ -74,7 +66,7 @@ export default function LocaleLayout({ enableSystem disableTransitionOnChange > - + {children} diff --git a/app/[locale]/not-found.tsx b/app/not-found.tsx similarity index 100% rename from app/[locale]/not-found.tsx rename to app/not-found.tsx diff --git a/app/[locale]/types/nezha-api.ts b/app/types/nezha-api.ts similarity index 100% rename from app/[locale]/types/nezha-api.ts rename to app/types/nezha-api.ts diff --git a/app/[locale]/types/utils.ts b/app/types/utils.ts similarity index 100% rename from app/[locale]/types/utils.ts rename to app/types/utils.ts diff --git a/auto-i18n-config.json b/auto-i18n-config.json deleted file mode 100644 index bbf5176..0000000 --- a/auto-i18n-config.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "defaultLang": "en", - "translatorServerName": "azure", - "needLangs": ["en", "zh", "zh-t", "ja"], - "brandWords": [], - "unMoveToLocaleDirFiles": [], - "enableStaticRendering": false, - "enableSubPageRedirectToLocale": false, - "disableDefaultLangRedirect": true -} diff --git a/components/LanguageSwitcher.tsx b/components/LanguageSwitcher.tsx index 5f944a2..008b8ea 100644 --- a/components/LanguageSwitcher.tsx +++ b/components/LanguageSwitcher.tsx @@ -7,45 +7,18 @@ import { DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; +import { localeItems } from "@/i18n-metadata"; +import { setUserLocale } from "@/i18n/locale"; import { useLocale } from "next-intl"; -import { usePathname, useRouter } from "next/navigation"; import * as React from "react"; -import { localeItems } from "../i18n-metadata"; - export function LanguageSwitcher() { const locale = useLocale(); - const router = useRouter(); - const pathname = usePathname(); - const handleChange = (code: string) => { - const newLocale = code; - - const rootPath = "/"; - const currentLocalePath = `/${locale}`; - const newLocalePath = `/${newLocale}`; - - // Function to construct new path with locale prefix - const constructLocalePath = (path: string, newLocale: string) => { - if (path.startsWith(currentLocalePath)) { - return path.replace(currentLocalePath, `/${newLocale}`); - } else { - return `/${newLocale}${path}`; - } - }; - - if (pathname === rootPath || !pathname) { - router.push(newLocalePath); - } else if ( - pathname === currentLocalePath || - pathname === `${currentLocalePath}/` - ) { - router.push(newLocalePath); - } else { - const newPath = constructLocalePath(pathname, newLocale); - router.push(newPath); - } - }; + function onChange(value: string) { + const locale = value; + setUserLocale(locale); + } return ( @@ -57,10 +30,7 @@ export function LanguageSwitcher() { {localeItems.map((item) => ( - handleChange(item.code)} - > + onChange(item.code)}> {item.name} ))} diff --git a/components/ServerCard.tsx b/components/ServerCard.tsx index ff07e5c..8772e22 100644 --- a/components/ServerCard.tsx +++ b/components/ServerCard.tsx @@ -1,5 +1,4 @@ -import { NezhaAPISafe } from "@/app/[locale]/types/nezha-api"; -import ServerCardPopover from "@/components/ServerCardPopover"; +import { NezhaAPISafe } from "@/app/types/nezha-api"; import ServerFlag from "@/components/ServerFlag"; import ServerUsageBar from "@/components/ServerUsageBar"; import { Badge } from "@/components/ui/badge"; @@ -12,6 +11,7 @@ import { import getEnv from "@/lib/env-entry"; import { cn, formatBytes, formatNezhaInfo } from "@/lib/utils"; import { useLocale, useTranslations } from "next-intl"; +import Link from "next/link"; import { useRouter } from "next/navigation"; export default function ServerCard({ @@ -28,98 +28,95 @@ export default function ServerCard({ const showNetTransfer = getEnv("NEXT_PUBLIC_ShowNetTransfer") === "true"; - const locale = useLocale(); - return online ? ( - { - router.push(`/${locale}/${id}`); - }} - > -
+ - -
- {showFlag ? : null} -
-

- {name} -

-
-
-
-
-

{t("CPU")}

-
- {cpu.toFixed(2)}% -
- -
-
-

{t("Mem")}

-
- {mem.toFixed(2)}% -
- -
-
-

{t("STG")}

-
- {stg.toFixed(2)}% -
- -
-
-

{t("Upload")}

-
- {up.toFixed(2)}M/s -
-
-
-

{t("Download")}

-
- {down.toFixed(2)}M/s -
-
-
- {showNetTransfer && ( -
{ - router.push(`/${locale}/network/${id}`); - }} - className={"flex items-center justify-between gap-1"} + +
- - {t("Upload")}:{formatBytes(serverInfo.status.NetOutTransfer)} - - - {t("Download")}:{formatBytes(serverInfo.status.NetInTransfer)} - + {showFlag ? : null} +
+

+ {name} +

+
+
+
+
+

{t("CPU")}

+
+ {cpu.toFixed(2)}% +
+ +
+
+

{t("Mem")}

+
+ {mem.toFixed(2)}% +
+ +
+
+

{t("STG")}

+
+ {stg.toFixed(2)}% +
+ +
+
+

{t("Upload")}

+
+ {up.toFixed(2)}M/s +
+
+
+

{t("Download")}

+
+ {down.toFixed(2)}M/s +
+
- )} -
- + {showNetTransfer && ( +
{ + router.push(`/${id}`); + }} + className={"flex items-center justify-between gap-1"} + > + + {t("Upload")}:{formatBytes(serverInfo.status.NetOutTransfer)} + + + {t("Download")}:{formatBytes(serverInfo.status.NetInTransfer)} + +
+ )} +
+
+ ) : ( ; diff --git a/components/Switch.tsx b/components/Switch.tsx index 1a83a6c..e84a080 100644 --- a/components/Switch.tsx +++ b/components/Switch.tsx @@ -2,6 +2,7 @@ import { cn } from "@/lib/utils"; import { motion } from "framer-motion"; +import { useTranslations } from "next-intl"; import React, { createRef, useEffect, useRef, useState } from "react"; export default function Switch({ @@ -15,6 +16,7 @@ export default function Switch({ }) { const scrollRef = useRef(null); const tagRefs = useRef(allTag.map(() => createRef())); + const t = useTranslations("ServerListClient"); useEffect(() => { const savedTag = sessionStorage.getItem("selectedTag"); @@ -82,7 +84,9 @@ export default function Switch({ /> )}
-

{tag}

+

+ {tag === "defaultTag" ? t("defaultTag") : tag} +

))} diff --git a/i18n.ts b/i18n.ts deleted file mode 100644 index f000d4e..0000000 --- a/i18n.ts +++ /dev/null @@ -1,14 +0,0 @@ -// @auto-i18n-check. Please do not delete the line. -import { getRequestConfig } from "next-intl/server"; -import { notFound } from "next/navigation"; - -import { locales } from "./i18n-metadata"; - -export default getRequestConfig(async ({ locale }) => { - // Validate that the incoming `locale` parameter is valid - if (!locales.includes(locale as any)) notFound(); - - return { - messages: (await import(`./messages/${locale}.json`)).default, - }; -}); diff --git a/i18n/locale.ts b/i18n/locale.ts new file mode 100644 index 0000000..04fbcce --- /dev/null +++ b/i18n/locale.ts @@ -0,0 +1,14 @@ +"use server"; + +import getEnv from "@/lib/env-entry"; +import { cookies } from "next/headers"; + +const COOKIE_NAME = "NEXT_LOCALE"; + +export async function getUserLocale() { + return cookies().get(COOKIE_NAME)?.value || (getEnv("DefaultLocale") ?? "en"); +} + +export async function setUserLocale(locale: string) { + cookies().set(COOKIE_NAME, locale); +} diff --git a/i18n/request.ts b/i18n/request.ts new file mode 100644 index 0000000..36d4458 --- /dev/null +++ b/i18n/request.ts @@ -0,0 +1,11 @@ +import { getUserLocale } from "@/i18n/locale"; +import { getRequestConfig } from "next-intl/server"; + +export default getRequestConfig(async () => { + const locale = await getUserLocale(); + + return { + locale, + messages: (await import(`../messages/${locale}.json`)).default, + }; +}); diff --git a/lib/serverFetch.tsx b/lib/serverFetch.tsx index 4cbf3bb..5663c63 100644 --- a/lib/serverFetch.tsx +++ b/lib/serverFetch.tsx @@ -1,7 +1,7 @@ "use server"; -import { NezhaAPI, ServerApi } from "@/app/[locale]/types/nezha-api"; -import { MakeOptional } from "@/app/[locale]/types/utils"; +import { NezhaAPI, ServerApi } from "@/app/types/nezha-api"; +import { MakeOptional } from "@/app/types/utils"; import getEnv from "@/lib/env-entry"; import { unstable_noStore as noStore } from "next/cache"; diff --git a/lib/utils.ts b/lib/utils.ts index cd40047..024faf3 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -1,4 +1,4 @@ -import { NezhaAPISafe } from "@/app/[locale]/types/nezha-api"; +import { NezhaAPISafe } from "@/app/types/nezha-api"; import { type ClassValue, clsx } from "clsx"; import { twMerge } from "tailwind-merge"; diff --git a/middleware.ts b/middleware.ts deleted file mode 100644 index fa0386d..0000000 --- a/middleware.ts +++ /dev/null @@ -1,23 +0,0 @@ -// @auto-i18n-check. Please do not delete the line. -import createMiddleware from "next-intl/middleware"; - -import { defaultLocale, locales } from "./i18n-metadata"; - -// export { auth as middleware } from "@/auth" - -export default createMiddleware({ - // A list of all locales that are supported - locales: locales, - - // Used when no locale matches - defaultLocale: defaultLocale, - - // 'always': This is the default, The home page will also be redirected to the default language, such as www.abc.com to www.abc.com/en - // 'as-needed': The default page is not redirected. For example, if you open www.abc.com, it is still www.abc.com - localePrefix: "always", -}); - -export const config = { - // Match only internationalized pathnames - matcher: ["/", "/(en|zh|zh-t|ja)/:path*"], -};