
@djangocfg/i18n (英语)
轻量级,用于@djangocfg包的型号安全的i18n库.
特征
- 17种语言 - en, ru, ko, ja, de, fr, zh, it, es, nl, ar, tr, pt-BR, pl, sv, no, da - == 翻译: 翻译:
- 类型安全 – 自动完成的完整类型脚本支持
- 包括国别学习 ——管理终端的地块
- 独立工作 - 部件工作没有提供者
安装
pnpm add @djangocfg/i18n
快速启动
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>
}
子路径导入( 服务器组件)
// ✅ 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'
| 路径 | 说明 |
|---|---|
@djangocfg/i18n | 完整软件包(客户组件) |
@djangocfg/i18n/locales | 仅限本地化数据( 服务器安全) |
@djangocfg/i18n/utils | 公用事业 mergeTranslations (服务器安全) |
自足副本
部件运送自己的打字词典并共享一个地块合同:
import { createCopy, type Locale } from '@djangocfg/i18n/copy'
const DICTIONARIES: Record<Locale, MyCopy> = { en, ru, /* ... */ 'pt-BR': ptBR }
export const useMyCopy = createCopy(DICTIONARIES).useCopy
见 @docs/SELF_CONTAINED_COPY.md。 。 。 .
国 际
# 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
钩子
| 钩子 | 说明 |
|---|---|
useT() | 返回翻译功能(建议) |
useI18n() | 完整背景 : { t, locale, setLocale, translations } |
useLocale() | 返回当前区域字符串 |
useTypedT<T>() | 具有编译时键验证的类型安全 |
使用T( )
function MyComponent() {
const t = useT()
return <button>{t('ui.form.save')}</button>
}
使用I18n( )
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>
)
}
类型安全后置集成
覆盖 useTranslations 在你的应用软件 global.d.ts 以获取编译时间密钥验证.
// 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>;
}
扩展翻译
// 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: 'Выберите...' } },
})
内插
t('ui.pagination.showing', { from: 1, to: 10, total: 100 })
// => "1-10 of 100"
翻译密钥路径
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)
没有提供者的工程
使用组件 useT() 回到英语默认值时, 当使用在提供者之外.
许可证
麻省理工学院