@djangocfg/i18n

中文文档
2.1.655发布于 2026/11/12MITTypeScriptES Module近 30 天下载 2万

Lightweight i18n library for @djangocfg packages with built-in translations for English, Russian, and Korean

原始使用文档
文档目录15 节
  1. Features
  2. Installation
  3. Quick Start
  4. Subpath Imports (Server Components)
  5. Self-contained copy
  6. CLI
  7. Hooks
  8. useT()
  9. useI18n()
  10. Type-safe next-intl Integration
  11. Extending Translations
  12. Interpolation
  13. Translation Key Paths
  14. Works Without Provider
  15. License

@djangocfg/i18n

@djangocfg/i18n

Lightweight, type-safe i18n library for @djangocfg packages.

Features

  • 17 languages — en, ru, ko, ja, de, fr, zh, it, es, nl, ar, tr, pt-BR, pl, sv, no, da
  • Type-safe — Full TypeScript support with autocomplete
  • CLI included — Manage locales from the terminal
  • Works standalone — Components work without provider

Installation

pnpm add @djangocfg/i18n

Quick Start

import { I18nProvider, useT, ru } from '@djangocfg/i18n'

<I18nProvider locale="ru" translations={ru}>
  <App />
</I18nProvider>

function MyComponent() {
  const t = useT()
  return <span>{t('ui.form.save')}</span>
}

Subpath Imports (Server Components)

// ✅ Server-safe (no React Context)
import { en, ru, ko } from '@djangocfg/i18n/locales'
import { mergeTranslations } from '@djangocfg/i18n/utils'

// ❌ Client-only (has React Context)
import { I18nProvider, useT } from '@djangocfg/i18n'
PathDescription
@djangocfg/i18nFull package (client components)
@djangocfg/i18n/localesLocale data only (server-safe)
@djangocfg/i18n/utilsUtilities like mergeTranslations (server-safe)

Self-contained copy

Widgets ship their own typed dictionaries and share one locale contract:

import { createCopy, type Locale } from '@djangocfg/i18n/copy'

const DICTIONARIES: Record<Locale, MyCopy> = { en, ru, /* ... */ 'pt-BR': ptBR }
export const useMyCopy = createCopy(DICTIONARIES).useCopy

See @docs/SELF_CONTAINED_COPY.md.

CLI

# Check missing keys across locales
pnpm i18n check

# List / search keys
pnpm i18n list
pnpm i18n list tour        # filter by pattern
pnpm i18n list -v          # with values

# Add key to all locales at once
pnpm i18n add "tools.new" '{"en":"New","ru":"Новый"}'

# Work with app locales in another directory
pnpm i18n check -d ../../apps/hub/i18n/locales
pnpm i18n list -d ../../apps/hub/i18n/locales

Hooks

HookDescription
useT()Returns translation function (recommended)
useI18n()Full context: { t, locale, setLocale, translations }
useLocale()Returns current locale string
useTypedT<T>()Type-safe with compile-time key validation

useT()

function MyComponent() {
  const t = useT()
  return <button>{t('ui.form.save')}</button>
}

useI18n()

function LocaleSwitcher() {
  const { t, locale, setLocale } = useI18n()

  return (
    <select value={locale} onChange={(e) => setLocale(e.target.value)}>
      <option value="en">English</option>
      <option value="ru">Russian</option>
    </select>
  )
}

Type-safe next-intl Integration

Override useTranslations in your app's global.d.ts to get compile-time key validation.

// global.d.ts
type _Messages = import('@djangocfg/i18n').I18nTranslations &
  import('./i18n/locales/types').AppTranslations;

type _NSKeys = import('@djangocfg/i18n').NamespaceKeys<
  _Messages,
  import('@djangocfg/i18n').NestedKeyOf<_Messages>
>;

declare module 'next-intl' {
  export function useTranslations<NS extends _NSKeys>(
    namespace: NS,
  ): import('@djangocfg/i18n').IntlTranslator<_Messages, NS>;
}

Extending Translations

// Spread merge (recommended for next-intl)
import { en as baseEn } from '@djangocfg/i18n/locales'
const messages = { ...baseEn, ...appEn }

// Deep merge with overrides
import { mergeTranslations, ru } from '@djangocfg/i18n'
const customRu = mergeTranslations(ru, {
  ui: { select: { placeholder: 'Выберите...' } },
})

Interpolation

t('ui.pagination.showing', { from: 1, to: 10, total: 100 })
// => "1-10 of 100"

Translation Key Paths

ui.*         - UI components (select, form, dialog, table, pagination)
layouts.*    - Layout components (sidebar, auth, profile, theme)
api.*        - API/network messages
tools.*      - Heavy tools (tour, upload, code, image)

Works Without Provider

Components using useT() fall back to English defaults when used outside a provider.

License

MIT

版本与发布记录

@djangocfg/i18n 共发布 464 个版本,首次发布于 2026/01/23;过去 12 个月发布了 464 个版本。

版本发布日期距今
2.1.655latest2026/11/12今天
2.1.6532026/11/12今天
2.1.6522026/11/11今天
2.1.6512026/11/111 天前
2.1.6502026/11/111 天前
2.1.6482026/11/111 天前

关于 @djangocfg/i18n 的常见问题

@djangocfg/i18n 是什么?

Lightweight i18n library for @djangocfg packages with built-in translations for English, Russian, and Korean

如何安装 @djangocfg/i18n?

在项目目录运行 npm install @djangocfg/i18n,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。它还提供命令行工具 i18n,可以用 npx i18n 直接运行。

@djangocfg/i18n 支持 TypeScript 吗?

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

@djangocfg/i18n 支持 ESM 还是 CommonJS?

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

@djangocfg/i18n 可以商用吗?

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

@djangocfg/i18n 的下载量有多少?

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

@djangocfg/i18n 还在维护吗?

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