@intlayer/docs 7.1.0-canary.0 → 7.1.0-canary.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/en/packages/intlayer/getPrefix.md +54 -42
- package/package.json +11 -11
|
@@ -28,13 +28,13 @@ history:
|
|
|
28
28
|
|
|
29
29
|
## Description
|
|
30
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
|
|
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 an object containing three different prefix formats for flexible URL construction.
|
|
32
32
|
|
|
33
33
|
**Key Features:**
|
|
34
34
|
|
|
35
|
-
- Takes a locale as the first parameter (
|
|
36
|
-
- Optional `options` object with `defaultLocale
|
|
37
|
-
- Returns
|
|
35
|
+
- Takes a locale as the first parameter (required)
|
|
36
|
+
- Optional `options` object with `defaultLocale` and `mode`
|
|
37
|
+
- Returns an object with `prefix`, and `localePrefix` properties
|
|
38
38
|
- Supports all routing modes: `prefix-no-default`, `prefix-all`, `no-prefix`, and `search-params`
|
|
39
39
|
- Lightweight utility for determining when to add locale prefixes
|
|
40
40
|
|
|
@@ -44,26 +44,27 @@ The `getPrefix` function determines the URL prefix for a given locale based on t
|
|
|
44
44
|
|
|
45
45
|
```typescript
|
|
46
46
|
getPrefix(
|
|
47
|
-
locale
|
|
47
|
+
locale: Locales, // Required
|
|
48
48
|
options?: { // Optional
|
|
49
49
|
defaultLocale?: Locales;
|
|
50
50
|
mode?: 'prefix-no-default' | 'prefix-all' | 'no-prefix' | 'search-params';
|
|
51
|
-
addSlash?: boolean;
|
|
52
51
|
}
|
|
53
|
-
):
|
|
52
|
+
): GetPrefixResult
|
|
53
|
+
|
|
54
|
+
type GetPrefixResult = {
|
|
55
|
+
prefix: string; // e.g., 'fr/' or ''
|
|
56
|
+
localePrefix?: Locale; // e.g., 'fr' or undefined
|
|
57
|
+
}
|
|
54
58
|
```
|
|
55
59
|
|
|
56
60
|
---
|
|
57
61
|
|
|
58
62
|
## Parameters
|
|
59
63
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
- `locale?: Locales`
|
|
63
|
-
- **Description**: The locale to check for prefix. If not provided, uses the configured default locale.
|
|
64
|
+
- `locale: Locales`
|
|
65
|
+
- **Description**: The locale to generate the prefix for. If the value is falsy (undefined, null, empty string), the function returns an empty string.
|
|
64
66
|
- **Type**: `Locales`
|
|
65
|
-
- **Required**:
|
|
66
|
-
- **Default**: [`Project Configuration`](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/configuration.md#middleware)
|
|
67
|
+
- **Required**: Yes
|
|
67
68
|
|
|
68
69
|
- `options?: object`
|
|
69
70
|
- **Description**: Configuration object for prefix determination.
|
|
@@ -80,20 +81,17 @@ getPrefix(
|
|
|
80
81
|
- **Type**: `'prefix-no-default' | 'prefix-all' | 'no-prefix' | 'search-params'`
|
|
81
82
|
- **Default**: [`Project Configuration`](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/configuration.md#middleware)
|
|
82
83
|
- **Modes**:
|
|
83
|
-
- `prefix-no-default`: Returns empty
|
|
84
|
-
- `prefix-all`: Returns prefix
|
|
85
|
-
- `no-prefix`: Returns empty
|
|
86
|
-
- `search-params`: Returns empty
|
|
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`
|
|
84
|
+
- `prefix-no-default`: Returns empty strings when locale matches default locale
|
|
85
|
+
- `prefix-all`: Returns prefix for all locales including default
|
|
86
|
+
- `no-prefix`: Returns empty strings (no prefix in URLs)
|
|
87
|
+
- `search-params`: Returns empty strings (locale in query parameters)
|
|
92
88
|
|
|
93
89
|
### Returns
|
|
94
90
|
|
|
95
|
-
- **Type**: `
|
|
96
|
-
- **Description**:
|
|
91
|
+
- **Type**: `GetPrefixResult`
|
|
92
|
+
- **Description**: An object containing three different prefix formats:
|
|
93
|
+
- `prefix`: The path prefix with trailing slash (e.g., `'fr/'`, `''`)
|
|
94
|
+
- `localePrefix`: The locale identifier without slashes (e.g., `'fr'`, `undefined`)
|
|
97
95
|
|
|
98
96
|
---
|
|
99
97
|
|
|
@@ -109,26 +107,28 @@ getPrefix(Locales.ENGLISH, {
|
|
|
109
107
|
defaultLocale: Locales.ENGLISH,
|
|
110
108
|
mode: "prefix-all",
|
|
111
109
|
});
|
|
112
|
-
// Returns: 'en/'
|
|
110
|
+
// Returns: { prefix: 'en/', localePrefix: 'en' }
|
|
113
111
|
|
|
114
112
|
// Check prefix for French locale
|
|
115
113
|
getPrefix(Locales.FRENCH, {
|
|
116
114
|
defaultLocale: Locales.ENGLISH,
|
|
117
115
|
mode: "prefix-no-default",
|
|
118
116
|
});
|
|
119
|
-
// Returns: '
|
|
117
|
+
// Returns: { prefix: 'fr/', localePrefix: 'fr' }
|
|
120
118
|
```
|
|
121
119
|
|
|
122
120
|
```javascript codeFormat="esm"
|
|
123
121
|
import { getPrefix, Locales } from "intlayer";
|
|
124
122
|
|
|
125
|
-
getPrefix(Locales.ENGLISH, { mode: "prefix-all" });
|
|
123
|
+
getPrefix(Locales.ENGLISH, { mode: "prefix-all" });
|
|
124
|
+
// Returns: { prefix: '', localePrefix: undefined }
|
|
126
125
|
```
|
|
127
126
|
|
|
128
127
|
```javascript codeFormat="commonjs"
|
|
129
128
|
const { getPrefix, Locales } = require("intlayer");
|
|
130
129
|
|
|
131
|
-
getPrefix(Locales.ENGLISH, { mode: "prefix-all" });
|
|
130
|
+
getPrefix(Locales.ENGLISH, { mode: "prefix-all" });
|
|
131
|
+
// Returns: { prefix: '', localePrefix: undefined }
|
|
132
132
|
```
|
|
133
133
|
|
|
134
134
|
### Different Routing Modes
|
|
@@ -141,29 +141,28 @@ getPrefix(Locales.ENGLISH, {
|
|
|
141
141
|
mode: "prefix-all",
|
|
142
142
|
defaultLocale: Locales.ENGLISH,
|
|
143
143
|
});
|
|
144
|
-
// Returns:
|
|
144
|
+
// Returns: { prefix: '/en', localePrefix: 'en' }
|
|
145
145
|
|
|
146
146
|
// prefix-no-default: No prefix when locale matches default
|
|
147
147
|
getPrefix(Locales.ENGLISH, {
|
|
148
148
|
mode: "prefix-no-default",
|
|
149
149
|
defaultLocale: Locales.ENGLISH,
|
|
150
150
|
});
|
|
151
|
-
// Returns:
|
|
151
|
+
// Returns: { prefix: '', localePrefix: undefined }
|
|
152
152
|
|
|
153
153
|
// prefix-no-default: Returns prefix when locale differs from default
|
|
154
154
|
getPrefix(Locales.FRENCH, {
|
|
155
155
|
mode: "prefix-no-default",
|
|
156
156
|
defaultLocale: Locales.ENGLISH,
|
|
157
157
|
});
|
|
158
|
-
// Returns:
|
|
158
|
+
// Returns: { prefix: 'fr/', localePrefix: 'fr' }
|
|
159
159
|
|
|
160
160
|
// no-prefix & search-params: Never returns prefix
|
|
161
|
-
getPrefix(Locales.ENGLISH, { mode: "no-prefix" });
|
|
162
|
-
|
|
161
|
+
getPrefix(Locales.ENGLISH, { mode: "no-prefix" });
|
|
162
|
+
// Returns: { prefix: '', localePrefix: undefined }
|
|
163
163
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
// Returns: "en" (without trailing slash)
|
|
164
|
+
getPrefix(Locales.ENGLISH, { mode: "search-params" });
|
|
165
|
+
// Returns: { prefix: '', localePrefix: undefined }
|
|
167
166
|
```
|
|
168
167
|
|
|
169
168
|
### Practical Example
|
|
@@ -173,9 +172,18 @@ import { getPrefix, Locales } from "intlayer";
|
|
|
173
172
|
|
|
174
173
|
// Build URLs with the appropriate prefix for a specific locale
|
|
175
174
|
const locale = Locales.FRENCH;
|
|
176
|
-
const prefix = getPrefix(locale, {
|
|
177
|
-
|
|
178
|
-
|
|
175
|
+
const { prefix, localePrefix } = getPrefix(locale, {
|
|
176
|
+
defaultLocale: Locales.ENGLISH,
|
|
177
|
+
mode: "prefix-no-default",
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
// Using prefix for path construction
|
|
181
|
+
const url1 = `/${prefix}about`.replace(/\/+/g, "/");
|
|
182
|
+
// Result: "/fr/about"
|
|
183
|
+
|
|
184
|
+
// Using localePrefix for locale identification
|
|
185
|
+
console.log(`Current locale: ${localePrefix}`);
|
|
186
|
+
// Output: "Current locale: fr"
|
|
179
187
|
```
|
|
180
188
|
|
|
181
189
|
---
|
|
@@ -190,12 +198,16 @@ const url = `/${prefix}about`.replace(/\/+/g, "/");
|
|
|
190
198
|
## TypeScript
|
|
191
199
|
|
|
192
200
|
```typescript
|
|
201
|
+
type GetPrefixResult = {
|
|
202
|
+
prefix: string; // The path prefix with trailing slash (e.g., 'fr/' or '')
|
|
203
|
+
localePrefix?: Locale; // The locale identifier without slashes (e.g., 'fr' or undefined)
|
|
204
|
+
};
|
|
205
|
+
|
|
193
206
|
function getPrefix(
|
|
194
|
-
locale
|
|
207
|
+
locale: Locales,
|
|
195
208
|
options?: {
|
|
196
209
|
defaultLocale?: Locales;
|
|
197
210
|
mode?: "prefix-no-default" | "prefix-all" | "no-prefix" | "search-params";
|
|
198
|
-
addSlash?: boolean;
|
|
199
211
|
}
|
|
200
|
-
):
|
|
212
|
+
): GetPrefixResult;
|
|
201
213
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intlayer/docs",
|
|
3
|
-
"version": "7.1.0-canary.
|
|
3
|
+
"version": "7.1.0-canary.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Intlayer documentation",
|
|
6
6
|
"keywords": [
|
|
@@ -72,13 +72,13 @@
|
|
|
72
72
|
"watch": "webpack --config ./webpack.config.ts --watch"
|
|
73
73
|
},
|
|
74
74
|
"dependencies": {
|
|
75
|
-
"@intlayer/config": "7.1.0-canary.
|
|
76
|
-
"@intlayer/core": "7.1.0-canary.
|
|
77
|
-
"@intlayer/types": "7.1.0-canary.
|
|
75
|
+
"@intlayer/config": "7.1.0-canary.2",
|
|
76
|
+
"@intlayer/core": "7.1.0-canary.2",
|
|
77
|
+
"@intlayer/types": "7.1.0-canary.2"
|
|
78
78
|
},
|
|
79
79
|
"devDependencies": {
|
|
80
|
-
"@intlayer/api": "7.1.0-canary.
|
|
81
|
-
"@intlayer/cli": "7.1.0-canary.
|
|
80
|
+
"@intlayer/api": "7.1.0-canary.2",
|
|
81
|
+
"@intlayer/cli": "7.1.0-canary.2",
|
|
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.1.0-canary.
|
|
95
|
-
"@intlayer/cli": "7.1.0-canary.
|
|
96
|
-
"@intlayer/config": "7.1.0-canary.
|
|
97
|
-
"@intlayer/core": "7.1.0-canary.
|
|
98
|
-
"@intlayer/types": "7.1.0-canary.
|
|
94
|
+
"@intlayer/api": "7.1.0-canary.2",
|
|
95
|
+
"@intlayer/cli": "7.1.0-canary.2",
|
|
96
|
+
"@intlayer/config": "7.1.0-canary.2",
|
|
97
|
+
"@intlayer/core": "7.1.0-canary.2",
|
|
98
|
+
"@intlayer/types": "7.1.0-canary.2"
|
|
99
99
|
},
|
|
100
100
|
"engines": {
|
|
101
101
|
"node": ">=14.18"
|