@better-i18n/next

中文文档
0.10.0发布于 2026/10/26MITTypeScriptES Module近 30 天下载 2450

Better-i18n Next.js integration

原始使用文档
文档目录14 节
  1. Features
  2. Installation
  3. Quick Start
  4. 1. Create i18n config
  5. 2. Setup request config
  6. 3. Add middleware
  7. 4. Use translations
  8. Advanced Middleware
  9. Client Hook
  10. Server Utilities
  11. Configuration Options
  12. Debug Logging
  13. Documentation
  14. License

@better-i18n/next

Next.js integration for Better i18n. Provides seamless next-intl integration with CDN-powered translation fetching.

Features

  • Zero-Config Sync - Connects to your project via i18n.config.ts
  • Edge-Ready Caching - Built-in ISR support for manifest and messages
  • Type-Safe - Full TypeScript support
  • next-intl First - Designed as a message provider for next-intl

Installation

npm install @better-i18n/next next-intl
# or
bun add @better-i18n/next next-intl

Quick Start

1. Create i18n config

// 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. Setup request config

// src/i18n/request.ts
import { i18n } from "../i18n.config";

export default i18n.requestConfig;

3. Add middleware

// middleware.ts
import { i18n } from "./i18n.config";

export default i18n.middleware;

export const config = {
  matcher: ["/((?!api|_next|.*\\..*).*)"],
};

4. Use translations

// app/page.tsx
import { useTranslations } from "next-intl";

export default function Home() {
  const t = useTranslations("common");
  return <h1>{t("welcome")}</h1>;
}

Advanced Middleware

For more control, use 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);

Client Hook

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

Server Utilities

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

Configuration Options

OptionTypeDefaultDescription
projectstringrequiredProject identifier (org/project format)
defaultLocalestringrequiredDefault/fallback locale code
cdnBaseUrlstringautoCDN base URL (auto-detected)
localePrefix"as-needed" | "always" | "never""as-needed"URL locale prefix behavior
cookieNamestring"locale"Cookie name for locale persistence
manifestRevalidateSecondsnumber3600Next.js ISR revalidation for manifest
messagesRevalidateSecondsnumber30Next.js ISR revalidation for messages
storageTranslationStorageundefinedPersistent cache for offline fallback (see @better-i18n/core)
staticDataRecord | () => PromiseundefinedBundled translations as last-resort fallback
fetchTimeoutnumber10000CDN fetch timeout in ms
retryCountnumber1Retry attempts on CDN failure
debugbooleanfalseEnable debug logging
logLevelLogLevel"warn"Logging verbosity

Debug Logging

Enable with environment variables:

  • BETTER_I18N_DEBUG=1
  • BETTER_I18N_LOG_LEVEL=debug|info|warn|error|silent

Documentation

Full documentation available at docs.better-i18n.com/next

License

MIT © Better i18n

版本与发布记录

@better-i18n/next 共发布 39 个版本,首次发布于 2025/12/20;过去 12 个月发布了 39 个版本。

版本发布日期距今
0.10.0latest2026/10/2612 天前
0.9.32026/09/091 个月前
0.9.22026/08/102 个月前
0.9.12026/08/092 个月前
0.9.02026/05/305 个月前
0.8.22026/05/155 个月前

关于 @better-i18n/next 的常见问题

@better-i18n/next 是什么?

Better-i18n Next.js integration

如何安装 @better-i18n/next?

在项目目录运行 npm install @better-i18n/next,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:next(>=15.0.0)、react(>=18.0.0)、next-intl(>=4.0.0)、react-dom(>=18.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@better-i18n/next 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@better-i18n/next 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@better-i18n/next 可以商用吗?

@better-i18n/next 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@better-i18n/next 的下载量有多少?

根据 npm 官方下载统计,@better-i18n/next 在 2026/10/08 – 2026/11/06 这 30 天内被下载 2,450 次,日均约 82 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@better-i18n/next 还在维护吗?

最新版本 0.10.0 发布于 2026/10/26(12 天前);至今共发布 39 个版本,其中过去 12 个月发布了 39 个;首次发布于 2025/12/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。