@better-i18n/core

中文文档
0.15.0发布于 2026/10/26MITTypeScriptES Module零依赖近 30 天下载 4326

Framework-agnostic core utilities for Better i18n

原始使用文档
文档目录10 节
  1. Features
  2. Installation
  3. Usage
  4. Configuration
  5. Offline Fallback
  6. Storage Adapter
  7. Static Data
  8. Framework Integrations
  9. Documentation
  10. License

@better-i18n/core

Framework-agnostic core utilities for fetching translations from Better i18n CDN.

Features

  • Framework Agnostic - Works with any JavaScript runtime
  • Edge-Ready - Optimized for edge environments (Cloudflare, Vercel, etc.)
  • Type-Safe - Full TypeScript support
  • Cached - Built-in manifest caching with configurable TTL
  • Offline Fallback - 5-layer fallback chain: TtlCache → CDN (with ETag 304) → persistent storage → static data
  • Resilient - Configurable fetch timeout and retry with exponential backoff

Installation

npm install @better-i18n/core
# or
bun add @better-i18n/core

Usage

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

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

// Fetch messages for a locale
const messages = await i18n.getMessages("en");

// Get available locales
const locales = await i18n.getLocales();
// ["en", "tr", "de", ...]

// Get language options with metadata (for UI)
const languages = await i18n.getLanguages();
// [{ code: "en", name: "English", nativeName: "English", flagUrl: "..." }, ...]

// Get manifest
const manifest = await i18n.getManifest();

Configuration

interface I18nCoreConfig {
  // Required
  project: string;        // "org/project" format
  defaultLocale: string;  // e.g., "en"

  // Optional
  cdnBaseUrl?: string;           // default: "https://cdn.better-i18n.com"
  manifestCacheTtlMs?: number;   // default: 300000 (5 minutes)
  debug?: boolean;               // default: false
  logLevel?: LogLevel;           // default: "warn"
  fetch?: typeof fetch;          // custom fetch function

  // Fallback & Resilience
  storage?: TranslationStorage;  // persistent cache (localStorage, AsyncStorage, etc.)
  staticData?: Record<string, Messages> | (() => Promise<Record<string, Messages>>);
  fetchTimeout?: number;         // default: 10000 (10s) — abort if CDN doesn't respond
  retryCount?: number;           // default: 1 — retry attempts on CDN failure
}

Offline Fallback

Translations are resolved through a 5-layer fallback chain:

TtlCache (in-memory) → CDN fetch → CDN 304 (ETag) → Storage (persistent cache) → Static Data (bundled) → throw
  1. TtlCache — In-memory cache with configurable TTL (default: 5 minutes). Instant, no I/O.
  2. CDN fetch — Fresh translation data from Better i18n CDN.
  3. CDN 304 (ETag) — If CDN returns "not modified", reuse cached version.
  4. Storage — Persistent cache (localStorage, AsyncStorage, etc.) survives app restarts.
  5. Static Data — Bundled translations as absolute last resort.

Storage Adapter

Persist translations locally so they survive CDN outages and app restarts:

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

const i18n = createI18nCore({
  project: "your-org/your-project",
  defaultLocale: "en",
  storage: createLocalStorage(), // browser localStorage
});

Built-in adapters:

AdapterEnvironmentImport
createLocalStorage()Browser@better-i18n/core
createMemoryStorage()Any (non-persistent)@better-i18n/core
createAutoStorage()Auto-detect (localStorage or memory)@better-i18n/core

You can also provide any object implementing { get, set, remove? } for custom stores (e.g., AsyncStorage, MMKV).

Static Data

Bundle translations as a last-resort fallback:

const i18n = createI18nCore({
  project: "your-org/your-project",
  defaultLocale: "en",
  // Static import
  staticData: {
    en: { common: { hello: "Hello" } },
    tr: { common: { hello: "Merhaba" } },
  },
  // Or lazy import (code-split)
  // staticData: () => import('./fallback-translations.json'),
});

Framework Integrations

This package is the foundation for all Better i18n SDKs:

PackageFrameworkBuilt On
@better-i18n/nextNext.jsnext-intl
@better-i18n/use-intlReact, Vite, TanStack Startuse-intl
@better-i18n/expoExpo / React Nativei18next

If you're using one of these frameworks, install the framework SDK instead — @better-i18n/core is included automatically as a dependency.

Documentation

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

License

MIT © Better i18n

版本与发布记录

@better-i18n/core 共发布 31 个版本,首次发布于 2026/01/16;过去 12 个月发布了 31 个版本。

版本发布日期距今
0.15.0latest2026/10/2612 天前
0.14.22026/09/091 个月前
0.14.12026/08/102 个月前
0.14.02026/08/092 个月前
0.13.02026/05/305 个月前
0.12.02026/05/155 个月前

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

@better-i18n/core 是什么?

Framework-agnostic core utilities for Better i18n

如何安装 @better-i18n/core?

在项目目录运行 npm install @better-i18n/core,或使用 pnpm add / yarn add / bun add。

@better-i18n/core 支持 TypeScript 吗?

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

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

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

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

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

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

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

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

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