@i18n-micro/path-strategy 1.2.0 → 1.3.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/README.md +75 -29
- package/dist/base-strategy-BHkH418r.cjs +2 -0
- package/dist/base-strategy-BHkH418r.cjs.map +1 -0
- package/dist/base-strategy-CSEFam3u.js +585 -0
- package/dist/base-strategy-CSEFam3u.js.map +1 -0
- package/dist/common-BaGdobUC.js +114 -0
- package/dist/common-BaGdobUC.js.map +1 -0
- package/dist/common-sNHyO9pg.cjs +2 -0
- package/dist/common-sNHyO9pg.cjs.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +63 -352
- package/dist/index.mjs +21 -34
- package/dist/index.mjs.map +1 -1
- package/dist/no-prefix-strategy.cjs +1 -1
- package/dist/no-prefix-strategy.cjs.map +1 -1
- package/dist/no-prefix-strategy.d.ts +38 -262
- package/dist/no-prefix-strategy.mjs +17 -32
- package/dist/no-prefix-strategy.mjs.map +1 -1
- package/dist/prefix-and-default-strategy.cjs +1 -1
- package/dist/prefix-and-default-strategy.cjs.map +1 -1
- package/dist/prefix-and-default-strategy.d.ts +36 -259
- package/dist/prefix-and-default-strategy.mjs +75 -125
- package/dist/prefix-and-default-strategy.mjs.map +1 -1
- package/dist/prefix-except-default-strategy.cjs +1 -1
- package/dist/prefix-except-default-strategy.cjs.map +1 -1
- package/dist/prefix-except-default-strategy.d.ts +41 -269
- package/dist/prefix-except-default-strategy.mjs +228 -251
- package/dist/prefix-except-default-strategy.mjs.map +1 -1
- package/dist/prefix-strategy.cjs +1 -1
- package/dist/prefix-strategy.cjs.map +1 -1
- package/dist/prefix-strategy.d.ts +36 -257
- package/dist/prefix-strategy.mjs +16 -53
- package/dist/prefix-strategy.mjs.map +1 -1
- package/dist/types.d.ts +23 -78
- package/package.json +5 -3
- package/dist/base-strategy-DDkINDnZ.js +0 -922
- package/dist/base-strategy-DDkINDnZ.js.map +0 -1
- package/dist/base-strategy-iCda2UF-.cjs +0 -2
- package/dist/base-strategy-iCda2UF-.cjs.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -2,9 +2,10 @@ import { I18nRouteParams } from '@i18n-micro/types';
|
|
|
2
2
|
import { Locale } from '@i18n-micro/types';
|
|
3
3
|
import { Strategies } from '@i18n-micro/types';
|
|
4
4
|
|
|
5
|
+
export declare function analyzeRoute(ctx: PathStrategyContext, route: ResolvedRouteLike): RouteAnalysis;
|
|
6
|
+
|
|
5
7
|
export declare abstract class BasePathStrategy implements PathStrategy {
|
|
6
|
-
|
|
7
|
-
protected resolver: RouteResolver;
|
|
8
|
+
readonly ctx: PathStrategyContext;
|
|
8
9
|
constructor(ctx: PathStrategyContext);
|
|
9
10
|
setRouter(router: RouterAdapter): void;
|
|
10
11
|
getDefaultLocale(): string;
|
|
@@ -15,142 +16,39 @@ export declare abstract class BasePathStrategy implements PathStrategy {
|
|
|
15
16
|
getRouteLocales(): PathStrategyContext['routeLocales'];
|
|
16
17
|
getRoutesLocaleLinks(): PathStrategyContext['routesLocaleLinks'];
|
|
17
18
|
getNoPrefixRedirect(): boolean | undefined;
|
|
18
|
-
/**
|
|
19
|
-
* Returns base route name without locale prefix/suffix.
|
|
20
|
-
* @param route - Route object
|
|
21
|
-
* @param locale - Optional locale to strip suffix for (if not provided, tries all locales)
|
|
22
|
-
*/
|
|
23
19
|
getRouteBaseName(route: RouteLike, locale?: string): string | null;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
* Returns normalized RouteLike object.
|
|
38
|
-
*/
|
|
39
|
-
protected preserveQueryAndHash(target: RouteLike | string, source?: RouteLike | null): RouteLike | string;
|
|
40
|
-
protected resolvePathWithParams(path: string, params?: Record<string, unknown>): string;
|
|
41
|
-
protected getPathWithoutLocaleAndBaseName(route: ResolvedRouteLike): {
|
|
42
|
-
pathWithoutLocale: string;
|
|
43
|
-
baseRouteName: string | null;
|
|
44
|
-
};
|
|
45
|
-
/** Look up custom path segment for targetLocale in globalLocaleRoutes. */
|
|
46
|
-
protected getCustomPathSegment(route: ResolvedRouteLike, targetLocale: string): string | null;
|
|
47
|
-
protected getAllowedLocalesForRoute(route: ResolvedRouteLike): string[];
|
|
48
|
-
getCanonicalPath(_route: ResolvedRouteLike, _targetLocale: string): string | null;
|
|
49
|
-
protected getPathForUnlocalizedRouteByName(routeName: string): string | null;
|
|
50
|
-
/**
|
|
51
|
-
* Try to resolve route by localized name. Returns RouteLike with query/hash from sourceRoute to preserve them.
|
|
52
|
-
*/
|
|
53
|
-
protected tryResolveByLocalizedName(routeName: string, targetLocale: string, sourceRoute?: RouteLike): RouteLike | null;
|
|
54
|
-
/**
|
|
55
|
-
* Try to resolve route by localized name with params. Returns RouteLike to preserve query/hash.
|
|
56
|
-
*/
|
|
57
|
-
protected tryResolveByLocalizedNameWithParams(routeName: string, targetLocale: string, params: Record<string, unknown>, sourceRoute?: RouteLike): RouteLike | null;
|
|
58
|
-
protected getPathForUnlocalizedRoute(route: ResolvedRouteLike): string | null;
|
|
59
|
-
/**
|
|
60
|
-
* Builds localized path from baseName + params when router does not have the route.
|
|
61
|
-
* Tries two conventions:
|
|
62
|
-
* 1) Hyphen form (Nuxt test-[id].vue → /test-:id): when single param key equals last baseName segment (e.g. test-id + id → test-:id).
|
|
63
|
-
* 2) Slash form (kebab→slash): path segments from baseName, last N replaced by :paramKey (e.g. test-id → /test/:id).
|
|
64
|
-
*/
|
|
65
|
-
protected buildPathFromBaseNameAndParams(baseName: string, params: Record<string, unknown>, targetLocale: string): string | null;
|
|
66
|
-
protected getPathWithoutLocale(path: string): {
|
|
20
|
+
getBaseRouteName(route: RouteLike, locale: string): string | null;
|
|
21
|
+
buildLocalizedName(baseName: string, locale: string): string;
|
|
22
|
+
buildLocalizedRouteName(baseName: string, locale: string): string;
|
|
23
|
+
resolveLocaleFromPath(path: string): string | null;
|
|
24
|
+
detectLocaleFromName(name: string | null): string | null;
|
|
25
|
+
abstract switchLocaleRoute(fromLocale: string, toLocale: string, route: ResolvedRouteLike, options: SwitchLocaleOptions): RouteLike | string;
|
|
26
|
+
abstract resolveLocaleRoute(targetLocale: string, normalized: NormalizedRouteInput, _currentRoute?: ResolvedRouteLike): RouteLike | string;
|
|
27
|
+
abstract getRedirect(currentPath: string, targetLocale: string): string | null;
|
|
28
|
+
abstract getClientRedirect(currentPath: string, preferredLocale: string): string | null;
|
|
29
|
+
buildLocalizedPath(path: string, locale: string, _isCustom: boolean): string;
|
|
30
|
+
formatPathForResolve(path: string, fromLocale: string, _toLocale: string): string;
|
|
31
|
+
resolvePathForLocale(path: string, targetLocale: string): string;
|
|
32
|
+
getPathWithoutLocale(path: string): {
|
|
67
33
|
pathWithoutLocale: string;
|
|
68
34
|
localeFromPath: string | null;
|
|
69
35
|
};
|
|
70
36
|
getLocaleFromPath(path: string): string | null;
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
* Use in middleware: strategy.getRedirect(to.fullPath, detectedLocale)
|
|
75
|
-
*/
|
|
76
|
-
abstract getRedirect(currentPath: string, targetLocale: string): string | null;
|
|
77
|
-
/**
|
|
78
|
-
* Returns path to redirect to for client-side navigation based on preferred locale.
|
|
79
|
-
* Returns null if no redirect needed. Each strategy implements its own logic.
|
|
80
|
-
*/
|
|
81
|
-
abstract getClientRedirect(currentPath: string, preferredLocale: string): string | null;
|
|
82
|
-
/**
|
|
83
|
-
* Checks if the current path should return 404.
|
|
84
|
-
* Returns error message if 404 should be returned, null otherwise.
|
|
85
|
-
* Base implementation checks unlocalized routes and routeLocales restrictions.
|
|
86
|
-
*/
|
|
87
|
-
shouldReturn404(currentPath: string): string | null;
|
|
88
|
-
/**
|
|
89
|
-
* Builds SEO attributes (canonical + hreflangs) from current route.
|
|
90
|
-
* Respects routeLocales: only allowed locales for this route get an hreflang entry.
|
|
91
|
-
* routesLocaleLinks is used when resolving the route key for routeLocales lookup.
|
|
92
|
-
*/
|
|
93
|
-
getSeoAttributes(currentRoute: ResolvedRouteLike): SeoAttributes;
|
|
94
|
-
/**
|
|
95
|
-
* Builds full URL (path + optional baseUrl for locale).
|
|
96
|
-
*/
|
|
97
|
-
protected buildFullUrl(localeCode: string, path: string): string;
|
|
98
|
-
/** When router knows neither targetName nor baseName — what to return (strategy may override). */
|
|
99
|
-
protected getSwitchLocaleFallbackWhenNoRoute(route: ResolvedRouteLike, targetName: string): RouteLike | string;
|
|
100
|
-
/**
|
|
101
|
-
* Default: baseName → buildLocalizedRouteName → hasRoute → applyBaseUrl; fallback to baseName.
|
|
102
|
-
*/
|
|
103
|
-
switchLocaleRoute(fromLocale: string, toLocale: string, route: ResolvedRouteLike, options: SwitchLocaleOptions): RouteLike | string;
|
|
104
|
-
/**
|
|
105
|
-
* Template Method: BaseStrategy knows "how" (normalize → delegate to strategy).
|
|
106
|
-
* Always returns RouteLike with path and fullPath (never a string).
|
|
107
|
-
*/
|
|
37
|
+
getCanonicalPath(route: ResolvedRouteLike, targetLocale: string): string | null;
|
|
38
|
+
applyBaseUrl(localeCode: string, route: RouteLike | string): RouteLike | string;
|
|
39
|
+
getSwitchLocaleFallbackWhenNoRoute(route: ResolvedRouteLike, targetName: string): RouteLike | string;
|
|
108
40
|
localeRoute(targetLocale: string, routeOrPath: RouteLike | string, currentRoute?: ResolvedRouteLike): RouteLike;
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
* Normalizes localeRoute input into a single structure (path string or route with resolved).
|
|
113
|
-
*/
|
|
114
|
-
protected normalizeRouteInput(routeOrPath: RouteLike | string, _currentRoute?: ResolvedRouteLike): NormalizedRouteInput;
|
|
115
|
-
/** Logging when ctx.debug (for localeRoute debugging). Disabled by default. */
|
|
116
|
-
private debugLog;
|
|
117
|
-
/**
|
|
118
|
-
* Default resolution: uses buildLocalizedPath and buildLocalizedRouteName.
|
|
119
|
-
* Strategies with different logic (e.g. prefix-except-default) override this method.
|
|
120
|
-
*/
|
|
121
|
-
protected resolveLocaleRoute(targetLocale: string, normalized: NormalizedRouteInput, _currentRoute?: ResolvedRouteLike): RouteLike | string;
|
|
122
|
-
/**
|
|
123
|
-
* Extracts locale from URL path by checking the first path segment.
|
|
124
|
-
*/
|
|
125
|
-
private extractLocaleFromPath;
|
|
126
|
-
/**
|
|
127
|
-
* Determines current locale from route, considering hashMode, noPrefix, prefix_and_default at /, etc.
|
|
128
|
-
*/
|
|
129
|
-
getCurrentLocale(route: ResolvedRouteLike, getDefaultLocale?: () => string | null | undefined): string;
|
|
130
|
-
/**
|
|
131
|
-
* Returns the route name for plugin translation loading.
|
|
132
|
-
* If disablePageLocales is true, returns 'index'.
|
|
133
|
-
*/
|
|
41
|
+
_ensureRouteLike(value: RouteLike | string, source?: RouteLike | null): RouteLike;
|
|
42
|
+
_normalizeRouteInput(routeOrPath: RouteLike | string, _currentRoute?: ResolvedRouteLike): NormalizedRouteInput;
|
|
43
|
+
getCurrentLocale(route: ResolvedRouteLike, defaultLocaleOverride?: string | null): string;
|
|
134
44
|
getPluginRouteName(route: ResolvedRouteLike, locale: string): string;
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
*/
|
|
138
|
-
getCurrentLocaleName(route: ResolvedRouteLike): string | null;
|
|
139
|
-
/**
|
|
140
|
-
* Formats path for router.resolve based on strategy.
|
|
141
|
-
* Each strategy should override this with its own logic.
|
|
142
|
-
*/
|
|
143
|
-
abstract formatPathForResolve(path: string, fromLocale: string, toLocale: string): string;
|
|
45
|
+
getCurrentLocaleName(route: ResolvedRouteLike, defaultLocaleOverride?: string | null): string | null;
|
|
46
|
+
shouldReturn404(currentPath: string): string | null;
|
|
144
47
|
}
|
|
145
48
|
|
|
146
49
|
/** Builds localized name: localized-{baseName}-{locale}. */
|
|
147
50
|
export declare function buildLocalizedName(baseName: string, locale: string, prefix?: string): string;
|
|
148
51
|
|
|
149
|
-
/**
|
|
150
|
-
* Creates localized route object preserving params, query, hash from source.
|
|
151
|
-
*/
|
|
152
|
-
export declare function createLocalizedRouteObject(name: string | undefined, path: string, fullPath: string, source: RouteLike | ResolvedRouteLike, resolvedParams?: Record<string, unknown>, resolvedQuery?: Record<string, unknown>, resolvedHash?: string): RouteLike;
|
|
153
|
-
|
|
154
52
|
/**
|
|
155
53
|
* Creates the appropriate path strategy instance for the given context.
|
|
156
54
|
* Used when all strategies are bundled (e.g. tests, server). For runtime client,
|
|
@@ -158,76 +56,43 @@ export declare function createLocalizedRouteObject(name: string | undefined, pat
|
|
|
158
56
|
*/
|
|
159
57
|
export declare function createPathStrategy(ctx: PathStrategyContext): PathStrategy;
|
|
160
58
|
|
|
161
|
-
/**
|
|
162
|
-
* Determines locale from first path segment.
|
|
163
|
-
*/
|
|
164
|
-
export declare function getLocaleFromPath(path: string, localeCodes: string[]): string | null;
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Parses path: if first segment is a known locale, returns path without it and locale code.
|
|
168
|
-
*/
|
|
169
|
-
export declare function getPathWithoutLocale(path: string, localeCodes: string[]): PathWithoutLocaleResult;
|
|
170
|
-
|
|
171
59
|
/**
|
|
172
60
|
* Base route name without prefix (localized-) and suffix (-{locale}).
|
|
173
|
-
* Suffix is stripped only when it is a full segment (preceded by hyphen),
|
|
174
|
-
* so names like product-screen are not broken when locale is en.
|
|
175
61
|
*/
|
|
176
|
-
export declare function getRouteBaseName(route:
|
|
62
|
+
export declare function getRouteBaseName(route: {
|
|
63
|
+
name?: string | null;
|
|
64
|
+
} & Record<string, any>, options: GetRouteBaseNameOptions): string | null;
|
|
177
65
|
|
|
178
66
|
export declare interface GetRouteBaseNameOptions {
|
|
179
|
-
locales:
|
|
67
|
+
locales: {
|
|
68
|
+
code: string;
|
|
69
|
+
}[];
|
|
180
70
|
localizedRouteNamePrefix?: string;
|
|
181
71
|
}
|
|
182
72
|
|
|
183
73
|
/** Custom path rules: route key -> locale -> path segment. false = unlocalized route. */
|
|
184
74
|
export declare type GlobalLocaleRoutes = Record<string, Record<string, string> | false | boolean>;
|
|
185
75
|
|
|
186
|
-
export declare interface HreflangTag {
|
|
187
|
-
rel: 'alternate';
|
|
188
|
-
href: string;
|
|
189
|
-
hreflang: string;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
76
|
/**
|
|
193
|
-
* Returns true if the given route name
|
|
194
|
-
* - Base name: 'index' or ''.
|
|
195
|
-
* - Full route name: 'index', or 'localized-index-{locale}' for any locale in localeCodes.
|
|
196
|
-
* Use this instead of ad-hoc checks like (name === 'index' || name.endsWith('-index') || name === 'localized-index-' + defaultLocale).
|
|
77
|
+
* Returns true if the given route name refers to the index (root) route.
|
|
197
78
|
*/
|
|
198
79
|
export declare function isIndexRouteName(name: string | null | undefined, options?: IsIndexRouteNameOptions): boolean;
|
|
199
80
|
|
|
200
81
|
export declare interface IsIndexRouteNameOptions {
|
|
201
82
|
localizedRouteNamePrefix?: string;
|
|
202
|
-
localeCodes?: string[];
|
|
83
|
+
localeCodes?: readonly string[];
|
|
203
84
|
}
|
|
204
85
|
|
|
205
86
|
export declare class NoPrefixPathStrategy extends BasePathStrategy {
|
|
206
|
-
|
|
207
|
-
protected buildLocalizedRouteName(baseName: string, locale: string): string;
|
|
87
|
+
buildLocalizedPath(path: string, _locale: string, _isCustom: boolean): string;
|
|
208
88
|
getCanonicalPath(route: ResolvedRouteLike, targetLocale: string): string | null;
|
|
209
89
|
resolveLocaleFromPath(_path: string): string | null;
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* In no_prefix strategy, URL should not contain locale. If path starts with a locale segment,
|
|
215
|
-
* redirect to path without it. Controlled by ctx.noPrefixRedirect (default: true).
|
|
216
|
-
*/
|
|
90
|
+
getSwitchLocaleFallbackWhenNoRoute(route: ResolvedRouteLike, _targetName: string): RouteLike | string;
|
|
91
|
+
switchLocaleRoute(fromLocale: string, toLocale: string, route: ResolvedRouteLike, options: SwitchLocaleOptions): RouteLike | string;
|
|
92
|
+
resolveLocaleRoute(targetLocale: string, normalized: NormalizedRouteInput, currentRoute?: ResolvedRouteLike): RouteLike | string;
|
|
217
93
|
getRedirect(currentPath: string, _targetLocale: string): string | null;
|
|
218
|
-
/**
|
|
219
|
-
* Formats path for router.resolve.
|
|
220
|
-
* no_prefix: path as-is, no locale prefix.
|
|
221
|
-
*/
|
|
222
94
|
formatPathForResolve(path: string, _fromLocale: string, _toLocale: string): string;
|
|
223
|
-
/**
|
|
224
|
-
* no_prefix strategy: never redirect based on locale preference.
|
|
225
|
-
* Locale is handled via cookie/useState only, not URL.
|
|
226
|
-
*/
|
|
227
95
|
getClientRedirect(_currentPath: string, _preferredLocale: string): string | null;
|
|
228
|
-
/**
|
|
229
|
-
* no_prefix strategy: no 404 checks related to locale prefix.
|
|
230
|
-
*/
|
|
231
96
|
shouldReturn404(_currentPath: string): string | null;
|
|
232
97
|
}
|
|
233
98
|
|
|
@@ -243,55 +108,21 @@ export declare type NormalizedRouteInput = {
|
|
|
243
108
|
};
|
|
244
109
|
|
|
245
110
|
export declare interface PathStrategy {
|
|
246
|
-
/**
|
|
247
|
-
* Returns a route object to navigate to another locale from the current page.
|
|
248
|
-
*/
|
|
249
111
|
switchLocaleRoute(fromLocale: string, toLocale: string, route: ResolvedRouteLike, options: SwitchLocaleOptions): RouteLike | string;
|
|
250
|
-
/**
|
|
251
|
-
* Localizes the given route (object or string) for the target locale.
|
|
252
|
-
* Always returns RouteLike (path and fullPath set). Analogue of $localeRoute / resolveLocalizedRoute.
|
|
253
|
-
*/
|
|
254
112
|
localeRoute(targetLocale: string, routeOrPath: RouteLike | string, currentRoute?: ResolvedRouteLike): RouteLike;
|
|
255
|
-
/**
|
|
256
|
-
* Checks if there is a custom path for the given route in globalLocaleRoutes.
|
|
257
|
-
* Used by redirect logic. Returns a path or null.
|
|
258
|
-
*/
|
|
259
113
|
getCanonicalPath(route: ResolvedRouteLike, targetLocale: string): string | null;
|
|
260
|
-
/**
|
|
261
|
-
* Determines locale from URL path (strategy-specific).
|
|
262
|
-
* Returns locale code or null if path does not contain a locale (e.g. no_prefix).
|
|
263
|
-
*/
|
|
264
114
|
resolveLocaleFromPath(path: string): string | null;
|
|
265
|
-
/**
|
|
266
|
-
* Tries to determine locale from URL path (first segment).
|
|
267
|
-
* Used for initial state and redirect logic. Base implementation in BasePathStrategy.
|
|
268
|
-
*/
|
|
269
115
|
getLocaleFromPath(path: string): string | null;
|
|
270
|
-
/**
|
|
271
|
-
* Returns path to redirect to for the given current path and target locale, or null if no redirect needed.
|
|
272
|
-
* Use in middleware: const redirectPath = strategy.getRedirect(to.fullPath, detectedLocale)
|
|
273
|
-
*/
|
|
274
116
|
getRedirect(currentPath: string, targetLocale: string): string | null;
|
|
275
|
-
/**
|
|
276
|
-
* Checks if the current path should return 404.
|
|
277
|
-
* Returns error message if 404 should be returned, null otherwise.
|
|
278
|
-
* Checks: invalid locale prefix, unlocalized routes with prefix, route locale restrictions.
|
|
279
|
-
*/
|
|
280
117
|
shouldReturn404(currentPath: string): string | null;
|
|
281
|
-
/**
|
|
282
|
-
* Returns path to redirect to for client-side navigation based on preferred locale.
|
|
283
|
-
* Returns null if no redirect needed.
|
|
284
|
-
* Used by client redirect plugin - each strategy implements its own logic.
|
|
285
|
-
*/
|
|
286
118
|
getClientRedirect(currentPath: string, preferredLocale: string): string | null;
|
|
287
|
-
/**
|
|
288
|
-
* Returns SEO attributes (canonical, hreflangs) for useHead.
|
|
289
|
-
*/
|
|
290
|
-
getSeoAttributes(currentRoute: ResolvedRouteLike): SeoAttributes;
|
|
291
|
-
/**
|
|
292
|
-
* Sets the router adapter (e.g. when creating singleton and passing router later).
|
|
293
|
-
*/
|
|
294
119
|
setRouter(router: RouterAdapter): void;
|
|
120
|
+
/** Determines current locale from route (considers strategy, hashMode, params, path). */
|
|
121
|
+
getCurrentLocale(route: ResolvedRouteLike, defaultLocaleOverride?: string | null): string;
|
|
122
|
+
/** Returns the base route name for translation loading. */
|
|
123
|
+
getPluginRouteName(route: ResolvedRouteLike, locale: string): string;
|
|
124
|
+
/** Returns displayName of the current locale, or null. */
|
|
125
|
+
getCurrentLocaleName(route: ResolvedRouteLike, defaultLocaleOverride?: string | null): string | null;
|
|
295
126
|
getDefaultLocale(): string;
|
|
296
127
|
getLocales(): Locale[];
|
|
297
128
|
getStrategy(): Strategies;
|
|
@@ -300,34 +131,7 @@ export declare interface PathStrategy {
|
|
|
300
131
|
getRouteLocales(): Record<string, string[]> | undefined;
|
|
301
132
|
getRoutesLocaleLinks(): Record<string, string> | undefined;
|
|
302
133
|
getNoPrefixRedirect(): boolean | undefined;
|
|
303
|
-
/**
|
|
304
|
-
* Determines current locale from route, considering hashMode, noPrefix, etc.
|
|
305
|
-
* @param route - Current route object
|
|
306
|
-
* @param getDefaultLocale - Optional getter for locale state (for hashMode/noPrefix)
|
|
307
|
-
*/
|
|
308
|
-
getCurrentLocale(route: ResolvedRouteLike, getDefaultLocale?: () => string | null | undefined): string;
|
|
309
|
-
/**
|
|
310
|
-
* Returns the route name for plugin translation loading.
|
|
311
|
-
* If disablePageLocales is true, returns 'index'.
|
|
312
|
-
*/
|
|
313
|
-
getPluginRouteName(route: ResolvedRouteLike, locale: string): string;
|
|
314
|
-
/**
|
|
315
|
-
* Returns base route name without locale prefix/suffix.
|
|
316
|
-
* @param route - Route object
|
|
317
|
-
* @param locale - Optional locale to strip suffix for (if not provided, tries all locales)
|
|
318
|
-
*/
|
|
319
134
|
getRouteBaseName(route: RouteLike, locale?: string): string | null;
|
|
320
|
-
/**
|
|
321
|
-
* Returns displayName of the current locale, or null if not found.
|
|
322
|
-
*/
|
|
323
|
-
getCurrentLocaleName(route: ResolvedRouteLike): string | null;
|
|
324
|
-
/**
|
|
325
|
-
* Formats path for router.resolve based on strategy.
|
|
326
|
-
* Returns path with or without locale prefix depending on strategy.
|
|
327
|
-
* @param path - Original path (e.g. '/about')
|
|
328
|
-
* @param fromLocale - Current locale
|
|
329
|
-
* @param toLocale - Target locale
|
|
330
|
-
*/
|
|
331
135
|
formatPathForResolve(path: string, fromLocale: string, toLocale: string): string;
|
|
332
136
|
}
|
|
333
137
|
|
|
@@ -335,6 +139,8 @@ export declare interface PathStrategyContext {
|
|
|
335
139
|
strategy: Strategies;
|
|
336
140
|
defaultLocale: string;
|
|
337
141
|
locales: Locale[];
|
|
142
|
+
/** Pre-computed locale codes (derived from locales). Set automatically at construction. */
|
|
143
|
+
localeCodes?: readonly string[];
|
|
338
144
|
localizedRouteNamePrefix: string;
|
|
339
145
|
globalLocaleRoutes?: GlobalLocaleRoutes;
|
|
340
146
|
routeLocales?: Record<string, string[]>;
|
|
@@ -346,11 +152,10 @@ export declare interface PathStrategyContext {
|
|
|
346
152
|
hashMode?: boolean;
|
|
347
153
|
/** When true, all pages use only global translations (no page-specific loading) */
|
|
348
154
|
disablePageLocales?: boolean;
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
localeFromPath: string | null;
|
|
155
|
+
/** Pre-computed: globalLocaleRoutes has at least one entry. Set automatically at construction. */
|
|
156
|
+
_hasGR?: boolean;
|
|
157
|
+
/** Pre-computed: routeLocales has at least one entry. Set automatically at construction. */
|
|
158
|
+
_hasRL?: boolean;
|
|
354
159
|
}
|
|
355
160
|
|
|
356
161
|
/**
|
|
@@ -358,86 +163,38 @@ export declare interface PathWithoutLocaleResult {
|
|
|
358
163
|
* We use prefixed route names for consistency (localized-name-en).
|
|
359
164
|
*/
|
|
360
165
|
export declare class PrefixAndDefaultPathStrategy extends BasePathStrategy {
|
|
361
|
-
protected buildLocalizedPath(path: string, locale: string, _isCustom: boolean): string;
|
|
362
|
-
protected buildLocalizedRouteName(baseName: string, locale: string): string;
|
|
363
166
|
switchLocaleRoute(fromLocale: string, toLocale: string, route: ResolvedRouteLike, options: SwitchLocaleOptions): RouteLike | string;
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
protected detectLocaleFromName(name: string | null): string | null;
|
|
367
|
-
resolveLocaleFromPath(path: string): string | null;
|
|
167
|
+
private rewriteWithLocalePrefix;
|
|
168
|
+
resolveLocaleRoute(targetLocale: string, normalized: NormalizedRouteInput, currentRoute?: ResolvedRouteLike): RouteLike | string;
|
|
368
169
|
getRedirect(currentPath: string, detectedLocale: string): string | null;
|
|
369
|
-
private buildPathWithPrefix;
|
|
370
|
-
/**
|
|
371
|
-
* Formats path for router.resolve.
|
|
372
|
-
* prefix_and_default: always add locale prefix.
|
|
373
|
-
*/
|
|
374
|
-
formatPathForResolve(path: string, fromLocale: string, _toLocale: string): string;
|
|
375
|
-
/**
|
|
376
|
-
* prefix_and_default: both / and /locale are valid for any locale.
|
|
377
|
-
* Does NOT redirect if user explicitly navigates to a locale path.
|
|
378
|
-
* Only redirects from paths without locale prefix.
|
|
379
|
-
*/
|
|
380
170
|
getClientRedirect(currentPath: string, _preferredLocale: string): string | null;
|
|
381
171
|
}
|
|
382
172
|
|
|
383
173
|
export declare class PrefixExceptDefaultPathStrategy extends BasePathStrategy {
|
|
384
|
-
/**
|
|
385
|
-
* For this strategy a prefix is required for all non-default locales.
|
|
386
|
-
*/
|
|
387
174
|
protected shouldHavePrefix(locale: string): boolean;
|
|
388
|
-
|
|
389
|
-
|
|
175
|
+
buildLocalizedPath(path: string, locale: string, _isCustom: boolean): string;
|
|
176
|
+
buildLocalizedRouteName(baseName: string, locale: string): string;
|
|
390
177
|
switchLocaleRoute(fromLocale: string, toLocale: string, route: ResolvedRouteLike, options: SwitchLocaleOptions): RouteLike | string;
|
|
391
|
-
|
|
178
|
+
resolveLocaleRoute(targetLocale: string, normalized: NormalizedRouteInput, currentRoute?: ResolvedRouteLike): RouteLike | string;
|
|
179
|
+
private resolveLocaleRouteSimple;
|
|
180
|
+
private resolveLocaleRouteFull;
|
|
181
|
+
private resolveLocaleRouteFallback;
|
|
392
182
|
getCanonicalPath(route: ResolvedRouteLike, targetLocale: string): string | null;
|
|
393
183
|
resolveLocaleFromPath(path: string): string | null;
|
|
394
184
|
getRedirect(currentPath: string, detectedLocale: string): string | null;
|
|
395
185
|
shouldReturn404(currentPath: string): string | null;
|
|
396
|
-
/** True if gr[key] is a locale rules object (Record<locale, path>). */
|
|
397
186
|
private isLocaleRules;
|
|
398
|
-
/**
|
|
399
|
-
* For a nested route name (e.g. activity-locale-hiking), returns parent key and slash-key
|
|
400
|
-
* when the route is nested (child or parent in globalLocaleRoutes). Otherwise null.
|
|
401
|
-
*/
|
|
402
187
|
private getNestedRouteInfo;
|
|
403
|
-
|
|
188
|
+
private buildNestedCustomResult;
|
|
404
189
|
private getParentPathForTarget;
|
|
405
|
-
private buildPathWithPrefix;
|
|
406
|
-
/**
|
|
407
|
-
* Simple locale detector based on route name suffix.
|
|
408
|
-
* Looks for known locale codes at the end of the name.
|
|
409
|
-
*/
|
|
410
|
-
protected detectLocaleFromName(name: string | null): string | null;
|
|
411
|
-
/**
|
|
412
|
-
* Formats path for router.resolve.
|
|
413
|
-
* prefix_except_default: add prefix only for non-default locale.
|
|
414
|
-
*/
|
|
415
190
|
formatPathForResolve(path: string, fromLocale: string, toLocale: string): string;
|
|
416
|
-
/**
|
|
417
|
-
* prefix_except_default: redirect based on preferred locale.
|
|
418
|
-
* Uses shouldHavePrefix to determine if locale needs prefix.
|
|
419
|
-
* Also handles custom paths from globalLocaleRoutes.
|
|
420
|
-
*/
|
|
421
191
|
getClientRedirect(currentPath: string, preferredLocale: string): string | null;
|
|
422
192
|
}
|
|
423
193
|
|
|
424
194
|
export declare class PrefixPathStrategy extends BasePathStrategy {
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
getCanonicalPath(route: ResolvedRouteLike, targetLocale: string): string | null;
|
|
428
|
-
resolveLocaleFromPath(path: string): string | null;
|
|
195
|
+
switchLocaleRoute(fromLocale: string, toLocale: string, route: ResolvedRouteLike, options: SwitchLocaleOptions): RouteLike | string;
|
|
196
|
+
resolveLocaleRoute(targetLocale: string, normalized: NormalizedRouteInput, currentRoute?: ResolvedRouteLike): RouteLike | string;
|
|
429
197
|
getRedirect(currentPath: string, detectedLocale: string): string | null;
|
|
430
|
-
private buildPathWithPrefix;
|
|
431
|
-
/**
|
|
432
|
-
* Formats path for router.resolve.
|
|
433
|
-
* prefix: always add locale prefix.
|
|
434
|
-
*/
|
|
435
|
-
formatPathForResolve(path: string, fromLocale: string, _toLocale: string): string;
|
|
436
|
-
/**
|
|
437
|
-
* prefix strategy: redirect only if URL has no locale prefix.
|
|
438
|
-
* Does NOT redirect if user explicitly navigates to a locale path.
|
|
439
|
-
* Uses custom paths from globalLocaleRoutes when available.
|
|
440
|
-
*/
|
|
441
198
|
getClientRedirect(currentPath: string, preferredLocale: string): string | null;
|
|
442
199
|
}
|
|
443
200
|
|
|
@@ -448,8 +205,10 @@ export declare interface ResolvedRouteLike extends RouteLike {
|
|
|
448
205
|
fullPath: string;
|
|
449
206
|
}
|
|
450
207
|
|
|
451
|
-
|
|
452
|
-
|
|
208
|
+
export declare interface RouteAnalysis {
|
|
209
|
+
pathWithoutLocale: string;
|
|
210
|
+
baseRouteName: string | null;
|
|
211
|
+
}
|
|
453
212
|
|
|
454
213
|
/** Route-like shape (compatible with vue-router). */
|
|
455
214
|
export declare interface RouteLike {
|
|
@@ -467,54 +226,6 @@ export declare interface RouterAdapter {
|
|
|
467
226
|
getRoutes?: () => unknown[];
|
|
468
227
|
}
|
|
469
228
|
|
|
470
|
-
export declare class RouteResolver {
|
|
471
|
-
private ctx;
|
|
472
|
-
constructor(ctx: PathStrategyContext);
|
|
473
|
-
/**
|
|
474
|
-
* Substitutes params into path template (:key, :key(), [...key]).
|
|
475
|
-
* Uses a single pre-compiled regex for all keys in one pass — no per-key RegExp allocation.
|
|
476
|
-
*/
|
|
477
|
-
resolvePathWithParams(path: string, params?: Record<string, unknown>): string;
|
|
478
|
-
/**
|
|
479
|
-
* Analyzes route: path without locale and base name (normalizer + route-name).
|
|
480
|
-
*/
|
|
481
|
-
private analyzeRoute;
|
|
482
|
-
/** Public access to route analysis (for strategies). */
|
|
483
|
-
getPathWithoutLocaleAndBaseName(route: ResolvedRouteLike): {
|
|
484
|
-
pathWithoutLocale: string;
|
|
485
|
-
baseRouteName: string | null;
|
|
486
|
-
};
|
|
487
|
-
/**
|
|
488
|
-
* Lookup keys for config (same order for resolveCustomPath, getPathForUnlocalizedRoute, getAllowedLocalesForRoute).
|
|
489
|
-
*/
|
|
490
|
-
private getLookupKeys;
|
|
491
|
-
/**
|
|
492
|
-
* Resolves custom path for targetLocale from globalLocaleRoutes.
|
|
493
|
-
*/
|
|
494
|
-
resolveCustomPath(route: ResolvedRouteLike, targetLocale: string): string | null;
|
|
495
|
-
/**
|
|
496
|
-
* Unlocalized route (globalLocaleRoutes[key] === false) — returns path without locale.
|
|
497
|
-
*/
|
|
498
|
-
getPathForUnlocalizedRoute(route: ResolvedRouteLike): string | null;
|
|
499
|
-
/**
|
|
500
|
-
* Unlocalized by name (when no route object available).
|
|
501
|
-
*/
|
|
502
|
-
getPathForUnlocalizedRouteByName(routeName: string): string | null;
|
|
503
|
-
/**
|
|
504
|
-
* Allowed locales for route (routeLocales).
|
|
505
|
-
*/
|
|
506
|
-
getAllowedLocalesForRoute(route: ResolvedRouteLike): string[];
|
|
507
|
-
/**
|
|
508
|
-
* Parent path for nested route (parent key -> targetLocale path).
|
|
509
|
-
*/
|
|
510
|
-
getParentPathForNested(nameSegments: string[], targetLocale: string): string;
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
export declare interface SeoAttributes {
|
|
514
|
-
canonical?: string;
|
|
515
|
-
hreflangs?: HreflangTag[];
|
|
516
|
-
}
|
|
517
|
-
|
|
518
229
|
export declare interface SwitchLocaleOptions {
|
|
519
230
|
i18nRouteParams?: I18nRouteParams;
|
|
520
231
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,45 +1,32 @@
|
|
|
1
|
-
import { B as
|
|
2
|
-
import { NoPrefixPathStrategy as
|
|
3
|
-
import { PrefixPathStrategy as
|
|
4
|
-
import { PrefixAndDefaultPathStrategy as
|
|
5
|
-
import { PrefixExceptDefaultPathStrategy as
|
|
6
|
-
function
|
|
7
|
-
return {
|
|
8
|
-
...e ? { name: e } : {},
|
|
9
|
-
path: r,
|
|
10
|
-
fullPath: o,
|
|
11
|
-
params: f ?? t.params ?? {},
|
|
12
|
-
query: i ?? t.query ?? {},
|
|
13
|
-
hash: t.hash ?? s ?? ""
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
function g(e) {
|
|
1
|
+
import { B as P, a as g, b as d, g as l, i as x } from "./base-strategy-CSEFam3u.js";
|
|
2
|
+
import { NoPrefixPathStrategy as a } from "./no-prefix-strategy.mjs";
|
|
3
|
+
import { PrefixPathStrategy as r } from "./prefix-strategy.mjs";
|
|
4
|
+
import { PrefixAndDefaultPathStrategy as f } from "./prefix-and-default-strategy.mjs";
|
|
5
|
+
import { PrefixExceptDefaultPathStrategy as t } from "./prefix-except-default-strategy.mjs";
|
|
6
|
+
function s(e) {
|
|
17
7
|
switch (e.strategy) {
|
|
18
8
|
case "no_prefix":
|
|
19
|
-
return new
|
|
9
|
+
return new a(e);
|
|
20
10
|
case "prefix":
|
|
21
|
-
return new
|
|
11
|
+
return new r(e);
|
|
22
12
|
case "prefix_except_default":
|
|
23
|
-
return new
|
|
13
|
+
return new t(e);
|
|
24
14
|
case "prefix_and_default":
|
|
25
|
-
return new
|
|
15
|
+
return new f(e);
|
|
26
16
|
default:
|
|
27
|
-
return new
|
|
17
|
+
return new t(e);
|
|
28
18
|
}
|
|
29
19
|
}
|
|
30
20
|
export {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
_ as getPathWithoutLocale,
|
|
42
|
-
L as getRouteBaseName,
|
|
43
|
-
N as isIndexRouteName
|
|
21
|
+
P as BasePathStrategy,
|
|
22
|
+
a as NoPrefixPathStrategy,
|
|
23
|
+
f as PrefixAndDefaultPathStrategy,
|
|
24
|
+
t as PrefixExceptDefaultPathStrategy,
|
|
25
|
+
r as PrefixPathStrategy,
|
|
26
|
+
g as analyzeRoute,
|
|
27
|
+
d as buildLocalizedName,
|
|
28
|
+
s as createPathStrategy,
|
|
29
|
+
l as getRouteBaseName,
|
|
30
|
+
x as isIndexRouteName
|
|
44
31
|
};
|
|
45
32
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../src/
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/strategies/factory.ts"],"sourcesContent":["import type { PathStrategy, PathStrategyContext } from '../types'\nimport { NoPrefixPathStrategy } from './no-prefix'\nimport { PrefixPathStrategy } from './prefix'\nimport { PrefixAndDefaultPathStrategy } from './prefix-and-default'\nimport { PrefixExceptDefaultPathStrategy } from './prefix-except-default'\n\n/**\n * Creates the appropriate path strategy instance for the given context.\n * Used when all strategies are bundled (e.g. tests, server). For runtime client,\n * use Nuxt alias #i18n-strategy to import only the selected strategy.\n */\nexport function createPathStrategy(ctx: PathStrategyContext): PathStrategy {\n switch (ctx.strategy) {\n case 'no_prefix':\n return new NoPrefixPathStrategy(ctx)\n case 'prefix':\n return new PrefixPathStrategy(ctx)\n case 'prefix_except_default':\n return new PrefixExceptDefaultPathStrategy(ctx)\n case 'prefix_and_default':\n return new PrefixAndDefaultPathStrategy(ctx)\n default:\n return new PrefixExceptDefaultPathStrategy(ctx)\n }\n}\n"],"names":["createPathStrategy","ctx","NoPrefixPathStrategy","PrefixPathStrategy","PrefixExceptDefaultPathStrategy","PrefixAndDefaultPathStrategy"],"mappings":";;;;;AAWO,SAASA,EAAmBC,GAAwC;AACzE,UAAQA,EAAI,UAAA;AAAA,IACV,KAAK;AACH,aAAO,IAAIC,EAAqBD,CAAG;AAAA,IACrC,KAAK;AACH,aAAO,IAAIE,EAAmBF,CAAG;AAAA,IACnC,KAAK;AACH,aAAO,IAAIG,EAAgCH,CAAG;AAAA,IAChD,KAAK;AACH,aAAO,IAAII,EAA6BJ,CAAG;AAAA,IAC7C;AACE,aAAO,IAAIG,EAAgCH,CAAG;AAAA,EAAA;AAEpD;"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("./base-strategy-BHkH418r.cjs"),n=require("./common-sNHyO9pg.cjs");class u extends o.BasePathStrategy{buildLocalizedPath(e,r,t){return o.normalizePath(e)}getCanonicalPath(e,r){const t=o.resolveCustomPath(this.ctx,e,r)??"";return t?t.charCodeAt(0)===47?t:`/${t}`:null}resolveLocaleFromPath(e){return null}getSwitchLocaleFallbackWhenNoRoute(e,r){return e}switchLocaleRoute(e,r,t,l){return n.defaultSwitchLocaleRoute(this,e,r,t,l)}resolveLocaleRoute(e,r,t){return n.defaultResolveLocaleRoute(this,e,r)}getRedirect(e,r){if(this.ctx.noPrefixRedirect===!1)return null;const t=this.getLocaleFromPath(e);if(!t)return null;const l=`/${t}`;let a=e.slice(l.length);return(!a||a.charCodeAt(0)!==47)&&(a=`/${a||""}`),o.cleanDoubleSlashes(a)||"/"}formatPathForResolve(e,r,t){return e}getClientRedirect(e,r){return null}shouldReturn404(e){return null}}exports.NoPrefixPathStrategy=u;exports.Strategy=u;
|
|
2
2
|
//# sourceMappingURL=no-prefix-strategy.cjs.map
|