@i18n-micro/path-strategy 1.1.3 → 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.
Files changed (40) hide show
  1. package/README.md +75 -29
  2. package/dist/base-strategy-BHkH418r.cjs +2 -0
  3. package/dist/base-strategy-BHkH418r.cjs.map +1 -0
  4. package/dist/base-strategy-CSEFam3u.js +585 -0
  5. package/dist/base-strategy-CSEFam3u.js.map +1 -0
  6. package/dist/common-BaGdobUC.js +114 -0
  7. package/dist/common-BaGdobUC.js.map +1 -0
  8. package/dist/common-sNHyO9pg.cjs +2 -0
  9. package/dist/common-sNHyO9pg.cjs.map +1 -0
  10. package/dist/index.cjs +1 -1
  11. package/dist/index.cjs.map +1 -1
  12. package/dist/index.d.ts +63 -352
  13. package/dist/index.mjs +21 -34
  14. package/dist/index.mjs.map +1 -1
  15. package/dist/no-prefix-strategy.cjs +1 -1
  16. package/dist/no-prefix-strategy.cjs.map +1 -1
  17. package/dist/no-prefix-strategy.d.ts +38 -262
  18. package/dist/no-prefix-strategy.mjs +17 -32
  19. package/dist/no-prefix-strategy.mjs.map +1 -1
  20. package/dist/prefix-and-default-strategy.cjs +1 -1
  21. package/dist/prefix-and-default-strategy.cjs.map +1 -1
  22. package/dist/prefix-and-default-strategy.d.ts +36 -259
  23. package/dist/prefix-and-default-strategy.mjs +75 -125
  24. package/dist/prefix-and-default-strategy.mjs.map +1 -1
  25. package/dist/prefix-except-default-strategy.cjs +1 -1
  26. package/dist/prefix-except-default-strategy.cjs.map +1 -1
  27. package/dist/prefix-except-default-strategy.d.ts +41 -269
  28. package/dist/prefix-except-default-strategy.mjs +228 -251
  29. package/dist/prefix-except-default-strategy.mjs.map +1 -1
  30. package/dist/prefix-strategy.cjs +1 -1
  31. package/dist/prefix-strategy.cjs.map +1 -1
  32. package/dist/prefix-strategy.d.ts +36 -257
  33. package/dist/prefix-strategy.mjs +16 -53
  34. package/dist/prefix-strategy.mjs.map +1 -1
  35. package/dist/types.d.ts +23 -78
  36. package/package.json +6 -4
  37. package/dist/base-strategy-BNdOdpRE.cjs +0 -2
  38. package/dist/base-strategy-BNdOdpRE.cjs.map +0 -1
  39. package/dist/base-strategy-DmOQzfP1.js +0 -922
  40. package/dist/base-strategy-DmOQzfP1.js.map +0 -1
@@ -3,8 +3,7 @@ import { Locale } from '@i18n-micro/types';
3
3
  import { Strategies } from '@i18n-micro/types';
4
4
 
5
5
  declare abstract class BasePathStrategy implements PathStrategy {
6
- protected ctx: PathStrategyContext;
7
- protected resolver: RouteResolver;
6
+ readonly ctx: PathStrategyContext;
8
7
  constructor(ctx: PathStrategyContext);
9
8
  setRouter(router: RouterAdapter): void;
10
9
  getDefaultLocale(): string;
@@ -15,143 +14,39 @@ declare abstract class BasePathStrategy implements PathStrategy {
15
14
  getRouteLocales(): PathStrategyContext['routeLocales'];
16
15
  getRoutesLocaleLinks(): PathStrategyContext['routesLocaleLinks'];
17
16
  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
17
  getRouteBaseName(route: RouteLike, locale?: string): string | null;
24
- /** Alias for internal use - strips localization prefix/suffix for specific locale. */
25
- protected getBaseRouteName(route: RouteLike, locale: string): string | null;
26
- /** Resolves target path for a locale, checking globalLocaleRoutes first. */
27
- protected resolvePathForLocale(path: string, targetLocale: string): string;
28
- protected buildLocalizedName(baseName: string, locale: string): string;
29
- /** Builds path for target locale (strategy decides: with or without prefix). */
30
- protected abstract buildLocalizedPath(path: string, locale: string, isCustom: boolean): string;
31
- /** Builds localized route name for target locale. */
32
- protected abstract buildLocalizedRouteName(baseName: string, locale: string): string;
33
- protected getLocaleObject(code: string): Locale | undefined;
34
- protected applyBaseUrl(localeCode: string, route: RouteLike | string): RouteLike | string;
35
- /**
36
- * Merges target route (strategy result) with query and hash from source route.
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): {
18
+ getBaseRouteName(route: RouteLike, locale: string): string | null;
19
+ buildLocalizedName(baseName: string, locale: string): string;
20
+ buildLocalizedRouteName(baseName: string, locale: string): string;
21
+ resolveLocaleFromPath(path: string): string | null;
22
+ detectLocaleFromName(name: string | null): string | null;
23
+ abstract switchLocaleRoute(fromLocale: string, toLocale: string, route: ResolvedRouteLike, options: SwitchLocaleOptions): RouteLike | string;
24
+ abstract resolveLocaleRoute(targetLocale: string, normalized: NormalizedRouteInput, _currentRoute?: ResolvedRouteLike): RouteLike | string;
25
+ abstract getRedirect(currentPath: string, targetLocale: string): string | null;
26
+ abstract getClientRedirect(currentPath: string, preferredLocale: string): string | null;
27
+ buildLocalizedPath(path: string, locale: string, _isCustom: boolean): string;
28
+ formatPathForResolve(path: string, fromLocale: string, _toLocale: string): string;
29
+ resolvePathForLocale(path: string, targetLocale: string): string;
30
+ getPathWithoutLocale(path: string): {
67
31
  pathWithoutLocale: string;
68
32
  localeFromPath: string | null;
69
33
  };
70
34
  getLocaleFromPath(path: string): string | null;
71
- abstract resolveLocaleFromPath(path: string): string | null;
72
- /**
73
- * Returns path to redirect to, or null if no redirect needed.
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
- */
35
+ getCanonicalPath(route: ResolvedRouteLike, targetLocale: string): string | null;
36
+ applyBaseUrl(localeCode: string, route: RouteLike | string): RouteLike | string;
37
+ getSwitchLocaleFallbackWhenNoRoute(route: ResolvedRouteLike, targetName: string): RouteLike | string;
108
38
  localeRoute(targetLocale: string, routeOrPath: RouteLike | string, currentRoute?: ResolvedRouteLike): RouteLike;
109
- /** Normalizes resolveLocaleRoute result into RouteLike (path and fullPath always set). */
110
- protected ensureRouteLike(value: RouteLike | string, source?: RouteLike | null): RouteLike;
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 'general'.
133
- */
39
+ _ensureRouteLike(value: RouteLike | string, source?: RouteLike | null): RouteLike;
40
+ _normalizeRouteInput(routeOrPath: RouteLike | string, _currentRoute?: ResolvedRouteLike): NormalizedRouteInput;
41
+ getCurrentLocale(route: ResolvedRouteLike, defaultLocaleOverride?: string | null): string;
134
42
  getPluginRouteName(route: ResolvedRouteLike, locale: string): string;
135
- /**
136
- * Returns displayName of the current locale, or null if not found.
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;
43
+ getCurrentLocaleName(route: ResolvedRouteLike, defaultLocaleOverride?: string | null): string | null;
44
+ shouldReturn404(currentPath: string): string | null;
144
45
  }
145
46
 
146
47
  /** Custom path rules: route key -> locale -> path segment. false = unlocalized route. */
147
48
  declare type GlobalLocaleRoutes = Record<string, Record<string, string> | false | boolean>;
148
49
 
149
- declare interface HreflangTag {
150
- rel: 'alternate';
151
- href: string;
152
- hreflang: string;
153
- }
154
-
155
50
  /** Normalized input for resolveLocaleRoute (template method). */
156
51
  declare type NormalizedRouteInput = {
157
52
  kind: 'path';
@@ -164,55 +59,21 @@ declare type NormalizedRouteInput = {
164
59
  };
165
60
 
166
61
  declare interface PathStrategy {
167
- /**
168
- * Returns a route object to navigate to another locale from the current page.
169
- */
170
62
  switchLocaleRoute(fromLocale: string, toLocale: string, route: ResolvedRouteLike, options: SwitchLocaleOptions): RouteLike | string;
171
- /**
172
- * Localizes the given route (object or string) for the target locale.
173
- * Always returns RouteLike (path and fullPath set). Analogue of $localeRoute / resolveLocalizedRoute.
174
- */
175
63
  localeRoute(targetLocale: string, routeOrPath: RouteLike | string, currentRoute?: ResolvedRouteLike): RouteLike;
176
- /**
177
- * Checks if there is a custom path for the given route in globalLocaleRoutes.
178
- * Used by redirect logic. Returns a path or null.
179
- */
180
64
  getCanonicalPath(route: ResolvedRouteLike, targetLocale: string): string | null;
181
- /**
182
- * Determines locale from URL path (strategy-specific).
183
- * Returns locale code or null if path does not contain a locale (e.g. no_prefix).
184
- */
185
65
  resolveLocaleFromPath(path: string): string | null;
186
- /**
187
- * Tries to determine locale from URL path (first segment).
188
- * Used for initial state and redirect logic. Base implementation in BasePathStrategy.
189
- */
190
66
  getLocaleFromPath(path: string): string | null;
191
- /**
192
- * Returns path to redirect to for the given current path and target locale, or null if no redirect needed.
193
- * Use in middleware: const redirectPath = strategy.getRedirect(to.fullPath, detectedLocale)
194
- */
195
67
  getRedirect(currentPath: string, targetLocale: string): string | null;
196
- /**
197
- * Checks if the current path should return 404.
198
- * Returns error message if 404 should be returned, null otherwise.
199
- * Checks: invalid locale prefix, unlocalized routes with prefix, route locale restrictions.
200
- */
201
68
  shouldReturn404(currentPath: string): string | null;
202
- /**
203
- * Returns path to redirect to for client-side navigation based on preferred locale.
204
- * Returns null if no redirect needed.
205
- * Used by client redirect plugin - each strategy implements its own logic.
206
- */
207
69
  getClientRedirect(currentPath: string, preferredLocale: string): string | null;
208
- /**
209
- * Returns SEO attributes (canonical, hreflangs) for useHead.
210
- */
211
- getSeoAttributes(currentRoute: ResolvedRouteLike): SeoAttributes;
212
- /**
213
- * Sets the router adapter (e.g. when creating singleton and passing router later).
214
- */
215
70
  setRouter(router: RouterAdapter): void;
71
+ /** Determines current locale from route (considers strategy, hashMode, params, path). */
72
+ getCurrentLocale(route: ResolvedRouteLike, defaultLocaleOverride?: string | null): string;
73
+ /** Returns the base route name for translation loading. */
74
+ getPluginRouteName(route: ResolvedRouteLike, locale: string): string;
75
+ /** Returns displayName of the current locale, or null. */
76
+ getCurrentLocaleName(route: ResolvedRouteLike, defaultLocaleOverride?: string | null): string | null;
216
77
  getDefaultLocale(): string;
217
78
  getLocales(): Locale[];
218
79
  getStrategy(): Strategies;
@@ -221,34 +82,7 @@ declare interface PathStrategy {
221
82
  getRouteLocales(): Record<string, string[]> | undefined;
222
83
  getRoutesLocaleLinks(): Record<string, string> | undefined;
223
84
  getNoPrefixRedirect(): boolean | undefined;
224
- /**
225
- * Determines current locale from route, considering hashMode, noPrefix, etc.
226
- * @param route - Current route object
227
- * @param getDefaultLocale - Optional getter for locale state (for hashMode/noPrefix)
228
- */
229
- getCurrentLocale(route: ResolvedRouteLike, getDefaultLocale?: () => string | null | undefined): string;
230
- /**
231
- * Returns the route name for plugin translation loading.
232
- * If disablePageLocales is true, returns 'general'.
233
- */
234
- getPluginRouteName(route: ResolvedRouteLike, locale: string): string;
235
- /**
236
- * Returns base route name without locale prefix/suffix.
237
- * @param route - Route object
238
- * @param locale - Optional locale to strip suffix for (if not provided, tries all locales)
239
- */
240
85
  getRouteBaseName(route: RouteLike, locale?: string): string | null;
241
- /**
242
- * Returns displayName of the current locale, or null if not found.
243
- */
244
- getCurrentLocaleName(route: ResolvedRouteLike): string | null;
245
- /**
246
- * Formats path for router.resolve based on strategy.
247
- * Returns path with or without locale prefix depending on strategy.
248
- * @param path - Original path (e.g. '/about')
249
- * @param fromLocale - Current locale
250
- * @param toLocale - Target locale
251
- */
252
86
  formatPathForResolve(path: string, fromLocale: string, toLocale: string): string;
253
87
  }
254
88
 
@@ -256,6 +90,8 @@ declare interface PathStrategyContext {
256
90
  strategy: Strategies;
257
91
  defaultLocale: string;
258
92
  locales: Locale[];
93
+ /** Pre-computed locale codes (derived from locales). Set automatically at construction. */
94
+ localeCodes?: readonly string[];
259
95
  localizedRouteNamePrefix: string;
260
96
  globalLocaleRoutes?: GlobalLocaleRoutes;
261
97
  routeLocales?: Record<string, string[]>;
@@ -267,6 +103,10 @@ declare interface PathStrategyContext {
267
103
  hashMode?: boolean;
268
104
  /** When true, all pages use only global translations (no page-specific loading) */
269
105
  disablePageLocales?: boolean;
106
+ /** Pre-computed: globalLocaleRoutes has at least one entry. Set automatically at construction. */
107
+ _hasGR?: boolean;
108
+ /** Pre-computed: routeLocales has at least one entry. Set automatically at construction. */
109
+ _hasRL?: boolean;
270
110
  }
271
111
 
272
112
  /**
@@ -274,25 +114,10 @@ declare interface PathStrategyContext {
274
114
  * We use prefixed route names for consistency (localized-name-en).
275
115
  */
276
116
  declare class PrefixAndDefaultPathStrategy extends BasePathStrategy {
277
- protected buildLocalizedPath(path: string, locale: string, _isCustom: boolean): string;
278
- protected buildLocalizedRouteName(baseName: string, locale: string): string;
279
117
  switchLocaleRoute(fromLocale: string, toLocale: string, route: ResolvedRouteLike, options: SwitchLocaleOptions): RouteLike | string;
280
- protected resolveLocaleRoute(targetLocale: string, normalized: NormalizedRouteInput, currentRoute?: ResolvedRouteLike): RouteLike | string;
281
- getCanonicalPath(route: ResolvedRouteLike, targetLocale: string): string | null;
282
- protected detectLocaleFromName(name: string | null): string | null;
283
- resolveLocaleFromPath(path: string): string | null;
118
+ private rewriteWithLocalePrefix;
119
+ resolveLocaleRoute(targetLocale: string, normalized: NormalizedRouteInput, currentRoute?: ResolvedRouteLike): RouteLike | string;
284
120
  getRedirect(currentPath: string, detectedLocale: string): string | null;
285
- private buildPathWithPrefix;
286
- /**
287
- * Formats path for router.resolve.
288
- * prefix_and_default: always add locale prefix.
289
- */
290
- formatPathForResolve(path: string, fromLocale: string, _toLocale: string): string;
291
- /**
292
- * prefix_and_default: both / and /locale are valid for any locale.
293
- * Does NOT redirect if user explicitly navigates to a locale path.
294
- * Only redirects from paths without locale prefix.
295
- */
296
121
  getClientRedirect(currentPath: string, _preferredLocale: string): string | null;
297
122
  }
298
123
  export { PrefixAndDefaultPathStrategy }
@@ -321,54 +146,6 @@ declare interface RouterAdapter {
321
146
  getRoutes?: () => unknown[];
322
147
  }
323
148
 
324
- declare class RouteResolver {
325
- private ctx;
326
- constructor(ctx: PathStrategyContext);
327
- /**
328
- * Substitutes params into path template (:key, :key(), [...key]).
329
- * Uses a single pre-compiled regex for all keys in one pass — no per-key RegExp allocation.
330
- */
331
- resolvePathWithParams(path: string, params?: Record<string, unknown>): string;
332
- /**
333
- * Analyzes route: path without locale and base name (normalizer + route-name).
334
- */
335
- private analyzeRoute;
336
- /** Public access to route analysis (for strategies). */
337
- getPathWithoutLocaleAndBaseName(route: ResolvedRouteLike): {
338
- pathWithoutLocale: string;
339
- baseRouteName: string | null;
340
- };
341
- /**
342
- * Lookup keys for config (same order for resolveCustomPath, getPathForUnlocalizedRoute, getAllowedLocalesForRoute).
343
- */
344
- private getLookupKeys;
345
- /**
346
- * Resolves custom path for targetLocale from globalLocaleRoutes.
347
- */
348
- resolveCustomPath(route: ResolvedRouteLike, targetLocale: string): string | null;
349
- /**
350
- * Unlocalized route (globalLocaleRoutes[key] === false) — returns path without locale.
351
- */
352
- getPathForUnlocalizedRoute(route: ResolvedRouteLike): string | null;
353
- /**
354
- * Unlocalized by name (when no route object available).
355
- */
356
- getPathForUnlocalizedRouteByName(routeName: string): string | null;
357
- /**
358
- * Allowed locales for route (routeLocales).
359
- */
360
- getAllowedLocalesForRoute(route: ResolvedRouteLike): string[];
361
- /**
362
- * Parent path for nested route (parent key -> targetLocale path).
363
- */
364
- getParentPathForNested(nameSegments: string[], targetLocale: string): string;
365
- }
366
-
367
- declare interface SeoAttributes {
368
- canonical?: string;
369
- hreflangs?: HreflangTag[];
370
- }
371
-
372
149
  declare interface SwitchLocaleOptions {
373
150
  i18nRouteParams?: I18nRouteParams;
374
151
  }
@@ -1,146 +1,96 @@
1
- import { B as F, j as i, n as U, h as u, k as R, d as v, t as S, w as q, a as w, g as C, i as k } from "./base-strategy-DmOQzfP1.js";
2
- class _ extends F {
3
- buildLocalizedPath(t, e, a) {
4
- return i(e, U(t));
5
- }
6
- buildLocalizedRouteName(t, e) {
7
- return this.buildLocalizedName(t, e);
8
- }
9
- switchLocaleRoute(t, e, a, l) {
10
- const h = this.getBaseRouteName(a, t);
11
- if (!h) return a;
12
- const o = this.buildLocalizedName(h, e), c = `${this.getLocalizedRouteNamePrefix()}${h}`;
13
- let n, d = !1;
14
- if (this.ctx.router.hasRoute(o))
15
- n = o;
16
- else if (this.ctx.router.hasRoute(c))
17
- n = c, d = !0;
18
- else if (this.ctx.router.hasRoute(h))
19
- n = h;
1
+ import { B as L, j as b, E as n, F as W, l as v, k as w, t as F, a as N, q as S, r as j, o as U, i as k, w as C, x as G, p as K, c as O } from "./base-strategy-CSEFam3u.js";
2
+ class D extends L {
3
+ switchLocaleRoute(t, h, l, i) {
4
+ const e = this.getBaseRouteName(l, t);
5
+ if (!e) return l;
6
+ const a = this.getLocalizedRouteNamePrefix(), o = b(this.ctx.router, a, e, h);
7
+ let u, m = !1;
8
+ if (o)
9
+ u = o.name, m = o.needsLocaleParam;
10
+ else if (this.ctx.router.hasRoute(e))
11
+ u = e;
20
12
  else
21
- return { ...a, name: o };
22
- const p = l.i18nRouteParams?.[e] || {}, f = { ...a.params || {}, ...p };
23
- return d ? f.locale = e : delete f.locale, this.applyBaseUrl(e, {
24
- name: n,
25
- params: f,
26
- query: a.query,
27
- hash: a.hash
13
+ return { ...l, name: this.buildLocalizedName(e, h) };
14
+ const c = i.i18nRouteParams?.[h] || {}, p = { ...l.params || {}, ...c };
15
+ return m ? p.locale = h : delete p.locale, this.applyBaseUrl(h, {
16
+ name: u,
17
+ params: p,
18
+ query: l.query,
19
+ hash: l.hash
28
20
  });
29
21
  }
30
- resolveLocaleRoute(t, e, a) {
31
- if (e.kind === "path") {
32
- const s = this.resolvePathForLocale(e.path, t), r = `/${t}`;
33
- return this.applyBaseUrl(t, i(r, u(s)));
22
+ rewriteWithLocalePrefix(t, h) {
23
+ const l = t.path ?? "", { pathWithoutLocale: i } = this.getPathWithoutLocale(l), e = n(i, h), a = W(e, t.query, t.hash);
24
+ return { ...t, path: e, fullPath: a };
25
+ }
26
+ resolveLocaleRoute(t, h, l) {
27
+ if (h.kind === "path") {
28
+ const s = this.resolvePathForLocale(h.path, t);
29
+ return this.applyBaseUrl(t, n(s, t));
34
30
  }
35
- const { inputName: l, sourceRoute: h, resolved: o } = e;
36
- if (l) {
37
- const s = this.getPathForUnlocalizedRouteByName(l);
31
+ const { inputName: i, sourceRoute: e, resolved: a } = h, o = this.getLocalizedRouteNamePrefix();
32
+ if (i) {
33
+ const s = v(this.ctx, i);
38
34
  if (s !== null) return s;
39
35
  }
40
- const c = h.params && Object.keys(h.params ?? {}).length > 0;
41
- if (l && c) {
42
- const s = this.tryResolveByLocalizedNameWithParams(l, t, h.params ?? {}, h);
43
- if (s !== null) {
44
- const r = s.path ?? "", { pathWithoutLocale: m } = this.getPathWithoutLocale(r), P = m === "/" || m === "" ? `/${t}` : i(`/${t}`, u(m)), W = R(P, s.query, s.hash), B = {
45
- ...s,
46
- path: P,
47
- fullPath: W
48
- };
49
- return this.applyBaseUrl(t, B);
50
- }
51
- }
52
- const n = this.getPathForUnlocalizedRoute(o);
53
- if (n !== null) return this.applyBaseUrl(t, n);
54
- const d = this.getCustomPathSegment(o, t);
55
- if (d !== null) {
56
- const s = `/${t}`;
57
- return this.applyBaseUrl(t, i(s, u(d)));
36
+ const u = w(e.params);
37
+ if (i && u) {
38
+ const s = F(
39
+ this.ctx.router,
40
+ o,
41
+ i,
42
+ t,
43
+ e.params ?? {},
44
+ e
45
+ );
46
+ if (s !== null)
47
+ return this.applyBaseUrl(t, this.rewriteWithLocalePrefix(s, t));
58
48
  }
59
- const p = this.getRouteBaseName(o) ?? l ?? o.name?.toString() ?? null;
60
- if (l && !c) {
61
- let s = this.tryResolveByLocalizedName(l, t, h);
62
- const r = this.getLocalizedRouteNamePrefix();
63
- if (s === null && p != null && p !== l && l.startsWith(r) && (s = this.tryResolveByLocalizedName(p, t, h)), s !== null) {
64
- const m = s.path ?? "", { pathWithoutLocale: P } = this.getPathWithoutLocale(m), W = P === "/" || P === "" ? `/${t}` : i(`/${t}`, u(P)), B = R(W, s.query, s.hash), g = {
65
- ...s,
66
- path: W,
67
- fullPath: B
68
- };
69
- return this.applyBaseUrl(t, g);
49
+ const m = this.ctx._hasGR === !0, c = this.getRouteBaseName(a) ?? i ?? a.name?.toString() ?? null;
50
+ if (m) {
51
+ const s = N(this.ctx, a), r = S(this.ctx, a, s);
52
+ if (r !== null) return this.applyBaseUrl(t, r);
53
+ const P = j(this.ctx, a, t, s);
54
+ if (P !== null)
55
+ return this.applyBaseUrl(t, n(P, t));
56
+ if (a.path && a.path !== "/" && a.name) {
57
+ const { pathWithoutLocale: x } = s;
58
+ if (x && x !== "/")
59
+ return this.applyBaseUrl(t, n(x, t));
70
60
  }
71
61
  }
72
- if (o.path && o.path !== "/" && o.name) {
73
- const { pathWithoutLocale: s } = this.getPathWithoutLocaleAndBaseName(o);
74
- if (s && s !== "/")
75
- return this.applyBaseUrl(t, i(`/${t}`, u(s)));
62
+ if (i && !u) {
63
+ let s = U(this.ctx.router, o, i, t, e);
64
+ if (s === null && c != null && c !== i && i.startsWith(o) && (s = U(this.ctx.router, o, c, t, e)), s !== null)
65
+ return this.applyBaseUrl(t, this.rewriteWithLocalePrefix(s, t));
66
+ }
67
+ if (!m && a.path && a.path !== "/" && a.name) {
68
+ const s = N(this.ctx, a), { pathWithoutLocale: r } = s;
69
+ if (r && r !== "/")
70
+ return this.applyBaseUrl(t, n(r, t));
76
71
  }
77
- const f = a ? this.detectLocaleFromName(a.name) : this.detectLocaleFromName(o.name), y = f ? this.getBaseRouteName(o, f) : p;
78
- if (!y) {
79
- const { pathWithoutLocale: s } = this.getPathWithoutLocale(o.path ?? "/"), r = s === "/" || s === "" ? `/${t}` : i(`/${t}`, u(s)), m = R(r, h.query, h.hash);
72
+ const p = this.detectLocaleFromName(l ? l.name : a.name), f = p ? this.getBaseRouteName(a, p) : c;
73
+ if (!f) {
74
+ const { pathWithoutLocale: s } = this.getPathWithoutLocale(a.path ?? "/"), r = n(s, t), P = W(r, e.query, e.hash);
80
75
  return this.applyBaseUrl(t, {
81
- ...h,
76
+ ...e,
82
77
  path: r,
83
- fullPath: m
78
+ fullPath: P
84
79
  });
85
80
  }
86
- const $ = this.buildLocalizedName(y, t), z = v(y) ? "/" : i("/", S(y)), L = i(`/${t}`, u(z)), N = this.applyBaseUrl(t, L), x = typeof N == "string" ? N : N.path ?? L, b = {
87
- name: $,
88
- path: x,
89
- fullPath: x,
90
- params: { ...o.params, ...h.params },
91
- query: { ...o.query, ...h.query },
92
- hash: h.hash ?? o.hash
93
- };
94
- return this.applyBaseUrl(t, b);
95
- }
96
- getCanonicalPath(t, e) {
97
- const a = this.getCustomPathSegment(t, e);
98
- return a ? i(`/${e}`, u(a)) : null;
99
- }
100
- detectLocaleFromName(t) {
101
- if (!t) return null;
102
- for (const e of this.ctx.locales)
103
- if (t.endsWith(`-${e.code}`))
104
- return e.code;
105
- return null;
106
- }
107
- resolveLocaleFromPath(t) {
108
- const { localeFromPath: e } = this.getPathWithoutLocale(t);
109
- return e;
110
- }
111
- getRedirect(t, e) {
112
- const { pathWithoutLocale: a, localeFromPath: l } = this.getPathWithoutLocale(t), h = this.ctx.globalLocaleRoutes;
113
- if (h && l !== null) {
114
- const n = a === "/" ? "/" : q(a);
115
- if (h[a] === !1 || h[n] === !1)
116
- return w(a === "/" ? "/" : a);
117
- }
118
- const o = this.buildPathWithPrefix(a, e), c = C(t);
119
- return l === e && k(c, o) ? null : o;
120
- }
121
- buildPathWithPrefix(t, e) {
122
- const a = this.resolvePathForLocale(t, e);
123
- return a === "/" || a === "" ? `/${e}` : i(`/${e}`, a);
81
+ const q = this.buildLocalizedName(f, t), z = k(f) ? "/" : C("/", G(f)), R = n(z, t), d = this.applyBaseUrl(t, R), B = typeof d == "string" ? d : d.path ?? R, y = { name: q, path: B, fullPath: B };
82
+ return (a.params || e.params) && (y.params = a.params !== e.params ? Object.assign({}, a.params, e.params) : a.params), (a.query || e.query) && (y.query = a.query !== e.query ? Object.assign({}, a.query, e.query) : a.query), y.hash = e.hash || a.hash, this.applyBaseUrl(t, y);
124
83
  }
125
- /**
126
- * Formats path for router.resolve.
127
- * prefix_and_default: always add locale prefix.
128
- */
129
- formatPathForResolve(t, e, a) {
130
- return `/${e}${t}`;
84
+ getRedirect(t, h) {
85
+ return K(this, t, h);
131
86
  }
132
- /**
133
- * prefix_and_default: both / and /locale are valid for any locale.
134
- * Does NOT redirect if user explicitly navigates to a locale path.
135
- * Only redirects from paths without locale prefix.
136
- */
137
- getClientRedirect(t, e) {
138
- const { pathWithoutLocale: a, localeFromPath: l } = this.getPathWithoutLocale(t), h = this.ctx.globalLocaleRoutes, o = a === "/" ? "/" : a.replace(/^\//, "");
139
- return h && (h[a] === !1 || h[o] === !1) || l !== null || t === "/" || t === "", null;
87
+ getClientRedirect(t, h) {
88
+ const { pathWithoutLocale: l, localeFromPath: i } = this.getPathWithoutLocale(t);
89
+ return O(l, this.ctx.globalLocaleRoutes) || i !== null, null;
140
90
  }
141
91
  }
142
92
  export {
143
- _ as PrefixAndDefaultPathStrategy,
144
- _ as Strategy
93
+ D as PrefixAndDefaultPathStrategy,
94
+ D as Strategy
145
95
  };
146
96
  //# sourceMappingURL=prefix-and-default-strategy.mjs.map