Better I18NBetter I18N

API Reference

Complete API documentation for @better-i18n/core

Complete API reference for the core package. All framework SDKs (Next.js, Vite, TanStack Start, Expo) are built on top of these primitives.

createI18nCore

Creates an i18n core instance for fetching translations, languages, and manifests from the CDN.

import { createI18nCore } from "@better-i18n/core";

const i18n = createI18nCore({
  project: "org/project",
  defaultLocale: "en",
});

Instance Methods


clearManifestCache

Clears the global manifest cache across all instances. Useful for testing or forcing a fresh fetch.

import { clearManifestCache } from "@better-i18n/core";

clearManifestCache();

extractLanguages

Extracts and normalizes language information from a raw manifest response. Useful when you have a manifest and need to convert it to UI-friendly language options.

import { extractLanguages } from "@better-i18n/core";

const manifest = await i18n.getManifest();
const languages = extractLanguages(manifest);
// [{ code: "en", name: "English", nativeName: "English", isDefault: true }, ...]
ParameterTypeDescription
manifestManifestResponseRaw manifest from CDN

Returns: LanguageOption[] — Normalized language options

In most cases, use i18n.getLanguages() instead — it handles fetching the manifest and extracting languages in one call.


TtlCache

Generic in-memory cache with automatic TTL expiration. Used internally for manifest caching, but available for custom use.

import { TtlCache } from "@better-i18n/core";

const cache = new TtlCache<string>();

// Store with 60s TTL
cache.set("key", "value", 60_000);

// Retrieve (returns undefined if expired)
const value = cache.get("key"); // "value"

// Check existence
cache.has("key"); // true

// Manual removal
cache.delete("key");

// Clear all entries
cache.clear();

Methods

MethodSignatureDescription
get(key: string) => T | undefinedGet value (auto-deletes if expired)
set(key: string, value: T, ttlMs: number) => voidStore with TTL in ms
has(key: string) => booleanCheck if key exists and is not expired
delete(key: string) => booleanRemove a key
clear() => voidClear all entries

detectLocale

Framework-agnostic locale detection with priority-based selection. Detects the best locale from path, cookie, and header sources.

import { detectLocale } from "@better-i18n/core";

const result = detectLocale({
  pathLocale: "tr",
  cookieLocale: "en",
  headerLocale: "de",
  defaultLocale: "en",
  availableLocales: ["en", "tr", "de"],
  project: "org/project",
});

console.log(result.locale);        // "tr"
console.log(result.detectedFrom);  // "path"
console.log(result.shouldSetCookie); // true

Detection Priority

  1. Path — Locale from URL (e.g., /tr/about)
  2. Cookie — Stored user preference
  3. Header — Browser's Accept-Language header
  4. Default — Fallback to defaultLocale

Options

OptionTypeDescription
projectstringProject identifier
defaultLocalestringFallback locale
pathLocalestring | nullLocale from URL path
cookieLocalestring | nullLocale from cookie
headerLocalestring | nullLocale from Accept-Language
availableLocalesstring[]Supported locale codes

Result

PropertyTypeDescription
localestringDetected locale code
detectedFrom"path" | "cookie" | "header" | "default"Detection source
shouldSetCookiebooleanWhether to update the locale cookie

Configuration Utilities


createLogger

Creates a namespaced logger instance with level filtering.

import { createLogger, normalizeConfig } from "@better-i18n/core";

const config = normalizeConfig({
  project: "org/project",
  defaultLocale: "en",
  debug: true,
});

const logger = createLogger(config, "my-module");
logger.debug("loading translations"); // [better-i18n:my-module] loading translations
logger.info("ready");                 // [better-i18n:my-module] ready
logger.warn("cache miss");            // [better-i18n:my-module] cache miss
logger.error("fetch failed");         // [better-i18n:my-module] fetch failed

Log Levels

LevelValueDescription
"debug"0All messages
"info"1Info and above
"warn"2Warnings and above (default)
"error"3Errors only
"silent"4No output

Types

All types are exported from @better-i18n/core:

import type {
  // Configuration
  I18nCoreConfig,
  NormalizedConfig,
  ParsedProject,

  // Manifest
  ManifestResponse,
  ManifestLanguage,
  ManifestFile,
  LanguageOption,

  // Messages
  Messages,
  Locale,

  // Instance
  I18nCore,

  // Cache
  CacheEntry,

  // Logger
  Logger,
  LogLevel,

  // Locale URL utilities
  LocaleConfig,

  // Middleware/Detection
  I18nMiddlewareConfig,
  LocaleDetectionOptions,
  LocaleDetectionResult,
  LocalePrefix,
} from "@better-i18n/core";

On this page