@intlayer/docs 7.3.11 → 7.3.12
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/dist/cjs/generated/docs.entry.cjs +19 -0
- package/dist/cjs/generated/docs.entry.cjs.map +1 -1
- package/dist/esm/generated/docs.entry.mjs +19 -0
- package/dist/esm/generated/docs.entry.mjs.map +1 -1
- package/dist/types/generated/docs.entry.d.ts +1 -0
- package/dist/types/generated/docs.entry.d.ts.map +1 -1
- package/docs/ar/intlayer_with_nuxt.md +305 -421
- package/docs/ar/intlayer_with_react_router_v7.md +33 -4
- package/docs/ar/intlayer_with_react_router_v7_fs_routes.md +516 -0
- package/docs/ar/intlayer_with_tanstack.md +2 -12
- package/docs/ar/intlayer_with_vite+vue.md +1 -0
- package/docs/de/intlayer_with_nuxt.md +296 -394
- package/docs/de/intlayer_with_react_router_v7.md +33 -4
- package/docs/de/intlayer_with_react_router_v7_fs_routes.md +573 -0
- package/docs/de/intlayer_with_tanstack.md +1 -0
- package/docs/de/intlayer_with_vite+vue.md +1 -0
- package/docs/en/intlayer_with_nuxt.md +242 -321
- package/docs/en/intlayer_with_react_router_v7.md +24 -0
- package/docs/en/intlayer_with_react_router_v7_fs_routes.md +570 -0
- package/docs/en/intlayer_with_tanstack.md +2 -12
- package/docs/en/intlayer_with_vite+vue.md +49 -48
- package/docs/en-GB/intlayer_with_nuxt.md +262 -358
- package/docs/en-GB/intlayer_with_react_router_v7.md +33 -4
- package/docs/en-GB/intlayer_with_react_router_v7_fs_routes.md +513 -0
- package/docs/en-GB/intlayer_with_tanstack.md +2 -12
- package/docs/en-GB/intlayer_with_vite+vue.md +1 -0
- package/docs/es/intlayer_with_nuxt.md +284 -375
- package/docs/es/intlayer_with_react_router_v7.md +33 -4
- package/docs/es/intlayer_with_react_router_v7_fs_routes.md +575 -0
- package/docs/es/intlayer_with_tanstack.md +1 -0
- package/docs/es/intlayer_with_vite+vue.md +1 -2
- package/docs/fr/intlayer_with_nuxt.md +288 -387
- package/docs/fr/intlayer_with_react_router_v7.md +34 -5
- package/docs/fr/intlayer_with_react_router_v7_fs_routes.md +574 -0
- package/docs/fr/intlayer_with_tanstack.md +1 -0
- package/docs/fr/intlayer_with_vite+vue.md +1 -0
- package/docs/hi/intlayer_with_nuxt.md +318 -434
- package/docs/hi/intlayer_with_react_router_v7.md +33 -4
- package/docs/hi/intlayer_with_react_router_v7_fs_routes.md +518 -0
- package/docs/hi/intlayer_with_tanstack.md +2 -12
- package/docs/hi/intlayer_with_vite+vue.md +1 -0
- package/docs/id/intlayer_with_nuxt.md +275 -376
- package/docs/id/intlayer_with_react_router_v7.md +29 -4
- package/docs/id/intlayer_with_react_router_v7_fs_routes.md +521 -0
- package/docs/id/intlayer_with_tanstack.md +2 -12
- package/docs/id/intlayer_with_vite+vue.md +1 -0
- package/docs/it/intlayer_with_nuxt.md +312 -408
- package/docs/it/intlayer_with_react_router_v7.md +33 -4
- package/docs/it/intlayer_with_react_router_v7_fs_routes.md +574 -0
- package/docs/it/intlayer_with_tanstack.md +1 -0
- package/docs/ja/intlayer_with_nuxt.md +319 -414
- package/docs/ja/intlayer_with_react_router_v7.md +33 -4
- package/docs/ja/intlayer_with_react_router_v7_fs_routes.md +574 -0
- package/docs/ja/intlayer_with_tanstack.md +2 -12
- package/docs/ja/intlayer_with_vite+vue.md +1 -0
- package/docs/ko/intlayer_with_nuxt.md +307 -406
- package/docs/ko/intlayer_with_react_router_v7.md +33 -4
- package/docs/ko/intlayer_with_react_router_v7_fs_routes.md +515 -0
- package/docs/ko/intlayer_with_tanstack.md +2 -12
- package/docs/ko/intlayer_with_vite+vue.md +1 -0
- package/docs/pl/intlayer_with_nuxt.md +282 -457
- package/docs/pl/intlayer_with_react_router_v7.md +32 -5
- package/docs/pl/intlayer_with_react_router_v7_fs_routes.md +615 -0
- package/docs/pl/intlayer_with_tanstack.md +2 -12
- package/docs/pl/intlayer_with_vite+vue.md +1 -0
- package/docs/pt/intlayer_with_nuxt.md +288 -403
- package/docs/pt/intlayer_with_react_router_v7.md +28 -0
- package/docs/pt/intlayer_with_tanstack.md +1 -0
- package/docs/ru/intlayer_with_nuxt.md +300 -410
- package/docs/ru/intlayer_with_react_router_v7.md +33 -4
- package/docs/ru/intlayer_with_react_router_v7_fs_routes.md +574 -0
- package/docs/ru/intlayer_with_tanstack.md +1 -0
- package/docs/ru/intlayer_with_vite+vue.md +1 -0
- package/docs/tr/intlayer_with_nuxt.md +327 -392
- package/docs/tr/intlayer_with_react_router_v7.md +33 -4
- package/docs/tr/intlayer_with_react_router_v7_fs_routes.md +572 -0
- package/docs/tr/intlayer_with_tanstack.md +2 -12
- package/docs/tr/intlayer_with_vite+vue.md +1 -0
- package/docs/vi/intlayer_with_nuxt.md +282 -399
- package/docs/vi/intlayer_with_react_router_v7.md +29 -4
- package/docs/vi/intlayer_with_react_router_v7_fs_routes.md +523 -0
- package/docs/vi/intlayer_with_tanstack.md +2 -12
- package/docs/vi/intlayer_with_vite+vue.md +1 -0
- package/docs/zh/intlayer_with_nuxt.md +311 -444
- package/docs/zh/intlayer_with_react_router_v7.md +33 -4
- package/docs/zh/intlayer_with_react_router_v7_fs_routes.md +516 -0
- package/docs/zh/intlayer_with_tanstack.md +2 -12
- package/docs/zh/intlayer_with_vite+vue.md +1 -0
- package/package.json +6 -6
- package/src/generated/docs.entry.ts +19 -0
|
@@ -18,6 +18,7 @@ slugs:
|
|
|
18
18
|
- vite-and-react
|
|
19
19
|
- react-router-v7
|
|
20
20
|
applicationTemplate: https://github.com/aymericzip/intlayer-react-router-v7-template
|
|
21
|
+
youtubeVideo: https://www.youtube.com/watch?v=dS9L7uJeak4
|
|
21
22
|
history:
|
|
22
23
|
- version: 6.1.5
|
|
23
24
|
date: 2025-10-03
|
|
@@ -31,6 +32,10 @@ history:
|
|
|
31
32
|
|
|
32
33
|
本指南演示了如何在 React Router v7 项目中集成 **Intlayer**,实现无缝国际化,支持基于区域的路由、TypeScript 支持以及现代开发实践。
|
|
33
34
|
|
|
35
|
+
## Table of Contents
|
|
36
|
+
|
|
37
|
+
<TOC/>
|
|
38
|
+
|
|
34
39
|
## 什么是 Intlayer?
|
|
35
40
|
|
|
36
41
|
**Intlayer** 是一个创新的开源国际化(i18n)库,旨在简化现代 Web 应用的多语言支持。
|
|
@@ -75,6 +80,29 @@ pnpm add vite-intlayer --save-dev
|
|
|
75
80
|
|
|
76
81
|
### 第 2 步:项目配置
|
|
77
82
|
|
|
83
|
+
## 在 React Router v7 应用程序中使用基于文件系统的路由设置 Intlayer 的分步指南
|
|
84
|
+
|
|
85
|
+
<Tab defaultTab="video">
|
|
86
|
+
<TabItem label="Video" value="video">
|
|
87
|
+
|
|
88
|
+
<iframe title="How to translate your React Router v7 app using Intlayer" class="m-auto aspect-[16/9] w-full overflow-hidden rounded-lg border-0" allow="autoplay; gyroscope;" loading="lazy" width="1080" height="auto" src="https://www.youtube.com/embed/dS9L7uJeak4?autoplay=0&origin=http://intlayer.org&controls=0&rel=1"/>
|
|
89
|
+
|
|
90
|
+
</TabItem>
|
|
91
|
+
<TabItem label="Code" value="code">
|
|
92
|
+
|
|
93
|
+
<iframe
|
|
94
|
+
src="https://stackblitz.com/github/aymericzip/intlayer-react-router-v7-template?embed=1&ctl=1&file=intlayer.config.ts"
|
|
95
|
+
className="m-auto overflow-hidden rounded-lg border-0 max-md:size-full max-md:h-[700px] md:aspect-16/9 md:w-full"
|
|
96
|
+
title="Demo CodeSandbox - How to Internationalize your application using Intlayer"
|
|
97
|
+
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
|
|
98
|
+
loading="lazy"
|
|
99
|
+
/>
|
|
100
|
+
|
|
101
|
+
</TabItem>
|
|
102
|
+
</Tab>
|
|
103
|
+
|
|
104
|
+
See [Application Template](https://github.com/aymericzip/intlayer-react-router-v7-template) on GitHub.
|
|
105
|
+
|
|
78
106
|
创建一个配置文件来配置您的应用程序语言:
|
|
79
107
|
|
|
80
108
|
```typescript fileName="intlayer.config.ts" codeFormat="typescript"
|
|
@@ -258,7 +286,7 @@ export const LocalizedLink: FC<LinkProps> = (props) => {
|
|
|
258
286
|
如果你想导航到本地化路由,可以使用 `useLocalizedNavigate` 钩子:
|
|
259
287
|
|
|
260
288
|
```tsx fileName="app/hooks/useLocalizedNavigate.ts"
|
|
261
|
-
import { useLocale } from "intlayer";
|
|
289
|
+
import { useLocale } from "react-intlayer";
|
|
262
290
|
import { type NavigateOptions, type To, useNavigate } from "react-router";
|
|
263
291
|
|
|
264
292
|
import { locacalizeTo } from "~/components/localized-link";
|
|
@@ -316,6 +344,7 @@ import {
|
|
|
316
344
|
getLocaleName,
|
|
317
345
|
getLocalizedUrl,
|
|
318
346
|
getPathWithoutLocale,
|
|
347
|
+
Locales,
|
|
319
348
|
} from "intlayer";
|
|
320
349
|
import { setLocaleInStorage, useIntlayer, useLocale } from "react-intlayer";
|
|
321
350
|
import { Link, useLocation } from "react-router";
|
|
@@ -409,13 +438,13 @@ export default function RootLayout() {
|
|
|
409
438
|
> 注意,要在生产环境中使用 `intlayerProxy`,你需要将 `vite-intlayer` 包从 `devDependencies` 切换到 `dependencies`。
|
|
410
439
|
|
|
411
440
|
```typescript {3,7} fileName="vite.config.ts"
|
|
441
|
+
import { reactRouter } from "@react-router/dev/vite";
|
|
412
442
|
import { defineConfig } from "vite";
|
|
413
|
-
import react from "@vitejs/plugin-react-swc";
|
|
414
443
|
import { intlayer, intlayerProxy } from "vite-intlayer";
|
|
444
|
+
import tsconfigPaths from "vite-tsconfig-paths";
|
|
415
445
|
|
|
416
|
-
// https://vitejs.dev/config/
|
|
417
446
|
export default defineConfig({
|
|
418
|
-
plugins: [
|
|
447
|
+
plugins: [reactRouter(), tsconfigPaths(), intlayer(), intlayerProxy()],
|
|
419
448
|
});
|
|
420
449
|
```
|
|
421
450
|
|
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
---
|
|
2
|
+
createdAt: 2025-09-04
|
|
3
|
+
updatedAt: 2025-10-03
|
|
4
|
+
title: 如何翻译您的React Router v7应用 – i18n指南 2025
|
|
5
|
+
description: 学习如何使用 Intlayer 为您的 React Router v7 应用添加国际化(i18n)。按照本综合指南,使您的应用支持多语言和基于区域的路由。
|
|
6
|
+
keywords:
|
|
7
|
+
- 国际化
|
|
8
|
+
- 文档
|
|
9
|
+
- Intlayer
|
|
10
|
+
- React Router v7
|
|
11
|
+
- React
|
|
12
|
+
- i18n
|
|
13
|
+
- TypeScript
|
|
14
|
+
- 区域路由
|
|
15
|
+
slugs:
|
|
16
|
+
- doc
|
|
17
|
+
- environment
|
|
18
|
+
- vite-and-react
|
|
19
|
+
- react-router-v7-fs-routes
|
|
20
|
+
applicationTemplate: https://github.com/aymericzip/intlayer-react-router-v7-template
|
|
21
|
+
youtubeVideo: https://www.youtube.com/watch?v=dS9L7uJeak4
|
|
22
|
+
history:
|
|
23
|
+
- version: 6.1.5
|
|
24
|
+
date: 2025-10-03
|
|
25
|
+
changes: 更新文档
|
|
26
|
+
- version: 5.8.2
|
|
27
|
+
date: 2025-09-04
|
|
28
|
+
changes: 添加 React Router v7 支持
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
# 使用Intlayer翻译您的React Router v7 | 国际化(i18n)
|
|
32
|
+
|
|
33
|
+
本指南演示了如何在 React Router v7 项目中集成 **Intlayer**,实现无缝国际化,支持基于区域的路由、TypeScript 支持以及现代开发实践。
|
|
34
|
+
|
|
35
|
+
## Table of Contents
|
|
36
|
+
|
|
37
|
+
<TOC/>
|
|
38
|
+
|
|
39
|
+
## 什么是 Intlayer?
|
|
40
|
+
|
|
41
|
+
**Intlayer** 是一个创新的开源国际化(i18n)库,旨在简化现代 Web 应用的多语言支持。
|
|
42
|
+
|
|
43
|
+
使用 Intlayer,您可以:
|
|
44
|
+
|
|
45
|
+
- **通过组件级声明式字典轻松管理翻译**。
|
|
46
|
+
- **动态本地化元数据、路由和内容**。
|
|
47
|
+
- **确保 TypeScript 支持**,通过自动生成类型,提升自动补全和错误检测能力。
|
|
48
|
+
- **享受高级功能**,如动态区域检测和切换。
|
|
49
|
+
- **通过 React Router v7 的基于配置的路由系统启用区域感知路由**。
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## 在 React Router v7 应用中设置 Intlayer 的分步指南
|
|
54
|
+
|
|
55
|
+
### 第一步:安装依赖
|
|
56
|
+
|
|
57
|
+
使用您喜欢的包管理器安装必要的包:
|
|
58
|
+
|
|
59
|
+
```bash packageManager="npm"
|
|
60
|
+
npm install intlayer react-intlayer
|
|
61
|
+
npm install vite-intlayer --save-dev
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
```bash packageManager="pnpm"
|
|
65
|
+
pnpm add intlayer react-intlayer
|
|
66
|
+
pnpm add vite-intlayer --save-dev
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
- **intlayer**
|
|
70
|
+
|
|
71
|
+
- **intlayer**
|
|
72
|
+
|
|
73
|
+
提供国际化工具的核心包,用于配置管理、翻译、[内容声明](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/dictionary/get_started.md)、转译以及[命令行工具](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/intlayer_cli.md)。
|
|
74
|
+
|
|
75
|
+
- **react-intlayer**
|
|
76
|
+
将 Intlayer 集成到 React 应用中的包。它提供了 React 国际化的上下文提供者和钩子。
|
|
77
|
+
|
|
78
|
+
- **vite-intlayer**
|
|
79
|
+
包含用于将 Intlayer 集成到 [Vite 打包工具](https://vite.dev/guide/why.html#why-bundle-for-production) 的 Vite 插件,以及用于检测用户首选语言、管理 Cookie 和处理 URL 重定向的中间件。
|
|
80
|
+
|
|
81
|
+
### 第 2 步:项目配置
|
|
82
|
+
|
|
83
|
+
## 在 React Router v7 应用程序中使用基于文件系统的路由设置 Intlayer 的分步指南
|
|
84
|
+
|
|
85
|
+
<Tab defaultTab="video">
|
|
86
|
+
<TabItem label="Video" value="video">
|
|
87
|
+
|
|
88
|
+
<iframe title="How to translate your React Router v7 (File-System Routes) app using Intlayer" class="m-auto aspect-[16/9] w-full overflow-hidden rounded-lg border-0" allow="autoplay; gyroscope;" loading="lazy" width="1080" height="auto" src="https://www.youtube.com/embed/dS9L7uJeak4?autoplay=0&origin=http://intlayer.org&controls=0&rel=1"/>
|
|
89
|
+
|
|
90
|
+
</TabItem>
|
|
91
|
+
<TabItem label="Code" value="code">
|
|
92
|
+
|
|
93
|
+
<iframe
|
|
94
|
+
src="https://stackblitz.com/github/aymericzip/intlayer-react-router-v7-template?embed=1&ctl=1&file=intlayer.config.ts"
|
|
95
|
+
className="m-auto overflow-hidden rounded-lg border-0 max-md:size-full max-md:h-[700px] md:aspect-16/9 md:w-full"
|
|
96
|
+
title="Demo CodeSandbox - How to Internationalize your application using Intlayer"
|
|
97
|
+
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
|
|
98
|
+
loading="lazy"
|
|
99
|
+
/>
|
|
100
|
+
|
|
101
|
+
</TabItem>
|
|
102
|
+
</Tab>
|
|
103
|
+
|
|
104
|
+
See [Application Template](https://github.com/aymericzip/intlayer-react-router-v7-template) on GitHub.
|
|
105
|
+
|
|
106
|
+
创建一个配置文件来配置您的应用程序语言:
|
|
107
|
+
|
|
108
|
+
```typescript fileName="intlayer.config.ts" codeFormat="typescript"
|
|
109
|
+
import { type IntlayerConfig, Locales } from "intlayer";
|
|
110
|
+
|
|
111
|
+
const config: IntlayerConfig = {
|
|
112
|
+
internationalization: {
|
|
113
|
+
defaultLocale: Locales.ENGLISH, // 默认语言
|
|
114
|
+
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH], // 支持的语言列表
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
export default config;
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
```javascript fileName="intlayer.config.mjs" codeFormat="esm"
|
|
122
|
+
import { Locales } from "intlayer";
|
|
123
|
+
|
|
124
|
+
/** @type {import('intlayer').IntlayerConfig} */
|
|
125
|
+
const config = {
|
|
126
|
+
internationalization: {
|
|
127
|
+
defaultLocale: Locales.ENGLISH, // 默认语言
|
|
128
|
+
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH], // 支持的语言列表
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
export default config;
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
```javascript fileName="intlayer.config.cjs" codeFormat="commonjs"
|
|
136
|
+
const { Locales } = require("intlayer");
|
|
137
|
+
|
|
138
|
+
/** @type {import('intlayer').IntlayerConfig} */
|
|
139
|
+
// 配置对象,定义国际化设置
|
|
140
|
+
const config = {
|
|
141
|
+
internationalization: {
|
|
142
|
+
defaultLocale: Locales.ENGLISH, // 默认语言
|
|
143
|
+
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH], // 支持的语言列表
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
module.exports = config;
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
> 通过此配置文件,您可以设置本地化的 URL、中间件重定向、cookie 名称、内容声明的位置和扩展名,禁用控制台中的 Intlayer 日志等。有关可用参数的完整列表,请参阅[配置文档](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/configuration.md)。
|
|
151
|
+
|
|
152
|
+
### 第三步:在您的 Vite 配置中集成 Intlayer
|
|
153
|
+
|
|
154
|
+
将 intlayer 插件添加到您的配置中:
|
|
155
|
+
|
|
156
|
+
```typescript fileName="vite.config.ts"
|
|
157
|
+
import { reactRouter } from "@react-router/dev/vite";
|
|
158
|
+
import { defineConfig } from "vite";
|
|
159
|
+
import { intlayer } from "vite-intlayer";
|
|
160
|
+
import tsconfigPaths from "vite-tsconfig-paths";
|
|
161
|
+
|
|
162
|
+
export default defineConfig({
|
|
163
|
+
plugins: [reactRouter(), tsconfigPaths(), intlayer()],
|
|
164
|
+
});
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
> `intlayer()` Vite 插件用于将 Intlayer 集成到 Vite 中。它确保内容声明文件的构建,并在开发模式下监视这些文件。它在 Vite 应用中定义了 Intlayer 的环境变量。此外,它还提供别名以优化性能。
|
|
168
|
+
|
|
169
|
+
### 第4步:配置 React Router v7 路由
|
|
170
|
+
|
|
171
|
+
设置支持多语言的路由配置:
|
|
172
|
+
|
|
173
|
+
```typescript fileName="app/routes.ts"
|
|
174
|
+
import { layout, route, type RouteConfig } from "@react-router/dev/routes";
|
|
175
|
+
|
|
176
|
+
export default [
|
|
177
|
+
layout("routes/layout.tsx", [
|
|
178
|
+
route("/:lang?", "routes/page.tsx"), // 本地化主页
|
|
179
|
+
route("/:lang?/about", "routes/about/page.tsx"), // 本地化关于页面
|
|
180
|
+
]),
|
|
181
|
+
] satisfies RouteConfig;
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### 第5步:创建布局组件
|
|
185
|
+
|
|
186
|
+
设置根布局和特定语言环境的布局:
|
|
187
|
+
|
|
188
|
+
#### 根布局
|
|
189
|
+
|
|
190
|
+
```tsx fileName="app/routes/layout.tsx"
|
|
191
|
+
import { IntlayerProvider } from "react-intlayer";
|
|
192
|
+
import { Outlet } from "react-router";
|
|
193
|
+
|
|
194
|
+
import type { Route } from "./+types/layout";
|
|
195
|
+
|
|
196
|
+
export default function RootLayout({ params }: Route.ComponentProps) {
|
|
197
|
+
const { locale } = params;
|
|
198
|
+
|
|
199
|
+
return (
|
|
200
|
+
<IntlayerProvider locale={locale}>
|
|
201
|
+
<Outlet />
|
|
202
|
+
</IntlayerProvider>
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### 第6步:声明您的内容
|
|
208
|
+
|
|
209
|
+
创建并管理您的内容声明以存储翻译:
|
|
210
|
+
|
|
211
|
+
```tsx fileName="app/routes/[lang]/page.content.ts"
|
|
212
|
+
import { t, type Dictionary } from "intlayer";
|
|
213
|
+
|
|
214
|
+
const pageContent = {
|
|
215
|
+
key: "page",
|
|
216
|
+
content: {
|
|
217
|
+
title: t({
|
|
218
|
+
en: "Welcome to React Router v7 + Intlayer",
|
|
219
|
+
es: "Bienvenido a React Router v7 + Intlayer",
|
|
220
|
+
fr: "Bienvenue sur React Router v7 + Intlayer",
|
|
221
|
+
}),
|
|
222
|
+
description: t({
|
|
223
|
+
en: "Build multilingual applications with ease using React Router v7 and Intlayer.",
|
|
224
|
+
es: "Cree aplicaciones multilingües fácilmente usando React Router v7 y Intlayer.",
|
|
225
|
+
fr: "Créez des applications multilingues facilement avec React Router v7 et Intlayer.",
|
|
226
|
+
}),
|
|
227
|
+
aboutLink: t({
|
|
228
|
+
en: "了解我们",
|
|
229
|
+
es: "Aprender Sobre Nosotros",
|
|
230
|
+
fr: "En savoir plus sur nous",
|
|
231
|
+
}),
|
|
232
|
+
homeLink: t({
|
|
233
|
+
en: "首页",
|
|
234
|
+
es: "Inicio",
|
|
235
|
+
fr: "Accueil",
|
|
236
|
+
}),
|
|
237
|
+
},
|
|
238
|
+
} satisfies Dictionary;
|
|
239
|
+
|
|
240
|
+
export default pageContent;
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
> 您的内容声明可以定义在应用程序中的任何位置,只要它们被包含在 `contentDir` 目录中(默认是 `./app`)。并且文件扩展名需匹配内容声明文件扩展名(默认是 `.content.{json,ts,tsx,js,jsx,mjs,mjx,cjs,cjx}`)。
|
|
244
|
+
|
|
245
|
+
> 更多详情,请参阅[内容声明文档](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/dictionary/get_started.md)。
|
|
246
|
+
|
|
247
|
+
### 第7步:创建支持多语言的组件
|
|
248
|
+
|
|
249
|
+
创建一个 `LocalizedLink` 组件,用于支持多语言的导航:
|
|
250
|
+
|
|
251
|
+
```tsx fileName="app/components/localized-link.tsx"
|
|
252
|
+
import type { FC } from "react";
|
|
253
|
+
|
|
254
|
+
import { getLocalizedUrl, type LocalesValues } from "intlayer";
|
|
255
|
+
import { useLocale } from "react-intlayer";
|
|
256
|
+
import { Link, type LinkProps, type To } from "react-router";
|
|
257
|
+
|
|
258
|
+
const isExternalLink = (to: string) => /^(https?:)?\/\//.test(to); // 判断是否为外部链接
|
|
259
|
+
|
|
260
|
+
export const locacalizeTo = (to: To, locale: LocalesValues): To => {
|
|
261
|
+
if (typeof to === "string") {
|
|
262
|
+
if (isExternalLink(to)) {
|
|
263
|
+
return to; // 如果是外部链接,直接返回
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return getLocalizedUrl(to, locale); // 获取本地化的URL
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (isExternalLink(to.pathname ?? "")) {
|
|
270
|
+
return to; // 如果路径名是外部链接,直接返回
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return {
|
|
274
|
+
...to,
|
|
275
|
+
pathname: getLocalizedUrl(to.pathname ?? "", locale), // 本地化路径名
|
|
276
|
+
};
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
export const LocalizedLink: FC<LinkProps> = (props) => {
|
|
280
|
+
const { locale } = useLocale(); // 获取当前语言环境
|
|
281
|
+
|
|
282
|
+
return <Link {...props} to={locacalizeTo(props.to, locale)} />;
|
|
283
|
+
};
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
如果你想导航到本地化路由,可以使用 `useLocalizedNavigate` 钩子:
|
|
287
|
+
|
|
288
|
+
```tsx fileName="app/hooks/useLocalizedNavigate.ts"
|
|
289
|
+
import { useLocale } from "react-intlayer";
|
|
290
|
+
import { type NavigateOptions, type To, useNavigate } from "react-router";
|
|
291
|
+
|
|
292
|
+
import { locacalizeTo } from "~/components/localized-link";
|
|
293
|
+
|
|
294
|
+
export const useLocalizedNavigate = () => {
|
|
295
|
+
const navigate = useNavigate();
|
|
296
|
+
const { locale } = useLocale();
|
|
297
|
+
|
|
298
|
+
const localizedNavigate = (to: To, options?: NavigateOptions) => {
|
|
299
|
+
const localedTo = locacalizeTo(to, locale);
|
|
300
|
+
|
|
301
|
+
navigate(localedTo, options);
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
return localizedNavigate;
|
|
305
|
+
};
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
### 第8步:在你的页面中使用 Intlayer
|
|
309
|
+
|
|
310
|
+
在您的整个应用程序中访问内容字典:
|
|
311
|
+
|
|
312
|
+
#### 本地化主页
|
|
313
|
+
|
|
314
|
+
```tsx fileName="app/routes/[lang]/page.tsx"
|
|
315
|
+
import { useIntlayer } from "react-intlayer";
|
|
316
|
+
import { LocalizedLink } from "~/components/localized-link";
|
|
317
|
+
|
|
318
|
+
export default function Page() {
|
|
319
|
+
const { title, description, aboutLink } = useIntlayer("page");
|
|
320
|
+
|
|
321
|
+
return (
|
|
322
|
+
<div>
|
|
323
|
+
<h1>{title}</h1>
|
|
324
|
+
<p>{description}</p>
|
|
325
|
+
<nav>
|
|
326
|
+
<LocalizedLink to="/about">{aboutLink}</LocalizedLink>
|
|
327
|
+
</nav>
|
|
328
|
+
</div>
|
|
329
|
+
);
|
|
330
|
+
}
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
> 想了解更多关于 `useIntlayer` 钩子的内容,请参阅[文档](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/packages/react-intlayer/useIntlayer.md)。
|
|
334
|
+
|
|
335
|
+
### 第9步:创建语言切换组件
|
|
336
|
+
|
|
337
|
+
创建一个组件,允许用户切换语言:
|
|
338
|
+
|
|
339
|
+
```tsx fileName="app/components/locale-switcher.tsx"
|
|
340
|
+
import type { FC } from "react";
|
|
341
|
+
|
|
342
|
+
import {
|
|
343
|
+
getHTMLTextDir,
|
|
344
|
+
getLocaleName,
|
|
345
|
+
getLocalizedUrl,
|
|
346
|
+
getPathWithoutLocale,
|
|
347
|
+
Locales,
|
|
348
|
+
} from "intlayer";
|
|
349
|
+
import { setLocaleInStorage, useIntlayer, useLocale } from "react-intlayer";
|
|
350
|
+
import { Link, useLocation } from "react-router";
|
|
351
|
+
|
|
352
|
+
export const LocaleSwitcher: FC = () => {
|
|
353
|
+
const { localeSwitcherLabel } = useIntlayer("locale-switcher"); // 使用 useIntlayer 钩子获取本地化标签
|
|
354
|
+
const { pathname } = useLocation(); // 获取当前路径名
|
|
355
|
+
|
|
356
|
+
const { availableLocales, locale } = useLocale(); // 获取可用语言和当前语言
|
|
357
|
+
|
|
358
|
+
const pathWithoutLocale = getPathWithoutLocale(pathname); // 获取不带语言前缀的路径
|
|
359
|
+
|
|
360
|
+
return (
|
|
361
|
+
<ol>
|
|
362
|
+
{availableLocales.map((localeItem) => (
|
|
363
|
+
<li key={localeItem}>
|
|
364
|
+
<Link
|
|
365
|
+
aria-current={localeItem === locale ? "page" : undefined}
|
|
366
|
+
aria-label={`${localeSwitcherLabel.value} ${getLocaleName(localeItem)}`}
|
|
367
|
+
onClick={() => setLocale(localeItem)}
|
|
368
|
+
to={getLocalizedUrl(pathWithoutLocale, localeItem)}
|
|
369
|
+
>
|
|
370
|
+
<span>
|
|
371
|
+
{/* 语言环境 - 例如 FR */}
|
|
372
|
+
{localeItem}
|
|
373
|
+
</span>
|
|
374
|
+
<span>
|
|
375
|
+
{/* 语言在其自身语言环境中的名称 - 例如 Français */}
|
|
376
|
+
{getLocaleName(localeItem, locale)}
|
|
377
|
+
</span>
|
|
378
|
+
<span dir={getHTMLTextDir(localeItem)} lang={localeItem}>
|
|
379
|
+
{/* 语言在当前语言环境中的名称 - 例如当前语言环境为 Locales.SPANISH 时显示 Francés */}
|
|
380
|
+
{getLocaleName(localeItem)}
|
|
381
|
+
</span>
|
|
382
|
+
<span dir="ltr" lang={Locales.ENGLISH}>
|
|
383
|
+
{/* 语言的英文名称 - 例如 French */}
|
|
384
|
+
{getLocaleName(localeItem, Locales.ENGLISH)}
|
|
385
|
+
</span>
|
|
386
|
+
</Link>
|
|
387
|
+
</li>
|
|
388
|
+
))}
|
|
389
|
+
</ol>
|
|
390
|
+
);
|
|
391
|
+
};
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
> 想了解更多关于 `useLocale` 钩子的内容,请参阅[文档](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/packages/react-intlayer/useLocale.md)。
|
|
395
|
+
|
|
396
|
+
### 第10步:添加HTML属性管理(可选)
|
|
397
|
+
|
|
398
|
+
创建一个钩子来管理HTML的 lang 和 dir 属性:
|
|
399
|
+
|
|
400
|
+
```tsx fileName="app/hooks/useI18nHTMLAttributes.tsx"
|
|
401
|
+
import { getHTMLTextDir } from "intlayer";
|
|
402
|
+
import { useEffect } from "react";
|
|
403
|
+
import { useLocale } from "react-intlayer";
|
|
404
|
+
|
|
405
|
+
export const useI18nHTMLAttributes = () => {
|
|
406
|
+
const { locale } = useLocale();
|
|
407
|
+
|
|
408
|
+
useEffect(() => {
|
|
409
|
+
document.documentElement.lang = locale;
|
|
410
|
+
document.documentElement.dir = getHTMLTextDir(locale);
|
|
411
|
+
}, [locale]);
|
|
412
|
+
};
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
然后在你的根组件中使用它:
|
|
416
|
+
|
|
417
|
+
```tsx fileName="app/routes/layout.tsx"
|
|
418
|
+
import { Outlet } from "react-router";
|
|
419
|
+
import { IntlayerProvider } from "react-intlayer";
|
|
420
|
+
|
|
421
|
+
import { useI18nHTMLAttributes } from "app/hooks/useI18nHTMLAttributes"; // 导入该钩子
|
|
422
|
+
|
|
423
|
+
export default function RootLayout() {
|
|
424
|
+
useI18nHTMLAttributes(); // 调用该钩子
|
|
425
|
+
|
|
426
|
+
return (
|
|
427
|
+
<IntlayerProvider>
|
|
428
|
+
<Outlet />
|
|
429
|
+
</IntlayerProvider>
|
|
430
|
+
);
|
|
431
|
+
}
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
### 第11步:添加中间件(可选)
|
|
435
|
+
|
|
436
|
+
你也可以使用 `intlayerProxy` 为你的应用添加服务器端路由。该插件会根据 URL 自动检测当前的语言环境,并设置相应的语言环境 cookie。如果未指定语言环境,插件将根据用户浏览器的语言偏好确定最合适的语言环境。如果仍未检测到语言环境,则会重定向到默认语言环境。
|
|
437
|
+
|
|
438
|
+
> 注意,要在生产环境中使用 `intlayerProxy`,你需要将 `vite-intlayer` 包从 `devDependencies` 切换到 `dependencies`。
|
|
439
|
+
|
|
440
|
+
```typescript {3,7} fileName="vite.config.ts"
|
|
441
|
+
import { reactRouter } from "@react-router/dev/vite";
|
|
442
|
+
import { defineConfig } from "vite";
|
|
443
|
+
import { intlayer, intlayerProxy } from "vite-intlayer";
|
|
444
|
+
import tsconfigPaths from "vite-tsconfig-paths";
|
|
445
|
+
|
|
446
|
+
export default defineConfig({
|
|
447
|
+
plugins: [reactRouter(), tsconfigPaths(), intlayer(), intlayerProxy()],
|
|
448
|
+
});
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
---
|
|
452
|
+
|
|
453
|
+
## 配置 TypeScript
|
|
454
|
+
|
|
455
|
+
Intlayer 使用模块增强来利用 TypeScript 的优势,使您的代码库更强大。
|
|
456
|
+
|
|
457
|
+
确保您的 TypeScript 配置包含自动生成的类型:
|
|
458
|
+
|
|
459
|
+
```json5 fileName="tsconfig.json"
|
|
460
|
+
{
|
|
461
|
+
// ... 您现有的配置
|
|
462
|
+
include: [
|
|
463
|
+
// ... 您现有的包含项
|
|
464
|
+
".intlayer/**/*.ts", // 包含自动生成的类型
|
|
465
|
+
],
|
|
466
|
+
}
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
---
|
|
470
|
+
|
|
471
|
+
## Git 配置
|
|
472
|
+
|
|
473
|
+
建议忽略 Intlayer 生成的文件,这样可以避免将它们提交到您的 Git 仓库。
|
|
474
|
+
|
|
475
|
+
为此,您可以在 `.gitignore` 文件中添加以下指令:
|
|
476
|
+
|
|
477
|
+
```plaintext fileName=".gitignore"
|
|
478
|
+
# 忽略 Intlayer 生成的文件
|
|
479
|
+
.intlayer
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
---
|
|
483
|
+
|
|
484
|
+
## VS Code 扩展
|
|
485
|
+
|
|
486
|
+
为了提升您使用 Intlayer 的开发体验,您可以安装官方的 **Intlayer VS Code 扩展**。
|
|
487
|
+
|
|
488
|
+
[从 VS Code 市场安装](https://marketplace.visualstudio.com/items?itemName=intlayer.intlayer-vs-code-extension)
|
|
489
|
+
|
|
490
|
+
该扩展提供:
|
|
491
|
+
|
|
492
|
+
- 翻译键的**自动补全**。
|
|
493
|
+
- 缺失翻译的**实时错误检测**。
|
|
494
|
+
- 翻译内容的**内联预览**。
|
|
495
|
+
- 轻松创建和更新翻译的**快速操作**。
|
|
496
|
+
|
|
497
|
+
有关如何使用该扩展的更多详细信息,请参阅 [Intlayer VS Code 扩展文档](https://intlayer.org/doc/vs-code-extension)。
|
|
498
|
+
|
|
499
|
+
---
|
|
500
|
+
|
|
501
|
+
## 深入了解
|
|
502
|
+
|
|
503
|
+
要更进一步,您可以实现[可视化编辑器](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/intlayer_visual_editor.md)或使用[内容管理系统(CMS)](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/intlayer_CMS.md)来外部化您的内容。
|
|
504
|
+
|
|
505
|
+
---
|
|
506
|
+
|
|
507
|
+
## 文档参考
|
|
508
|
+
|
|
509
|
+
- [Intlayer 文档](https://intlayer.org)
|
|
510
|
+
- [React Router v7 文档](https://reactrouter.com/)
|
|
511
|
+
- [useIntlayer 钩子](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/packages/react-intlayer/useIntlayer.md)
|
|
512
|
+
- [useLocale 钩子](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/packages/react-intlayer/useLocale.md)
|
|
513
|
+
- [内容声明](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/dictionary/get_started.md)
|
|
514
|
+
- [配置](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/configuration.md)
|
|
515
|
+
|
|
516
|
+
本综合指南提供了将 Intlayer 与 React Router v7 集成所需的一切,支持完全国际化的应用程序,具备基于区域设置的路由和 TypeScript 支持。
|
|
@@ -17,6 +17,7 @@ slugs:
|
|
|
17
17
|
- environment
|
|
18
18
|
- tanstack-start
|
|
19
19
|
applicationTemplate: https://github.com/aymericzip/intlayer-tanstack-start-template
|
|
20
|
+
youtubeVideo: https://www.youtube.com/watch?v=_XTdKVWaeqg
|
|
20
21
|
history:
|
|
21
22
|
- version: 7.3.9
|
|
22
23
|
date: 2025-12-05
|
|
@@ -577,19 +578,8 @@ export const getLocaleServer = createServerFn().handler(async () => {
|
|
|
577
578
|
return getCookie(name, cookieString);
|
|
578
579
|
},
|
|
579
580
|
// Get the header from the request (default: 'x-intlayer-locale')
|
|
580
|
-
getHeader: (name) => getRequestHeader(name),
|
|
581
581
|
// Fallback using Accept-Language negotiation
|
|
582
|
-
|
|
583
|
-
const headers = getRequestHeaders();
|
|
584
|
-
const result: Record<string, string> = {};
|
|
585
|
-
|
|
586
|
-
// Convert the TypedHeaders into a plain Record<string, string>
|
|
587
|
-
for (const [key, value] of headers.entries()) {
|
|
588
|
-
result[key] = value;
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
return result;
|
|
592
|
-
},
|
|
582
|
+
getHeader: (name) => getRequestHeader(name),
|
|
593
583
|
});
|
|
594
584
|
|
|
595
585
|
// Retrieve some content using getIntlayer()
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intlayer/docs",
|
|
3
|
-
"version": "7.3.
|
|
3
|
+
"version": "7.3.12",
|
|
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": "7.3.
|
|
77
|
-
"@intlayer/core": "7.3.
|
|
78
|
-
"@intlayer/types": "7.3.
|
|
76
|
+
"@intlayer/config": "7.3.12",
|
|
77
|
+
"@intlayer/core": "7.3.12",
|
|
78
|
+
"@intlayer/types": "7.3.12"
|
|
79
79
|
},
|
|
80
80
|
"devDependencies": {
|
|
81
|
-
"@intlayer/api": "7.3.
|
|
82
|
-
"@intlayer/cli": "7.3.
|
|
81
|
+
"@intlayer/api": "7.3.12",
|
|
82
|
+
"@intlayer/cli": "7.3.12",
|
|
83
83
|
"@types/node": "24.10.1",
|
|
84
84
|
"@utils/ts-config": "1.0.4",
|
|
85
85
|
"@utils/ts-config-types": "1.0.4",
|
|
@@ -1079,6 +1079,25 @@ export const docsEntry = {
|
|
|
1079
1079
|
id: readLocale('intlayer_with_react_router_v7.md', 'id'),
|
|
1080
1080
|
vi: readLocale('intlayer_with_react_router_v7.md', 'vi'),
|
|
1081
1081
|
} as unknown as Record<LocalesValues, Promise<string>>,
|
|
1082
|
+
'./docs/en/intlayer_with_react_router_v7_fs_routes.md': {
|
|
1083
|
+
en: readLocale('intlayer_with_react_router_v7_fs_routes.md', 'en'),
|
|
1084
|
+
ru: readLocale('intlayer_with_react_router_v7_fs_routes.md', 'ru'),
|
|
1085
|
+
ja: readLocale('intlayer_with_react_router_v7_fs_routes.md', 'ja'),
|
|
1086
|
+
fr: readLocale('intlayer_with_react_router_v7_fs_routes.md', 'fr'),
|
|
1087
|
+
ko: readLocale('intlayer_with_react_router_v7_fs_routes.md', 'ko'),
|
|
1088
|
+
zh: readLocale('intlayer_with_react_router_v7_fs_routes.md', 'zh'),
|
|
1089
|
+
es: readLocale('intlayer_with_react_router_v7_fs_routes.md', 'es'),
|
|
1090
|
+
de: readLocale('intlayer_with_react_router_v7_fs_routes.md', 'de'),
|
|
1091
|
+
ar: readLocale('intlayer_with_react_router_v7_fs_routes.md', 'ar'),
|
|
1092
|
+
it: readLocale('intlayer_with_react_router_v7_fs_routes.md', 'it'),
|
|
1093
|
+
'en-GB': readLocale('intlayer_with_react_router_v7_fs_routes.md', 'en-GB'),
|
|
1094
|
+
pt: readLocale('intlayer_with_react_router_v7_fs_routes.md', 'pt'),
|
|
1095
|
+
hi: readLocale('intlayer_with_react_router_v7_fs_routes.md', 'hi'),
|
|
1096
|
+
tr: readLocale('intlayer_with_react_router_v7_fs_routes.md', 'tr'),
|
|
1097
|
+
pl: readLocale('intlayer_with_react_router_v7_fs_routes.md', 'pl'),
|
|
1098
|
+
id: readLocale('intlayer_with_react_router_v7_fs_routes.md', 'id'),
|
|
1099
|
+
vi: readLocale('intlayer_with_react_router_v7_fs_routes.md', 'vi'),
|
|
1100
|
+
} as unknown as Record<LocalesValues, Promise<string>>,
|
|
1082
1101
|
'./docs/en/intlayer_with_svelte_kit.md': {
|
|
1083
1102
|
en: readLocale('intlayer_with_svelte_kit.md', 'en'),
|
|
1084
1103
|
ru: readLocale('intlayer_with_svelte_kit.md', 'ru'),
|