@intlayer/docs 7.3.11 → 7.3.13
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 +294 -438
- 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 +284 -410
- 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 +237 -341
- 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 +254 -378
- 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 +271 -390
- 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 +278 -405
- 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 +303 -447
- 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 +266 -395
- 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 +299 -423
- 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 +309 -432
- 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 +295 -422
- 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 +273 -476
- 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 +277 -420
- 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 +287 -425
- 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 +313 -406
- 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 +273 -418
- 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 +300 -461
- 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 +10 -11
- 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
|
|
@@ -85,6 +86,29 @@ pnpm add vite-intlayer --save-dev
|
|
|
85
86
|
|
|
86
87
|
### Step 2: Configuration of your project
|
|
87
88
|
|
|
89
|
+
## Step-by-Step Guide to Set Up Intlayer in a React Router v7 Application with File-System Routes
|
|
90
|
+
|
|
91
|
+
<Tab defaultTab="video">
|
|
92
|
+
<TabItem label="Video" value="video">
|
|
93
|
+
|
|
94
|
+
<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"/>
|
|
95
|
+
|
|
96
|
+
</TabItem>
|
|
97
|
+
<TabItem label="Code" value="code">
|
|
98
|
+
|
|
99
|
+
<iframe
|
|
100
|
+
src="https://stackblitz.com/github/aymericzip/intlayer-react-router-v7-template?embed=1&ctl=1&file=intlayer.config.ts"
|
|
101
|
+
className="m-auto overflow-hidden rounded-lg border-0 max-md:size-full max-md:h-[700px] md:aspect-16/9 md:w-full"
|
|
102
|
+
title="Demo CodeSandbox - How to Internationalize your application using Intlayer"
|
|
103
|
+
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
|
|
104
|
+
loading="lazy"
|
|
105
|
+
/>
|
|
106
|
+
|
|
107
|
+
</TabItem>
|
|
108
|
+
</Tab>
|
|
109
|
+
|
|
110
|
+
See [Application Template](https://github.com/aymericzip/intlayer-react-router-v7-template) on GitHub.
|
|
111
|
+
|
|
88
112
|
Create a config file to configure the languages of your application:
|
|
89
113
|
|
|
90
114
|
```typescript fileName="intlayer.config.ts" codeFormat="typescript"
|
|
@@ -0,0 +1,570 @@
|
|
|
1
|
+
---
|
|
2
|
+
createdAt: 2025-12-07
|
|
3
|
+
updatedAt: 2025-12-07
|
|
4
|
+
title: How to translate your React Router v7 (File-System Routes) app – i18n guide 2025
|
|
5
|
+
description: Learn how to add internationalization (i18n) to your React Router v7 application using Intlayer with file-system based routing. Follow this comprehensive guide to make your app multilingual with locale-aware routing.
|
|
6
|
+
keywords:
|
|
7
|
+
- Internationalization
|
|
8
|
+
- Documentation
|
|
9
|
+
- Intlayer
|
|
10
|
+
- React Router v7
|
|
11
|
+
- fs-routes
|
|
12
|
+
- File System Routes
|
|
13
|
+
- React
|
|
14
|
+
- i18n
|
|
15
|
+
- TypeScript
|
|
16
|
+
- Locale Routing
|
|
17
|
+
slugs:
|
|
18
|
+
- doc
|
|
19
|
+
- environment
|
|
20
|
+
- vite-and-react
|
|
21
|
+
- react-router-v7-fs-routes
|
|
22
|
+
applicationTemplate: https://github.com/aymericzip/intlayer-react-router-v7-fs-routes-template
|
|
23
|
+
youtubeVideo: https://www.youtube.com/watch?v=dS9L7uJeak4
|
|
24
|
+
history:
|
|
25
|
+
- version: 7.3.4
|
|
26
|
+
date: 2025-12-08
|
|
27
|
+
changes: Init history
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
# Translate your React Router v7 (File-System Routes) website using Intlayer | Internationalization (i18n)
|
|
31
|
+
|
|
32
|
+
This guide demonstrates how to integrate **Intlayer** for seamless internationalization in React Router v7 projects using **file-system based routing** (`@react-router/fs-routes`) with locale-aware routing, TypeScript support, and modern development practices.
|
|
33
|
+
|
|
34
|
+
## Table of Contents
|
|
35
|
+
|
|
36
|
+
<TOC/>
|
|
37
|
+
|
|
38
|
+
## What is Intlayer?
|
|
39
|
+
|
|
40
|
+
**Intlayer** is an innovative, open-source internationalization (i18n) library designed to simplify multilingual support in modern web applications.
|
|
41
|
+
|
|
42
|
+
With Intlayer, you can:
|
|
43
|
+
|
|
44
|
+
- **Easily manage translations** using declarative dictionaries at the component level.
|
|
45
|
+
- **Dynamically localize metadata**, routes, and content.
|
|
46
|
+
- **Ensure TypeScript support** with autogenerated types, improving autocompletion and error detection.
|
|
47
|
+
- **Benefit from advanced features**, like dynamic locale detection and switching.
|
|
48
|
+
- **Enable locale-aware routing** with React Router v7's file-system based routing system.
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## Step-by-Step Guide to Set Up Intlayer in a React Router v7 Application with File-System Routes
|
|
53
|
+
|
|
54
|
+
<Tab defaultTab="video">
|
|
55
|
+
<TabItem label="Video" value="video">
|
|
56
|
+
|
|
57
|
+
<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"/>
|
|
58
|
+
|
|
59
|
+
</TabItem>
|
|
60
|
+
<TabItem label="Code" value="code">
|
|
61
|
+
|
|
62
|
+
<iframe
|
|
63
|
+
src="https://stackblitz.com/github/aymericzip/intlayer-react-router-v7-fs-routes-template?embed=1&ctl=1&file=intlayer.config.ts"
|
|
64
|
+
className="m-auto overflow-hidden rounded-lg border-0 max-md:size-full max-md:h-[700px] md:aspect-16/9 md:w-full"
|
|
65
|
+
title="Demo CodeSandbox - How to Internationalize your application using Intlayer"
|
|
66
|
+
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
|
|
67
|
+
loading="lazy"
|
|
68
|
+
/>
|
|
69
|
+
|
|
70
|
+
</TabItem>
|
|
71
|
+
</Tab>
|
|
72
|
+
|
|
73
|
+
See [Application Template](https://github.com/aymericzip/intlayer-react-router-v7-fs-routes-template) on GitHub.
|
|
74
|
+
|
|
75
|
+
### Step 1: Install Dependencies
|
|
76
|
+
|
|
77
|
+
Install the necessary packages using your preferred package manager:
|
|
78
|
+
|
|
79
|
+
```bash packageManager="npm"
|
|
80
|
+
npm install intlayer react-intlayer
|
|
81
|
+
npm install vite-intlayer --save-dev
|
|
82
|
+
npm install @react-router/fs-routes --save-dev
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
```bash packageManager="pnpm"
|
|
86
|
+
pnpm add intlayer react-intlayer
|
|
87
|
+
pnpm add vite-intlayer --save-dev
|
|
88
|
+
pnpm add @react-router/fs-routes --save-dev
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
- **intlayer**
|
|
92
|
+
|
|
93
|
+
The core package that provides internationalization tools for configuration management, translation, [content declaration](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/dictionary/content_file.md), transpilation, and [CLI commands](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/cli/index.md).
|
|
94
|
+
|
|
95
|
+
- **react-intlayer**
|
|
96
|
+
The package that integrates Intlayer with React application. It provides context providers and hooks for React internationalization.
|
|
97
|
+
|
|
98
|
+
- **vite-intlayer**
|
|
99
|
+
Includes the Vite plugin for integrating Intlayer with the [Vite bundler](https://vite.dev/guide/why.html#why-bundle-for-production), as well as middleware for detecting the user's preferred locale, managing cookies, and handling URL redirection.
|
|
100
|
+
|
|
101
|
+
- **@react-router/fs-routes**
|
|
102
|
+
The package that enables file-system based routing for React Router v7.
|
|
103
|
+
|
|
104
|
+
### Step 2: Configuration of your project
|
|
105
|
+
|
|
106
|
+
Create a config file to configure the languages of your application:
|
|
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
|
+
const config = {
|
|
140
|
+
internationalization: {
|
|
141
|
+
defaultLocale: Locales.ENGLISH,
|
|
142
|
+
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
module.exports = config;
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
> Through this configuration file, you can set up localized URLs, middleware redirection, cookie names, the location and extension of your content declarations, disable Intlayer logs in the console, and more. For a complete list of available parameters, refer to the [configuration documentation](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/configuration.md).
|
|
150
|
+
|
|
151
|
+
### Step 3: Integrate Intlayer in Your Vite Configuration
|
|
152
|
+
|
|
153
|
+
Add the intlayer plugin into your configuration:
|
|
154
|
+
|
|
155
|
+
```typescript fileName="vite.config.ts"
|
|
156
|
+
import { reactRouter } from "@react-router/dev/vite";
|
|
157
|
+
import { defineConfig } from "vite";
|
|
158
|
+
import { intlayer } from "vite-intlayer";
|
|
159
|
+
import tsconfigPaths from "vite-tsconfig-paths";
|
|
160
|
+
|
|
161
|
+
export default defineConfig({
|
|
162
|
+
plugins: [reactRouter(), tsconfigPaths(), intlayer()],
|
|
163
|
+
});
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
> The `intlayer()` Vite plugin is used to integrate Intlayer with Vite. It ensures the building of content declaration files and monitors them in development mode. It defines Intlayer environment variables within the Vite application. Additionally, it provides aliases to optimize performance.
|
|
167
|
+
|
|
168
|
+
### Step 4: Configure React Router v7 File-System Routes
|
|
169
|
+
|
|
170
|
+
Set up your routing configuration to use file-system based routes with `flatRoutes`:
|
|
171
|
+
|
|
172
|
+
```typescript fileName="app/routes.ts"
|
|
173
|
+
import type { RouteConfig } from "@react-router/dev/routes";
|
|
174
|
+
import { flatRoutes } from "@react-router/fs-routes";
|
|
175
|
+
import { configuration } from "intlayer";
|
|
176
|
+
|
|
177
|
+
const routes: RouteConfig = flatRoutes({
|
|
178
|
+
// Ignore content declaration files from being treated as routes
|
|
179
|
+
ignoredRouteFiles: configuration.content.fileExtensions.map(
|
|
180
|
+
(fileExtension) => `**/*${fileExtension}`
|
|
181
|
+
),
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
export default routes;
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
> The `flatRoutes` function from `@react-router/fs-routes` enables file-system based routing, where the file structure in the `routes/` directory determines your application's routes. The `ignoredRouteFiles` option ensures that Intlayer content declaration files (`.content.ts`, etc.) are not treated as route files.
|
|
188
|
+
|
|
189
|
+
### Step 5: Create Route Files with File-System Conventions
|
|
190
|
+
|
|
191
|
+
With file-system routing, you use a flat naming convention where dots (`.`) represent path segments and parentheses `()` denote optional segments.
|
|
192
|
+
|
|
193
|
+
Create the following files in your `app/routes/` directory:
|
|
194
|
+
|
|
195
|
+
#### File Structure
|
|
196
|
+
|
|
197
|
+
```bash
|
|
198
|
+
app/routes/
|
|
199
|
+
├── ($locale)._layout.tsx # Layout wrapper for locale routes
|
|
200
|
+
├── ($locale)._index.tsx # Home page (/:locale?)
|
|
201
|
+
├── ($locale)._index.content.ts # Home page content
|
|
202
|
+
├── ($locale).about.tsx # About page (/:locale?/about)
|
|
203
|
+
└── ($locale).about.content.ts # About page content
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
The naming conventions:
|
|
207
|
+
|
|
208
|
+
- `($locale)` - Optional dynamic segment for the locale parameter
|
|
209
|
+
- `_layout` - Layout route that wraps child routes
|
|
210
|
+
- `_index` - Index route (renders at the parent path)
|
|
211
|
+
- `.` (dot) - Separates path segments (e.g., `($locale).about` → `/:locale?/about`)
|
|
212
|
+
|
|
213
|
+
#### Layout Component
|
|
214
|
+
|
|
215
|
+
```tsx fileName="app/routes/($locale)._layout.tsx"
|
|
216
|
+
import { IntlayerProvider } from "react-intlayer";
|
|
217
|
+
import { Outlet } from "react-router";
|
|
218
|
+
|
|
219
|
+
import { useI18nHTMLAttributes } from "~/hooks/useI18nHTMLAttributes";
|
|
220
|
+
|
|
221
|
+
import type { Route } from "./+types/($locale)._layout";
|
|
222
|
+
|
|
223
|
+
export default function RootLayout({ params }: Route.ComponentProps) {
|
|
224
|
+
useI18nHTMLAttributes();
|
|
225
|
+
|
|
226
|
+
const { locale } = params;
|
|
227
|
+
|
|
228
|
+
return (
|
|
229
|
+
<IntlayerProvider locale={locale}>
|
|
230
|
+
<Outlet />
|
|
231
|
+
</IntlayerProvider>
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
#### Index Page
|
|
237
|
+
|
|
238
|
+
```tsx fileName="app/routes/($locale)._index.tsx"
|
|
239
|
+
import { useIntlayer } from "react-intlayer";
|
|
240
|
+
import { LocalizedLink } from "~/components/localized-link";
|
|
241
|
+
|
|
242
|
+
import type { Route } from "./+types/($locale)._index";
|
|
243
|
+
|
|
244
|
+
export default function Page() {
|
|
245
|
+
const { title, description, aboutLink } = useIntlayer("page");
|
|
246
|
+
|
|
247
|
+
return (
|
|
248
|
+
<div>
|
|
249
|
+
<h1>{title}</h1>
|
|
250
|
+
<p>{description}</p>
|
|
251
|
+
<nav>
|
|
252
|
+
<LocalizedLink to="/about">{aboutLink}</LocalizedLink>
|
|
253
|
+
</nav>
|
|
254
|
+
</div>
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
#### About Page
|
|
260
|
+
|
|
261
|
+
```tsx fileName="app/routes/($locale).about.tsx"
|
|
262
|
+
import { useIntlayer } from "react-intlayer";
|
|
263
|
+
import { LocalizedLink } from "~/components/localized-link";
|
|
264
|
+
|
|
265
|
+
import type { Route } from "./+types/($locale).about";
|
|
266
|
+
|
|
267
|
+
export default function AboutPage() {
|
|
268
|
+
const { title, content, homeLink } = useIntlayer("about");
|
|
269
|
+
|
|
270
|
+
return (
|
|
271
|
+
<div>
|
|
272
|
+
<h1>{title}</h1>
|
|
273
|
+
<p>{content}</p>
|
|
274
|
+
<nav>
|
|
275
|
+
<LocalizedLink to="/">{homeLink}</LocalizedLink>
|
|
276
|
+
</nav>
|
|
277
|
+
</div>
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
### Step 6: Declare Your Content
|
|
283
|
+
|
|
284
|
+
Create and manage your content declarations to store translations. Place content files alongside your route files:
|
|
285
|
+
|
|
286
|
+
```tsx fileName="app/routes/($locale)._index.content.ts"
|
|
287
|
+
import { t, type Dictionary } from "intlayer";
|
|
288
|
+
|
|
289
|
+
const pageContent = {
|
|
290
|
+
key: "page",
|
|
291
|
+
content: {
|
|
292
|
+
title: t({
|
|
293
|
+
en: "Welcome to React Router v7 + Intlayer",
|
|
294
|
+
es: "Bienvenido a React Router v7 + Intlayer",
|
|
295
|
+
fr: "Bienvenue sur React Router v7 + Intlayer",
|
|
296
|
+
}),
|
|
297
|
+
description: t({
|
|
298
|
+
en: "Build multilingual applications with ease using React Router v7 and Intlayer.",
|
|
299
|
+
es: "Cree aplicaciones multilingües fácilmente usando React Router v7 y Intlayer.",
|
|
300
|
+
fr: "Créez des applications multilingues facilement avec React Router v7 et Intlayer.",
|
|
301
|
+
}),
|
|
302
|
+
aboutLink: t({
|
|
303
|
+
en: "Learn About Us",
|
|
304
|
+
es: "Aprender Sobre Nosotros",
|
|
305
|
+
fr: "En savoir plus sur nous",
|
|
306
|
+
}),
|
|
307
|
+
},
|
|
308
|
+
} satisfies Dictionary;
|
|
309
|
+
|
|
310
|
+
export default pageContent;
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
```tsx fileName="app/routes/($locale).about.content.ts"
|
|
314
|
+
import { t, type Dictionary } from "intlayer";
|
|
315
|
+
|
|
316
|
+
const aboutContent = {
|
|
317
|
+
key: "about",
|
|
318
|
+
content: {
|
|
319
|
+
title: t({
|
|
320
|
+
en: "About Us",
|
|
321
|
+
es: "Sobre Nosotros",
|
|
322
|
+
fr: "À propos de nous",
|
|
323
|
+
}),
|
|
324
|
+
content: t({
|
|
325
|
+
en: "This is the about page content.",
|
|
326
|
+
es: "Este es el contenido de la página de información.",
|
|
327
|
+
fr: "Ceci est le contenu de la page à propos.",
|
|
328
|
+
}),
|
|
329
|
+
homeLink: t({
|
|
330
|
+
en: "Home",
|
|
331
|
+
es: "Inicio",
|
|
332
|
+
fr: "Accueil",
|
|
333
|
+
}),
|
|
334
|
+
},
|
|
335
|
+
} satisfies Dictionary;
|
|
336
|
+
|
|
337
|
+
export default aboutContent;
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
> Your content declarations can be defined anywhere in your application as soon they are included into the `contentDir` directory (by default, `./app`). And match the content declaration file extension (by default, `.content.{json,ts,tsx,js,jsx,mjs,mjx,cjs,cjx}`).
|
|
341
|
+
|
|
342
|
+
> For more details, refer to the [content declaration documentation](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/dictionary/content_file.md).
|
|
343
|
+
|
|
344
|
+
### Step 7: Create Locale-Aware Components
|
|
345
|
+
|
|
346
|
+
Create a `LocalizedLink` component for locale-aware navigation:
|
|
347
|
+
|
|
348
|
+
```tsx fileName="app/components/localized-link.tsx"
|
|
349
|
+
import type { FC } from "react";
|
|
350
|
+
|
|
351
|
+
import { getLocalizedUrl, type LocalesValues } from "intlayer";
|
|
352
|
+
import { useLocale } from "react-intlayer";
|
|
353
|
+
import { Link, type LinkProps, type To } from "react-router";
|
|
354
|
+
|
|
355
|
+
const isExternalLink = (to: string) => /^(https?:)?\/\//.test(to);
|
|
356
|
+
|
|
357
|
+
export const locacalizeTo = (to: To, locale: LocalesValues): To => {
|
|
358
|
+
if (typeof to === "string") {
|
|
359
|
+
if (isExternalLink(to)) {
|
|
360
|
+
return to;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
return getLocalizedUrl(to, locale);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
if (isExternalLink(to.pathname ?? "")) {
|
|
367
|
+
return to;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
return {
|
|
371
|
+
...to,
|
|
372
|
+
pathname: getLocalizedUrl(to.pathname ?? "", locale),
|
|
373
|
+
};
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
export const LocalizedLink: FC<LinkProps> = (props) => {
|
|
377
|
+
const { locale } = useLocale();
|
|
378
|
+
|
|
379
|
+
return <Link {...props} to={locacalizeTo(props.to, locale)} />;
|
|
380
|
+
};
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
In the case you want to navigate to the localized routes, you can use the `useLocalizedNavigate` hook:
|
|
384
|
+
|
|
385
|
+
```tsx fileName="app/hooks/useLocalizedNavigate.ts"
|
|
386
|
+
import { useLocale } from "react-intlayer";
|
|
387
|
+
import { type NavigateOptions, type To, useNavigate } from "react-router";
|
|
388
|
+
|
|
389
|
+
import { locacalizeTo } from "~/components/localized-link";
|
|
390
|
+
|
|
391
|
+
export const useLocalizedNavigate = () => {
|
|
392
|
+
const navigate = useNavigate();
|
|
393
|
+
const { locale } = useLocale();
|
|
394
|
+
|
|
395
|
+
const localizedNavigate = (to: To, options?: NavigateOptions) => {
|
|
396
|
+
const localedTo = locacalizeTo(to, locale);
|
|
397
|
+
|
|
398
|
+
navigate(localedTo, options);
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
return localizedNavigate;
|
|
402
|
+
};
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
### Step 8: Create a Locale Switcher Component
|
|
406
|
+
|
|
407
|
+
Create a component to allow users to change languages:
|
|
408
|
+
|
|
409
|
+
```tsx fileName="app/components/locale-switcher.tsx"
|
|
410
|
+
import type { FC } from "react";
|
|
411
|
+
|
|
412
|
+
import {
|
|
413
|
+
getHTMLTextDir,
|
|
414
|
+
getLocaleName,
|
|
415
|
+
getLocalizedUrl,
|
|
416
|
+
getPathWithoutLocale,
|
|
417
|
+
Locales,
|
|
418
|
+
} from "intlayer";
|
|
419
|
+
import { useIntlayer, useLocale } from "react-intlayer";
|
|
420
|
+
import { Link, useLocation } from "react-router";
|
|
421
|
+
|
|
422
|
+
export const LocaleSwitcher: FC = () => {
|
|
423
|
+
const { localeSwitcherLabel } = useIntlayer("locale-switcher");
|
|
424
|
+
const { pathname } = useLocation();
|
|
425
|
+
|
|
426
|
+
const { availableLocales, locale } = useLocale();
|
|
427
|
+
|
|
428
|
+
const pathWithoutLocale = getPathWithoutLocale(pathname);
|
|
429
|
+
|
|
430
|
+
return (
|
|
431
|
+
<ol>
|
|
432
|
+
{availableLocales.map((localeItem) => (
|
|
433
|
+
<li key={localeItem}>
|
|
434
|
+
<Link
|
|
435
|
+
aria-current={localeItem === locale ? "page" : undefined}
|
|
436
|
+
aria-label={`${localeSwitcherLabel.value} ${getLocaleName(localeItem)}`}
|
|
437
|
+
reloadDocument // Reload the page to apply the new locale
|
|
438
|
+
to={getLocalizedUrl(pathWithoutLocale, localeItem)}
|
|
439
|
+
>
|
|
440
|
+
<span>
|
|
441
|
+
{/* Locale - e.g. FR */}
|
|
442
|
+
{localeItem}
|
|
443
|
+
</span>
|
|
444
|
+
<span>
|
|
445
|
+
{/* Language in its own Locale - e.g. Français */}
|
|
446
|
+
{getLocaleName(localeItem, locale)}
|
|
447
|
+
</span>
|
|
448
|
+
<span dir={getHTMLTextDir(localeItem)} lang={localeItem}>
|
|
449
|
+
{/* Language in current Locale - e.g. Francés with current locale set to Locales.SPANISH */}
|
|
450
|
+
{getLocaleName(localeItem)}
|
|
451
|
+
</span>
|
|
452
|
+
<span dir="ltr" lang={Locales.ENGLISH}>
|
|
453
|
+
{/* Language in English - e.g. French */}
|
|
454
|
+
{getLocaleName(localeItem, Locales.ENGLISH)}
|
|
455
|
+
</span>
|
|
456
|
+
</Link>
|
|
457
|
+
</li>
|
|
458
|
+
))}
|
|
459
|
+
</ol>
|
|
460
|
+
);
|
|
461
|
+
};
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
> To Learn more about the `useLocale` hook, refer to the [documentation](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/packages/react-intlayer/useLocale.md).
|
|
465
|
+
|
|
466
|
+
### Step 9: Add HTML Attributes Management (Optional)
|
|
467
|
+
|
|
468
|
+
Create a hook to manage HTML lang and dir attributes:
|
|
469
|
+
|
|
470
|
+
```tsx fileName="app/hooks/useI18nHTMLAttributes.tsx"
|
|
471
|
+
import { getHTMLTextDir } from "intlayer";
|
|
472
|
+
import { useEffect } from "react";
|
|
473
|
+
import { useLocale } from "react-intlayer";
|
|
474
|
+
|
|
475
|
+
export const useI18nHTMLAttributes = () => {
|
|
476
|
+
const { locale } = useLocale();
|
|
477
|
+
|
|
478
|
+
useEffect(() => {
|
|
479
|
+
document.documentElement.lang = locale;
|
|
480
|
+
document.documentElement.dir = getHTMLTextDir(locale);
|
|
481
|
+
}, [locale]);
|
|
482
|
+
};
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
This hook is already used in the layout component (`($locale)._layout.tsx`) shown in Step 5.
|
|
486
|
+
|
|
487
|
+
### Step 10: Add middleware (Optional)
|
|
488
|
+
|
|
489
|
+
You can also use the `intlayerProxy` to add server-side routing to your application. This plugin will automatically detect the current locale based on the URL and set the appropriate locale cookie. If no locale is specified, the plugin will determine the most appropriate locale based on the user's browser language preferences. If no locale is detected, it will redirect to the default locale.
|
|
490
|
+
|
|
491
|
+
> Note that to use the `intlayerProxy` in production, you need to switch the `vite-intlayer` package from `devDependencies` to `dependencies`.
|
|
492
|
+
|
|
493
|
+
```typescript {3,7} fileName="vite.config.ts"
|
|
494
|
+
import { reactRouter } from "@react-router/dev/vite";
|
|
495
|
+
import { defineConfig } from "vite";
|
|
496
|
+
import { intlayer, intlayerProxy } from "vite-intlayer";
|
|
497
|
+
import tsconfigPaths from "vite-tsconfig-paths";
|
|
498
|
+
|
|
499
|
+
export default defineConfig({
|
|
500
|
+
plugins: [reactRouter(), tsconfigPaths(), intlayer(), intlayerProxy()],
|
|
501
|
+
});
|
|
502
|
+
```
|
|
503
|
+
|
|
504
|
+
---
|
|
505
|
+
|
|
506
|
+
## Configure TypeScript
|
|
507
|
+
|
|
508
|
+
Intlayer uses module augmentation to get benefits of TypeScript and make your codebase stronger.
|
|
509
|
+
|
|
510
|
+
Ensure your TypeScript configuration includes the autogenerated types:
|
|
511
|
+
|
|
512
|
+
```json5 fileName="tsconfig.json"
|
|
513
|
+
{
|
|
514
|
+
// ... your existing configurations
|
|
515
|
+
include: [
|
|
516
|
+
// ... your existing includes
|
|
517
|
+
".intlayer/**/*.ts", // Include the auto-generated types
|
|
518
|
+
],
|
|
519
|
+
}
|
|
520
|
+
```
|
|
521
|
+
|
|
522
|
+
---
|
|
523
|
+
|
|
524
|
+
## Git Configuration
|
|
525
|
+
|
|
526
|
+
It is recommended to ignore the files generated by Intlayer. This allows you to avoid committing them to your Git repository.
|
|
527
|
+
|
|
528
|
+
To do this, you can add the following instructions to your `.gitignore` file:
|
|
529
|
+
|
|
530
|
+
```plaintext fileName=".gitignore"
|
|
531
|
+
# Ignore the files generated by Intlayer
|
|
532
|
+
.intlayer
|
|
533
|
+
```
|
|
534
|
+
|
|
535
|
+
---
|
|
536
|
+
|
|
537
|
+
## VS Code Extension
|
|
538
|
+
|
|
539
|
+
To improve your development experience with Intlayer, you can install the official **Intlayer VS Code Extension**.
|
|
540
|
+
|
|
541
|
+
[Install from the VS Code Marketplace](https://marketplace.visualstudio.com/items?itemName=intlayer.intlayer-vs-code-extension)
|
|
542
|
+
|
|
543
|
+
This extension provides:
|
|
544
|
+
|
|
545
|
+
- **Autocompletion** for translation keys.
|
|
546
|
+
- **Real-time error detection** for missing translations.
|
|
547
|
+
- **Inline previews** of translated content.
|
|
548
|
+
- **Quick actions** to easily create and update translations.
|
|
549
|
+
|
|
550
|
+
For more details on how to use the extension, refer to the [Intlayer VS Code Extension documentation](https://intlayer.org/doc/vs-code-extension).
|
|
551
|
+
|
|
552
|
+
---
|
|
553
|
+
|
|
554
|
+
## Go Further
|
|
555
|
+
|
|
556
|
+
To go further, you can implement the [visual editor](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/intlayer_visual_editor.md) or externalize your content using the [CMS](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/intlayer_CMS.md).
|
|
557
|
+
|
|
558
|
+
---
|
|
559
|
+
|
|
560
|
+
## Documentation References
|
|
561
|
+
|
|
562
|
+
- [Intlayer Documentation](https://intlayer.org)
|
|
563
|
+
- [React Router v7 Documentation](https://reactrouter.com/)
|
|
564
|
+
- [React Router fs-routes Documentation](https://reactrouter.com/how-to/file-route-conventions)
|
|
565
|
+
- [useIntlayer hook](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/packages/react-intlayer/useIntlayer.md)
|
|
566
|
+
- [useLocale hook](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/packages/react-intlayer/useLocale.md)
|
|
567
|
+
- [Content Declaration](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/dictionary/content_file.md)
|
|
568
|
+
- [Configuration](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/configuration.md)
|
|
569
|
+
|
|
570
|
+
This comprehensive guide provides everything you need to integrate Intlayer with React Router v7 using file-system based routing for a fully internationalized application with locale-aware routing and TypeScript support.
|
|
@@ -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
|
|
@@ -597,19 +598,8 @@ export const getLocaleServer = createServerFn().handler(async () => {
|
|
|
597
598
|
return getCookie(name, cookieString);
|
|
598
599
|
},
|
|
599
600
|
// Get the header from the request (default: 'x-intlayer-locale')
|
|
600
|
-
getHeader: (name) => getRequestHeader(name),
|
|
601
601
|
// Fallback using Accept-Language negotiation
|
|
602
|
-
|
|
603
|
-
const headers = getRequestHeaders();
|
|
604
|
-
const result: Record<string, string> = {};
|
|
605
|
-
|
|
606
|
-
// Convert the TypedHeaders into a plain Record<string, string>
|
|
607
|
-
for (const [key, value] of headers.entries()) {
|
|
608
|
-
result[key] = value;
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
return result;
|
|
612
|
-
},
|
|
602
|
+
getHeader: (name) => getRequestHeader(name),
|
|
613
603
|
});
|
|
614
604
|
|
|
615
605
|
// Retrieve some content using getIntlayer()
|