@intlayer/docs 7.1.0-canary.1 → 7.1.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.
@@ -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 the appropriate prefix string (e.g., `'en/'`, `'fr/'`) or an empty string.
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
35
  - Takes a locale as the first parameter (required)
36
- - Optional `options` object with `defaultLocale`, `mode`, and `addSlash`
37
- - Returns a prefix string based on the provided locale that can be used for URL construction
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
 
@@ -48,9 +48,13 @@ getPrefix(
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
- ): string
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
  ---
@@ -77,20 +81,17 @@ getPrefix(
77
81
  - **Type**: `'prefix-no-default' | 'prefix-all' | 'no-prefix' | 'search-params'`
78
82
  - **Default**: [`Project Configuration`](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/configuration.md#middleware)
79
83
  - **Modes**:
80
- - `prefix-no-default`: Returns empty string when locale matches default locale
81
- - `prefix-all`: Returns prefix with default locale
82
- - `no-prefix`: Returns empty string (no prefix in URLs)
83
- - `search-params`: Returns empty string (locale in query parameters)
84
-
85
- - `options.addSlash?: boolean`
86
- - **Description**: Whether to add a trailing slash to the prefix. If `true`, returns `'en/'`; if `false`, returns `'en'`.
87
- - **Type**: `boolean`
88
- - **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)
89
88
 
90
89
  ### Returns
91
90
 
92
- - **Type**: `string`
93
- - **Description**: The prefix string for the given locale (e.g., `'en/'`, `'fr/'`) or an empty string depending on the routing mode and whether the locale matches the default.
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`)
94
95
 
95
96
  ---
96
97
 
@@ -106,26 +107,28 @@ getPrefix(Locales.ENGLISH, {
106
107
  defaultLocale: Locales.ENGLISH,
107
108
  mode: "prefix-all",
108
109
  });
109
- // Returns: 'en/'
110
+ // Returns: { prefix: 'en/', localePrefix: 'en' }
110
111
 
111
112
  // Check prefix for French locale
112
113
  getPrefix(Locales.FRENCH, {
113
114
  defaultLocale: Locales.ENGLISH,
114
115
  mode: "prefix-no-default",
115
116
  });
116
- // Returns: 'fr/'
117
+ // Returns: { prefix: 'fr/', localePrefix: 'fr' }
117
118
  ```
118
119
 
119
120
  ```javascript codeFormat="esm"
120
121
  import { getPrefix, Locales } from "intlayer";
121
122
 
122
- getPrefix(Locales.ENGLISH, { mode: "prefix-all" }); // Returns: "en/"
123
+ getPrefix(Locales.ENGLISH, { mode: "prefix-all" });
124
+ // Returns: { prefix: '', localePrefix: undefined }
123
125
  ```
124
126
 
125
127
  ```javascript codeFormat="commonjs"
126
128
  const { getPrefix, Locales } = require("intlayer");
127
129
 
128
- getPrefix(Locales.ENGLISH, { mode: "prefix-all" }); // Returns: "en/"
130
+ getPrefix(Locales.ENGLISH, { mode: "prefix-all" });
131
+ // Returns: { prefix: '', localePrefix: undefined }
129
132
  ```
130
133
 
131
134
  ### Different Routing Modes
@@ -138,29 +141,28 @@ getPrefix(Locales.ENGLISH, {
138
141
  mode: "prefix-all",
139
142
  defaultLocale: Locales.ENGLISH,
140
143
  });
141
- // Returns: "en/"
144
+ // Returns: { prefix: '/en', localePrefix: 'en' }
142
145
 
143
146
  // prefix-no-default: No prefix when locale matches default
144
147
  getPrefix(Locales.ENGLISH, {
145
148
  mode: "prefix-no-default",
146
149
  defaultLocale: Locales.ENGLISH,
147
150
  });
148
- // Returns: ""
151
+ // Returns: { prefix: '', localePrefix: undefined }
149
152
 
150
153
  // prefix-no-default: Returns prefix when locale differs from default
151
154
  getPrefix(Locales.FRENCH, {
152
155
  mode: "prefix-no-default",
153
156
  defaultLocale: Locales.ENGLISH,
154
157
  });
155
- // Returns: "fr/"
158
+ // Returns: { prefix: 'fr/', localePrefix: 'fr' }
156
159
 
157
160
  // no-prefix & search-params: Never returns prefix
158
- getPrefix(Locales.ENGLISH, { mode: "no-prefix" }); // Returns: ""
159
- getPrefix(Locales.ENGLISH, { mode: "search-params" }); // Returns: ""
161
+ getPrefix(Locales.ENGLISH, { mode: "no-prefix" });
162
+ // Returns: { prefix: '', localePrefix: undefined }
160
163
 
161
- // Without trailing slash
162
- getPrefix(Locales.ENGLISH, { mode: "prefix-all", addSlash: false });
163
- // Returns: "en" (without trailing slash)
164
+ getPrefix(Locales.ENGLISH, { mode: "search-params" });
165
+ // Returns: { prefix: '', localePrefix: undefined }
164
166
  ```
165
167
 
166
168
  ### Practical Example
@@ -170,12 +172,18 @@ import { getPrefix, Locales } from "intlayer";
170
172
 
171
173
  // Build URLs with the appropriate prefix for a specific locale
172
174
  const locale = Locales.FRENCH;
173
- const prefix = getPrefix(locale, {
175
+ const { prefix, localePrefix } = getPrefix(locale, {
174
176
  defaultLocale: Locales.ENGLISH,
175
177
  mode: "prefix-no-default",
176
178
  });
177
- const url = `/${prefix}about`.replace(/\/+/g, "/");
178
- // Result: "/fr/about" (because French differs from default English)
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
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
- ): string;
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.1",
3
+ "version": "7.1.0",
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.1",
76
- "@intlayer/core": "7.1.0-canary.1",
77
- "@intlayer/types": "7.1.0-canary.1"
75
+ "@intlayer/config": "7.1.0",
76
+ "@intlayer/core": "7.1.0",
77
+ "@intlayer/types": "7.1.0"
78
78
  },
79
79
  "devDependencies": {
80
- "@intlayer/api": "7.1.0-canary.1",
81
- "@intlayer/cli": "7.1.0-canary.1",
80
+ "@intlayer/api": "7.1.0",
81
+ "@intlayer/cli": "7.1.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.1.0-canary.1",
95
- "@intlayer/cli": "7.1.0-canary.1",
96
- "@intlayer/config": "7.1.0-canary.1",
97
- "@intlayer/core": "7.1.0-canary.1",
98
- "@intlayer/types": "7.1.0-canary.1"
94
+ "@intlayer/api": "7.1.0",
95
+ "@intlayer/cli": "7.1.0",
96
+ "@intlayer/config": "7.1.0",
97
+ "@intlayer/core": "7.1.0",
98
+ "@intlayer/types": "7.1.0"
99
99
  },
100
100
  "engines": {
101
101
  "node": ">=14.18"