Files
neo-blog/web/src/i18n/request.ts
Snowykami e659de23fb ️ 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.
2025-07-26 09:48:23 +08:00

47 lines
1.6 KiB
TypeScript

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();
}