️ feat: refactor sorting parameters in post listing API and components

- Renamed `orderedBy` to `orderBy` and `reverse` to `desc` in ListPostsParams interface and related functions.
- Updated all usages of the sorting parameters in the post listing logic to reflect the new naming convention.

feat: add user-related API functions

- Implemented `getLoginUser` and `getUserById` functions in the user API to fetch user details.
- Enhanced user model to include `language` property.

feat: integrate next-intl for internationalization

- Added `next-intl` plugin to Next.js configuration for improved localization support.
- Removed previous i18n implementation and replaced it with a new structure using JSON files for translations.
- Created locale files for English, Japanese, and Chinese with basic translations.
- Implemented a request configuration to handle user locales and messages dynamically.

fix: clean up unused imports and code

- Removed unused i18n utility functions and language settings from device context.
- Cleaned up commented-out code in blog card component and sidebar.

chore: update dependencies

- Added `deepmerge` for merging locale messages.
- Updated package.json and pnpm-lock.yaml to reflect new dependencies.
This commit is contained in:
2025-07-26 09:48:23 +08:00
parent 9984f665d4
commit e659de23fb
46 changed files with 660 additions and 331 deletions

View File

@ -1,6 +1,7 @@
import { BACKEND_URL } from "@/api/client";
import type { NextConfig } from "next";
import createNextIntlPlugin from 'next-intl/plugin';
const nextConfig: NextConfig = {
output: "standalone",
@ -31,4 +32,5 @@ const nextConfig: NextConfig = {
]
}
};
export default nextConfig;
const withNextIntl = createNextIntlPlugin();
export default withNextIntl(nextConfig);

View File

@ -16,14 +16,14 @@
"axios": "^1.11.0",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"deepmerge": "^4.3.1",
"field-conv": "^1.0.9",
"framer-motion": "^12.23.9",
"i18next": "^25.3.2",
"lucide-react": "^0.525.0",
"next": "15.4.1",
"next-intl": "^4.3.4",
"react": "19.1.0",
"react-dom": "19.1.0",
"react-i18next": "^15.6.1",
"react-icons": "^5.5.0",
"tailwind-merge": "^3.3.1"
},

183
web/pnpm-lock.yaml generated
View File

@ -29,30 +29,30 @@ importers:
clsx:
specifier: ^2.1.1
version: 2.1.1
deepmerge:
specifier: ^4.3.1
version: 4.3.1
field-conv:
specifier: ^1.0.9
version: 1.0.9
framer-motion:
specifier: ^12.23.9
version: 12.23.9(react-dom@19.1.0(react@19.1.0))(react@19.1.0)
i18next:
specifier: ^25.3.2
version: 25.3.2(typescript@5.8.3)
lucide-react:
specifier: ^0.525.0
version: 0.525.0(react@19.1.0)
next:
specifier: 15.4.1
version: 15.4.1(react-dom@19.1.0(react@19.1.0))(react@19.1.0)
next-intl:
specifier: ^4.3.4
version: 4.3.4(next@15.4.1(react-dom@19.1.0(react@19.1.0))(react@19.1.0))(react@19.1.0)(typescript@5.8.3)
react:
specifier: 19.1.0
version: 19.1.0
react-dom:
specifier: 19.1.0
version: 19.1.0(react@19.1.0)
react-i18next:
specifier: ^15.6.1
version: 15.6.1(i18next@25.3.2(typescript@5.8.3))(react-dom@19.1.0(react@19.1.0))(react@19.1.0)(typescript@5.8.3)
react-icons:
specifier: ^5.5.0
version: 5.5.0(react@19.1.0)
@ -101,10 +101,6 @@ packages:
resolution: {integrity: sha512-30iZtAPgz+LTIYoeivqYo853f02jBYSd5uGnGpkFV0M3xOt9aN73erkgYAmZU43x4VfqcnLxW9Kpg3R5LC4YYw==}
engines: {node: '>=6.0.0'}
'@babel/runtime@7.27.6':
resolution: {integrity: sha512-vbavdySgbTTrmFE+EsiqUTzlOr5bzlnJtUv9PynGCAKvfQqjIXbvFdumPM/GxMDfyuGMJaJAU6TO4zc1Jf1i8Q==}
engines: {node: '>=6.9.0'}
'@emnapi/core@1.4.4':
resolution: {integrity: sha512-A9CnAbC6ARNMKcIcrQwq6HeHCjpcBZ5wSx4U01WXCqEKlrzB9F9315WDNHkrs2xbx7YjjSxbUYxuN6EQzpcY2g==}
@ -152,6 +148,24 @@ packages:
resolution: {integrity: sha512-1+WqvgNMhmlAambTvT3KPtCl/Ibr68VldY2XY40SL1CE0ZXiakFR/cbTspaF5HsnpDMvcYYoJHfl4980NBjGag==}
engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0}
'@formatjs/ecma402-abstract@2.3.4':
resolution: {integrity: sha512-qrycXDeaORzIqNhBOx0btnhpD1c+/qFIHAN9znofuMJX6QBwtbrmlpWfD4oiUUD2vJUOIYFA/gYtg2KAMGG7sA==}
'@formatjs/fast-memoize@2.2.7':
resolution: {integrity: sha512-Yabmi9nSvyOMrlSeGGWDiH7rf3a7sIwplbvo/dlz9WCIjzIQAfy1RMf4S0X3yG724n5Ghu2GmEl5NJIV6O9sZQ==}
'@formatjs/icu-messageformat-parser@2.11.2':
resolution: {integrity: sha512-AfiMi5NOSo2TQImsYAg8UYddsNJ/vUEv/HaNqiFjnI3ZFfWihUtD5QtuX6kHl8+H+d3qvnE/3HZrfzgdWpsLNA==}
'@formatjs/icu-skeleton-parser@1.8.14':
resolution: {integrity: sha512-i4q4V4qslThK4Ig8SxyD76cp3+QJ3sAqr7f6q9VVfeGtxG9OhiAk3y9XF6Q41OymsKzsGQ6OQQoJNY4/lI8TcQ==}
'@formatjs/intl-localematcher@0.5.10':
resolution: {integrity: sha512-af3qATX+m4Rnd9+wHcjJ4w2ijq+rAVP3CCinJQvFv1kgSu1W6jypUmvleJxcewdxmutM8dmIRZFxO/IQBZmP2Q==}
'@formatjs/intl-localematcher@0.6.1':
resolution: {integrity: sha512-ePEgLgVCqi2BBFnTMWPfIghu6FkbZnnBVhO2sSxvLfrdFw7wCHAHiDoM2h4NRgjbaY7+B7HgOLZGkK187pZTZg==}
'@humanfs/core@0.19.1':
resolution: {integrity: sha512-5DyQ4+1JEUzejeK1JGICcideyfUbGixgS9jNgex5nqkW+cY7WZhxBigmieN5Qnw9ZosSNVC9KQKyb+GUaGyKUA==}
engines: {node: '>=18.18.0'}
@ -631,6 +645,9 @@ packages:
'@rushstack/eslint-patch@1.12.0':
resolution: {integrity: sha512-5EwMtOqvJMMa3HbmxLlF74e+3/HhwBTMcvt3nqVJgGCozO6hzIPOBlwm8mGVNR9SN2IJpxSnlxczyDjcn7qIyw==}
'@schummar/icu-type-parser@1.21.5':
resolution: {integrity: sha512-bXHSaW5jRTmke9Vd0h5P7BtWZG9Znqb8gSDxZnxaGSJnGwPLDPfS+3g0BKzeWqzgZPsIVZkM7m2tbo18cm5HBw==}
'@swc/helpers@0.5.15':
resolution: {integrity: sha512-JQ5TuMi45Owi4/BIMAJBoSQoOJu12oOk/gADqlcUL9JEdHB8vyjUSsxqeNXnmXHjYKMi2WcYtezGEEhqUI/E2g==}
@ -1097,9 +1114,16 @@ packages:
supports-color:
optional: true
decimal.js@10.6.0:
resolution: {integrity: sha512-YpgQiITW3JXGntzdUmyUR1V812Hn8T1YVXhCu+wO3OpS4eU9l4YdD3qjyiKdV6mvV29zapkMeD390UVEf2lkUg==}
deep-is@0.1.4:
resolution: {integrity: sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==}
deepmerge@4.3.1:
resolution: {integrity: sha512-3sUqbMEc77XqpdNO7FRyRog+eW3ph+GYCbj+rK+uYyRMuwsVy0rMiVtPn+QJlKFvWP/1PYpapqYn0Me2knFn+A==}
engines: {node: '>=0.10.0'}
define-data-property@1.1.4:
resolution: {integrity: sha512-rBMvIzlpA8v6E+SJZoo++HAYqsLrkg7MSfIinMPFhmkorw7X+dOXVJQs+QT69zGkzMyfDnIMN2Wid1+NbL3T+A==}
engines: {node: '>= 0.4'}
@ -1449,17 +1473,6 @@ packages:
resolution: {integrity: sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==}
engines: {node: '>= 0.4'}
html-parse-stringify@3.0.1:
resolution: {integrity: sha512-KknJ50kTInJ7qIScF3jeaFRpMpE8/lfiTdzf/twXyPBLAGrLRTmkz3AdTnKeh40X8k9L2fdYwEp/42WGXIRGcg==}
i18next@25.3.2:
resolution: {integrity: sha512-JSnbZDxRVbphc5jiptxr3o2zocy5dEqpVm9qCGdJwRNO+9saUJS0/u4LnM/13C23fUEWxAylPqKU/NpMV/IjqA==}
peerDependencies:
typescript: ^5
peerDependenciesMeta:
typescript:
optional: true
ignore@5.3.2:
resolution: {integrity: sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==}
engines: {node: '>= 4'}
@ -1480,6 +1493,9 @@ packages:
resolution: {integrity: sha512-4gd7VpWNQNB4UKKCFFVcp1AVv+FMOgs9NKzjHKusc8jTMhd5eL1NqQqOpE0KzMds804/yHlglp3uxgluOqAPLw==}
engines: {node: '>= 0.4'}
intl-messageformat@10.7.16:
resolution: {integrity: sha512-UmdmHUmp5CIKKjSoE10la5yfU+AYJAaiYLsodbjL4lji83JNvgOQUjGaGhGrpFCb0Uh7sl7qfP1IyILa8Z40ug==}
is-array-buffer@3.0.5:
resolution: {integrity: sha512-DDfANUiiG2wC1qawP66qlTugJeL5HyzMpfr8lLK+jMQirGzNod0B12cFB/9q838Ru27sBwfw78/rdoU7RERz6A==}
engines: {node: '>= 0.4'}
@ -1786,6 +1802,20 @@ packages:
natural-compare@1.4.0:
resolution: {integrity: sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==}
negotiator@1.0.0:
resolution: {integrity: sha512-8Ofs/AUQh8MaEcrlq5xOX0CQ9ypTF5dl78mjlMNfOK08fzpgTHQRQPBxcPlEtIw0yRpws+Zo/3r+5WRby7u3Gg==}
engines: {node: '>= 0.6'}
next-intl@4.3.4:
resolution: {integrity: sha512-VWLIDlGbnL/o4LnveJTJD1NOYN8lh3ZAGTWw2krhfgg53as3VsS4jzUVnArJdqvwtlpU/2BIDbWTZ7V4o1jFEw==}
peerDependencies:
next: ^12.0.0 || ^13.0.0 || ^14.0.0 || ^15.0.0
react: ^16.8.0 || ^17.0.0 || ^18.0.0 || >=19.0.0-rc <19.0.0 || ^19.0.0
typescript: ^5.0.0
peerDependenciesMeta:
typescript:
optional: true
next@15.4.1:
resolution: {integrity: sha512-eNKB1q8C7o9zXF8+jgJs2CzSLIU3T6bQtX6DcTnCq1sIR1CJ0GlSyRs1BubQi3/JgCnr9Vr+rS5mOMI38FFyQw==}
engines: {node: ^18.18.0 || ^19.8.0 || >= 20.0.0}
@ -1915,22 +1945,6 @@ packages:
peerDependencies:
react: ^19.1.0
react-i18next@15.6.1:
resolution: {integrity: sha512-uGrzSsOUUe2sDBG/+FJq2J1MM+Y4368/QW8OLEKSFvnDflHBbZhSd1u3UkW0Z06rMhZmnB/AQrhCpYfE5/5XNg==}
peerDependencies:
i18next: '>= 23.2.3'
react: '>= 16.8.0'
react-dom: '*'
react-native: '*'
typescript: ^5
peerDependenciesMeta:
react-dom:
optional: true
react-native:
optional: true
typescript:
optional: true
react-icons@5.5.0:
resolution: {integrity: sha512-MEFcXdkP3dLo8uumGI5xN3lDFNsRtrjbOEKDLD7yv76v4wpnEq2Lt2qeHaQOr34I/wPN3s3+N08WkQ+CW37Xiw==}
peerDependencies:
@ -2222,6 +2236,11 @@ packages:
'@types/react':
optional: true
use-intl@4.3.4:
resolution: {integrity: sha512-sHfiU0QeJ1rirNWRxvCyvlSh9+NczcOzRnPyMeo2rtHXhVnBsvMRjE+UG4eh3lRhCxrvcqei/I0lBxsc59on1w==}
peerDependencies:
react: ^17.0.0 || ^18.0.0 || >=19.0.0-rc <19.0.0 || ^19.0.0
use-sidecar@1.1.3:
resolution: {integrity: sha512-Fedw0aZvkhynoPYlA5WXrMCAMm+nSWdZt6lzJQ7Ok8S6Q+VsHmHpRWndVRJ8Be0ZbkfPc5LRYH+5XrzXcEeLRQ==}
engines: {node: '>=10'}
@ -2232,10 +2251,6 @@ packages:
'@types/react':
optional: true
void-elements@3.1.0:
resolution: {integrity: sha512-Dhxzh5HZuiHQhbvTW9AMetFfBHDMYpo23Uo9btPXgdYP+3T5S+p+jgNy7spra+veYhBP2dCSgxR/i2Y02h5/6w==}
engines: {node: '>=0.10.0'}
which-boxed-primitive@1.1.1:
resolution: {integrity: sha512-TbX3mj8n0odCBFVlY8AxkqcHASw3L60jIuF8jFP78az3C2YhmGvqbHBpAjTRH2/xqYunrJ9g1jSyjCjpoWzIAA==}
engines: {node: '>= 0.4'}
@ -2278,8 +2293,6 @@ snapshots:
'@jridgewell/gen-mapping': 0.3.12
'@jridgewell/trace-mapping': 0.3.29
'@babel/runtime@7.27.6': {}
'@emnapi/core@1.4.4':
dependencies:
'@emnapi/wasi-threads': 1.0.3
@ -2340,6 +2353,36 @@ snapshots:
'@eslint/core': 0.15.1
levn: 0.4.1
'@formatjs/ecma402-abstract@2.3.4':
dependencies:
'@formatjs/fast-memoize': 2.2.7
'@formatjs/intl-localematcher': 0.6.1
decimal.js: 10.6.0
tslib: 2.8.1
'@formatjs/fast-memoize@2.2.7':
dependencies:
tslib: 2.8.1
'@formatjs/icu-messageformat-parser@2.11.2':
dependencies:
'@formatjs/ecma402-abstract': 2.3.4
'@formatjs/icu-skeleton-parser': 1.8.14
tslib: 2.8.1
'@formatjs/icu-skeleton-parser@1.8.14':
dependencies:
'@formatjs/ecma402-abstract': 2.3.4
tslib: 2.8.1
'@formatjs/intl-localematcher@0.5.10':
dependencies:
tslib: 2.8.1
'@formatjs/intl-localematcher@0.6.1':
dependencies:
tslib: 2.8.1
'@humanfs/core@0.19.1': {}
'@humanfs/node@0.16.6':
@ -2719,6 +2762,8 @@ snapshots:
'@rushstack/eslint-patch@1.12.0': {}
'@schummar/icu-type-parser@1.21.5': {}
'@swc/helpers@0.5.15':
dependencies:
tslib: 2.8.1
@ -3199,8 +3244,12 @@ snapshots:
dependencies:
ms: 2.1.3
decimal.js@10.6.0: {}
deep-is@0.1.4: {}
deepmerge@4.3.1: {}
define-data-property@1.1.4:
dependencies:
es-define-property: 1.0.1
@ -3699,16 +3748,6 @@ snapshots:
dependencies:
function-bind: 1.1.2
html-parse-stringify@3.0.1:
dependencies:
void-elements: 3.1.0
i18next@25.3.2(typescript@5.8.3):
dependencies:
'@babel/runtime': 7.27.6
optionalDependencies:
typescript: 5.8.3
ignore@5.3.2: {}
ignore@7.0.5: {}
@ -3726,6 +3765,13 @@ snapshots:
hasown: 2.0.2
side-channel: 1.1.0
intl-messageformat@10.7.16:
dependencies:
'@formatjs/ecma402-abstract': 2.3.4
'@formatjs/fast-memoize': 2.2.7
'@formatjs/icu-messageformat-parser': 2.11.2
tslib: 2.8.1
is-array-buffer@3.0.5:
dependencies:
call-bind: 1.0.8
@ -4003,6 +4049,18 @@ snapshots:
natural-compare@1.4.0: {}
negotiator@1.0.0: {}
next-intl@4.3.4(next@15.4.1(react-dom@19.1.0(react@19.1.0))(react@19.1.0))(react@19.1.0)(typescript@5.8.3):
dependencies:
'@formatjs/intl-localematcher': 0.5.10
negotiator: 1.0.0
next: 15.4.1(react-dom@19.1.0(react@19.1.0))(react@19.1.0)
react: 19.1.0
use-intl: 4.3.4(react@19.1.0)
optionalDependencies:
typescript: 5.8.3
next@15.4.1(react-dom@19.1.0(react@19.1.0))(react@19.1.0):
dependencies:
'@next/env': 15.4.1
@ -4140,16 +4198,6 @@ snapshots:
react: 19.1.0
scheduler: 0.26.0
react-i18next@15.6.1(i18next@25.3.2(typescript@5.8.3))(react-dom@19.1.0(react@19.1.0))(react@19.1.0)(typescript@5.8.3):
dependencies:
'@babel/runtime': 7.27.6
html-parse-stringify: 3.0.1
i18next: 25.3.2(typescript@5.8.3)
react: 19.1.0
optionalDependencies:
react-dom: 19.1.0(react@19.1.0)
typescript: 5.8.3
react-icons@5.5.0(react@19.1.0):
dependencies:
react: 19.1.0
@ -4541,6 +4589,13 @@ snapshots:
optionalDependencies:
'@types/react': 19.1.8
use-intl@4.3.4(react@19.1.0):
dependencies:
'@formatjs/fast-memoize': 2.2.7
'@schummar/icu-type-parser': 1.21.5
intl-messageformat: 10.7.16
react: 19.1.0
use-sidecar@1.1.3(@types/react@19.1.8)(react@19.1.0):
dependencies:
detect-node-es: 1.1.0
@ -4549,8 +4604,6 @@ snapshots:
optionalDependencies:
'@types/react': 19.1.8
void-elements@3.1.0: {}
which-boxed-primitive@1.1.1:
dependencies:
is-bigint: 1.1.0

View File

@ -5,8 +5,8 @@ import axiosClient from "./client";
interface ListPostsParams {
page?: number;
size?: number;
orderedBy?: string;
reverse?: boolean;
orderBy?: string;
desc?: boolean;
keywords?: string;
}
@ -24,16 +24,16 @@ export async function getPostById(id: string): Promise<Post | null> {
export async function listPosts({
page = 1,
size = 10,
orderedBy = 'updated_at',
reverse = false,
orderBy = 'updated_at',
desc = false,
keywords = ''
}: ListPostsParams = {}): Promise<BaseResponse<Post[]>> {
const res = await axiosClient.get<BaseResponse<Post[]>>("/post/list", {
params: {
page,
size,
orderedBy,
reverse,
orderBy,
desc,
keywords
}
});

View File

@ -43,4 +43,18 @@ export async function ListOidcConfigs(): Promise<BaseResponse<OidcConfig[]>> {
"/user/oidc/list"
);
return res.data;
}
export async function getLoginUser(token: string = ""): Promise<BaseResponse<User>> {
const res = await axiosClient.get<BaseResponse<User>>("/user/me", {
headers: {
Authorization: `Bearer ${token}`
}
});
return res.data;
}
export async function getUserById(id: number): Promise<BaseResponse<User>> {
const res = await axiosClient.get<BaseResponse<User>>(`/user/u/${id}`);
return res.data;
}

View File

@ -2,6 +2,8 @@ import type { Metadata } from "next";
import { Geist, Geist_Mono } from "next/font/google";
import "./globals.css";
import { DeviceProvider } from "@/contexts/device-context";
import { NextIntlClientProvider } from 'next-intl';
import { getLocale } from 'next-intl/server';
const geistSans = Geist({
variable: "--font-geist-sans",
@ -29,7 +31,7 @@ export default function RootLayout({
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
>
<DeviceProvider>
{children}
<NextIntlClientProvider>{children}</NextIntlClientProvider>
</DeviceProvider>
</body>
</html>

View File

@ -3,7 +3,7 @@ import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle }
import { Badge } from "@/components/ui/badge";
import Link from "next/link";
import Image from "next/image";
import { Calendar, Eye, Heart, MessageCircle, Lock } from "lucide-react";
import { Calendar, Eye, Heart, MessageCircle, Lock } from "lucide-react";
import { cn } from "@/lib/utils";
import config from "@/config";
@ -23,27 +23,7 @@ export function BlogCard({ post, className }: BlogCardProps) {
});
};
// 计算阅读时间(估算)
// const getReadingTime = (content: string) => {
// const wordsPerMinute = 200;
// const wordCount = content.length;
// const minutes = Math.ceil(wordCount / wordsPerMinute);
// return `${minutes} 分钟阅读`;
// };
// // 根据内容类型获取图标
// const getContentTypeIcon = (type: Post['type']) => {
// switch (type) {
// case 'markdown':
// return '📝';
// case 'html':
// return '🌐';
// case 'text':
// return '📄';
// default:
// return '📝';
// }
// };
// TODO: 阅读时间估计
return (
<Card className={cn(
@ -131,7 +111,7 @@ export function BlogCard({ post, className }: BlogCardProps) {
<CardTitle className="line-clamp-2 group-hover:text-primary transition-colors text-lg leading-tight">
{post.title}
</CardTitle>
</CardHeader>
{/* Card Content - 主要内容 */}
<CardContent className="flex-1">

View File

@ -5,8 +5,8 @@ import { Badge } from "@/components/ui/badge";
import type { Label } from "@/models/label";
import type { Post } from "@/models/post";
import type configType from '@/config';
import { t } from "i18next";
import { useEffect, useState } from "react";
import { useTranslations } from "next-intl";
// 侧边栏父组件,接收卡片组件列表
export default function Sidebar({ cards }: { cards: React.ReactNode[] }) {
@ -115,10 +115,11 @@ export function SidebarIframe(props?: { src?: string; scriptSrc?: string; title?
title = "External Content",
height = "400px",
} = props || {};
const t = useTranslations('HomePage');
return (
<Card>
<CardHeader>
<CardTitle>{t(title)}</CardTitle>
<CardTitle>{t("title")}</CardTitle>
</CardHeader>
<CardContent className="p-0">
<iframe

View File

@ -30,28 +30,28 @@ export default function BlogHome() {
const fetchPosts = async () => {
try {
setLoading(true);
let orderedBy: string;
let reverse: boolean;
let orderBy: string;
let desc: boolean;
switch (sortType) {
case 'latest':
orderedBy = 'updated_at';
reverse = false;
orderBy = 'updated_at';
desc = true;
break;
case 'popular':
orderedBy = 'heat';
reverse = false;
orderBy = 'heat';
desc = true;
break;
default:
orderedBy = 'updated_at';
reverse = false;
orderBy = 'updated_at';
desc = true;
}
// 处理关键词,空格分割转逗号
const keywords = debouncedSearch.trim() ? debouncedSearch.trim().split(/\s+/).join(",") : undefined;
const data = await listPosts({
page: 1,
size: 10,
orderedBy,
reverse,
orderBy: orderBy,
desc: desc,
keywords
});
setPosts(data.data);

View File

View File

@ -2,8 +2,6 @@
import React, { createContext, useCallback, useContext, useEffect, useState } from "react";
import i18n, { getDefaultLang } from "@/utils/i18n";
type Mode = "light" | "dark";
type Lang = string;
@ -12,8 +10,6 @@ interface DeviceContextProps {
mode: Mode;
setMode: (mode: Mode) => void;
toggleMode: () => void;
lang: Lang;
setLang: (lang: Lang) => void;
viewport: {
width: number;
height: number;
@ -25,8 +21,6 @@ const DeviceContext = createContext<DeviceContextProps>({
mode: "light",
setMode: () => {},
toggleMode: () => {},
lang: "zh-cn",
setLang: () => {},
viewport: {
width: 0,
height: 0,
@ -36,7 +30,6 @@ const DeviceContext = createContext<DeviceContextProps>({
export const DeviceProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const [isMobile, setIsMobile] = useState(false);
const [mode, setModeState] = useState<Mode>("light");
const [lang, setLangState] = useState<Lang>(getDefaultLang());
const [viewport, setViewport] = useState({
width: typeof window !== "undefined" ? window.innerWidth : 0,
height: typeof window !== "undefined" ? window.innerHeight : 0,
@ -92,15 +85,6 @@ export const DeviceProvider: React.FC<{ children: React.ReactNode }> = ({ childr
}
}, []);
// 初始化语言
useEffect(() => {
if (typeof window !== "undefined") {
const savedLang = localStorage.getItem("language") || getDefaultLang();
setLangState(savedLang);
i18n.changeLanguage(savedLang);
}
}, []);
const setMode = useCallback((newMode: Mode) => {
setModeState(newMode);
document.documentElement.classList.toggle("dark", newMode === "dark");
@ -124,15 +108,9 @@ export const DeviceProvider: React.FC<{ children: React.ReactNode }> = ({ childr
});
}, []);
const setLang = useCallback((newLang: Lang) => {
setLangState(newLang);
i18n.changeLanguage(newLang);
localStorage.setItem("language", newLang);
}, []);
return (
<DeviceContext.Provider
value={{ isMobile, mode, setMode, toggleMode, lang, setLang, viewport }}
value={{ isMobile, mode, setMode, toggleMode, viewport }}
>
{children}
</DeviceContext.Provider>

47
web/src/i18n/request.ts Normal file
View File

@ -0,0 +1,47 @@
import { getRequestConfig } from 'next-intl/server';
import { cookies, headers } from 'next/headers';
import deepmerge from 'deepmerge';
import { getLoginUser } from '@/api/user';
export default getRequestConfig(async () => {
const locales = await getUserLocales();
const messages = await Promise.all(
locales.map(async (locale) => {
try {
return (await import(`@/locales/${locale}.json`)).default;
} catch (error) {
return {};
}
})
).then((msgs) => msgs.reduce((acc, msg) => deepmerge(acc, msg), {}));
return {
locale: locales[0],
messages
};
});
export async function getUserLocales(): Promise<string[]> {
let locales: string[] = ["zh-CN", "zh", "en-US", "en"];
const headersList = await headers();
const cookieStore = await cookies();
try {
const token = cookieStore.get('token')?.value || '';
const user = (await getLoginUser(token)).data;
locales.push(user.language);
locales.push(user.language.split('-')[0]);
} catch (error) {
console.info("获取用户信息失败,使用默认语言", error);
}
const languageInCookie = cookieStore.get('language')?.value;
if (languageInCookie) {
locales.push(languageInCookie);
locales.push(languageInCookie.split('-')[0]);
}
const acceptLanguage = headersList.get('accept-language');
if (acceptLanguage) {
const languages = acceptLanguage.split(',').map(lang => lang.split(';')[0]);
const languagesWithoutRegion = languages.map(lang => lang.split('-')[0]);
locales = [...new Set([...locales, ...languages, ...languagesWithoutRegion])];
}
return locales.reverse();
}

5
web/src/locales/en.json Normal file
View File

@ -0,0 +1,5 @@
{
"HomePage": {
"title": "Hello world!"
}
}

5
web/src/locales/ja.json Normal file
View File

@ -0,0 +1,5 @@
{
"HomePage": {
"title": "Hello world!"
}
}

View File

@ -0,0 +1,5 @@
{
"HomePage": {
"title": "Hello world!"
}
}

View File

@ -6,4 +6,5 @@ export interface User {
email: string;
gender: string;
role: string;
language: string;
}

View File

@ -1,27 +0,0 @@
import { initReactI18next } from "react-i18next";
import i18n from "i18next";
import resources from "./locales";
export const getDefaultLang = () => {
if (typeof window !== "undefined") {
return (
localStorage.getItem("language") ||
navigator.language.replace("_", "-") || // 保证格式
"zh-CN"
);
}
return "zh-CN";
};
i18n.use(initReactI18next).init({
resources: resources,
lng: getDefaultLang(),
fallbackLng: "zh-CN",
interpolation: {
escapeValue: false,
},
});
export default i18n;

View File

@ -1,30 +0,0 @@
const resources = {
translation: {
name: "English",
hello: "Hello",
login: {
login: "Login",
failed: "Login failed",
forgotPassword: "Forgot password?",
username: "Username",
usernameOrEmail: "Username or Email",
password: "Password",
remember: "Remember this device",
captcha: {
no: "No captcha required",
failed: "Captcha verification failed, please try again",
fetchFailed: "Failed to fetch captcha, please try again later",
processing: "Waiting for verification...",
reCaptchaProcessing: "Processing reCAPTCHA verification, please wait...",
reCaptchaFailed: "reCAPTCHA verification failed, please try again",
reCaptchaSuccess: "reCAPTCHA verification successful",
},
oidc: {
fetchFailed: "Failed to fetch OIDC providers, please try again later",
use: "Login with {{provider}}",
},
},
},
};
export default resources;

View File

@ -1,9 +0,0 @@
import enUS from "./en-us";
import zhCN from "./zh-cn";
const resources = {
"zh-CN": zhCN,
"en-US": enUS,
};
export default resources;

View File

@ -1,30 +0,0 @@
const resources = {
translation: {
name: "中文",
hello: "你好",
login: {
login: "登录",
failed: "登录失败",
forgotPassword: "忘了密码?",
username: "用户名",
usernameOrEmail: "用户名或邮箱",
password: "密码",
remember: "记住这个设备",
captcha: {
no: "无需进行机器人挑战",
failed: "机器人挑战失败,请重试",
fetchFailed: "获取验证码失败,请稍后再试",
processing: "等待验证...",
reCaptchaProcessing: "正在处理 reCAPTCHA 验证,请稍候...",
reCaptchaFailed: "reCAPTCHA 验证失败,请重试",
reCaptchaSuccess: "reCAPTCHA 验证成功",
},
oidc: {
fetchFailed: "获取 OIDC 提供商失败,请稍后再试",
use: "使用 {{provider}} 登录",
},
},
},
};
export default resources;