@intlayer/docs 9.0.0-canary.5 → 9.0.0-canary.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/ar/intlayer_with_react_native+expo.md +56 -79
- package/docs/ar/packages/react-native-intlayer/exports.md +48 -14
- package/docs/de/intlayer_with_react_native+expo.md +50 -68
- package/docs/de/packages/react-native-intlayer/exports.md +47 -13
- package/docs/en/intlayer_with_react_native+expo.md +14 -18
- package/docs/en/packages/react-native-intlayer/exports.md +47 -13
- package/docs/en-GB/intlayer_with_react_native+expo.md +40 -94
- package/docs/en-GB/packages/react-native-intlayer/exports.md +51 -18
- package/docs/es/intlayer_with_react_native+expo.md +43 -80
- package/docs/es/packages/react-native-intlayer/exports.md +47 -13
- package/docs/fr/intlayer_with_react_native+expo.md +54 -68
- package/docs/fr/packages/react-native-intlayer/exports.md +47 -13
- package/docs/hi/intlayer_with_react_native+expo.md +90 -87
- package/docs/hi/packages/react-native-intlayer/exports.md +48 -14
- package/docs/id/intlayer_with_react_native+expo.md +45 -33
- package/docs/id/packages/react-native-intlayer/exports.md +47 -13
- package/docs/it/intlayer_with_react_native+expo.md +32 -72
- package/docs/it/packages/react-native-intlayer/exports.md +47 -13
- package/docs/ja/intlayer_with_react_native+expo.md +98 -137
- package/docs/ja/packages/react-native-intlayer/exports.md +47 -14
- package/docs/ko/intlayer_with_react_native+expo.md +231 -29
- package/docs/ko/packages/react-native-intlayer/exports.md +48 -14
- package/docs/pl/intlayer_with_react_native+expo.md +75 -46
- package/docs/pl/packages/react-native-intlayer/exports.md +47 -13
- package/docs/pt/intlayer_with_react_native+expo.md +38 -75
- package/docs/pt/packages/react-native-intlayer/exports.md +47 -13
- package/docs/ru/intlayer_with_react_native+expo.md +71 -60
- package/docs/ru/packages/react-native-intlayer/exports.md +49 -16
- package/docs/tr/intlayer_with_react_native+expo.md +30 -56
- package/docs/tr/packages/react-native-intlayer/exports.md +48 -15
- package/docs/uk/intlayer_with_react_native+expo.md +64 -52
- package/docs/uk/packages/react-native-intlayer/exports.md +47 -13
- package/docs/vi/intlayer_with_react_native+expo.md +43 -32
- package/docs/vi/packages/react-native-intlayer/exports.md +47 -13
- package/docs/zh/intlayer_with_react_native+expo.md +216 -82
- package/docs/zh/packages/react-native-intlayer/exports.md +48 -14
- package/package.json +6 -6
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
createdAt: 2025-06-18
|
|
3
|
-
updatedAt: 2026-
|
|
3
|
+
updatedAt: 2026-06-25
|
|
4
4
|
title: "Expo + React Native i18n - 翻译你的应用的完整指南"
|
|
5
5
|
description: "告别 i18next。2026 年构建多语言 (i18n) Expo + React Native 应用的完整指南。使用 AI 代理翻译并优化包体积、SEO 和性能。"
|
|
6
6
|
keywords:
|
|
@@ -17,6 +17,9 @@ slugs:
|
|
|
17
17
|
applicationTemplate: https://github.com/aymericzip/intlayer-react-native-template
|
|
18
18
|
applicationShowcase: https://intlayer-react-native.vercel.app
|
|
19
19
|
history:
|
|
20
|
+
- version: 9.0.0
|
|
21
|
+
date: 2026-06-25
|
|
22
|
+
changes: "直接从 react-native-intlayer 导入 provider 和 hook;react-intlayer 不再需要作为直接依赖项"
|
|
20
23
|
- version: 8.9.0
|
|
21
24
|
date: 2026-05-04
|
|
22
25
|
changes: "更新 Solid useIntlayer API 用法以直接访问属性"
|
|
@@ -29,7 +32,7 @@ history:
|
|
|
29
32
|
author: aymericzip
|
|
30
33
|
---
|
|
31
34
|
|
|
32
|
-
# 翻译您的 Expo and React Native 应用 |
|
|
35
|
+
# 翻译您的 Expo and React Native 应用 | 国际化(i18n)
|
|
33
36
|
|
|
34
37
|
<Tabs defaultTab="code">
|
|
35
38
|
<Tab label="代码" value="code">
|
|
@@ -37,7 +40,7 @@ author: aymericzip
|
|
|
37
40
|
<iframe
|
|
38
41
|
src="https://ide.intlayer.org/aymericzip/intlayer-react-native-template?file=intlayer.config.ts"
|
|
39
42
|
className="m-auto overflow-hidden rounded-lg border-0 max-md:size-full max-md:h-[700px] md:aspect-16/9 md:w-full"
|
|
40
|
-
title="Demo CodeSandbox - Intlayer"
|
|
43
|
+
title="Demo CodeSandbox - 如何使用 Intlayer 国际化您的应用"
|
|
41
44
|
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
|
|
42
45
|
loading="lazy"
|
|
43
46
|
/>
|
|
@@ -56,24 +59,66 @@ author: aymericzip
|
|
|
56
59
|
</Tab>
|
|
57
60
|
</Tabs>
|
|
58
61
|
|
|
59
|
-
|
|
62
|
+
## 目录
|
|
60
63
|
|
|
61
|
-
|
|
64
|
+
<TOC/>
|
|
62
65
|
|
|
63
|
-
|
|
64
|
-
用于配置、字典内容、类型生成和 CLI 命令的核心 i18n 工具包。
|
|
66
|
+
## 为什么选择 Intlayer 而非其他方案?
|
|
65
67
|
|
|
66
|
-
-
|
|
67
|
-
React 集成提供了上下文提供程序和 React 挂钩,您将在 React Native 中使用它们来获取和切换语言环境。
|
|
68
|
+
与 `react-native-localize` 或 `i18next` 等主流方案相比,Intlayer 是一个集成了以下优化功能的解决方案:
|
|
68
69
|
|
|
69
|
-
|
|
70
|
-
React Native 集成提供了 Metro 插件,用于将 Intlayer 与 React Native 捆绑器集成。
|
|
70
|
+
<AccordionGroup>
|
|
71
71
|
|
|
72
|
-
|
|
72
|
+
<Accordion header="完整的 React Native 覆盖">
|
|
73
|
+
|
|
74
|
+
Intlayer 经过优化,可与 React Native 和 Expo 完美配合,提供**组件级内容作用域**、**TypeScript 支持**以及在移动应用中扩展国际化 (i18n) 所需的所有功能。
|
|
75
|
+
|
|
76
|
+
</Accordion>
|
|
77
|
+
|
|
78
|
+
<Accordion header="可维护性">
|
|
79
|
+
|
|
80
|
+
将应用内容限定作用域**有助于维护**大规模应用程序。您可以复制或删除单个功能文件夹,而无需承受审查整个内容代码库的心理负担。此外,Intlayer **具有完整的类型支持**,确保内容的准确性。
|
|
81
|
+
|
|
82
|
+
</Accordion>
|
|
83
|
+
|
|
84
|
+
<Accordion header="AI 代理">
|
|
85
|
+
|
|
86
|
+
内容共置**减少了大型语言模型 (LLM) 所需的上下文**。Intlayer 还附带了一套工具,例如用于检测缺失翻译的 **CLI**、**[LSP](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/lsp.md)**、**[MCP](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/mcp_server.md)** 和 **[agent skills](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/agent_skills.md)**,使 AI 代理的开发者体验 (DX) 更加顺畅。
|
|
87
|
+
|
|
88
|
+
</Accordion>
|
|
89
|
+
|
|
90
|
+
<Accordion header="自动化">
|
|
91
|
+
|
|
92
|
+
使用您选择的 LLM,以 AI 提供商的成本在 CI/CD 管道中自动进行翻译。Intlayer 还提供**编译器**来自动提取内容,以及[网络平台](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/intlayer_CMS.md)来帮助**在后台进行翻译**。
|
|
93
|
+
|
|
94
|
+
</Accordion>
|
|
95
|
+
|
|
96
|
+
<Accordion header="性能">
|
|
97
|
+
|
|
98
|
+
将大型 JSON 文件连接到组件可能会导致性能和响应性问题。Intlayer 在构建时优化内容加载。
|
|
99
|
+
|
|
100
|
+
</Accordion>
|
|
101
|
+
|
|
102
|
+
<Accordion header="非开发者扩展">
|
|
103
|
+
|
|
104
|
+
Intlayer 不仅仅是一个 i18n 解决方案,它还提供**自托管的[可视化编辑器](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/intlayer_visual_editor.md)**和**[完整 CMS](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/intlayer_CMS.md)**,帮助您**实时**管理多语言内容,使翻译人员、文案人员和其他团队成员的协作更加无缝。内容可以本地和/或远程存储。
|
|
105
|
+
|
|
106
|
+
</Accordion>
|
|
107
|
+
|
|
108
|
+
<Accordion header="包体积">
|
|
73
109
|
|
|
74
|
-
|
|
110
|
+
无需将大型 JSON 文件加载到页面中,只需加载必要的内容。Intlayer 帮助**将包体积和视图大小减少多达 50%**。
|
|
75
111
|
|
|
76
|
-
|
|
112
|
+
</Accordion>
|
|
113
|
+
</AccordionGroup>
|
|
114
|
+
|
|
115
|
+
<Steps>
|
|
116
|
+
|
|
117
|
+
<Step number={1} title="安装依赖">
|
|
118
|
+
|
|
119
|
+
请参阅 GitHub 上的[应用模板](https://github.com/aymericzip/intlayer-react-native-template)。
|
|
120
|
+
|
|
121
|
+
在您的 React Native 项目中,安装以下包:
|
|
77
122
|
|
|
78
123
|
```bash packageManager="npm"
|
|
79
124
|
npx intlayer-cli init --interactive
|
|
@@ -96,55 +141,101 @@ bunx intlayer-cli init --interactive
|
|
|
96
141
|
> 该命令将检测您的环境并安装所需的软件包。例如:
|
|
97
142
|
|
|
98
143
|
```bash packageManager="npm"
|
|
99
|
-
npm install intlayer react-intlayer
|
|
100
|
-
npm install --save-dev react-native-intlayer
|
|
144
|
+
npm install intlayer react-native-intlayer
|
|
101
145
|
```
|
|
102
146
|
|
|
103
147
|
```bash packageManager="pnpm"
|
|
104
|
-
pnpm add intlayer react-intlayer
|
|
105
|
-
pnpm add --save-dev react-native-intlayer
|
|
148
|
+
pnpm add intlayer react-native-intlayer
|
|
106
149
|
```
|
|
107
150
|
|
|
108
151
|
```bash packageManager="yarn"
|
|
109
|
-
yarn add intlayer react-intlayer
|
|
110
|
-
yarn add --save-dev react-native-intlayer
|
|
152
|
+
yarn add intlayer react-native-intlayer
|
|
111
153
|
```
|
|
112
154
|
|
|
113
155
|
```bash packageManager="bun"
|
|
114
|
-
bun add intlayer react-intlayer
|
|
115
|
-
bun add --dev react-native-intlayer
|
|
156
|
+
bun add intlayer react-native-intlayer
|
|
116
157
|
```
|
|
117
158
|
|
|
118
|
-
|
|
159
|
+
### 套餐
|
|
119
160
|
|
|
120
|
-
|
|
161
|
+
- **intlayer**
|
|
162
|
+
用于配置、字典内容、类型生成和 CLI 命令的核心 i18n 工具包。
|
|
121
163
|
|
|
122
|
-
|
|
164
|
+
- **react-native-intlayer**
|
|
165
|
+
React Native 集成,提供您将用于获取和切换语言环境的上下文 provider 和 React hook、React Native polyfill,以及将 Intlayer 与 React Native 打包器集成的 Metro 插件。它重新导出 `react-intlayer` 的所有内容,因此在 React Native 应用中只需要这一个包。
|
|
123
166
|
|
|
124
|
-
|
|
125
|
-
import { Stack } from "expo-router";
|
|
126
|
-
import { getLocales } from "expo-localization";
|
|
127
|
-
import { IntlayerProvider } from "react-native-intlayer";
|
|
128
|
-
import { type FC } from "react";
|
|
167
|
+
---
|
|
129
168
|
|
|
169
|
+
</Step>
|
|
130
170
|
|
|
131
|
-
|
|
171
|
+
<Step number={2} title="创建 Intlayer 配置">
|
|
172
|
+
|
|
173
|
+
在您的项目根目录(或任何方便的位置)创建一个 **Intlayer 配置**文件。它可能如下所示:
|
|
174
|
+
|
|
175
|
+
```ts fileName="intlayer.config.ts" codeFormat={["typescript", "esm", "commonjs"]}
|
|
176
|
+
/**
|
|
177
|
+
* 如果 Locales 类型不可用,请尝试在 tsconfig.json 中将 moduleResolution 设置为 "bundler"
|
|
178
|
+
*/
|
|
179
|
+
import { Locales, type IntlayerConfig } from "intlayer";
|
|
180
|
+
|
|
181
|
+
const config: IntlayerConfig = {
|
|
182
|
+
internationalization: {
|
|
183
|
+
locales: [
|
|
184
|
+
Locales.ENGLISH,
|
|
185
|
+
Locales.FRENCH,
|
|
186
|
+
Locales.SPANISH,
|
|
187
|
+
// ... 添加您需要的其他语言环境
|
|
188
|
+
],
|
|
189
|
+
defaultLocale: Locales.ENGLISH,
|
|
190
|
+
},
|
|
191
|
+
};
|
|
132
192
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
193
|
+
export default config;
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
在此配置中,您可以:
|
|
197
|
+
|
|
198
|
+
- 配置**支持的语言环境列表**。
|
|
199
|
+
- 设置**默认**语言环境。
|
|
200
|
+
- 以后,您可以添加更多高级选项(例如日志、自定义内容目录等)。
|
|
201
|
+
- 请参阅 [Intlayer 配置文档](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/configuration.md) 了解更多信息。
|
|
202
|
+
|
|
203
|
+
</Step>
|
|
204
|
+
|
|
205
|
+
<Step number={3} title="添加 Metro 插件">
|
|
206
|
+
|
|
207
|
+
Metro 是 React Native 的打包器。它是使用 `react-native init` 命令创建的 React Native 项目的默认打包器。要在 Metro 中使用 Intlayer,您需要将插件添加到 `metro.config.js` 文件中:
|
|
208
|
+
|
|
209
|
+
```js fileName="metro.config.js"
|
|
210
|
+
const { getDefaultConfig } = require("expo/metro-config");
|
|
211
|
+
const { configMetroIntlayer } = require("react-native-intlayer/metro");
|
|
212
|
+
|
|
213
|
+
module.exports = (async () => {
|
|
214
|
+
const defaultConfig = getDefaultConfig(__dirname);
|
|
215
|
+
|
|
216
|
+
return await configMetroIntlayer(defaultConfig);
|
|
217
|
+
})();
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
> 注意:`configMetroIntlayer` 是一个 Promise 函数。如果您想同步使用它,请改用 `configMetroIntlayerSync`,或避免使用 IFFE(立即调用函数表达式)。
|
|
221
|
+
> 注意:`configMetroIntlayerSync` 不允许在服务器启动时构建 intlayer 字典
|
|
222
|
+
|
|
223
|
+
</Step>
|
|
224
|
+
|
|
225
|
+
<Step number={4} title="添加 Intlayer provider">
|
|
226
|
+
|
|
227
|
+
为了在您的应用程序中保持用户语言的同步,您需要使用来自 `react-native-intlayer` 的 `IntlayerProvider` 组件包裹您的根组件。
|
|
228
|
+
|
|
229
|
+
> 始终从 `react-native-intlayer` 导入。其 `IntlayerProvider` 包含 Intlayer 使用的 web API 的 polyfill,该包重新导出了 `react-intlayer` 中的所有 hook 和工具。
|
|
138
230
|
|
|
139
231
|
此外,您需要在 `index.js` 文件中添加 `intlayerPolyfill` 函数,以确保 Intlayer 能够正常工作。
|
|
140
232
|
|
|
141
|
-
```tsx fileName="app/_layout.tsx" codeFormat="typescript"
|
|
233
|
+
```tsx fileName="app/_layout.tsx" codeFormat={["typescript", "esm"]}
|
|
142
234
|
import { Stack } from "expo-router";
|
|
143
235
|
import { getLocales } from "expo-localization";
|
|
144
236
|
import { IntlayerProvider } from "react-native-intlayer";
|
|
145
237
|
import { type FC } from "react";
|
|
146
238
|
|
|
147
|
-
|
|
148
239
|
const getDeviceLocale = () => getLocales()[0]?.languageTag;
|
|
149
240
|
|
|
150
241
|
const RootLayout: FC = () => {
|
|
@@ -158,9 +249,24 @@ const RootLayout: FC = () => {
|
|
|
158
249
|
};
|
|
159
250
|
|
|
160
251
|
export default RootLayout;
|
|
161
|
-
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
</Step>
|
|
255
|
+
|
|
256
|
+
<Step number={5} title="声明您的内容">
|
|
162
257
|
|
|
163
|
-
|
|
258
|
+
在项目中任意位置(通常在 `src/` 目录中)创建**内容声明**文件,使用 Intlayer 支持的任何扩展格式:
|
|
259
|
+
|
|
260
|
+
- `.content.json`
|
|
261
|
+
- `.content.ts`
|
|
262
|
+
- `.content.tsx`
|
|
263
|
+
- `.content.js`
|
|
264
|
+
- `.content.jsx`
|
|
265
|
+
- `.content.mjs`
|
|
266
|
+
- `.content.mjx`
|
|
267
|
+
- `.content.cjs`
|
|
268
|
+
- `.content.cjx`
|
|
269
|
+
- 等等
|
|
164
270
|
|
|
165
271
|
> **Expo Router (web): 请将 `.content.*` 文件保留在 `app/` 目录之外。** Expo Router 会将 `app/` 内的每个 JavaScript/TypeScript 文件视为一个路由。在 web 上,它的路由发现会直接扫描文件系统,并且 **不** 遵循 Metro 的 `resolver.blockList`,因此同位置的 `*.content.ts` 会被注册为一个路由。像 `app/(tabs)/_layout.content.ts` 这样的文件甚至会被解析为布局(`.content` 部分被读取为平台后缀),这会与实际的 `_layout.tsx` 发生冲突并抛出错误:
|
|
166
272
|
>
|
|
@@ -170,35 +276,63 @@ export default RootLayout;
|
|
|
170
276
|
>
|
|
171
277
|
> 请将您的声明放置在 `app/` 之外的目录中(例如 `content/` 或 `src/content/`)。Intlayer 会发现项目中任意位置的 `.content.*` 文件,字典通过它们的 `key` 进行引用,因此不需要进行任何导入更改。在原生端这不是必需的(Metro 的 `blockList` 已经隐藏了它们),但使用非 `app/` 目录可确保这两个平台都能正常工作。
|
|
172
278
|
|
|
173
|
-
|
|
279
|
+
示例(带有 React Native TSX 节点的 TypeScript):
|
|
280
|
+
|
|
281
|
+
```tsx fileName="src/app.content.tsx" contentDeclarationFormat={["typescript", "esm", "commonjs"]}
|
|
282
|
+
import { t, type Dictionary } from "intlayer";
|
|
283
|
+
import type { ReactNode } from "react";
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* 我们"app"域的内容字典
|
|
287
|
+
*/
|
|
288
|
+
import { t, type Dictionary } from "intlayer";
|
|
289
|
+
|
|
290
|
+
const homeScreenContent = {
|
|
291
|
+
key: "home-screen",
|
|
292
|
+
content: {
|
|
293
|
+
title: t({
|
|
294
|
+
en: "Welcome!",
|
|
295
|
+
fr: "Bienvenue!",
|
|
296
|
+
es: "¡Bienvenido!",
|
|
297
|
+
}),
|
|
298
|
+
},
|
|
299
|
+
} satisfies Dictionary;
|
|
174
300
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
const { getLocales } = require("expo-localization");
|
|
178
|
-
const { IntlayerProvider } = require("react-native-intlayer");
|
|
301
|
+
export default homeScreenContent;
|
|
302
|
+
```
|
|
179
303
|
|
|
304
|
+
```json fileName="src/app.content.json" contentDeclarationFormat="json"
|
|
305
|
+
{
|
|
306
|
+
"$schema": "https://intlayer.org/schema.json",
|
|
307
|
+
"key": "app",
|
|
308
|
+
"content": {
|
|
309
|
+
"title": {
|
|
310
|
+
"nodeType": "translation",
|
|
311
|
+
"translation": {
|
|
312
|
+
"en": "Welcome!",
|
|
313
|
+
"fr": "Bienvenue!",
|
|
314
|
+
"es": "¡Bienvenido!"
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
```
|
|
180
320
|
|
|
181
|
-
|
|
182
|
-
const getDeviceLocale = () => getLocales()[0]?.languageTag;
|
|
321
|
+
> 有关内容声明的详细信息,请参阅 [Intlayer 内容文档](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/dictionary/content_file.md)。
|
|
183
322
|
|
|
184
|
-
|
|
185
|
-
return (
|
|
186
|
-
<IntlayerProvider defaultLocale={getDeviceLocale()}>
|
|
187
|
-
<Stack>
|
|
188
|
-
<Stack.Screen name="(tabs)" options={{ headerShown: false }} />
|
|
189
|
-
</Stack>
|
|
190
|
-
</IntlayerProvider>
|
|
191
|
-
);
|
|
192
|
-
};
|
|
323
|
+
---
|
|
193
324
|
|
|
194
|
-
|
|
195
|
-
|
|
325
|
+
</Step>
|
|
326
|
+
|
|
327
|
+
<Step number={6} title="在组件中使用 Intlayer">
|
|
328
|
+
|
|
329
|
+
在子组件中使用 `useIntlayer` hook 获取本地化内容。
|
|
196
330
|
|
|
197
331
|
### 示例
|
|
198
332
|
|
|
199
333
|
```tsx fileName="app/(tabs)/index.tsx" codeFormat={["typescript", "esm"]}
|
|
200
334
|
import { Image, StyleSheet, Platform } from "react-native";
|
|
201
|
-
import { useIntlayer } from "intlayer";
|
|
335
|
+
import { useIntlayer } from "react-native-intlayer";
|
|
202
336
|
import { HelloWave } from "@/components/HelloWave";
|
|
203
337
|
import ParallaxScrollView from "@/components/ParallaxScrollView";
|
|
204
338
|
import { ThemedText } from "@/components/ThemedText";
|
|
@@ -228,9 +362,9 @@ const HomeScreen = (): FC => {
|
|
|
228
362
|
|
|
229
363
|
const styles = StyleSheet.create({
|
|
230
364
|
titleContainer: {
|
|
231
|
-
flexDirection: "row",
|
|
232
|
-
alignItems: "center",
|
|
233
|
-
gap: 8,
|
|
365
|
+
flexDirection: "row",
|
|
366
|
+
alignItems: "center",
|
|
367
|
+
gap: 8,
|
|
234
368
|
},
|
|
235
369
|
});
|
|
236
370
|
|
|
@@ -239,21 +373,21 @@ export default HomeScreen;
|
|
|
239
373
|
|
|
240
374
|
> 当在基于字符串的属性中使用 `content.someKey`(例如按钮的 `title` 或 `Text` 组件的 `children`)时,**请调用 `content.someKey.value`** 来获取实际的字符串。
|
|
241
375
|
|
|
242
|
-
> 如果您的应用程序已经存在,您可以结合使用 [Intlayer 编译器](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/compiler.md) 和 [提取命令](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/cli/extract.md)
|
|
376
|
+
> 如果您的应用程序已经存在,您可以结合使用 [Intlayer 编译器](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/compiler.md) 和 [提取命令](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/cli/extract.md) 在一秒钟内转换数千个组件。
|
|
243
377
|
|
|
244
378
|
---
|
|
245
379
|
|
|
246
|
-
|
|
380
|
+
</Step>
|
|
247
381
|
|
|
248
|
-
<Step number={
|
|
382
|
+
<Step number={7} title="更改应用程序语言环境" isOptional={true}>
|
|
249
383
|
|
|
250
|
-
要在组件内部切换语言环境,可以使用 `useLocale`
|
|
384
|
+
要在组件内部切换语言环境,可以使用 `useLocale` hook 的 `setLocale` 方法:
|
|
251
385
|
|
|
252
386
|
```tsx fileName="src/components/LocaleSwitcher.tsx" codeFormat={["typescript", "esm"]}
|
|
253
387
|
import { type FC } from "react";
|
|
254
388
|
import { View, Text, TouchableOpacity, StyleSheet } from "react-native";
|
|
255
389
|
import { getLocaleName } from "intlayer";
|
|
256
|
-
import { useLocale } from "react-intlayer";
|
|
390
|
+
import { useLocale } from "react-native-intlayer";
|
|
257
391
|
|
|
258
392
|
export const LocaleSwitcher: FC = () => {
|
|
259
393
|
const { setLocale, availableLocales } = useLocale();
|
|
@@ -275,28 +409,28 @@ export const LocaleSwitcher: FC = () => {
|
|
|
275
409
|
|
|
276
410
|
const styles = StyleSheet.create({
|
|
277
411
|
container: {
|
|
278
|
-
flexDirection: "row",
|
|
279
|
-
justifyContent: "center",
|
|
280
|
-
alignItems: "center",
|
|
281
|
-
gap: 8,
|
|
412
|
+
flexDirection: "row",
|
|
413
|
+
justifyContent: "center",
|
|
414
|
+
alignItems: "center",
|
|
415
|
+
gap: 8,
|
|
282
416
|
},
|
|
283
417
|
button: {
|
|
284
|
-
paddingVertical: 6,
|
|
285
|
-
paddingHorizontal: 12,
|
|
286
|
-
borderRadius: 6,
|
|
287
|
-
backgroundColor: "#ddd",
|
|
418
|
+
paddingVertical: 6,
|
|
419
|
+
paddingHorizontal: 12,
|
|
420
|
+
borderRadius: 6,
|
|
421
|
+
backgroundColor: "#ddd",
|
|
288
422
|
},
|
|
289
423
|
text: {
|
|
290
|
-
fontSize: 14,
|
|
291
|
-
fontWeight: "500",
|
|
292
|
-
color: "#333",
|
|
424
|
+
fontSize: 14,
|
|
425
|
+
fontWeight: "500",
|
|
426
|
+
color: "#333",
|
|
293
427
|
},
|
|
294
428
|
});
|
|
295
429
|
```
|
|
296
430
|
|
|
297
431
|
这会触发所有使用 Intlayer 内容的组件重新渲染,现在显示新语言环境的翻译内容。
|
|
298
432
|
|
|
299
|
-
> 详情请参见 [`useLocale` 文档](https://github.com/aymericzip/intlayer/blob/main/docs/docs/
|
|
433
|
+
> 详情请参见 [`useLocale` 文档](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/packages/react-intlayer/useLocale.md)。
|
|
300
434
|
|
|
301
435
|
</Step>
|
|
302
436
|
|
|
@@ -367,13 +501,13 @@ Intlayer 会在一个隐藏文件夹中(默认是 `.intlayer`)生成类型
|
|
|
367
501
|
|
|
368
502
|
React Native 的稳定性不如 React Web,因此要特别注意版本对齐。
|
|
369
503
|
|
|
370
|
-
Intlayer 主要针对 Web Intl API;在 React Native 上,你必须包含适当的
|
|
504
|
+
Intlayer 主要针对 Web Intl API;在 React Native 上,你必须包含适当的 polyfill。
|
|
371
505
|
|
|
372
506
|
检查清单:
|
|
373
507
|
|
|
374
|
-
- 使用最新版本的 `intlayer
|
|
508
|
+
- 使用最新版本的 `intlayer` 和 `react-native-intlayer`。
|
|
375
509
|
- 启用 Intlayer polyfill。
|
|
376
|
-
- 如果你使用 `getLocaleName` 或其他基于 Intl-API 的工具,请提前导入这些
|
|
510
|
+
- 如果你使用 `getLocaleName` 或其他基于 Intl-API 的工具,请提前导入这些 polyfill(例如在 `index.js` 或 `App.tsx` 中):
|
|
377
511
|
|
|
378
512
|
```ts
|
|
379
513
|
import "intl";
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
---
|
|
2
2
|
createdAt: 2026-01-21
|
|
3
|
-
updatedAt: 2026-
|
|
3
|
+
updatedAt: 2026-06-25
|
|
4
4
|
title: react-native-intlayer 包文档
|
|
5
|
-
description: 为 Intlayer 提供的 React Native 支持,包含
|
|
5
|
+
description: 为 Intlayer 提供的 React Native 支持,包含 provider、hook、polyfill 和 Metro 配置。
|
|
6
6
|
keywords:
|
|
7
7
|
- react-native-intlayer
|
|
8
8
|
- react-native
|
|
@@ -14,15 +14,20 @@ slugs:
|
|
|
14
14
|
- react-native-intlayer
|
|
15
15
|
- exports
|
|
16
16
|
history:
|
|
17
|
+
- version: 9.0.0
|
|
18
|
+
date: 2026-06-25
|
|
19
|
+
changes: "重新导出完整的 react-intlayer API(hook、工具、format/html/markdown 子路径),使 React Native 应用只需依赖 react-native-intlayer"
|
|
17
20
|
- version: 8.0.0
|
|
18
21
|
date: 2026-01-21
|
|
19
|
-
changes: "
|
|
22
|
+
changes: "统一所有导出的文档"
|
|
20
23
|
author: aymericzip
|
|
21
24
|
---
|
|
22
25
|
|
|
23
26
|
# react-native-intlayer 包
|
|
24
27
|
|
|
25
|
-
`react-native-intlayer` 包提供将 Intlayer 集成到 React Native
|
|
28
|
+
`react-native-intlayer` 包提供将 Intlayer 集成到 React Native 应用中所需的工具。它重新导出完整的 `react-intlayer` API(hook 和工具),并提供适用于 React Native 的 `IntlayerProvider`,以及 React Native 所需的 polyfill 和 Metro 配置。
|
|
29
|
+
|
|
30
|
+
> 在 React Native 应用中,请从 `react-native-intlayer` 导入**所有内容**。您无需直接安装或导入 `react-intlayer`。
|
|
26
31
|
|
|
27
32
|
## 安装
|
|
28
33
|
|
|
@@ -34,14 +39,30 @@ npm install react-native-intlayer
|
|
|
34
39
|
|
|
35
40
|
### Provider 组件
|
|
36
41
|
|
|
37
|
-
| 组件 | 说明
|
|
38
|
-
| ------------------ |
|
|
39
|
-
| `IntlayerProvider` | 包裹应用并提供 Intlayer 上下文的 Provider
|
|
42
|
+
| 组件 | 说明 |
|
|
43
|
+
| ------------------ | ------------------------------------------------------------------------- |
|
|
44
|
+
| `IntlayerProvider` | 包裹应用并提供 Intlayer 上下文的 Provider 组件。自动应用所需的 polyfill。 |
|
|
45
|
+
|
|
46
|
+
```tsx
|
|
47
|
+
import { IntlayerProvider } from "react-native-intlayer";
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Hook 和工具
|
|
51
|
+
|
|
52
|
+
这些内容从 `react-intlayer` 重新导出,因此您可以直接从 `react-native-intlayer` 导入:
|
|
40
53
|
|
|
41
|
-
|
|
54
|
+
| 导出 | 说明 |
|
|
55
|
+
| ----------------------------------------------------------------------------------------------------------------- | ------------------------ |
|
|
56
|
+
| `useIntlayer` | 访问字典键的本地化内容。 |
|
|
57
|
+
| `useLocale` | 读取和更改当前语言环境。 |
|
|
58
|
+
| `useDictionary`, `useDictionaryAsync`, `useDictionaryDynamic`, `useLoadDynamic` | 以多种方式加载字典内容。 |
|
|
59
|
+
| `useI18n` | 兼容 i18next 的 hook。 |
|
|
60
|
+
| `t` | 内联翻译助手。 |
|
|
61
|
+
| `getIntlayer`, `getDictionary` | 命令式内容获取器。 |
|
|
62
|
+
| `localeCookie`, `localeInStorage`, `setLocaleCookie`, `setLocaleInStorage`, `useLocaleCookie`, `useLocaleStorage` | 语言环境持久化助手。 |
|
|
42
63
|
|
|
43
64
|
```tsx
|
|
44
|
-
import "react-native-intlayer";
|
|
65
|
+
import { useIntlayer, useLocale, t } from "react-native-intlayer";
|
|
45
66
|
```
|
|
46
67
|
|
|
47
68
|
### Polyfill
|
|
@@ -50,10 +71,25 @@ import "react-native-intlayer";
|
|
|
50
71
|
| ------------------ | ---------------------------------------------------------- |
|
|
51
72
|
| `intlayerPolyfill` | 为 React Native 应用启用 Intlayer 所需的 polyfill 的函数。 |
|
|
52
73
|
|
|
53
|
-
|
|
74
|
+
```tsx
|
|
75
|
+
import { intlayerPolyfill } from "react-native-intlayer";
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
> 当您导入 `IntlayerProvider` 时,polyfill 会自动应用。仅当您需要在 provider 挂载之前使用 polyfill 时,才需要手动调用 `intlayerPolyfill`。
|
|
79
|
+
|
|
80
|
+
### 格式化工具
|
|
81
|
+
|
|
82
|
+
数字、日期及其他基于 Intl 的格式化 hook 可从 `/format` 子路径获取:
|
|
83
|
+
|
|
84
|
+
```tsx
|
|
85
|
+
import { useNumber, useDate } from "react-native-intlayer/format";
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Markdown 和 HTML 渲染
|
|
54
89
|
|
|
55
90
|
```tsx
|
|
56
|
-
import "react-native-intlayer";
|
|
91
|
+
import { MarkdownProvider } from "react-native-intlayer/markdown";
|
|
92
|
+
import { HTMLProvider } from "react-native-intlayer/html";
|
|
57
93
|
```
|
|
58
94
|
|
|
59
95
|
### Metro 配置
|
|
@@ -66,8 +102,6 @@ import "react-native-intlayer";
|
|
|
66
102
|
| `configMetroIntlayerSync` | 同步函数,合并 Metro 配置但不准备 Intlayer 资源。 |
|
|
67
103
|
| `exclusionList` | 为 Metro 的 blockList 创建一个 RegExp 模式,以在打包时排除内容文件。 |
|
|
68
104
|
|
|
69
|
-
导入:
|
|
70
|
-
|
|
71
105
|
```tsx
|
|
72
|
-
import "react-native-intlayer/metro";
|
|
106
|
+
import { configMetroIntlayer } from "react-native-intlayer/metro";
|
|
73
107
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intlayer/docs",
|
|
3
|
-
"version": "9.0.0-canary.
|
|
3
|
+
"version": "9.0.0-canary.6",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Intlayer documentation",
|
|
6
6
|
"keywords": [
|
|
@@ -73,13 +73,13 @@
|
|
|
73
73
|
"watch": "webpack --config ./webpack.config.ts --watch"
|
|
74
74
|
},
|
|
75
75
|
"dependencies": {
|
|
76
|
-
"@intlayer/config": "9.0.0-canary.
|
|
77
|
-
"@intlayer/core": "9.0.0-canary.
|
|
78
|
-
"@intlayer/types": "9.0.0-canary.
|
|
76
|
+
"@intlayer/config": "9.0.0-canary.6",
|
|
77
|
+
"@intlayer/core": "9.0.0-canary.6",
|
|
78
|
+
"@intlayer/types": "9.0.0-canary.6"
|
|
79
79
|
},
|
|
80
80
|
"devDependencies": {
|
|
81
|
-
"@intlayer/api": "9.0.0-canary.
|
|
82
|
-
"@intlayer/cli": "9.0.0-canary.
|
|
81
|
+
"@intlayer/api": "9.0.0-canary.6",
|
|
82
|
+
"@intlayer/cli": "9.0.0-canary.6",
|
|
83
83
|
"@types/node": "25.9.4",
|
|
84
84
|
"@utils/ts-config": "1.0.4",
|
|
85
85
|
"@utils/ts-config-types": "1.0.4",
|