@intlayer/docs 7.3.10 → 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
|
This guide demonstrates how to integrate **Intlayer** for seamless internationalisation in React Router v7 projects with locale-aware routing, TypeScript support, and modern development practices.
|
|
33
34
|
|
|
35
|
+
## Table of Contents
|
|
36
|
+
|
|
37
|
+
<TOC/>
|
|
38
|
+
|
|
34
39
|
## What is Intlayer?
|
|
35
40
|
|
|
36
41
|
**Intlayer** is an innovative, open-source internationalisation (i18n) library designed to simplify multilingual support in modern web applications.
|
|
@@ -73,6 +78,29 @@ The core package that provides internationalisation tools for configuration mana
|
|
|
73
78
|
|
|
74
79
|
### Step 2: Configuration of your project
|
|
75
80
|
|
|
81
|
+
## Step-by-Step Guide to Set Up Intlayer in a React Router v7 Application with File-System Routes
|
|
82
|
+
|
|
83
|
+
<Tab defaultTab="video">
|
|
84
|
+
<TabItem label="Video" value="video">
|
|
85
|
+
|
|
86
|
+
<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"/>
|
|
87
|
+
|
|
88
|
+
</TabItem>
|
|
89
|
+
<TabItem label="Code" value="code">
|
|
90
|
+
|
|
91
|
+
<iframe
|
|
92
|
+
src="https://stackblitz.com/github/aymericzip/intlayer-react-router-v7-template?embed=1&ctl=1&file=intlayer.config.ts"
|
|
93
|
+
className="m-auto overflow-hidden rounded-lg border-0 max-md:size-full max-md:h-[700px] md:aspect-16/9 md:w-full"
|
|
94
|
+
title="Demo CodeSandbox - How to Internationalize your application using Intlayer"
|
|
95
|
+
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
|
|
96
|
+
loading="lazy"
|
|
97
|
+
/>
|
|
98
|
+
|
|
99
|
+
</TabItem>
|
|
100
|
+
</Tab>
|
|
101
|
+
|
|
102
|
+
See [Application Template](https://github.com/aymericzip/intlayer-react-router-v7-template) on GitHub.
|
|
103
|
+
|
|
76
104
|
Create a config file to configure the languages of your application:
|
|
77
105
|
|
|
78
106
|
```typescript fileName="intlayer.config.ts" codeFormat="typescript"
|
|
@@ -255,7 +283,7 @@ export const LocalizedLink: FC<LinkProps> = (props) => {
|
|
|
255
283
|
If you want to navigate to the localised routes, you can use the `useLocalizedNavigate` hook:
|
|
256
284
|
|
|
257
285
|
```tsx fileName="app/hooks/useLocalizedNavigate.ts"
|
|
258
|
-
import { useLocale } from "intlayer";
|
|
286
|
+
import { useLocale } from "react-intlayer";
|
|
259
287
|
import { type NavigateOptions, type To, useNavigate } from "react-router";
|
|
260
288
|
|
|
261
289
|
import { locacalizeTo } from "~/components/localized-link";
|
|
@@ -313,6 +341,7 @@ import {
|
|
|
313
341
|
getLocaleName,
|
|
314
342
|
getLocalizedUrl,
|
|
315
343
|
getPathWithoutLocale,
|
|
344
|
+
Locales,
|
|
316
345
|
} from "intlayer";
|
|
317
346
|
import { setLocaleInStorage, useIntlayer, useLocale } from "react-intlayer";
|
|
318
347
|
import { Link, useLocation } from "react-router";
|
|
@@ -406,13 +435,13 @@ You can also use the `intlayerProxy` to add server-side routing to your applicat
|
|
|
406
435
|
> Note that to use the `intlayerProxy` in production, you need to switch the `vite-intlayer` package from `devDependencies` to `dependencies`.
|
|
407
436
|
|
|
408
437
|
```typescript {3,7} fileName="vite.config.ts"
|
|
438
|
+
import { reactRouter } from "@react-router/dev/vite";
|
|
409
439
|
import { defineConfig } from "vite";
|
|
410
|
-
import react from "@vitejs/plugin-react-swc";
|
|
411
440
|
import { intlayer, intlayerProxy } from "vite-intlayer";
|
|
441
|
+
import tsconfigPaths from "vite-tsconfig-paths";
|
|
412
442
|
|
|
413
|
-
// https://vitejs.dev/config/
|
|
414
443
|
export default defineConfig({
|
|
415
|
-
plugins: [
|
|
444
|
+
plugins: [reactRouter(), tsconfigPaths(), intlayer(), intlayerProxy()],
|
|
416
445
|
});
|
|
417
446
|
```
|
|
418
447
|
|
|
@@ -0,0 +1,513 @@
|
|
|
1
|
+
---
|
|
2
|
+
createdAt: 2025-09-04
|
|
3
|
+
updatedAt: 2025-10-03
|
|
4
|
+
title: How to translate your React Router v7 app – i18n guide 2025
|
|
5
|
+
description: Learn how to add internationalisation (i18n) to your React Router v7 application using Intlayer. Follow this comprehensive guide to make your app multilingual with locale-aware routing.
|
|
6
|
+
keywords:
|
|
7
|
+
- Internationalisation
|
|
8
|
+
- Documentation
|
|
9
|
+
- Intlayer
|
|
10
|
+
- React Router v7
|
|
11
|
+
- React
|
|
12
|
+
- i18n
|
|
13
|
+
- TypeScript
|
|
14
|
+
- Locale Routing
|
|
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: Updated doc
|
|
26
|
+
- version: 5.8.2
|
|
27
|
+
date: 2025-09-04
|
|
28
|
+
changes: Added for React Router v7
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
# Getting Started Internationalising (i18n) with Intlayer and React Router v7
|
|
32
|
+
|
|
33
|
+
This guide demonstrates how to integrate **Intlayer** for seamless internationalisation in React Router v7 projects with locale-aware routing, TypeScript support, and modern development practices.
|
|
34
|
+
|
|
35
|
+
## Table of Contents
|
|
36
|
+
|
|
37
|
+
<TOC/>
|
|
38
|
+
|
|
39
|
+
## What is Intlayer?
|
|
40
|
+
|
|
41
|
+
**Intlayer** is an innovative, open-source internationalisation (i18n) library designed to simplify multilingual support in modern web applications.
|
|
42
|
+
|
|
43
|
+
With Intlayer, you can:
|
|
44
|
+
|
|
45
|
+
- **Easily manage translations** using declarative dictionaries at the component level.
|
|
46
|
+
- **Dynamically localise metadata**, routes, and content.
|
|
47
|
+
- **Ensure TypeScript support** with autogenerated types, improving autocompletion and error detection.
|
|
48
|
+
- **Benefit from advanced features**, like dynamic locale detection and switching.
|
|
49
|
+
- **Enable locale-aware routing** with React Router v7's configuration-based routing system.
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## Step-by-Step Guide to Set Up Intlayer in a React Router v7 Application
|
|
54
|
+
|
|
55
|
+
### Step 1: Install Dependencies
|
|
56
|
+
|
|
57
|
+
Install the necessary packages using your preferred package manager:
|
|
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
|
+
The core package that provides internationalisation tools for configuration management, translation, [content declaration](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en-GB/dictionary/get_started.md), transpilation, and [CLI commands](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en-GB/intlayer_cli.md).
|
|
72
|
+
|
|
73
|
+
- **react-intlayer**
|
|
74
|
+
The package that integrates Intlayer with a React application. It provides context providers and hooks for React internationalisation.
|
|
75
|
+
|
|
76
|
+
- **vite-intlayer**
|
|
77
|
+
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.
|
|
78
|
+
|
|
79
|
+
### Step 2: Configuration of your project
|
|
80
|
+
|
|
81
|
+
## Step-by-Step Guide to Set Up Intlayer in a React Router v7 Application with File-System Routes
|
|
82
|
+
|
|
83
|
+
<Tab defaultTab="video">
|
|
84
|
+
<TabItem label="Video" value="video">
|
|
85
|
+
|
|
86
|
+
<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"/>
|
|
87
|
+
|
|
88
|
+
</TabItem>
|
|
89
|
+
<TabItem label="Code" value="code">
|
|
90
|
+
|
|
91
|
+
<iframe
|
|
92
|
+
src="https://stackblitz.com/github/aymericzip/intlayer-react-router-v7-template?embed=1&ctl=1&file=intlayer.config.ts"
|
|
93
|
+
className="m-auto overflow-hidden rounded-lg border-0 max-md:size-full max-md:h-[700px] md:aspect-16/9 md:w-full"
|
|
94
|
+
title="Demo CodeSandbox - How to Internationalize your application using Intlayer"
|
|
95
|
+
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
|
|
96
|
+
loading="lazy"
|
|
97
|
+
/>
|
|
98
|
+
|
|
99
|
+
</TabItem>
|
|
100
|
+
</Tab>
|
|
101
|
+
|
|
102
|
+
See [Application Template](https://github.com/aymericzip/intlayer-react-router-v7-template) on GitHub.
|
|
103
|
+
|
|
104
|
+
Create a config file to configure the languages of your application:
|
|
105
|
+
|
|
106
|
+
```typescript fileName="intlayer.config.ts" codeFormat="typescript"
|
|
107
|
+
import { type IntlayerConfig, Locales } from "intlayer";
|
|
108
|
+
|
|
109
|
+
const config: IntlayerConfig = {
|
|
110
|
+
internationalization: {
|
|
111
|
+
defaultLocale: Locales.ENGLISH,
|
|
112
|
+
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export default config;
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
```javascript fileName="intlayer.config.mjs" codeFormat="esm"
|
|
120
|
+
import { Locales } from "intlayer";
|
|
121
|
+
|
|
122
|
+
/** @type {import('intlayer').IntlayerConfig} */
|
|
123
|
+
const config = {
|
|
124
|
+
internationalization: {
|
|
125
|
+
defaultLocale: Locales.ENGLISH,
|
|
126
|
+
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
export default config;
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
```javascript fileName="intlayer.config.cjs" codeFormat="commonjs"
|
|
134
|
+
const { Locales } = require("intlayer");
|
|
135
|
+
|
|
136
|
+
/** @type {import('intlayer').IntlayerConfig} */
|
|
137
|
+
const config = {
|
|
138
|
+
internationalization: {
|
|
139
|
+
defaultLocale: Locales.ENGLISH,
|
|
140
|
+
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
module.exports = config;
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
> Through this configuration file, you can set up localised 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-GB/configuration.md).
|
|
148
|
+
|
|
149
|
+
### Step 3: Integrate Intlayer in Your Vite Configuration
|
|
150
|
+
|
|
151
|
+
Add the intlayer plugin into your configuration:
|
|
152
|
+
|
|
153
|
+
```typescript fileName="vite.config.ts"
|
|
154
|
+
import { reactRouter } from "@react-router/dev/vite";
|
|
155
|
+
import { defineConfig } from "vite";
|
|
156
|
+
import { intlayer } from "vite-intlayer";
|
|
157
|
+
import tsconfigPaths from "vite-tsconfig-paths";
|
|
158
|
+
|
|
159
|
+
export default defineConfig({
|
|
160
|
+
plugins: [reactRouter(), tsconfigPaths(), intlayer()],
|
|
161
|
+
});
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
> 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 optimise performance.
|
|
165
|
+
|
|
166
|
+
### Step 4: Configure React Router v7 Routes
|
|
167
|
+
|
|
168
|
+
Set up your routing configuration with locale-aware routes:
|
|
169
|
+
|
|
170
|
+
```typescript fileName="app/routes.ts"
|
|
171
|
+
import { layout, route, type RouteConfig } from "@react-router/dev/routes";
|
|
172
|
+
|
|
173
|
+
export default [
|
|
174
|
+
layout("routes/layout.tsx", [
|
|
175
|
+
route("/:lang?", "routes/page.tsx"), // Localised home page
|
|
176
|
+
route("/:lang?/about", "routes/about/page.tsx"), // Localised about page
|
|
177
|
+
]),
|
|
178
|
+
] satisfies RouteConfig;
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### Step 5: Create Layout Components
|
|
182
|
+
|
|
183
|
+
Set up your root layout and locale-specific layouts:
|
|
184
|
+
|
|
185
|
+
#### Root Layout
|
|
186
|
+
|
|
187
|
+
```tsx fileName="app/routes/layout.tsx"
|
|
188
|
+
import { IntlayerProvider } from "react-intlayer";
|
|
189
|
+
import { Outlet } from "react-router";
|
|
190
|
+
|
|
191
|
+
import type { Route } from "./+types/layout";
|
|
192
|
+
|
|
193
|
+
export default function RootLayout({ params }: Route.ComponentProps) {
|
|
194
|
+
const { locale } = params;
|
|
195
|
+
|
|
196
|
+
return (
|
|
197
|
+
<IntlayerProvider locale={locale}>
|
|
198
|
+
<Outlet />
|
|
199
|
+
</IntlayerProvider>
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### Step 6: Declare Your Content
|
|
205
|
+
|
|
206
|
+
Create and manage your content declarations to store translations:
|
|
207
|
+
|
|
208
|
+
```tsx fileName="app/routes/[lang]/page.content.ts"
|
|
209
|
+
import { t, type Dictionary } from "intlayer";
|
|
210
|
+
|
|
211
|
+
const pageContent = {
|
|
212
|
+
key: "page",
|
|
213
|
+
content: {
|
|
214
|
+
title: t({
|
|
215
|
+
en: "Welcome to React Router v7 + Intlayer",
|
|
216
|
+
es: "Bienvenido a React Router v7 + Intlayer",
|
|
217
|
+
fr: "Bienvenue sur React Router v7 + Intlayer",
|
|
218
|
+
}),
|
|
219
|
+
description: t({
|
|
220
|
+
en: "Build multilingual applications with ease using React Router v7 and Intlayer.",
|
|
221
|
+
es: "Cree aplicaciones multilingües fácilmente usando React Router v7 y Intlayer.",
|
|
222
|
+
fr: "Créez des applications multilingues facilement avec React Router v7 et Intlayer.",
|
|
223
|
+
}),
|
|
224
|
+
aboutLink: t({
|
|
225
|
+
en: "Learn About Us",
|
|
226
|
+
es: "Aprender Sobre Nosotros",
|
|
227
|
+
fr: "En savoir plus sur nous",
|
|
228
|
+
}),
|
|
229
|
+
homeLink: t({
|
|
230
|
+
en: "Home",
|
|
231
|
+
es: "Inicio",
|
|
232
|
+
fr: "Accueil",
|
|
233
|
+
}),
|
|
234
|
+
},
|
|
235
|
+
} satisfies Dictionary;
|
|
236
|
+
|
|
237
|
+
export default pageContent;
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
> Your content declarations can be defined anywhere in your application as soon as they are included in 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}`).
|
|
241
|
+
|
|
242
|
+
> For more details, refer to the [content declaration documentation](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en-GB/dictionary/get_started.md).
|
|
243
|
+
|
|
244
|
+
### Step 7: Create Locale-Aware Components
|
|
245
|
+
|
|
246
|
+
Create a `LocalizedLink` component for locale-aware navigation:
|
|
247
|
+
|
|
248
|
+
```tsx fileName="app/components/localized-link.tsx"
|
|
249
|
+
import type { FC } from "react";
|
|
250
|
+
|
|
251
|
+
import { getLocalizedUrl, type LocalesValues } from "intlayer";
|
|
252
|
+
import { useLocale } from "react-intlayer";
|
|
253
|
+
import { Link, type LinkProps, type To } from "react-router";
|
|
254
|
+
|
|
255
|
+
const isExternalLink = (to: string) => /^(https?:)?\/\//.test(to);
|
|
256
|
+
|
|
257
|
+
export const locacalizeTo = (to: To, locale: LocalesValues): To => {
|
|
258
|
+
if (typeof to === "string") {
|
|
259
|
+
if (isExternalLink(to)) {
|
|
260
|
+
return to;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return getLocalizedUrl(to, locale);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if (isExternalLink(to.pathname ?? "")) {
|
|
267
|
+
return to;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return {
|
|
271
|
+
...to,
|
|
272
|
+
pathname: getLocalizedUrl(to.pathname ?? "", locale),
|
|
273
|
+
};
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
export const LocalizedLink: FC<LinkProps> = (props) => {
|
|
277
|
+
const { locale } = useLocale();
|
|
278
|
+
|
|
279
|
+
return <Link {...props} to={locacalizeTo(props.to, locale)} />;
|
|
280
|
+
};
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
If you want to navigate to the localised routes, you can use the `useLocalizedNavigate` hook:
|
|
284
|
+
|
|
285
|
+
```tsx fileName="app/hooks/useLocalizedNavigate.ts"
|
|
286
|
+
import { useLocale } from "react-intlayer";
|
|
287
|
+
import { type NavigateOptions, type To, useNavigate } from "react-router";
|
|
288
|
+
|
|
289
|
+
import { locacalizeTo } from "~/components/localized-link";
|
|
290
|
+
|
|
291
|
+
export const useLocalizedNavigate = () => {
|
|
292
|
+
const navigate = useNavigate();
|
|
293
|
+
const { locale } = useLocale();
|
|
294
|
+
|
|
295
|
+
const localisedNavigate = (to: To, options?: NavigateOptions) => {
|
|
296
|
+
const localedTo = locacalizeTo(to, locale);
|
|
297
|
+
|
|
298
|
+
navigate(localedTo, options);
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
return localisedNavigate;
|
|
302
|
+
};
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
### Step 8: Utilise Intlayer in Your Pages
|
|
306
|
+
|
|
307
|
+
Access your content dictionaries throughout your application:
|
|
308
|
+
|
|
309
|
+
#### Localised Home Page
|
|
310
|
+
|
|
311
|
+
```tsx fileName="app/routes/[lang]/page.tsx"
|
|
312
|
+
import { useIntlayer } from "intlayer";
|
|
313
|
+
import { LocalizedLink } from "~/components/localized-link";
|
|
314
|
+
|
|
315
|
+
export default function Page() {
|
|
316
|
+
const { title, description, aboutLink } = useIntlayer("page");
|
|
317
|
+
|
|
318
|
+
return (
|
|
319
|
+
<div>
|
|
320
|
+
<h1>{title}</h1>
|
|
321
|
+
<p>{description}</p>
|
|
322
|
+
<nav>
|
|
323
|
+
<LocalizedLink to="/about">{aboutLink}</LocalizedLink>
|
|
324
|
+
</nav>
|
|
325
|
+
</div>
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
> To learn more about the `useIntlayer` hook, refer to the [documentation](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en-GB/packages/react-intlayer/useIntlayer.md).
|
|
331
|
+
|
|
332
|
+
### Step 9: Create a Locale Switcher Component
|
|
333
|
+
|
|
334
|
+
Create a component to allow users to change languages:
|
|
335
|
+
|
|
336
|
+
```tsx fileName="app/components/locale-switcher.tsx"
|
|
337
|
+
import type { FC } from "react";
|
|
338
|
+
|
|
339
|
+
import {
|
|
340
|
+
getHTMLTextDir,
|
|
341
|
+
getLocaleName,
|
|
342
|
+
getLocalizedUrl,
|
|
343
|
+
getPathWithoutLocale,
|
|
344
|
+
Locales,
|
|
345
|
+
} from "intlayer";
|
|
346
|
+
import { setLocaleInStorage, useIntlayer, useLocale } from "react-intlayer";
|
|
347
|
+
import { Link, useLocation } from "react-router";
|
|
348
|
+
|
|
349
|
+
export const LocaleSwitcher: FC = () => {
|
|
350
|
+
const { localeSwitcherLabel } = useIntlayer("locale-switcher");
|
|
351
|
+
const { pathname } = useLocation();
|
|
352
|
+
|
|
353
|
+
const { availableLocales, locale } = useLocale();
|
|
354
|
+
|
|
355
|
+
const pathWithoutLocale = getPathWithoutLocale(pathname);
|
|
356
|
+
|
|
357
|
+
return (
|
|
358
|
+
<ol>
|
|
359
|
+
{availableLocales.map((localeItem) => (
|
|
360
|
+
<li key={localeItem}>
|
|
361
|
+
<Link
|
|
362
|
+
aria-current={localeItem === locale ? "page" : undefined}
|
|
363
|
+
aria-label={`${localeSwitcherLabel.value} ${getLocaleName(localeItem)}`}
|
|
364
|
+
onClick={() => setLocale(localeItem)}
|
|
365
|
+
to={getLocalizedUrl(pathWithoutLocale, localeItem)}
|
|
366
|
+
>
|
|
367
|
+
<span>
|
|
368
|
+
{/* Locale - e.g. FR */}
|
|
369
|
+
{localeItem}
|
|
370
|
+
</span>
|
|
371
|
+
<span>
|
|
372
|
+
{/* Language in its own Locale - e.g. Français */}
|
|
373
|
+
{getLocaleName(localeItem, locale)}
|
|
374
|
+
</span>
|
|
375
|
+
<span dir={getHTMLTextDir(localeItem)} lang={localeItem}>
|
|
376
|
+
{/* Language in current Locale - e.g. Francés with current locale set to Locales.SPANISH */}
|
|
377
|
+
{getLocaleName(localeItem)}
|
|
378
|
+
</span>
|
|
379
|
+
<span dir="ltr" lang={Locales.ENGLISH}>
|
|
380
|
+
{/* Language in English - e.g. French */}
|
|
381
|
+
{getLocaleName(localeItem, Locales.ENGLISH)}
|
|
382
|
+
</span>
|
|
383
|
+
</Link>
|
|
384
|
+
</li>
|
|
385
|
+
))}
|
|
386
|
+
</ol>
|
|
387
|
+
);
|
|
388
|
+
};
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
> To learn more about the `useLocale` hook, refer to the [documentation](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en-GB/packages/react-intlayer/useLocale.md).
|
|
392
|
+
|
|
393
|
+
### Step 10: Add HTML Attributes Management (Optional)
|
|
394
|
+
|
|
395
|
+
Create a hook to manage HTML lang and dir attributes:
|
|
396
|
+
|
|
397
|
+
```tsx fileName="app/hooks/useI18nHTMLAttributes.tsx"
|
|
398
|
+
import { getHTMLTextDir } from "intlayer";
|
|
399
|
+
import { useEffect } from "react";
|
|
400
|
+
import { useLocale } from "react-intlayer";
|
|
401
|
+
|
|
402
|
+
export const useI18nHTMLAttributes = () => {
|
|
403
|
+
const { locale } = useLocale();
|
|
404
|
+
|
|
405
|
+
useEffect(() => {
|
|
406
|
+
document.documentElement.lang = locale;
|
|
407
|
+
document.documentElement.dir = getHTMLTextDir(locale);
|
|
408
|
+
}, [locale]);
|
|
409
|
+
};
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
Then use it in your root component:
|
|
413
|
+
|
|
414
|
+
```tsx fileName="app/routes/layout.tsx"
|
|
415
|
+
import { Outlet } from "react-router";
|
|
416
|
+
import { IntlayerProvider } from "react-intlayer";
|
|
417
|
+
|
|
418
|
+
import { useI18nHTMLAttributes } from "app/hooks/useI18nHTMLAttributes"; // import the hook
|
|
419
|
+
|
|
420
|
+
export default function RootLayout() {
|
|
421
|
+
useI18nHTMLAttributes(); // call the hook
|
|
422
|
+
|
|
423
|
+
return (
|
|
424
|
+
<IntlayerProvider>
|
|
425
|
+
<Outlet />
|
|
426
|
+
</IntlayerProvider>
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
### Step 11: Add middleware (Optional)
|
|
432
|
+
|
|
433
|
+
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.
|
|
434
|
+
|
|
435
|
+
> Note that to use the `intlayerProxy` in production, you need to switch the `vite-intlayer` package from `devDependencies` to `dependencies`.
|
|
436
|
+
|
|
437
|
+
```typescript {3,7} fileName="vite.config.ts"
|
|
438
|
+
import { reactRouter } from "@react-router/dev/vite";
|
|
439
|
+
import { defineConfig } from "vite";
|
|
440
|
+
import { intlayer, intlayerProxy } from "vite-intlayer";
|
|
441
|
+
import tsconfigPaths from "vite-tsconfig-paths";
|
|
442
|
+
|
|
443
|
+
export default defineConfig({
|
|
444
|
+
plugins: [reactRouter(), tsconfigPaths(), intlayer(), intlayerProxy()],
|
|
445
|
+
});
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
---
|
|
449
|
+
|
|
450
|
+
## Configure TypeScript
|
|
451
|
+
|
|
452
|
+
Intlayer uses module augmentation to benefit from TypeScript and strengthen your codebase.
|
|
453
|
+
|
|
454
|
+
Ensure your TypeScript configuration includes the autogenerated types:
|
|
455
|
+
|
|
456
|
+
```json5 fileName="tsconfig.json"
|
|
457
|
+
{
|
|
458
|
+
// ... your existing configurations
|
|
459
|
+
include: [
|
|
460
|
+
// ... your existing includes
|
|
461
|
+
".intlayer/**/*.ts", // Include the auto-generated types
|
|
462
|
+
],
|
|
463
|
+
}
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
---
|
|
467
|
+
|
|
468
|
+
## Git Configuration
|
|
469
|
+
|
|
470
|
+
It is recommended to ignore the files generated by Intlayer. This prevents you from committing them to your Git repository.
|
|
471
|
+
|
|
472
|
+
To do this, you can add the following instructions to your `.gitignore` file:
|
|
473
|
+
|
|
474
|
+
```plaintext fileName=".gitignore"
|
|
475
|
+
# Ignore the files generated by Intlayer
|
|
476
|
+
.intlayer
|
|
477
|
+
```
|
|
478
|
+
|
|
479
|
+
---
|
|
480
|
+
|
|
481
|
+
## VS Code Extension
|
|
482
|
+
|
|
483
|
+
To enhance your development experience with Intlayer, you can install the official **Intlayer VS Code Extension**.
|
|
484
|
+
|
|
485
|
+
[Install from the VS Code Marketplace](https://marketplace.visualstudio.com/items?itemName=intlayer.intlayer-vs-code-extension)
|
|
486
|
+
|
|
487
|
+
This extension provides:
|
|
488
|
+
|
|
489
|
+
- **Autocompletion** for translation keys.
|
|
490
|
+
- **Real-time error detection** for missing translations.
|
|
491
|
+
- **Inline previews** of translated content.
|
|
492
|
+
- **Quick actions** to easily create and update translations.
|
|
493
|
+
|
|
494
|
+
For more details on how to use the extension, refer to the [Intlayer VS Code Extension documentation](https://intlayer.org/doc/vs-code-extension).
|
|
495
|
+
|
|
496
|
+
---
|
|
497
|
+
|
|
498
|
+
## Go Further
|
|
499
|
+
|
|
500
|
+
To go further, you can implement the [visual editor](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en-GB/intlayer_visual_editor.md) or externalise your content using the [CMS](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en-GB/intlayer_CMS.md).
|
|
501
|
+
|
|
502
|
+
---
|
|
503
|
+
|
|
504
|
+
## Documentation References
|
|
505
|
+
|
|
506
|
+
- [Intlayer Documentation](https://intlayer.org)
|
|
507
|
+
- [React Router v7 Documentation](https://reactrouter.com/)
|
|
508
|
+
- [useIntlayer hook](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en-GB/packages/react-intlayer/useIntlayer.md)
|
|
509
|
+
- [useLocale hook](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en-GB/packages/react-intlayer/useLocale.md)
|
|
510
|
+
- [Content Declaration](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en-GB/dictionary/get_started.md)
|
|
511
|
+
- [Configuration](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en-GB/configuration.md)
|
|
512
|
+
|
|
513
|
+
This comprehensive guide provides everything you need to integrate Intlayer with React Router v7 for a fully internationalised 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
|
|
@@ -588,19 +589,8 @@ export const getLocaleServer = createServerFn().handler(async () => {
|
|
|
588
589
|
return getCookie(name, cookieString);
|
|
589
590
|
},
|
|
590
591
|
// Get the header from the request (default: 'x-intlayer-locale')
|
|
591
|
-
getHeader: (name) => getRequestHeader(name),
|
|
592
592
|
// Fallback using Accept-Language negotiation
|
|
593
|
-
|
|
594
|
-
const headers = getRequestHeaders();
|
|
595
|
-
const result: Record<string, string> = {};
|
|
596
|
-
|
|
597
|
-
// Convert the TypedHeaders into a plain Record<string, string>
|
|
598
|
-
for (const [key, value] of headers.entries()) {
|
|
599
|
-
result[key] = value;
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
return result;
|
|
603
|
-
},
|
|
593
|
+
getHeader: (name) => getRequestHeader(name),
|
|
604
594
|
});
|
|
605
595
|
|
|
606
596
|
// Retrieve some content using getIntlayer()
|