# react-native-i18njs **Repository Path**: ws18250840411/react-native-i18njs ## Basic Information - **Project Name**: react-native-i18njs - **Description**: No description available - **Primary Language**: Unknown - **License**: ISC - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-01-13 - **Last Updated**: 2026-02-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # react-native-i18njs [![npm version](https://img.shields.io/npm/v/react-native-i18njs.svg)](https://www.npmjs.com/package/react-native-i18njs) [![License](https://img.shields.io/npm/l/react-native-i18njs.svg)](https://www.npmjs.com/package/react-native-i18njs) [![Platform](https://img.shields.io/badge/platform-react--native-blue.svg)](https://reactnative.dev) [![TypeScript](https://img.shields.io/badge/types-included-blue.svg)](https://www.typescriptlang.org) [![gzip size](https://img.shields.io/badge/gzip-%7E3.3%20KB-brightgreen.svg)](https://www.npmjs.com/package/react-native-i18njs) 一个**极致轻量**、类型安全、零心智负担的 React Native 国际化解决方案。 构建产物经 minify + gzip 后约 **3.3 KB**,对包体积几乎无感。 专为 React Native 设计,集成了最佳实践,解决繁琐配置、类型缺失、复杂 API 以及系统语言跟随等问题。 ## ✨ 特性 - 📦 **极小体积**:ESM 构建 gzip 约 3.3 KB,CJS 约 3.5 KB;源码极致压缩,无冗余依赖。 - ✨ **零配置启动**:内置智能默认值,安装即用。 - 🛡️ **极致类型安全**:完全 TypeScript 编写,提供从 Key 到插值参数的完整类型推导。 - 📱 **自动跟随系统**:基于 `react-native-localize`,自动检测并响应设备语言变更。 - ⚡ **高性能**:基于 `i18n-js` 核心,轻量高效,无多余运行时开销。 - 🔌 **灵活 API**:同时支持 Hook (`useI18n`)、高阶组件 (`withI18n`) 和全局函数 (`t`)。 - 📝 **富文本支持**:`Trans` 组件轻松处理嵌套样式和组件插值。 - 🌍 **格式化内置**:开箱即用的数字、货币、日期格式化支持。 ## 📦 体积说明 (Bundle Size) | 格式 | 未压缩 | gzip | |------|--------|------| | ESM (`.mjs`) | ~8.1 KB | **~3.3 KB** | | CJS (`.js`) | ~8.9 KB | **~3.5 KB** | 以上为单入口打包、开启 minify 后的体积;实际打进业务的增量取决于打包工具的 tree-shaking。推荐使用 ESM 以获得更优体积与 gzip 表现。 ## 📦 安装 ```bash npm install react-native-i18njs # 或者 yarn add react-native-i18njs ``` > **注意**:`react-native-localize` 为 peer dependency,请确保项目中已安装: > > ```bash > npm install react-native-localize > ``` ## 🚀 快速开始 ### 1. 定义翻译资源 建议在单独的文件中管理翻译资源,例如 `src/locales/index.ts`: ```ts // src/locales/index.ts export const translations = { en: { welcome: 'Welcome', hello: 'Hello, %{name}!', }, zh: { welcome: '欢迎', hello: '你好,%{name}!', }, }; // 导出类型以获得类型提示 export type Translations = typeof translations.en; ``` ### 2. 初始化 在你的 App 入口文件(如 `App.tsx`)中初始化: ```tsx import React from 'react'; import { initI18n, I18nProvider } from 'react-native-i18njs'; import { translations } from './src/locales'; import Home from './src/Home'; // 初始化配置 initI18n(translations, { defaultLocale: 'en', enableFallback: true, // 找不到翻译时回退到默认语言 }); export default function App() { return ( // 使用 Provider 以支持语言切换时的自动重渲染 ); } ``` ### 3. 在组件中使用 ```tsx // src/Home.tsx import React from 'react'; import { Text, Button, View } from 'react-native'; import { useI18n } from 'react-native-i18njs'; import { Translations } from './locales'; export default function Home() { // 传入泛型 Translations 以获得 key 的自动补全和类型检查 const { t, locale, setLocale } = useI18n(); return ( {t('welcome')} {/* 这里的 name 参数会有类型提示 */} {t('hello', { name: 'Trae' })} 当前语言: {locale}