@intlayer/docs 7.0.9-canary.2 → 7.1.0-canary.0
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/en/packages/intlayer/getLocalizedUrl.md +116 -97
- package/docs/en/packages/intlayer/getMultilingualUrls.md +194 -54
- package/docs/en/packages/intlayer/getPrefix.md +201 -0
- package/package.json +14 -14
- package/src/generated/docs.entry.ts +19 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
createdAt: 2025-08-23
|
|
3
|
-
updatedAt: 2025-
|
|
3
|
+
updatedAt: 2025-11-16
|
|
4
4
|
title: getMultilingualUrls Function Documentation | intlayer
|
|
5
5
|
description: See how to use the getMultilingualUrls function for intlayer package
|
|
6
6
|
keywords:
|
|
@@ -19,6 +19,9 @@ slugs:
|
|
|
19
19
|
- intlayer
|
|
20
20
|
- getMultilingualUrls
|
|
21
21
|
history:
|
|
22
|
+
- version: 7.1.0
|
|
23
|
+
date: 2025-11-16
|
|
24
|
+
changes: Refactored to use options parameter with mode instead of prefixDefault
|
|
22
25
|
- version: 5.5.10
|
|
23
26
|
date: 2025-06-29
|
|
24
27
|
changes: Init history
|
|
@@ -30,49 +33,120 @@ history:
|
|
|
30
33
|
|
|
31
34
|
The `getMultilingualUrls` function generates a mapping of multilingual URLs by prefixing the given URL with each supported locale. It can handle both absolute and relative URLs, applying the appropriate locale prefix based on the provided configuration or defaults.
|
|
32
35
|
|
|
36
|
+
**Key Features:**
|
|
37
|
+
|
|
38
|
+
- Only 1 parameter is required: `url`
|
|
39
|
+
- Optional `options` object with `locales`, `defaultLocale`, and `mode`
|
|
40
|
+
- Uses your project's internationalization configuration as defaults
|
|
41
|
+
- Supports multiple routing modes: `prefix-no-default`, `prefix-all`, `no-prefix`, and `search-params`
|
|
42
|
+
- Returns a mapping object with all locales as keys and their corresponding URLs as values
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
## Function Signature
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
getMultilingualUrls(
|
|
50
|
+
url: string, // Required
|
|
51
|
+
options?: { // Optional
|
|
52
|
+
locales?: Locales[];
|
|
53
|
+
defaultLocale?: Locales;
|
|
54
|
+
mode?: 'prefix-no-default' | 'prefix-all' | 'no-prefix' | 'search-params';
|
|
55
|
+
}
|
|
56
|
+
): StrictModeLocaleMap<string>
|
|
57
|
+
```
|
|
58
|
+
|
|
33
59
|
---
|
|
34
60
|
|
|
35
61
|
## Parameters
|
|
36
62
|
|
|
63
|
+
### Required Parameters
|
|
64
|
+
|
|
37
65
|
- `url: string`
|
|
38
66
|
- **Description**: The original URL string to be prefixed with locales.
|
|
39
67
|
- **Type**: `string`
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
-
|
|
47
|
-
- **
|
|
48
|
-
|
|
49
|
-
-
|
|
50
|
-
|
|
51
|
-
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
-
|
|
68
|
+
- **Required**: Yes
|
|
69
|
+
|
|
70
|
+
### Optional Parameters
|
|
71
|
+
|
|
72
|
+
- `options?: object`
|
|
73
|
+
- **Description**: Configuration object for URL localization behavior.
|
|
74
|
+
- **Type**: `object`
|
|
75
|
+
- **Required**: No (Optional)
|
|
76
|
+
|
|
77
|
+
- `options.locales?: Locales[]`
|
|
78
|
+
- **Description**: Array of supported locales. If not provided, uses the configured locales from your project configuration.
|
|
79
|
+
- **Type**: `Locales[]`
|
|
80
|
+
- **Default**: [`Project Configuration`](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/configuration.md#middleware)
|
|
81
|
+
|
|
82
|
+
- `options.defaultLocale?: Locales`
|
|
83
|
+
- **Description**: The default locale for the application. If not provided, uses the configured default locale from your project configuration.
|
|
84
|
+
- **Type**: `Locales`
|
|
85
|
+
- **Default**: [`Project Configuration`](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/configuration.md#middleware)
|
|
86
|
+
|
|
87
|
+
- `options.mode?: 'prefix-no-default' | 'prefix-all' | 'no-prefix' | 'search-params'`
|
|
88
|
+
- **Description**: The URL routing mode for locale handling. If not provided, uses the configured mode from your project configuration.
|
|
89
|
+
- **Type**: `'prefix-no-default' | 'prefix-all' | 'no-prefix' | 'search-params'`
|
|
90
|
+
- **Default**: [`Project Configuration`](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/configuration.md#middleware)
|
|
91
|
+
- **Modes**:
|
|
92
|
+
- `prefix-no-default`: No prefix for default locale, prefix for all others
|
|
93
|
+
- `prefix-all`: Prefix for all locales including default
|
|
94
|
+
- `no-prefix`: No locale prefix in URL
|
|
95
|
+
- `search-params`: Use query parameters for locale (e.g., `?locale=fr`)
|
|
55
96
|
|
|
56
97
|
### Returns
|
|
57
98
|
|
|
58
|
-
- **Type**: `
|
|
99
|
+
- **Type**: `StrictModeLocaleMap<string>`
|
|
59
100
|
- **Description**: An object mapping each locale to its corresponding multilingual URL.
|
|
60
101
|
|
|
61
102
|
---
|
|
62
103
|
|
|
63
104
|
## Example Usage
|
|
64
105
|
|
|
65
|
-
###
|
|
106
|
+
### Basic Usage (Uses Project Configuration)
|
|
66
107
|
|
|
67
108
|
```typescript codeFormat="typescript"
|
|
68
109
|
import { getMultilingualUrls, Locales } from "intlayer";
|
|
69
110
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
111
|
+
// Uses your project's configuration for locales, defaultLocale, and mode
|
|
112
|
+
getMultilingualUrls("/dashboard");
|
|
113
|
+
// Output (assuming project config has en, fr with mode 'prefix-no-default'):
|
|
114
|
+
// {
|
|
115
|
+
// en: "/dashboard",
|
|
116
|
+
// fr: "/fr/dashboard"
|
|
117
|
+
// }
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
```javascript codeFormat="esm"
|
|
121
|
+
import { getMultilingualUrls, Locales } from "intlayer";
|
|
122
|
+
|
|
123
|
+
getMultilingualUrls("/dashboard");
|
|
124
|
+
// Output: {
|
|
125
|
+
// en: "/dashboard",
|
|
126
|
+
// fr: "/fr/dashboard"
|
|
127
|
+
// }
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
```javascript codeFormat="commonjs"
|
|
131
|
+
const { getMultilingualUrls, Locales } = require("intlayer");
|
|
132
|
+
|
|
133
|
+
getMultilingualUrls("/dashboard");
|
|
134
|
+
// Output: {
|
|
135
|
+
// en: "/dashboard",
|
|
136
|
+
// fr: "/fr/dashboard"
|
|
137
|
+
// }
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Relative URLs with Options
|
|
141
|
+
|
|
142
|
+
```typescript codeFormat="typescript"
|
|
143
|
+
import { getMultilingualUrls, Locales } from "intlayer";
|
|
144
|
+
|
|
145
|
+
getMultilingualUrls("/dashboard", {
|
|
146
|
+
locales: [Locales.ENGLISH, Locales.FRENCH],
|
|
147
|
+
defaultLocale: Locales.ENGLISH,
|
|
148
|
+
mode: "prefix-no-default",
|
|
149
|
+
});
|
|
76
150
|
// Output: {
|
|
77
151
|
// en: "/dashboard",
|
|
78
152
|
// fr: "/fr/dashboard"
|
|
@@ -82,12 +156,11 @@ getMultilingualUrls(
|
|
|
82
156
|
```javascript codeFormat="esm"
|
|
83
157
|
import { getMultilingualUrls, Locales } from "intlayer";
|
|
84
158
|
|
|
85
|
-
getMultilingualUrls(
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
);
|
|
159
|
+
getMultilingualUrls("/dashboard", {
|
|
160
|
+
locales: [Locales.ENGLISH, Locales.FRENCH],
|
|
161
|
+
defaultLocale: Locales.ENGLISH,
|
|
162
|
+
mode: "prefix-no-default",
|
|
163
|
+
});
|
|
91
164
|
// Output: {
|
|
92
165
|
// en: "/dashboard",
|
|
93
166
|
// fr: "/fr/dashboard"
|
|
@@ -97,12 +170,11 @@ getMultilingualUrls(
|
|
|
97
170
|
```javascript codeFormat="commonjs"
|
|
98
171
|
const { getMultilingualUrls, Locales } = require("intlayer");
|
|
99
172
|
|
|
100
|
-
getMultilingualUrls(
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
);
|
|
173
|
+
getMultilingualUrls("/dashboard", {
|
|
174
|
+
locales: [Locales.ENGLISH, Locales.FRENCH],
|
|
175
|
+
defaultLocale: Locales.ENGLISH,
|
|
176
|
+
mode: "prefix-no-default",
|
|
177
|
+
});
|
|
106
178
|
// Output: {
|
|
107
179
|
// en: "/dashboard",
|
|
108
180
|
// fr: "/fr/dashboard"
|
|
@@ -112,18 +184,69 @@ getMultilingualUrls(
|
|
|
112
184
|
### Absolute URLs
|
|
113
185
|
|
|
114
186
|
```typescript
|
|
115
|
-
getMultilingualUrls(
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
);
|
|
187
|
+
getMultilingualUrls("https://example.com/dashboard", {
|
|
188
|
+
locales: [Locales.ENGLISH, Locales.FRENCH],
|
|
189
|
+
defaultLocale: Locales.ENGLISH,
|
|
190
|
+
mode: "prefix-all",
|
|
191
|
+
});
|
|
121
192
|
// Output: {
|
|
122
193
|
// en: "https://example.com/en/dashboard",
|
|
123
194
|
// fr: "https://example.com/fr/dashboard"
|
|
124
195
|
// }
|
|
125
196
|
```
|
|
126
197
|
|
|
198
|
+
### Different Routing Modes
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
// prefix-no-default: No prefix for default locale
|
|
202
|
+
getMultilingualUrls("/dashboard", {
|
|
203
|
+
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
204
|
+
defaultLocale: Locales.ENGLISH,
|
|
205
|
+
mode: "prefix-no-default",
|
|
206
|
+
});
|
|
207
|
+
// Output: {
|
|
208
|
+
// en: "/dashboard",
|
|
209
|
+
// fr: "/fr/dashboard",
|
|
210
|
+
// es: "/es/dashboard"
|
|
211
|
+
// }
|
|
212
|
+
|
|
213
|
+
// prefix-all: Prefix for all locales
|
|
214
|
+
getMultilingualUrls("/dashboard", {
|
|
215
|
+
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
216
|
+
defaultLocale: Locales.ENGLISH,
|
|
217
|
+
mode: "prefix-all",
|
|
218
|
+
});
|
|
219
|
+
// Output: {
|
|
220
|
+
// en: "/en/dashboard",
|
|
221
|
+
// fr: "/fr/dashboard",
|
|
222
|
+
// es: "/es/dashboard"
|
|
223
|
+
// }
|
|
224
|
+
|
|
225
|
+
// no-prefix: No locale prefix in URLs
|
|
226
|
+
getMultilingualUrls("/dashboard", {
|
|
227
|
+
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
228
|
+
defaultLocale: Locales.ENGLISH,
|
|
229
|
+
mode: "no-prefix",
|
|
230
|
+
});
|
|
231
|
+
// Output: {
|
|
232
|
+
// en: "/dashboard",
|
|
233
|
+
// fr: "/dashboard",
|
|
234
|
+
// es: "/dashboard"
|
|
235
|
+
// }
|
|
236
|
+
|
|
237
|
+
// search-params: Locale as query parameter
|
|
238
|
+
getMultilingualUrls("/dashboard", {
|
|
239
|
+
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
240
|
+
defaultLocale: Locales.ENGLISH,
|
|
241
|
+
mode: "search-params",
|
|
242
|
+
});
|
|
243
|
+
// Output: {
|
|
244
|
+
// en: "/dashboard?locale=en",
|
|
245
|
+
// fr: "/dashboard?locale=fr",
|
|
246
|
+
// es: "/dashboard?locale=es"
|
|
247
|
+
// }
|
|
248
|
+
```
|
|
249
|
+
|
|
127
250
|
---
|
|
128
251
|
|
|
129
252
|
## Edge Cases
|
|
@@ -132,11 +255,18 @@ getMultilingualUrls(
|
|
|
132
255
|
- The function removes any existing locale segment from the URL before generating the multilingual mappings.
|
|
133
256
|
|
|
134
257
|
- **Default Locale:**
|
|
135
|
-
- When `
|
|
258
|
+
- When `mode` is `'prefix-no-default'`, the function does not prefix the URL for the default locale.
|
|
259
|
+
- When `mode` is `'prefix-all'`, the function prefixes all locales including the default.
|
|
136
260
|
|
|
137
261
|
- **Unsupported Locales:**
|
|
138
262
|
- Only the locales provided in the `locales` array are considered for generating the URLs.
|
|
139
263
|
|
|
264
|
+
- **Routing Modes:**
|
|
265
|
+
- `'prefix-no-default'`: Default locale has no prefix, others do (e.g., `/dashboard`, `/fr/dashboard`)
|
|
266
|
+
- `'prefix-all'`: All locales have prefixes (e.g., `/en/dashboard`, `/fr/dashboard`)
|
|
267
|
+
- `'no-prefix'`: No locale prefixes in URLs (all locales return same URL)
|
|
268
|
+
- `'search-params'`: Locale specified via query parameter (e.g., `/dashboard?locale=fr`)
|
|
269
|
+
|
|
140
270
|
---
|
|
141
271
|
|
|
142
272
|
## Usage in Applications
|
|
@@ -190,11 +320,8 @@ The above configuration ensures that the application recognizes `ENGLISH`, `FREN
|
|
|
190
320
|
Using this configuration, the `getMultilingualUrls` function can dynamically generate multilingual URL mappings based on the application's supported locales:
|
|
191
321
|
|
|
192
322
|
```typescript
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
[Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
196
|
-
Locales.ENGLISH
|
|
197
|
-
);
|
|
323
|
+
// Using project configuration (no options needed)
|
|
324
|
+
getMultilingualUrls("/dashboard");
|
|
198
325
|
// Output:
|
|
199
326
|
// {
|
|
200
327
|
// en: "/dashboard",
|
|
@@ -202,12 +329,25 @@ getMultilingualUrls(
|
|
|
202
329
|
// es: "/es/dashboard"
|
|
203
330
|
// }
|
|
204
331
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
[Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
208
|
-
Locales.ENGLISH,
|
|
209
|
-
|
|
210
|
-
);
|
|
332
|
+
// With explicit options
|
|
333
|
+
getMultilingualUrls("/dashboard", {
|
|
334
|
+
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
335
|
+
defaultLocale: Locales.ENGLISH,
|
|
336
|
+
mode: "prefix-no-default",
|
|
337
|
+
});
|
|
338
|
+
// Output:
|
|
339
|
+
// {
|
|
340
|
+
// en: "/dashboard",
|
|
341
|
+
// fr: "/fr/dashboard",
|
|
342
|
+
// es: "/es/dashboard"
|
|
343
|
+
// }
|
|
344
|
+
|
|
345
|
+
// Absolute URLs with prefix-all mode
|
|
346
|
+
getMultilingualUrls("https://example.com/dashboard", {
|
|
347
|
+
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
348
|
+
defaultLocale: Locales.ENGLISH,
|
|
349
|
+
mode: "prefix-all",
|
|
350
|
+
});
|
|
211
351
|
// Output:
|
|
212
352
|
// {
|
|
213
353
|
// en: "https://example.com/en/dashboard",
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
---
|
|
2
|
+
createdAt: 2025-11-16
|
|
3
|
+
updatedAt: 2025-11-16
|
|
4
|
+
title: getPrefix Function Documentation | intlayer
|
|
5
|
+
description: See how to use the getPrefix function for intlayer package
|
|
6
|
+
keywords:
|
|
7
|
+
- getPrefix
|
|
8
|
+
- prefix
|
|
9
|
+
- Intlayer
|
|
10
|
+
- intlayer
|
|
11
|
+
- Internationalization
|
|
12
|
+
- Documentation
|
|
13
|
+
- Next.js
|
|
14
|
+
- JavaScript
|
|
15
|
+
- React
|
|
16
|
+
slugs:
|
|
17
|
+
- doc
|
|
18
|
+
- packages
|
|
19
|
+
- intlayer
|
|
20
|
+
- getPrefix
|
|
21
|
+
history:
|
|
22
|
+
- version: 7.1.0
|
|
23
|
+
date: 2025-11-16
|
|
24
|
+
changes: Initial documentation
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
# Documentation: `getPrefix` Function in `intlayer`
|
|
28
|
+
|
|
29
|
+
## Description
|
|
30
|
+
|
|
31
|
+
The `getPrefix` function determines the URL prefix for a given locale based on the routing mode configuration. It compares the locale with the default locale and returns the appropriate prefix string (e.g., `'en/'`) or an empty string.
|
|
32
|
+
|
|
33
|
+
**Key Features:**
|
|
34
|
+
|
|
35
|
+
- Takes a locale as the first parameter (optional)
|
|
36
|
+
- Optional `options` object with `defaultLocale`, `mode`, and `addSlash`
|
|
37
|
+
- Returns a prefix string that can be used for URL construction
|
|
38
|
+
- Supports all routing modes: `prefix-no-default`, `prefix-all`, `no-prefix`, and `search-params`
|
|
39
|
+
- Lightweight utility for determining when to add locale prefixes
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## Function Signature
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
getPrefix(
|
|
47
|
+
locale?: Locales, // Optional
|
|
48
|
+
options?: { // Optional
|
|
49
|
+
defaultLocale?: Locales;
|
|
50
|
+
mode?: 'prefix-no-default' | 'prefix-all' | 'no-prefix' | 'search-params';
|
|
51
|
+
addSlash?: boolean;
|
|
52
|
+
}
|
|
53
|
+
): string
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## Parameters
|
|
59
|
+
|
|
60
|
+
### Optional Parameters
|
|
61
|
+
|
|
62
|
+
- `locale?: Locales`
|
|
63
|
+
- **Description**: The locale to check for prefix. If not provided, uses the configured default locale.
|
|
64
|
+
- **Type**: `Locales`
|
|
65
|
+
- **Required**: No (Optional)
|
|
66
|
+
- **Default**: [`Project Configuration`](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/configuration.md#middleware)
|
|
67
|
+
|
|
68
|
+
- `options?: object`
|
|
69
|
+
- **Description**: Configuration object for prefix determination.
|
|
70
|
+
- **Type**: `object`
|
|
71
|
+
- **Required**: No (Optional)
|
|
72
|
+
|
|
73
|
+
- `options.defaultLocale?: Locales`
|
|
74
|
+
- **Description**: The default locale for the application. If not provided, uses the configured default locale from your project configuration.
|
|
75
|
+
- **Type**: `Locales`
|
|
76
|
+
- **Default**: [`Project Configuration`](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/configuration.md#middleware)
|
|
77
|
+
|
|
78
|
+
- `options.mode?: 'prefix-no-default' | 'prefix-all' | 'no-prefix' | 'search-params'`
|
|
79
|
+
- **Description**: The URL routing mode for locale handling. If not provided, uses the configured mode from your project configuration.
|
|
80
|
+
- **Type**: `'prefix-no-default' | 'prefix-all' | 'no-prefix' | 'search-params'`
|
|
81
|
+
- **Default**: [`Project Configuration`](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/configuration.md#middleware)
|
|
82
|
+
- **Modes**:
|
|
83
|
+
- `prefix-no-default`: Returns empty string when locale matches default locale
|
|
84
|
+
- `prefix-all`: Returns prefix with default locale
|
|
85
|
+
- `no-prefix`: Returns empty string (no prefix in URLs)
|
|
86
|
+
- `search-params`: Returns empty string (locale in query parameters)
|
|
87
|
+
|
|
88
|
+
- `options.addSlash?: boolean`
|
|
89
|
+
- **Description**: Whether to add a trailing slash to the prefix. If `true`, returns `'en/'`; if `false`, returns `'en'`.
|
|
90
|
+
- **Type**: `boolean`
|
|
91
|
+
- **Default**: `true`
|
|
92
|
+
|
|
93
|
+
### Returns
|
|
94
|
+
|
|
95
|
+
- **Type**: `string`
|
|
96
|
+
- **Description**: The prefix string for the default locale (e.g., `'en/'`, `'fr/'`) or an empty string.
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
## Example Usage
|
|
101
|
+
|
|
102
|
+
### Basic Usage
|
|
103
|
+
|
|
104
|
+
```typescript codeFormat="typescript"
|
|
105
|
+
import { getPrefix, Locales } from "intlayer";
|
|
106
|
+
|
|
107
|
+
// Check prefix for English locale
|
|
108
|
+
getPrefix(Locales.ENGLISH, {
|
|
109
|
+
defaultLocale: Locales.ENGLISH,
|
|
110
|
+
mode: "prefix-all",
|
|
111
|
+
});
|
|
112
|
+
// Returns: 'en/'
|
|
113
|
+
|
|
114
|
+
// Check prefix for French locale
|
|
115
|
+
getPrefix(Locales.FRENCH, {
|
|
116
|
+
defaultLocale: Locales.ENGLISH,
|
|
117
|
+
mode: "prefix-no-default",
|
|
118
|
+
});
|
|
119
|
+
// Returns: 'en/'
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
```javascript codeFormat="esm"
|
|
123
|
+
import { getPrefix, Locales } from "intlayer";
|
|
124
|
+
|
|
125
|
+
getPrefix(Locales.ENGLISH, { mode: "prefix-all" }); // Returns: "en/"
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
```javascript codeFormat="commonjs"
|
|
129
|
+
const { getPrefix, Locales } = require("intlayer");
|
|
130
|
+
|
|
131
|
+
getPrefix(Locales.ENGLISH, { mode: "prefix-all" }); // Returns: "en/"
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Different Routing Modes
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
import { getPrefix, Locales } from "intlayer";
|
|
138
|
+
|
|
139
|
+
// prefix-all: Always returns prefix
|
|
140
|
+
getPrefix(Locales.ENGLISH, {
|
|
141
|
+
mode: "prefix-all",
|
|
142
|
+
defaultLocale: Locales.ENGLISH,
|
|
143
|
+
});
|
|
144
|
+
// Returns: "en/"
|
|
145
|
+
|
|
146
|
+
// prefix-no-default: No prefix when locale matches default
|
|
147
|
+
getPrefix(Locales.ENGLISH, {
|
|
148
|
+
mode: "prefix-no-default",
|
|
149
|
+
defaultLocale: Locales.ENGLISH,
|
|
150
|
+
});
|
|
151
|
+
// Returns: ""
|
|
152
|
+
|
|
153
|
+
// prefix-no-default: Returns prefix when locale differs from default
|
|
154
|
+
getPrefix(Locales.FRENCH, {
|
|
155
|
+
mode: "prefix-no-default",
|
|
156
|
+
defaultLocale: Locales.ENGLISH,
|
|
157
|
+
});
|
|
158
|
+
// Returns: "en/"
|
|
159
|
+
|
|
160
|
+
// no-prefix & search-params: Never returns prefix
|
|
161
|
+
getPrefix(Locales.ENGLISH, { mode: "no-prefix" }); // Returns: ""
|
|
162
|
+
getPrefix(Locales.ENGLISH, { mode: "search-params" }); // Returns: ""
|
|
163
|
+
|
|
164
|
+
// Without trailing slash
|
|
165
|
+
getPrefix(Locales.ENGLISH, { mode: "prefix-all", addSlash: false });
|
|
166
|
+
// Returns: "en" (without trailing slash)
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Practical Example
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
import { getPrefix, Locales } from "intlayer";
|
|
173
|
+
|
|
174
|
+
// Build URLs with the appropriate prefix for a specific locale
|
|
175
|
+
const locale = Locales.FRENCH;
|
|
176
|
+
const prefix = getPrefix(locale, { defaultLocale: Locales.ENGLISH });
|
|
177
|
+
const url = `/${prefix}about`.replace(/\/+/g, "/");
|
|
178
|
+
// Result: "/en/about" if French is not default
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
---
|
|
182
|
+
|
|
183
|
+
## Related Functions
|
|
184
|
+
|
|
185
|
+
- [`getLocalizedUrl`](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/packages/intlayer/getLocalizedUrl.md): Generates a localized URL for a specific locale
|
|
186
|
+
- [`getMultilingualUrls`](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/packages/intlayer/getMultilingualUrls.md): Generates URLs for all configured locales
|
|
187
|
+
|
|
188
|
+
---
|
|
189
|
+
|
|
190
|
+
## TypeScript
|
|
191
|
+
|
|
192
|
+
```typescript
|
|
193
|
+
function getPrefix(
|
|
194
|
+
locale?: Locales,
|
|
195
|
+
options?: {
|
|
196
|
+
defaultLocale?: Locales;
|
|
197
|
+
mode?: "prefix-no-default" | "prefix-all" | "no-prefix" | "search-params";
|
|
198
|
+
addSlash?: boolean;
|
|
199
|
+
}
|
|
200
|
+
): string;
|
|
201
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intlayer/docs",
|
|
3
|
-
"version": "7.0
|
|
3
|
+
"version": "7.1.0-canary.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Intlayer documentation",
|
|
6
6
|
"keywords": [
|
|
@@ -58,9 +58,9 @@
|
|
|
58
58
|
"lint": "biome lint .",
|
|
59
59
|
"lint:fix": "biome lint --write .",
|
|
60
60
|
"prepublish": "cp -f ../README.md ./README.md",
|
|
61
|
-
"publish": "bun publish
|
|
62
|
-
"publish:canary": "bun publish --tag canary
|
|
63
|
-
"publish:latest": "bun publish --tag latest
|
|
61
|
+
"publish": "bun publish || true",
|
|
62
|
+
"publish:canary": "bun publish --tag canary || true",
|
|
63
|
+
"publish:latest": "bun publish --tag latest || true",
|
|
64
64
|
"review": "bun tools/review.ts",
|
|
65
65
|
"serve": "webpack serve --config ./webpack.config.ts",
|
|
66
66
|
"test": "vitest run",
|
|
@@ -72,13 +72,13 @@
|
|
|
72
72
|
"watch": "webpack --config ./webpack.config.ts --watch"
|
|
73
73
|
},
|
|
74
74
|
"dependencies": {
|
|
75
|
-
"@intlayer/config": "7.0
|
|
76
|
-
"@intlayer/core": "7.0
|
|
77
|
-
"@intlayer/types": "7.0
|
|
75
|
+
"@intlayer/config": "7.1.0-canary.0",
|
|
76
|
+
"@intlayer/core": "7.1.0-canary.0",
|
|
77
|
+
"@intlayer/types": "7.1.0-canary.0"
|
|
78
78
|
},
|
|
79
79
|
"devDependencies": {
|
|
80
|
-
"@intlayer/api": "7.0
|
|
81
|
-
"@intlayer/cli": "7.0
|
|
80
|
+
"@intlayer/api": "7.1.0-canary.0",
|
|
81
|
+
"@intlayer/cli": "7.1.0-canary.0",
|
|
82
82
|
"@types/node": "24.10.1",
|
|
83
83
|
"@utils/ts-config": "1.0.4",
|
|
84
84
|
"@utils/ts-config-types": "1.0.4",
|
|
@@ -91,11 +91,11 @@
|
|
|
91
91
|
"vitest": "4.0.8"
|
|
92
92
|
},
|
|
93
93
|
"peerDependencies": {
|
|
94
|
-
"@intlayer/api": "7.0
|
|
95
|
-
"@intlayer/cli": "7.0
|
|
96
|
-
"@intlayer/config": "7.0
|
|
97
|
-
"@intlayer/core": "7.0
|
|
98
|
-
"@intlayer/types": "7.0
|
|
94
|
+
"@intlayer/api": "7.1.0-canary.0",
|
|
95
|
+
"@intlayer/cli": "7.1.0-canary.0",
|
|
96
|
+
"@intlayer/config": "7.1.0-canary.0",
|
|
97
|
+
"@intlayer/core": "7.1.0-canary.0",
|
|
98
|
+
"@intlayer/types": "7.1.0-canary.0"
|
|
99
99
|
},
|
|
100
100
|
"engines": {
|
|
101
101
|
"node": ">=14.18"
|
|
@@ -1079,6 +1079,25 @@ export const docsEntry = {
|
|
|
1079
1079
|
id: readLocale('packages/intlayer/getPathWithoutLocale.md', 'id'),
|
|
1080
1080
|
vi: readLocale('packages/intlayer/getPathWithoutLocale.md', 'vi'),
|
|
1081
1081
|
} as unknown as Record<LocalesValues, Promise<string>>,
|
|
1082
|
+
'./docs/en/packages/intlayer/getPrefix.md': {
|
|
1083
|
+
en: readLocale('packages/intlayer/getPrefix.md', 'en'),
|
|
1084
|
+
ru: readLocale('packages/intlayer/getPrefix.md', 'ru'),
|
|
1085
|
+
ja: readLocale('packages/intlayer/getPrefix.md', 'ja'),
|
|
1086
|
+
fr: readLocale('packages/intlayer/getPrefix.md', 'fr'),
|
|
1087
|
+
ko: readLocale('packages/intlayer/getPrefix.md', 'ko'),
|
|
1088
|
+
zh: readLocale('packages/intlayer/getPrefix.md', 'zh'),
|
|
1089
|
+
es: readLocale('packages/intlayer/getPrefix.md', 'es'),
|
|
1090
|
+
de: readLocale('packages/intlayer/getPrefix.md', 'de'),
|
|
1091
|
+
ar: readLocale('packages/intlayer/getPrefix.md', 'ar'),
|
|
1092
|
+
it: readLocale('packages/intlayer/getPrefix.md', 'it'),
|
|
1093
|
+
'en-GB': readLocale('packages/intlayer/getPrefix.md', 'en-GB'),
|
|
1094
|
+
pt: readLocale('packages/intlayer/getPrefix.md', 'pt'),
|
|
1095
|
+
hi: readLocale('packages/intlayer/getPrefix.md', 'hi'),
|
|
1096
|
+
tr: readLocale('packages/intlayer/getPrefix.md', 'tr'),
|
|
1097
|
+
pl: readLocale('packages/intlayer/getPrefix.md', 'pl'),
|
|
1098
|
+
id: readLocale('packages/intlayer/getPrefix.md', 'id'),
|
|
1099
|
+
vi: readLocale('packages/intlayer/getPrefix.md', 'vi'),
|
|
1100
|
+
} as unknown as Record<LocalesValues, Promise<string>>,
|
|
1082
1101
|
'./docs/en/packages/intlayer/getTranslation.md': {
|
|
1083
1102
|
en: readLocale('packages/intlayer/getTranslation.md', 'en'),
|
|
1084
1103
|
ru: readLocale('packages/intlayer/getTranslation.md', 'ru'),
|