Skip to content

Commit 72d933e

Browse files
authored
🐛 Fix the issue where the Chinese language is displayed after refreshing the English interface.
2 parents f77c304 + 1ed5306 commit 72d933e

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

frontend/app/[locale]/layout.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import "../globals.css"
66
import { ReactNode } from 'react';
77
import path from 'path';
88
import fs from 'fs';
9+
import I18nProviderWrapper from "@/components/providers/I18nProviderWrapper"
910

1011
const inter = Inter({ subsets: ["latin"] })
1112

@@ -15,15 +16,18 @@ export async function generateMetadata({
1516
}: {
1617
params: { locale: string };
1718
}): Promise<Metadata> {
18-
const filePath = path.join(process.cwd(), 'public', 'locales', locale, 'common.json');
19-
const messages = JSON.parse(fs.readFileSync(filePath, 'utf8'));
19+
let messages: any = {}
20+
if (['zh', 'en'].includes(locale)) {
21+
const filePath = path.join(process.cwd(), 'public', 'locales', locale, 'common.json');
22+
messages = JSON.parse(fs.readFileSync(filePath, 'utf8'));
23+
}
2024

2125
return {
2226
title: {
23-
default: messages.layout.title,
24-
template: messages.layout.titleTemplate,
27+
default: messages.layout?.title,
28+
template: messages.layout?.titleTemplate,
2529
},
26-
description: messages.layout.description,
30+
description: messages.layout?.description,
2731
icons: {
2832
icon: '/modelengine-logo.png',
2933
shortcut: '/favicon.ico',
@@ -46,7 +50,7 @@ export default function RootLayout({
4650
</head>
4751
<body className={inter.className}>
4852
<ThemeProvider attribute="class" defaultTheme="light" enableSystem disableTransitionOnChange>
49-
{children}
53+
<I18nProviderWrapper>{children}</I18nProviderWrapper>
5054
</ThemeProvider>
5155
</body>
5256
</html>

frontend/app/[locale]/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"use client"
2-
import "./i18n"
2+
33
import { useState, useEffect } from "react"
44
import { useTranslation } from 'react-i18next'
55
import { Bot, Globe, Database, Zap, Mic, FileSearch, Shield, MessagesSquare, Microchip } from "lucide-react"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"use client"
2+
3+
import i18n from "@/app/i18n"
4+
import { I18nextProvider } from "react-i18next"
5+
import { ReactNode, useEffect, useState } from "react"
6+
import { usePathname } from "next/navigation"
7+
8+
export default function I18nProviderWrapper({
9+
children,
10+
}: {
11+
children: ReactNode
12+
}) {
13+
const [mounted, setMounted] = useState(false)
14+
const pathname = usePathname();
15+
16+
useEffect(() => {
17+
setMounted(true)
18+
}, [])
19+
20+
// Synchronize i18n language according to the URL
21+
useEffect(() => {
22+
if (!mounted) return;
23+
24+
const segments = pathname.split('/').filter(Boolean);
25+
const urlLocale = segments[0];
26+
27+
if (urlLocale === 'zh' || urlLocale === 'en') {
28+
if (i18n.language !== urlLocale) {
29+
i18n.changeLanguage(urlLocale);
30+
}
31+
document.cookie = `NEXT_LOCALE=${urlLocale}; path=/; max-age=31536000`;
32+
}
33+
}, [pathname, i18n, mounted]);
34+
35+
if (!mounted) {
36+
return null
37+
}
38+
39+
return <I18nextProvider i18n={i18n}>{children}</I18nextProvider>
40+
}

0 commit comments

Comments
 (0)