Skip to content
better-i18n.com
本页内容

The createI18n function is the entry point for the SDK. It accepts a configuration object that defines your project context and delivery preferences.

Basic Configuration #

The absolute minimum required is your project indentifier and default locale.

i18n.config.ts
import { createI18n } from "@better-i18n/next";

export const i18n = createI18n({
  projectId: "your-org/your-project",
  defaultLocale: "en",
});

Full Reference #

OptionTypeDefaultDescription
projectstringrequiredProject identifier in org/project format
defaultLocalestringrequiredDefault/fallback locale code
cdnBaseUrlstringautoCDN base URL (auto-detected)
localePrefix"as-needed" | "always" | "never""as-needed"URL locale prefix behavior
debugbooleanfalseEnable debug logging
logLevelLogLevel"warn"Logging verbosity
manifestCacheTtlMsnumber300000Manifest cache TTL in ms
manifestRevalidateSecondsnumber3600Next.js revalidate for manifest
messagesRevalidateSecondsnumber30Next.js revalidate for messages
cookieNamestring"locale"Name of the locale persistence cookie
explicitCookieNamestring`${cookieName}_explicit`Cookie written by useSetLocale to mark a deliberate choice (match middleware's detection.explicitCookieName)
timeZonestringSystem tzIANA timezone. Set explicitly to avoid ENVIRONMENT_FALLBACK warnings and prevent SSR/client hydration mismatches.
storageTranslationStoragePersistent storage adapter (offline fallback)
staticDataRecord<string, Messages> | (() => Promise<...>)Last-resort bundled translations for offline use
fetchTimeoutnumber10000CDN fetch timeout in ms
retryCountnumber1Retry attempts on CDN failure
namespacesstring[]Default namespaces to fetch on every call (namespaced_folders projects only). See Selective Loading.

Behavior Customization #

Locale Prefix

Control how locales appear in your URLs.

Code
**`"as-needed"`** (default) — Only non-default locales get a prefix. Clean URLs for your primary language.
```ts
// /about (English), /tr/about (Turkish)
createI18n({ projectId: "org/project", defaultLocale: "en", localePrefix: "as-needed" });
```

**`"always"`** — Every URL includes the locale, including the default. Best for explicit SEO signals.
```ts
// /en/about, /tr/about
createI18n({ projectId: "org/project", defaultLocale: "en", localePrefix: "always" });
```

**`"never"`** — No locale in URLs. Locale is determined by cookie and `Accept-Language` header.
```ts
// /about for all locales
createI18n({ projectId: "org/project", defaultLocale: "en", localePrefix: "never" });
```

> [!WARNING]

"never" mode requires specific file structure and middleware behavior. See Middleware — Locale Prefix Modes for the full setup guide.

Caching

Configure TTL for edge and runtime caching. ```ts createI18n({ projectId: "org/project", defaultLocale: "en", // Cache manifest in memory for 5 minutes manifestCacheTtlMs: 5 * 60 * 1000,

Code
  // Next.js ISR revalidation times
  manifestRevalidateSeconds: 3600,
  messagesRevalidateSeconds: 30,
});
```

Debug Mode

Enable detailed logging for troubleshooting. ts createI18n({ projectId: "org/project", defaultLocale: "en", debug: true, logLevel: "debug", });