mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-26 19:16:24 +00:00
fix: Closes #5 修复首选语言检测错误的问题
This commit is contained in:
@ -4,7 +4,7 @@ import "./globals.css";
|
|||||||
import { DeviceProvider } from "@/contexts/device-context";
|
import { DeviceProvider } from "@/contexts/device-context";
|
||||||
import { NextIntlClientProvider } from 'next-intl';
|
import { NextIntlClientProvider } from 'next-intl';
|
||||||
import config from "@/config";
|
import config from "@/config";
|
||||||
import { getUserLocales } from "@/i18n/request";
|
import { getUserLocales, getFirstLocale } from '@/i18n/request';
|
||||||
import { Toaster } from "@/components/ui/sonner"
|
import { Toaster } from "@/components/ui/sonner"
|
||||||
|
|
||||||
const geistSans = Geist({
|
const geistSans = Geist({
|
||||||
@ -28,7 +28,7 @@ export default async function RootLayout({
|
|||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
}>) {
|
}>) {
|
||||||
return (
|
return (
|
||||||
<html lang={(await getUserLocales())[0] || "en"} className="h-full">
|
<html lang={await getFirstLocale() || "en"} className="h-full">
|
||||||
<body
|
<body
|
||||||
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
|
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
|
||||||
>
|
>
|
||||||
|
@ -35,7 +35,6 @@ export function CommentItem(
|
|||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
const locale = useLocale();
|
const locale = useLocale();
|
||||||
console.log("locale", locale);
|
|
||||||
const t = useTranslations("Comment");
|
const t = useTranslations("Comment");
|
||||||
const commonT = useTranslations("Common");
|
const commonT = useTranslations("Common");
|
||||||
const clickToUserProfile = useToUserProfile();
|
const clickToUserProfile = useToUserProfile();
|
||||||
|
@ -10,7 +10,6 @@ export default getRequestConfig(async () => {
|
|||||||
try {
|
try {
|
||||||
return (await import(`@/locales/${locale}.json`)).default;
|
return (await import(`@/locales/${locale}.json`)).default;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.debug(`Failed to load locale ${locale}:`);
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -30,8 +29,7 @@ export async function getUserLocales(): Promise<string[]> {
|
|||||||
const user = (await getLoginUser(token)).data;
|
const user = (await getLoginUser(token)).data;
|
||||||
locales.push(user.language);
|
locales.push(user.language);
|
||||||
locales.push(user.language.split('-')[0]);
|
locales.push(user.language.split('-')[0]);
|
||||||
} catch (error) {
|
} catch {
|
||||||
console.info("获取用户信息失败,使用默认语言", error);
|
|
||||||
}
|
}
|
||||||
const languageInCookie = cookieStore.get('language')?.value;
|
const languageInCookie = cookieStore.get('language')?.value;
|
||||||
if (languageInCookie) {
|
if (languageInCookie) {
|
||||||
@ -46,3 +44,8 @@ export async function getUserLocales(): Promise<string[]> {
|
|||||||
}
|
}
|
||||||
return locales.reverse();
|
return locales.reverse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getFirstLocale(): Promise<string> {
|
||||||
|
const locales = await getUserLocales();
|
||||||
|
return locales.reverse()[0] || 'en';
|
||||||
|
}
|
Reference in New Issue
Block a user