@ better-i18n/ 下一个
下一个.js 整合 更好的i18n (英语)。提供无缝 next-intl 与 CDN 驱动的翻译获取集成 .
特征
- 零配置同步 - 通过下列方式连接您的项目:
i18n.config.ts - 边缘准备缓存 - 内设的伊斯兰社支持公开和信息
- 类型安全 - 全部类型脚本支持
- 下图 - 设计为
next-intl
安装
npm install @better-i18n/next next-intl
# or
bun add @better-i18n/next next-intl
快速启动
1. 创建i18n配置
// i18n.config.ts
import { createI18n } from "@better-i18n/next";
export const i18n = createI18n({
project: "your-org/your-project", // Format: "org-slug/project-slug"
defaultLocale: "en",
});
2. 设置请求配置
// src/i18n/request.ts
import { i18n } from "../i18n.config";
export default i18n.requestConfig;
3. 添加中间软件
// middleware.ts
import { i18n } from "./i18n.config";
export default i18n.middleware;
export const config = {
matcher: ["/((?!api|_next|.*\\..*).*)"],
};
4. 使用翻译
// app/page.tsx
import { useTranslations } from "next-intl";
export default function Home() {
const t = useTranslations("common");
return <h1>{t("welcome")}</h1>;
}
高级中件
为了更多的控制,使用 createBetterI18nMiddleware数字 :
import { createBetterI18nMiddleware, composeMiddleware } from "@better-i18n/next/middleware";
const i18nMiddleware = createBetterI18nMiddleware({
project: "org/project",
defaultLocale: "en",
detection: {
cookie: true,
browserLanguage: true,
cookieName: "locale",
cookieMaxAge: 31536000, // 1 year
},
});
// Compose with other middleware
export default composeMiddleware(i18nMiddleware, authMiddleware);
客户钩
"use client";
import { useManifestLanguages } from "@better-i18n/next/client";
export function LanguageSwitcher() {
const { languages, isLoading, error } = useManifestLanguages({
project: "your-org/your-project",
defaultLocale: "en",
});
if (isLoading) return <span>Loading...</span>;
if (error) return <span>Error: {error.message}</span>;
return (
<select>
{languages.map((lang) => (
<option key={lang.code} value={lang.code}>
{lang.nativeName || lang.name || lang.code}
</option>
))}
</select>
);
}
服务器公用事业
import {
getMessages,
getLocales,
getManifestLanguages,
} from "@better-i18n/next/server";
const config = { project: "org/project", defaultLocale: "en" };
const locales = await getLocales(config);
const messages = await getMessages(config, "tr");
const languages = await getManifestLanguages(config);
配置选项
| 选项 | 类型 | 默认 | 说明 |
|---|---|---|---|
project | string | 所需经费 | 项目标识符( E)org/project 格式) |
defaultLocale | string | 所需经费 | 默认/ 退后地块代码 |
cdnBaseUrl | string | 自动 | CDN 基础 URL (自动检测) |
localePrefix | "as-needed" | "always" | "never" | "as-needed" | URL 语言前缀行为 |
cookieName | string | "locale" | 地方持久性的 Cookie 名 |
manifestRevalidateSeconds | number | 3600 | 下一.js ISR 重新验证显示 |
messagesRevalidateSeconds | number | 30 | Next.js ISR 对消息的再验证 |
storage | TranslationStorage | undefined | 持续的离线后退缓存(见 @better-i18n/core(中文(简体) ) |
staticData | Record | () => Promise | undefined | 装订的翻译为后退 |
fetchTimeout | number | 10000 | CDN 获取超时( ms) |
retryCount | number | 1 | 重试对 CDN 失败的尝试 |
debug | boolean | false | 启用调试日志 |
logLevel | LogLevel | "warn" | 记录动词 |
除错日志
启用环境变量 :
BETTER_I18N_DEBUG=1BETTER_I18N_LOG_LEVEL=debug|info|warn|error|silent
文档
备有文件全文 docs.better-i18n.com/ next (中文(简体) )
许可证
麻省理工学院 更好的i18n (英语)