nuxtseo-shared 5.3.10 → 5.3.12
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/i18n-runtime.d.mts +98 -0
- package/dist/i18n-runtime.d.ts +98 -0
- package/dist/i18n-runtime.mjs +338 -0
- package/dist/i18n.d.mts +33 -4
- package/dist/i18n.d.ts +33 -4
- package/dist/i18n.mjs +163 -49
- package/dist/kit.mjs +30 -4
- package/dist/module.json +1 -1
- package/package.json +10 -6
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime-safe i18n route resolution.
|
|
3
|
+
*
|
|
4
|
+
* `./i18n` reaches for `@nuxt/kit` to read the installed module's options, so it
|
|
5
|
+
* can only run at build time. This entry carries no build-time dependencies and
|
|
6
|
+
* is importable from a module's runtime (nitro handlers, plugins, composables).
|
|
7
|
+
* Feed it the config `toRuntimeI18nConfig()` produces.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Resolved route paths keyed by route name, then locale code. Per-locale
|
|
11
|
+
* `false` disables that locale. A tagged unlocalized entry represents i18n's
|
|
12
|
+
* whole-route `false` after build-time materialization.
|
|
13
|
+
*
|
|
14
|
+
* ```
|
|
15
|
+
* { about: { en: '/about', fr: '/a-propos' }, admin: { _tag: 'unlocalized', path: '/admin' } }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
type LocalePagePaths = Record<string, string | false>;
|
|
19
|
+
interface UnlocalizedLocalePage {
|
|
20
|
+
/** Route excluded from i18n localization by a whole-route `pages` false. */
|
|
21
|
+
_tag: 'unlocalized';
|
|
22
|
+
/** Original resolved Nuxt route pattern. */
|
|
23
|
+
path: string;
|
|
24
|
+
/** The route has children, which i18n also leaves unlocalized. */
|
|
25
|
+
subtree?: true;
|
|
26
|
+
}
|
|
27
|
+
type LocalePages = Record<string, LocalePagePaths | UnlocalizedLocalePage | undefined>;
|
|
28
|
+
interface RuntimeLocale {
|
|
29
|
+
code: string;
|
|
30
|
+
hreflang: string;
|
|
31
|
+
name?: string;
|
|
32
|
+
nativeName?: string;
|
|
33
|
+
language?: string;
|
|
34
|
+
domain?: string;
|
|
35
|
+
domains?: string[];
|
|
36
|
+
defaultForDomains?: string[];
|
|
37
|
+
}
|
|
38
|
+
/** Runtime-safe subset of `AutoI18nConfig` used for route locale resolution. */
|
|
39
|
+
interface RuntimeI18nConfig {
|
|
40
|
+
defaultLocale: string;
|
|
41
|
+
strategy: 'no_prefix' | 'prefix_except_default' | 'prefix' | 'prefix_and_default';
|
|
42
|
+
locales: RuntimeLocale[];
|
|
43
|
+
differentDomains?: boolean;
|
|
44
|
+
multiDomainLocales?: boolean;
|
|
45
|
+
/** Translated route paths, when i18n is configured with custom routes. */
|
|
46
|
+
pages?: LocalePages;
|
|
47
|
+
}
|
|
48
|
+
interface LocaleAlternate {
|
|
49
|
+
code: string;
|
|
50
|
+
hreflang: string;
|
|
51
|
+
path: string;
|
|
52
|
+
domain?: string;
|
|
53
|
+
}
|
|
54
|
+
type LocaleAlternateResolution = {
|
|
55
|
+
_tag: 'pages';
|
|
56
|
+
alternates: LocaleAlternate[];
|
|
57
|
+
} | {
|
|
58
|
+
_tag: 'strategy';
|
|
59
|
+
alternates: LocaleAlternate[];
|
|
60
|
+
};
|
|
61
|
+
interface RouteLocaleInfo {
|
|
62
|
+
/** Resolved locale code for this route */
|
|
63
|
+
locale: string;
|
|
64
|
+
/** Route with locale prefix stripped (e.g. /fr/about → /about). For no_prefix this equals route. */
|
|
65
|
+
basePath: string;
|
|
66
|
+
}
|
|
67
|
+
interface RuntimeRouteContext {
|
|
68
|
+
/** Request host, used to resolve locales under domain-based strategies. */
|
|
69
|
+
host?: string;
|
|
70
|
+
/** Known current locale, required to translate no_prefix routes without a locale domain. */
|
|
71
|
+
locale?: string;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Resolve the canonical domain for a locale. Domainless locales are served on
|
|
75
|
+
* every domain, so their stable canonical is the default locale's domain.
|
|
76
|
+
*/
|
|
77
|
+
declare function resolveCanonicalLocaleDomain(locale: RuntimeLocale | undefined, defaultLocale?: RuntimeLocale): string | undefined;
|
|
78
|
+
/**
|
|
79
|
+
* Resolve which locale a route belongs to and the locale-stripped base path.
|
|
80
|
+
*/
|
|
81
|
+
declare function resolveLocaleFromRoute(route: string, i18n: RuntimeI18nConfig, context?: RuntimeRouteContext): RouteLocaleInfo;
|
|
82
|
+
/**
|
|
83
|
+
* Build the URL path for a base path under a given locale, honoring the i18n strategy.
|
|
84
|
+
*/
|
|
85
|
+
declare function localePath(basePath: string, locale: string, i18n: RuntimeI18nConfig, context?: RuntimeRouteContext): string;
|
|
86
|
+
/**
|
|
87
|
+
* Compute hreflang alternates for a given route.
|
|
88
|
+
* Returns the route itself plus all sibling locale variants.
|
|
89
|
+
*
|
|
90
|
+
* Translated slugs (i18n `pages`) can't be derived by adding or removing a
|
|
91
|
+
* locale prefix — `/about` and `/fr/a-propos` are the same page — so the route
|
|
92
|
+
* table is consulted first and prefix arithmetic is the fallback.
|
|
93
|
+
*/
|
|
94
|
+
declare function resolveLocaleAlternates(route: string, i18n: RuntimeI18nConfig, context?: RuntimeRouteContext): LocaleAlternateResolution;
|
|
95
|
+
declare function computeLocaleAlternates(route: string, i18n: RuntimeI18nConfig, context?: RuntimeRouteContext): LocaleAlternate[];
|
|
96
|
+
|
|
97
|
+
export { computeLocaleAlternates, localePath, resolveCanonicalLocaleDomain, resolveLocaleAlternates, resolveLocaleFromRoute };
|
|
98
|
+
export type { LocaleAlternate, LocaleAlternateResolution, LocalePagePaths, LocalePages, RouteLocaleInfo, RuntimeI18nConfig, RuntimeLocale, RuntimeRouteContext, UnlocalizedLocalePage };
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime-safe i18n route resolution.
|
|
3
|
+
*
|
|
4
|
+
* `./i18n` reaches for `@nuxt/kit` to read the installed module's options, so it
|
|
5
|
+
* can only run at build time. This entry carries no build-time dependencies and
|
|
6
|
+
* is importable from a module's runtime (nitro handlers, plugins, composables).
|
|
7
|
+
* Feed it the config `toRuntimeI18nConfig()` produces.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Resolved route paths keyed by route name, then locale code. Per-locale
|
|
11
|
+
* `false` disables that locale. A tagged unlocalized entry represents i18n's
|
|
12
|
+
* whole-route `false` after build-time materialization.
|
|
13
|
+
*
|
|
14
|
+
* ```
|
|
15
|
+
* { about: { en: '/about', fr: '/a-propos' }, admin: { _tag: 'unlocalized', path: '/admin' } }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
type LocalePagePaths = Record<string, string | false>;
|
|
19
|
+
interface UnlocalizedLocalePage {
|
|
20
|
+
/** Route excluded from i18n localization by a whole-route `pages` false. */
|
|
21
|
+
_tag: 'unlocalized';
|
|
22
|
+
/** Original resolved Nuxt route pattern. */
|
|
23
|
+
path: string;
|
|
24
|
+
/** The route has children, which i18n also leaves unlocalized. */
|
|
25
|
+
subtree?: true;
|
|
26
|
+
}
|
|
27
|
+
type LocalePages = Record<string, LocalePagePaths | UnlocalizedLocalePage | undefined>;
|
|
28
|
+
interface RuntimeLocale {
|
|
29
|
+
code: string;
|
|
30
|
+
hreflang: string;
|
|
31
|
+
name?: string;
|
|
32
|
+
nativeName?: string;
|
|
33
|
+
language?: string;
|
|
34
|
+
domain?: string;
|
|
35
|
+
domains?: string[];
|
|
36
|
+
defaultForDomains?: string[];
|
|
37
|
+
}
|
|
38
|
+
/** Runtime-safe subset of `AutoI18nConfig` used for route locale resolution. */
|
|
39
|
+
interface RuntimeI18nConfig {
|
|
40
|
+
defaultLocale: string;
|
|
41
|
+
strategy: 'no_prefix' | 'prefix_except_default' | 'prefix' | 'prefix_and_default';
|
|
42
|
+
locales: RuntimeLocale[];
|
|
43
|
+
differentDomains?: boolean;
|
|
44
|
+
multiDomainLocales?: boolean;
|
|
45
|
+
/** Translated route paths, when i18n is configured with custom routes. */
|
|
46
|
+
pages?: LocalePages;
|
|
47
|
+
}
|
|
48
|
+
interface LocaleAlternate {
|
|
49
|
+
code: string;
|
|
50
|
+
hreflang: string;
|
|
51
|
+
path: string;
|
|
52
|
+
domain?: string;
|
|
53
|
+
}
|
|
54
|
+
type LocaleAlternateResolution = {
|
|
55
|
+
_tag: 'pages';
|
|
56
|
+
alternates: LocaleAlternate[];
|
|
57
|
+
} | {
|
|
58
|
+
_tag: 'strategy';
|
|
59
|
+
alternates: LocaleAlternate[];
|
|
60
|
+
};
|
|
61
|
+
interface RouteLocaleInfo {
|
|
62
|
+
/** Resolved locale code for this route */
|
|
63
|
+
locale: string;
|
|
64
|
+
/** Route with locale prefix stripped (e.g. /fr/about → /about). For no_prefix this equals route. */
|
|
65
|
+
basePath: string;
|
|
66
|
+
}
|
|
67
|
+
interface RuntimeRouteContext {
|
|
68
|
+
/** Request host, used to resolve locales under domain-based strategies. */
|
|
69
|
+
host?: string;
|
|
70
|
+
/** Known current locale, required to translate no_prefix routes without a locale domain. */
|
|
71
|
+
locale?: string;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Resolve the canonical domain for a locale. Domainless locales are served on
|
|
75
|
+
* every domain, so their stable canonical is the default locale's domain.
|
|
76
|
+
*/
|
|
77
|
+
declare function resolveCanonicalLocaleDomain(locale: RuntimeLocale | undefined, defaultLocale?: RuntimeLocale): string | undefined;
|
|
78
|
+
/**
|
|
79
|
+
* Resolve which locale a route belongs to and the locale-stripped base path.
|
|
80
|
+
*/
|
|
81
|
+
declare function resolveLocaleFromRoute(route: string, i18n: RuntimeI18nConfig, context?: RuntimeRouteContext): RouteLocaleInfo;
|
|
82
|
+
/**
|
|
83
|
+
* Build the URL path for a base path under a given locale, honoring the i18n strategy.
|
|
84
|
+
*/
|
|
85
|
+
declare function localePath(basePath: string, locale: string, i18n: RuntimeI18nConfig, context?: RuntimeRouteContext): string;
|
|
86
|
+
/**
|
|
87
|
+
* Compute hreflang alternates for a given route.
|
|
88
|
+
* Returns the route itself plus all sibling locale variants.
|
|
89
|
+
*
|
|
90
|
+
* Translated slugs (i18n `pages`) can't be derived by adding or removing a
|
|
91
|
+
* locale prefix — `/about` and `/fr/a-propos` are the same page — so the route
|
|
92
|
+
* table is consulted first and prefix arithmetic is the fallback.
|
|
93
|
+
*/
|
|
94
|
+
declare function resolveLocaleAlternates(route: string, i18n: RuntimeI18nConfig, context?: RuntimeRouteContext): LocaleAlternateResolution;
|
|
95
|
+
declare function computeLocaleAlternates(route: string, i18n: RuntimeI18nConfig, context?: RuntimeRouteContext): LocaleAlternate[];
|
|
96
|
+
|
|
97
|
+
export { computeLocaleAlternates, localePath, resolveCanonicalLocaleDomain, resolveLocaleAlternates, resolveLocaleFromRoute };
|
|
98
|
+
export type { LocaleAlternate, LocaleAlternateResolution, LocalePagePaths, LocalePages, RouteLocaleInfo, RuntimeI18nConfig, RuntimeLocale, RuntimeRouteContext, UnlocalizedLocalePage };
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
function isUnlocalizedLocalePage(page) {
|
|
2
|
+
return page._tag === "unlocalized" && typeof page.path === "string";
|
|
3
|
+
}
|
|
4
|
+
function matchesUnlocalizedLocalePage(page, path) {
|
|
5
|
+
if (matchPagePattern(page.path, path))
|
|
6
|
+
return true;
|
|
7
|
+
if (!page.subtree)
|
|
8
|
+
return false;
|
|
9
|
+
const subtreePattern = `${page.path === "/" ? "" : page.path.replace(/\/$/, "")}/[...__nuxtSeoSubtree]`;
|
|
10
|
+
return !!matchPagePattern(subtreePattern, path);
|
|
11
|
+
}
|
|
12
|
+
function normalizeHost(value) {
|
|
13
|
+
return value.trim().toLowerCase().replace(/^[a-z][a-z\d+.-]*:\/\//, "").split("/")[0];
|
|
14
|
+
}
|
|
15
|
+
function localeDomains(locale) {
|
|
16
|
+
return locale.domains?.length ? locale.domains : locale.domain ? [locale.domain] : [];
|
|
17
|
+
}
|
|
18
|
+
function resolveLocaleFromHost(host, i18n) {
|
|
19
|
+
if (!host)
|
|
20
|
+
return void 0;
|
|
21
|
+
const normalizedHost = normalizeHost(host);
|
|
22
|
+
const domainDefault = i18n.locales.find(
|
|
23
|
+
(locale) => locale.defaultForDomains?.some((domain) => normalizeHost(domain) === normalizedHost)
|
|
24
|
+
);
|
|
25
|
+
if (domainDefault)
|
|
26
|
+
return domainDefault;
|
|
27
|
+
const matches = i18n.locales.filter(
|
|
28
|
+
(locale) => localeDomains(locale).some((domain) => normalizeHost(domain) === normalizedHost)
|
|
29
|
+
);
|
|
30
|
+
return matches.length === 1 ? matches[0] : matches.find((locale) => locale.code === i18n.defaultLocale);
|
|
31
|
+
}
|
|
32
|
+
function firstLocaleDomain(locale) {
|
|
33
|
+
return locale?.defaultForDomains?.[0] || locale?.domain || locale?.domains?.[0];
|
|
34
|
+
}
|
|
35
|
+
function resolveCanonicalLocaleDomain(locale, defaultLocale) {
|
|
36
|
+
return firstLocaleDomain(locale) || firstLocaleDomain(defaultLocale);
|
|
37
|
+
}
|
|
38
|
+
function splitRouteSuffix(route) {
|
|
39
|
+
const suffixIndex = route.search(/[?#]/);
|
|
40
|
+
const rawPathname = suffixIndex === -1 ? route : route.slice(0, suffixIndex);
|
|
41
|
+
return {
|
|
42
|
+
pathname: rawPathname ? rawPathname.startsWith("/") ? rawPathname : `/${rawPathname}` : "/",
|
|
43
|
+
suffix: suffixIndex === -1 ? "" : route.slice(suffixIndex)
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function resolveLocaleFromRoute(route, i18n, context = {}) {
|
|
47
|
+
const { pathname, suffix } = splitRouteSuffix(route);
|
|
48
|
+
if (i18n.strategy !== "no_prefix") {
|
|
49
|
+
const segments = pathname.split("/").filter(Boolean);
|
|
50
|
+
const first = segments[0];
|
|
51
|
+
const matched = first ? i18n.locales.find((l) => l.code === first) : void 0;
|
|
52
|
+
if (matched) {
|
|
53
|
+
const rest = segments.slice(1).join("/");
|
|
54
|
+
const trailingSlash = rest && pathname.endsWith("/") ? "/" : "";
|
|
55
|
+
return { locale: matched.code, basePath: `${rest ? `/${rest}${trailingSlash}` : "/"}${suffix}` };
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const contextLocale = context.locale ? i18n.locales.find((locale) => locale.code === context.locale) : void 0;
|
|
59
|
+
const domainLocale = resolveLocaleFromHost(context.host, i18n);
|
|
60
|
+
return { locale: contextLocale?.code || domainLocale?.code || i18n.defaultLocale, basePath: `${pathname}${suffix}` };
|
|
61
|
+
}
|
|
62
|
+
function localePath(basePath, locale, i18n, context = {}) {
|
|
63
|
+
const { pathname, suffix } = splitRouteSuffix(basePath);
|
|
64
|
+
if (i18n.strategy === "no_prefix")
|
|
65
|
+
return `${pathname}${suffix}`;
|
|
66
|
+
const isDefault = locale === i18n.defaultLocale;
|
|
67
|
+
const localeConfig = i18n.locales.find((item) => item.code === locale);
|
|
68
|
+
const normalizedHost = context.host ? normalizeHost(context.host) : void 0;
|
|
69
|
+
const matchesDomainDefault = !!normalizedHost && !!localeConfig?.defaultForDomains?.some((domain) => normalizeHost(domain) === normalizedHost);
|
|
70
|
+
const isDomainDefault = i18n.differentDomains || matchesDomainDefault;
|
|
71
|
+
if (i18n.strategy === "prefix_except_default" && (isDefault || isDomainDefault))
|
|
72
|
+
return `${pathname}${suffix}`;
|
|
73
|
+
if (i18n.strategy === "prefix_and_default" && isDomainDefault)
|
|
74
|
+
return `${pathname}${suffix}`;
|
|
75
|
+
if (pathname === "/")
|
|
76
|
+
return `/${locale}${suffix}`;
|
|
77
|
+
return `/${locale}${pathname}${suffix}`;
|
|
78
|
+
}
|
|
79
|
+
function toSegments(path) {
|
|
80
|
+
return path.split("/").filter(Boolean);
|
|
81
|
+
}
|
|
82
|
+
const PAGE_PARAM_PATTERN = /\[\[(\.\.\.)?([^[\]]+)\]\]|\[(\.\.\.)?([^[\]]+)\]|:(\w+)(?:\((\.\*)?\))?([?*+]?)/g;
|
|
83
|
+
function parsePageSegment(segment) {
|
|
84
|
+
const tokens = [];
|
|
85
|
+
let offset = 0;
|
|
86
|
+
for (const match of segment.matchAll(PAGE_PARAM_PATTERN)) {
|
|
87
|
+
const index = match.index;
|
|
88
|
+
if (index > offset)
|
|
89
|
+
tokens.push({ _tag: "static", value: segment.slice(offset, index) });
|
|
90
|
+
if (match[2]) {
|
|
91
|
+
tokens.push({ _tag: "param", param: { name: match[2], catchAll: !!match[1], optional: true } });
|
|
92
|
+
} else if (match[4]) {
|
|
93
|
+
tokens.push({ _tag: "param", param: { name: match[4], catchAll: !!match[3], optional: false } });
|
|
94
|
+
} else {
|
|
95
|
+
const modifier = match[7];
|
|
96
|
+
const routePattern = match[6];
|
|
97
|
+
tokens.push({
|
|
98
|
+
_tag: "param",
|
|
99
|
+
param: {
|
|
100
|
+
name: match[5],
|
|
101
|
+
catchAll: !!routePattern?.includes(".*") || modifier === "*" || modifier === "+",
|
|
102
|
+
optional: modifier === "?" || modifier === "*"
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
offset = index + match[0].length;
|
|
107
|
+
}
|
|
108
|
+
if (offset < segment.length)
|
|
109
|
+
tokens.push({ _tag: "static", value: segment.slice(offset) });
|
|
110
|
+
return tokens.length ? tokens : [{ _tag: "static", value: segment }];
|
|
111
|
+
}
|
|
112
|
+
function wholeSegmentParam(tokens) {
|
|
113
|
+
return tokens.length === 1 && tokens[0]?._tag === "param" ? tokens[0].param : null;
|
|
114
|
+
}
|
|
115
|
+
function segmentRanks(pattern) {
|
|
116
|
+
return toSegments(pattern).map((segment) => {
|
|
117
|
+
const tokens = parsePageSegment(segment);
|
|
118
|
+
const params = tokens.filter((token) => token._tag === "param");
|
|
119
|
+
if (!params.length)
|
|
120
|
+
return 6;
|
|
121
|
+
const hasStatic = tokens.some((token) => token._tag === "static");
|
|
122
|
+
if (hasStatic)
|
|
123
|
+
return params.some((token) => token.param.optional) ? 3 : 4;
|
|
124
|
+
const param = params[0].param;
|
|
125
|
+
if (param.catchAll)
|
|
126
|
+
return param.optional ? -1 : 0;
|
|
127
|
+
return param.optional ? 1 : 2;
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
function compareSpecificity(a, b) {
|
|
131
|
+
const length = Math.min(a.length, b.length);
|
|
132
|
+
for (let i = 0; i < length; i++) {
|
|
133
|
+
const diff = b[i] - a[i];
|
|
134
|
+
if (diff !== 0)
|
|
135
|
+
return diff;
|
|
136
|
+
}
|
|
137
|
+
return a.length - b.length;
|
|
138
|
+
}
|
|
139
|
+
function matchSegmentTokens(tokens, path) {
|
|
140
|
+
const failedStates = /* @__PURE__ */ new Set();
|
|
141
|
+
function visit(index, offset, params) {
|
|
142
|
+
const state = index * (path.length + 1) + offset;
|
|
143
|
+
if (failedStates.has(state))
|
|
144
|
+
return null;
|
|
145
|
+
if (index === tokens.length) {
|
|
146
|
+
if (offset === path.length)
|
|
147
|
+
return params;
|
|
148
|
+
failedStates.add(state);
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
const token = tokens[index];
|
|
152
|
+
if (token._tag === "static") {
|
|
153
|
+
const matched = path.startsWith(token.value, offset) ? visit(index + 1, offset + token.value.length, params) : null;
|
|
154
|
+
if (!matched)
|
|
155
|
+
failedStates.add(state);
|
|
156
|
+
return matched;
|
|
157
|
+
}
|
|
158
|
+
const minimumEnd = token.param.optional ? offset : offset + 1;
|
|
159
|
+
for (let end = minimumEnd; end <= path.length; end++) {
|
|
160
|
+
const value = path.slice(offset, end);
|
|
161
|
+
const nextParams = value ? { ...params, [token.param.name]: value } : params;
|
|
162
|
+
const matched = visit(index + 1, end, nextParams);
|
|
163
|
+
if (matched)
|
|
164
|
+
return matched;
|
|
165
|
+
}
|
|
166
|
+
failedStates.add(state);
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
return visit(0, 0, {});
|
|
170
|
+
}
|
|
171
|
+
function matchPagePattern(pattern, path) {
|
|
172
|
+
const patternSegments = toSegments(pattern);
|
|
173
|
+
const pathSegments = toSegments(path);
|
|
174
|
+
const failedStates = /* @__PURE__ */ new Set();
|
|
175
|
+
function visit(patternIndex, pathIndex, params) {
|
|
176
|
+
const state = patternIndex * (pathSegments.length + 1) + pathIndex;
|
|
177
|
+
if (failedStates.has(state))
|
|
178
|
+
return null;
|
|
179
|
+
if (patternIndex === patternSegments.length) {
|
|
180
|
+
if (pathIndex === pathSegments.length)
|
|
181
|
+
return params;
|
|
182
|
+
failedStates.add(state);
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
const tokens = parsePageSegment(patternSegments[patternIndex]);
|
|
186
|
+
const param = wholeSegmentParam(tokens);
|
|
187
|
+
if (param?.catchAll) {
|
|
188
|
+
const minimumEnd = param.optional ? pathIndex : pathIndex + 1;
|
|
189
|
+
for (let end = pathSegments.length; end >= minimumEnd; end--) {
|
|
190
|
+
const value = pathSegments.slice(pathIndex, end).join("/");
|
|
191
|
+
const nextParams = value ? { ...params, [param.name]: value } : params;
|
|
192
|
+
const matched2 = visit(patternIndex + 1, end, nextParams);
|
|
193
|
+
if (matched2)
|
|
194
|
+
return matched2;
|
|
195
|
+
}
|
|
196
|
+
failedStates.add(state);
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
const segment = pathSegments[pathIndex];
|
|
200
|
+
if (segment !== void 0) {
|
|
201
|
+
const segmentParams = matchSegmentTokens(tokens, segment);
|
|
202
|
+
if (segmentParams) {
|
|
203
|
+
const matched2 = visit(patternIndex + 1, pathIndex + 1, { ...params, ...segmentParams });
|
|
204
|
+
if (matched2)
|
|
205
|
+
return matched2;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
const matched = param?.optional ? visit(patternIndex + 1, pathIndex, params) : null;
|
|
209
|
+
if (!matched)
|
|
210
|
+
failedStates.add(state);
|
|
211
|
+
return matched;
|
|
212
|
+
}
|
|
213
|
+
return visit(0, 0, {});
|
|
214
|
+
}
|
|
215
|
+
function fillPagePattern(pattern, params) {
|
|
216
|
+
const filled = [];
|
|
217
|
+
for (const segment of toSegments(pattern)) {
|
|
218
|
+
const tokens = parsePageSegment(segment);
|
|
219
|
+
let value = "";
|
|
220
|
+
for (const token of tokens) {
|
|
221
|
+
if (token._tag === "static") {
|
|
222
|
+
value += token.value;
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
const paramValue = params[token.param.name];
|
|
226
|
+
if (paramValue === void 0) {
|
|
227
|
+
if (token.param.optional)
|
|
228
|
+
continue;
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
value += paramValue;
|
|
232
|
+
}
|
|
233
|
+
if (value)
|
|
234
|
+
filled.push(value);
|
|
235
|
+
}
|
|
236
|
+
if (!filled.length)
|
|
237
|
+
return "/";
|
|
238
|
+
return `/${filled.join("/")}${pattern.endsWith("/") ? "/" : ""}`;
|
|
239
|
+
}
|
|
240
|
+
function alternatesForEntry(localePaths, params, i18n, context) {
|
|
241
|
+
const untranslated = localePaths[i18n.defaultLocale];
|
|
242
|
+
const alternates = [];
|
|
243
|
+
const defaultLocale = i18n.locales.find((locale) => locale.code === i18n.defaultLocale);
|
|
244
|
+
for (const l of i18n.locales) {
|
|
245
|
+
const pattern = localePaths[l.code] ?? untranslated;
|
|
246
|
+
if (localePaths[l.code] === false)
|
|
247
|
+
continue;
|
|
248
|
+
if (typeof pattern !== "string")
|
|
249
|
+
continue;
|
|
250
|
+
const path = fillPagePattern(pattern, params);
|
|
251
|
+
if (path === null)
|
|
252
|
+
continue;
|
|
253
|
+
const domain = resolveCanonicalLocaleDomain(l, defaultLocale);
|
|
254
|
+
alternates.push({
|
|
255
|
+
code: l.code,
|
|
256
|
+
hreflang: l.hreflang || l.code,
|
|
257
|
+
path: localePath(path, l.code, i18n, { host: domain || context.host }),
|
|
258
|
+
...domain ? { domain } : {}
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
return alternates.length ? alternates : null;
|
|
262
|
+
}
|
|
263
|
+
function alternatesFromPages(basePath, routePath, locale, i18n, context) {
|
|
264
|
+
const pages = i18n.pages;
|
|
265
|
+
const hasDomainLocales = i18n.differentDomains || i18n.multiDomainLocales;
|
|
266
|
+
const hasContextLocale = !!context.locale && i18n.locales.some((locale2) => locale2.code === context.locale);
|
|
267
|
+
if (!pages)
|
|
268
|
+
return null;
|
|
269
|
+
const allowLocalized = i18n.strategy !== "no_prefix" || hasDomainLocales || hasContextLocale;
|
|
270
|
+
const matches = [];
|
|
271
|
+
for (const page of Object.values(pages)) {
|
|
272
|
+
if (!page)
|
|
273
|
+
continue;
|
|
274
|
+
if (isUnlocalizedLocalePage(page)) {
|
|
275
|
+
if (matchesUnlocalizedLocalePage(page, routePath))
|
|
276
|
+
matches.push({ _tag: "unlocalized", ranks: segmentRanks(page.path) });
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
if (!allowLocalized)
|
|
280
|
+
continue;
|
|
281
|
+
const pattern = page[locale];
|
|
282
|
+
if (!pattern)
|
|
283
|
+
continue;
|
|
284
|
+
const params = matchPagePattern(pattern, basePath);
|
|
285
|
+
if (params)
|
|
286
|
+
matches.push({ _tag: "localized", ranks: segmentRanks(pattern), localePaths: page, params });
|
|
287
|
+
}
|
|
288
|
+
matches.sort((a, b) => compareSpecificity(a.ranks, b.ranks));
|
|
289
|
+
for (const match of matches) {
|
|
290
|
+
if (match._tag === "unlocalized") {
|
|
291
|
+
const defaultLocale = i18n.locales.find((locale2) => locale2.code === i18n.defaultLocale);
|
|
292
|
+
if (defaultLocale) {
|
|
293
|
+
const domain = resolveCanonicalLocaleDomain(defaultLocale);
|
|
294
|
+
return [{
|
|
295
|
+
code: defaultLocale.code,
|
|
296
|
+
hreflang: defaultLocale.hreflang || defaultLocale.code,
|
|
297
|
+
path: routePath,
|
|
298
|
+
...domain ? { domain } : {}
|
|
299
|
+
}];
|
|
300
|
+
}
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
const alternates = alternatesForEntry(match.localePaths, match.params, i18n, context);
|
|
304
|
+
if (alternates)
|
|
305
|
+
return alternates;
|
|
306
|
+
}
|
|
307
|
+
return null;
|
|
308
|
+
}
|
|
309
|
+
function resolveLocaleAlternates(route, i18n, context = {}) {
|
|
310
|
+
const { locale, basePath } = resolveLocaleFromRoute(route, i18n, context);
|
|
311
|
+
const { pathname, suffix } = splitRouteSuffix(basePath);
|
|
312
|
+
const { pathname: routePathname } = splitRouteSuffix(route);
|
|
313
|
+
const translated = alternatesFromPages(pathname, routePathname, locale, i18n, context);
|
|
314
|
+
if (translated) {
|
|
315
|
+
return {
|
|
316
|
+
_tag: "pages",
|
|
317
|
+
alternates: translated.map((alternate) => ({ ...alternate, path: `${alternate.path}${suffix}` }))
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
const defaultLocale = i18n.locales.find((locale2) => locale2.code === i18n.defaultLocale);
|
|
321
|
+
return {
|
|
322
|
+
_tag: "strategy",
|
|
323
|
+
alternates: i18n.locales.map((l) => {
|
|
324
|
+
const domain = resolveCanonicalLocaleDomain(l, defaultLocale);
|
|
325
|
+
return {
|
|
326
|
+
code: l.code,
|
|
327
|
+
hreflang: l.hreflang || l.code,
|
|
328
|
+
path: localePath(basePath, l.code, i18n, { host: domain || context.host }),
|
|
329
|
+
...domain ? { domain } : {}
|
|
330
|
+
};
|
|
331
|
+
})
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
function computeLocaleAlternates(route, i18n, context = {}) {
|
|
335
|
+
return resolveLocaleAlternates(route, i18n, context).alternates;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
export { computeLocaleAlternates, localePath, resolveCanonicalLocaleDomain, resolveLocaleAlternates, resolveLocaleFromRoute };
|
package/dist/i18n.d.mts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { LocaleObject, NuxtI18nOptions } from '@nuxtjs/i18n';
|
|
2
|
+
import { UnlocalizedLocalePage, RuntimeI18nConfig } from './i18n-runtime.mjs';
|
|
3
|
+
export { LocaleAlternate, LocaleAlternateResolution, LocalePagePaths, LocalePages, RouteLocaleInfo, RuntimeLocale, RuntimeRouteContext } from './i18n-runtime.mjs';
|
|
2
4
|
|
|
3
5
|
declare const I18N_MODULES: readonly ["@nuxtjs/i18n", "nuxt-i18n-micro"];
|
|
4
6
|
type I18nModuleName = typeof I18N_MODULES[number];
|
|
@@ -12,8 +14,12 @@ interface AutoI18nConfig {
|
|
|
12
14
|
defaultLocale: string;
|
|
13
15
|
strategy: Strategies;
|
|
14
16
|
differentDomains?: boolean;
|
|
15
|
-
|
|
17
|
+
multiDomainLocales?: boolean;
|
|
18
|
+
routesNameSeparator?: string;
|
|
19
|
+
defaultLocaleRouteNameSuffix?: string;
|
|
20
|
+
pages?: Record<string, Record<string, string | false> | UnlocalizedLocalePage | false>;
|
|
16
21
|
}
|
|
22
|
+
type I18nPages = NonNullable<AutoI18nConfig['pages']>;
|
|
17
23
|
interface StrategyProps {
|
|
18
24
|
localeCode: string;
|
|
19
25
|
pageLocales: string;
|
|
@@ -47,7 +53,22 @@ declare function isCompactLocaleRoute(path: string): boolean;
|
|
|
47
53
|
*/
|
|
48
54
|
declare function expandCompactLocaleRoute(path: string, knownLocales?: string[]): ExpandedLocaleRoute[] | null;
|
|
49
55
|
declare function normalizeLocales(nuxtI18nConfig: NuxtI18nOptions): AutoI18nConfig['locales'];
|
|
50
|
-
|
|
56
|
+
interface ResolvedI18nRoute {
|
|
57
|
+
name?: string;
|
|
58
|
+
path: string;
|
|
59
|
+
children?: ResolvedI18nRoute[];
|
|
60
|
+
}
|
|
61
|
+
/** Normalize an i18n `pages` key to the corresponding resolved Nuxt route name. Build-time only. */
|
|
62
|
+
declare function normalizeI18nPageKey(key: string): string;
|
|
63
|
+
/**
|
|
64
|
+
* Complete partial i18n `pages` entries from Nuxt's resolved route table.
|
|
65
|
+
*
|
|
66
|
+
* Route names are identifiers, not URL paths. The resolved route path is the
|
|
67
|
+
* only safe fallback for nested, dynamic, grouped, or custom Nuxt routes.
|
|
68
|
+
* Whole-route `false` becomes a tagged entry carrying that resolved path.
|
|
69
|
+
*/
|
|
70
|
+
declare function materializeI18nPages(autoI18n: AutoI18nConfig, routes: readonly ResolvedI18nRoute[]): I18nPages | undefined;
|
|
71
|
+
declare function mapPathForI18nPages(path: string, autoI18n: AutoI18nConfig, routes?: readonly ResolvedI18nRoute[]): string[] | false;
|
|
51
72
|
interface I18nModuleResolution {
|
|
52
73
|
module: I18nModuleName;
|
|
53
74
|
isMicro: boolean;
|
|
@@ -61,7 +82,15 @@ declare function resolveI18nModule(): false | I18nModuleResolution;
|
|
|
61
82
|
declare function resolveI18nConfig(logger?: {
|
|
62
83
|
warn: (msg: string) => void;
|
|
63
84
|
}): Promise<false | AutoI18nConfig>;
|
|
85
|
+
/**
|
|
86
|
+
* Strip a build-time i18n config down to what `./i18n-runtime` needs, dropping
|
|
87
|
+
* the non-serializable `LocaleObject` extras so it can be handed to the runtime
|
|
88
|
+
* through `runtimeConfig`. Materialize partial and whole-route-false `pages`
|
|
89
|
+
* entries before calling this function; unresolved whole-route false entries
|
|
90
|
+
* are omitted instead of becoming fabricated runtime URLs.
|
|
91
|
+
*/
|
|
92
|
+
declare function toRuntimeI18nConfig(auto: AutoI18nConfig): RuntimeI18nConfig;
|
|
64
93
|
declare function mergeOnKey<T extends Record<string, any>>(arr: T[], key: keyof T): T[];
|
|
65
94
|
|
|
66
|
-
export { expandCompactLocaleRoute, generatePathForI18nPages, isCompactLocaleRoute, mapPathForI18nPages, mergeOnKey, normalizeLocales, resolveI18nConfig, resolveI18nModule, splitPathForI18nLocales };
|
|
67
|
-
export type { AutoI18nConfig, ExpandedLocaleRoute, I18nModuleResolution, NormalisedLocale, Strategies, StrategyProps };
|
|
95
|
+
export { RuntimeI18nConfig, UnlocalizedLocalePage, expandCompactLocaleRoute, generatePathForI18nPages, isCompactLocaleRoute, mapPathForI18nPages, materializeI18nPages, mergeOnKey, normalizeI18nPageKey, normalizeLocales, resolveI18nConfig, resolveI18nModule, splitPathForI18nLocales, toRuntimeI18nConfig };
|
|
96
|
+
export type { AutoI18nConfig, ExpandedLocaleRoute, I18nModuleResolution, NormalisedLocale, ResolvedI18nRoute, Strategies, StrategyProps };
|
package/dist/i18n.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { LocaleObject, NuxtI18nOptions } from '@nuxtjs/i18n';
|
|
2
|
+
import { UnlocalizedLocalePage, RuntimeI18nConfig } from './i18n-runtime.js';
|
|
3
|
+
export { LocaleAlternate, LocaleAlternateResolution, LocalePagePaths, LocalePages, RouteLocaleInfo, RuntimeLocale, RuntimeRouteContext } from './i18n-runtime.js';
|
|
2
4
|
|
|
3
5
|
declare const I18N_MODULES: readonly ["@nuxtjs/i18n", "nuxt-i18n-micro"];
|
|
4
6
|
type I18nModuleName = typeof I18N_MODULES[number];
|
|
@@ -12,8 +14,12 @@ interface AutoI18nConfig {
|
|
|
12
14
|
defaultLocale: string;
|
|
13
15
|
strategy: Strategies;
|
|
14
16
|
differentDomains?: boolean;
|
|
15
|
-
|
|
17
|
+
multiDomainLocales?: boolean;
|
|
18
|
+
routesNameSeparator?: string;
|
|
19
|
+
defaultLocaleRouteNameSuffix?: string;
|
|
20
|
+
pages?: Record<string, Record<string, string | false> | UnlocalizedLocalePage | false>;
|
|
16
21
|
}
|
|
22
|
+
type I18nPages = NonNullable<AutoI18nConfig['pages']>;
|
|
17
23
|
interface StrategyProps {
|
|
18
24
|
localeCode: string;
|
|
19
25
|
pageLocales: string;
|
|
@@ -47,7 +53,22 @@ declare function isCompactLocaleRoute(path: string): boolean;
|
|
|
47
53
|
*/
|
|
48
54
|
declare function expandCompactLocaleRoute(path: string, knownLocales?: string[]): ExpandedLocaleRoute[] | null;
|
|
49
55
|
declare function normalizeLocales(nuxtI18nConfig: NuxtI18nOptions): AutoI18nConfig['locales'];
|
|
50
|
-
|
|
56
|
+
interface ResolvedI18nRoute {
|
|
57
|
+
name?: string;
|
|
58
|
+
path: string;
|
|
59
|
+
children?: ResolvedI18nRoute[];
|
|
60
|
+
}
|
|
61
|
+
/** Normalize an i18n `pages` key to the corresponding resolved Nuxt route name. Build-time only. */
|
|
62
|
+
declare function normalizeI18nPageKey(key: string): string;
|
|
63
|
+
/**
|
|
64
|
+
* Complete partial i18n `pages` entries from Nuxt's resolved route table.
|
|
65
|
+
*
|
|
66
|
+
* Route names are identifiers, not URL paths. The resolved route path is the
|
|
67
|
+
* only safe fallback for nested, dynamic, grouped, or custom Nuxt routes.
|
|
68
|
+
* Whole-route `false` becomes a tagged entry carrying that resolved path.
|
|
69
|
+
*/
|
|
70
|
+
declare function materializeI18nPages(autoI18n: AutoI18nConfig, routes: readonly ResolvedI18nRoute[]): I18nPages | undefined;
|
|
71
|
+
declare function mapPathForI18nPages(path: string, autoI18n: AutoI18nConfig, routes?: readonly ResolvedI18nRoute[]): string[] | false;
|
|
51
72
|
interface I18nModuleResolution {
|
|
52
73
|
module: I18nModuleName;
|
|
53
74
|
isMicro: boolean;
|
|
@@ -61,7 +82,15 @@ declare function resolveI18nModule(): false | I18nModuleResolution;
|
|
|
61
82
|
declare function resolveI18nConfig(logger?: {
|
|
62
83
|
warn: (msg: string) => void;
|
|
63
84
|
}): Promise<false | AutoI18nConfig>;
|
|
85
|
+
/**
|
|
86
|
+
* Strip a build-time i18n config down to what `./i18n-runtime` needs, dropping
|
|
87
|
+
* the non-serializable `LocaleObject` extras so it can be handed to the runtime
|
|
88
|
+
* through `runtimeConfig`. Materialize partial and whole-route-false `pages`
|
|
89
|
+
* entries before calling this function; unresolved whole-route false entries
|
|
90
|
+
* are omitted instead of becoming fabricated runtime URLs.
|
|
91
|
+
*/
|
|
92
|
+
declare function toRuntimeI18nConfig(auto: AutoI18nConfig): RuntimeI18nConfig;
|
|
64
93
|
declare function mergeOnKey<T extends Record<string, any>>(arr: T[], key: keyof T): T[];
|
|
65
94
|
|
|
66
|
-
export { expandCompactLocaleRoute, generatePathForI18nPages, isCompactLocaleRoute, mapPathForI18nPages, mergeOnKey, normalizeLocales, resolveI18nConfig, resolveI18nModule, splitPathForI18nLocales };
|
|
67
|
-
export type { AutoI18nConfig, ExpandedLocaleRoute, I18nModuleResolution, NormalisedLocale, Strategies, StrategyProps };
|
|
95
|
+
export { RuntimeI18nConfig, UnlocalizedLocalePage, expandCompactLocaleRoute, generatePathForI18nPages, isCompactLocaleRoute, mapPathForI18nPages, materializeI18nPages, mergeOnKey, normalizeI18nPageKey, normalizeLocales, resolveI18nConfig, resolveI18nModule, splitPathForI18nLocales, toRuntimeI18nConfig };
|
|
96
|
+
export type { AutoI18nConfig, ExpandedLocaleRoute, I18nModuleResolution, NormalisedLocale, ResolvedI18nRoute, Strategies, StrategyProps };
|
package/dist/i18n.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { getNuxtModuleVersion, hasNuxtModuleCompatibility, hasNuxtModule } from '@nuxt/kit';
|
|
2
|
-
import {
|
|
2
|
+
import { withHttps, withBase } from 'ufo';
|
|
3
|
+
import { localePath, resolveLocaleAlternates, resolveLocaleFromRoute } from './i18n-runtime.mjs';
|
|
3
4
|
import { getNuxtModuleOptions } from './kit.mjs';
|
|
4
5
|
import 'node:url';
|
|
5
6
|
import 'pathe';
|
|
@@ -7,32 +8,44 @@ import 'pkg-types';
|
|
|
7
8
|
import 'std-env';
|
|
8
9
|
|
|
9
10
|
const I18N_MODULES = ["@nuxtjs/i18n", "nuxt-i18n-micro"];
|
|
10
|
-
|
|
11
|
+
function isUnlocalizedLocalePage(page) {
|
|
12
|
+
return page._tag === "unlocalized" && typeof page.path === "string";
|
|
13
|
+
}
|
|
11
14
|
function generatePathForI18nPages(ctx) {
|
|
12
15
|
const { localeCode, pageLocales, nuxtI18nConfig, forcedStrategy, normalisedLocales } = ctx;
|
|
13
16
|
const locale = normalisedLocales.find((l) => l.code === localeCode);
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
17
|
+
const strategy = forcedStrategy ?? nuxtI18nConfig.strategy;
|
|
18
|
+
const i18n = toRuntimeI18nConfig({
|
|
19
|
+
defaultLocale: nuxtI18nConfig.defaultLocale,
|
|
20
|
+
locales: normalisedLocales,
|
|
21
|
+
strategy: strategy === "prefix_and_default" ? "prefix_except_default" : strategy
|
|
22
|
+
});
|
|
23
|
+
const generated = localePath(pageLocales, localeCode, i18n);
|
|
24
|
+
const prefixed = generated === localePath(pageLocales, localeCode, { ...i18n, strategy: "prefix" });
|
|
25
|
+
const path = prefixed ? generated.slice(1) : pageLocales;
|
|
24
26
|
return locale?.domain ? withHttps(withBase(path, locale.domain)) : path;
|
|
25
27
|
}
|
|
26
28
|
function splitPathForI18nLocales(path, autoI18n) {
|
|
27
|
-
const
|
|
29
|
+
const selectedLocales = autoI18n.strategy === "prefix_except_default" ? autoI18n.locales.filter((l) => l.code !== autoI18n.defaultLocale) : autoI18n.locales;
|
|
28
30
|
if (!path || path.startsWith("/_"))
|
|
29
31
|
return path;
|
|
30
|
-
const
|
|
32
|
+
const localeCodes = new Set(selectedLocales.map((locale) => locale.code));
|
|
33
|
+
const runtimeI18n = toRuntimeI18nConfig(autoI18n);
|
|
34
|
+
const i18n = {
|
|
35
|
+
...runtimeI18n,
|
|
36
|
+
strategy: "prefix",
|
|
37
|
+
locales: runtimeI18n.locales.filter((locale) => localeCodes.has(locale.code))
|
|
38
|
+
};
|
|
39
|
+
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
|
|
40
|
+
const resolved = resolveLocaleFromRoute(normalizedPath, i18n);
|
|
41
|
+
const hasLocalePrefix = i18n.locales.some(
|
|
42
|
+
(l) => l.code === resolved.locale && localePath(resolved.basePath, l.code, i18n) === normalizedPath
|
|
43
|
+
);
|
|
31
44
|
if (hasLocalePrefix)
|
|
32
45
|
return path;
|
|
33
46
|
return [
|
|
34
47
|
path,
|
|
35
|
-
...locales.map((l) =>
|
|
48
|
+
...i18n.locales.map((l) => localePath(path, l.code, i18n))
|
|
36
49
|
];
|
|
37
50
|
}
|
|
38
51
|
const COMPACT_LOCALE_PATTERN = /^\/:locale\(([^)]+)\)/;
|
|
@@ -68,40 +81,110 @@ function normalizeLocales(nuxtI18nConfig) {
|
|
|
68
81
|
return { ...locale, _hreflang, _sitemap };
|
|
69
82
|
});
|
|
70
83
|
}
|
|
71
|
-
function
|
|
84
|
+
function joinRoutePath(parentPath, path) {
|
|
85
|
+
if (path.startsWith("/"))
|
|
86
|
+
return path;
|
|
87
|
+
if (!path)
|
|
88
|
+
return parentPath || "/";
|
|
89
|
+
return `${parentPath === "/" ? "" : parentPath.replace(/\/$/, "")}/${path}`;
|
|
90
|
+
}
|
|
91
|
+
function resolveLocalizedRouteName(name, autoI18n) {
|
|
92
|
+
if (!name)
|
|
93
|
+
return { name, isDefaultTree: false };
|
|
94
|
+
const separator = autoI18n.routesNameSeparator || "___";
|
|
95
|
+
const defaultSuffix = autoI18n.defaultLocaleRouteNameSuffix || "default";
|
|
96
|
+
const segments = name.split(separator);
|
|
97
|
+
const isDefaultTree = segments.at(-1) === defaultSuffix && autoI18n.locales.some((locale2) => locale2.code === segments.at(-2));
|
|
98
|
+
if (isDefaultTree)
|
|
99
|
+
segments.pop();
|
|
100
|
+
const locale = autoI18n.locales.find((locale2) => locale2.code === segments.at(-1))?.code;
|
|
101
|
+
if (locale)
|
|
102
|
+
segments.pop();
|
|
103
|
+
return {
|
|
104
|
+
name: segments.join(separator),
|
|
105
|
+
...locale ? { locale } : {},
|
|
106
|
+
isDefaultTree
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function flattenResolvedI18nRoutes(routes, autoI18n, parentPath = "") {
|
|
110
|
+
return routes.flatMap((route) => {
|
|
111
|
+
const path = joinRoutePath(parentPath, route.path);
|
|
112
|
+
return [
|
|
113
|
+
{ ...resolveLocalizedRouteName(route.name, autoI18n), path, hasChildren: !!route.children?.length },
|
|
114
|
+
...flattenResolvedI18nRoutes(route.children || [], autoI18n, path)
|
|
115
|
+
];
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
function normalizeI18nPageKey(key) {
|
|
119
|
+
return key.replace(/^\//, "").replace(/\/index$/, "").replace(/\[{1,2}(?:\.\.\.)?([^\]]+)\]{1,2}/g, "$1").split("/").filter((segment) => !/^\([^)]+\)$/.test(segment)).join("-");
|
|
120
|
+
}
|
|
121
|
+
function routeBasePath(route, autoI18n) {
|
|
122
|
+
if (!route.locale || autoI18n.strategy === "no_prefix")
|
|
123
|
+
return route.path;
|
|
124
|
+
return resolveLocaleFromRoute(route.path, toRuntimeI18nConfig(autoI18n), { locale: route.locale }).basePath;
|
|
125
|
+
}
|
|
126
|
+
function materializeI18nPages(autoI18n, routes) {
|
|
127
|
+
const pages = autoI18n.pages;
|
|
128
|
+
if (!pages)
|
|
129
|
+
return void 0;
|
|
130
|
+
const resolvedRoutes = flattenResolvedI18nRoutes(routes, autoI18n);
|
|
131
|
+
return Object.fromEntries(Object.entries(pages).map(([pageName, pageLocales]) => {
|
|
132
|
+
const normalizedPageName = normalizeI18nPageKey(pageName);
|
|
133
|
+
const namedRoutes = resolvedRoutes.filter((route) => route.name === pageName || route.name === normalizedPageName);
|
|
134
|
+
if (pageLocales === false) {
|
|
135
|
+
const route = namedRoutes.find((route2) => route2.locale === autoI18n.defaultLocale) || namedRoutes.find((route2) => !route2.locale) || namedRoutes[0];
|
|
136
|
+
return [pageName, route ? {
|
|
137
|
+
_tag: "unlocalized",
|
|
138
|
+
path: routeBasePath(route, autoI18n),
|
|
139
|
+
...route.hasChildren ? { subtree: true } : {}
|
|
140
|
+
} : false];
|
|
141
|
+
}
|
|
142
|
+
if (isUnlocalizedLocalePage(pageLocales))
|
|
143
|
+
return [pageName, pageLocales];
|
|
144
|
+
const explicitlyMatchedRoute = resolvedRoutes.find((route) => Object.values(pageLocales).some(
|
|
145
|
+
(configuredPath) => typeof configuredPath === "string" && routeBasePath(route, autoI18n) === configuredPath
|
|
146
|
+
));
|
|
147
|
+
const routeGroup = namedRoutes.length ? namedRoutes : explicitlyMatchedRoute?.name ? resolvedRoutes.filter((route) => route.name === explicitlyMatchedRoute.name) : [];
|
|
148
|
+
const defaultRoute = routeGroup.find((route) => route.locale === autoI18n.defaultLocale && route.isDefaultTree) || routeGroup.find((route) => route.locale === autoI18n.defaultLocale) || routeGroup.find((route) => !route.locale) || routeGroup.find((route) => route.locale && pageLocales[route.locale] === void 0);
|
|
149
|
+
const originalPath = defaultRoute ? routeBasePath(defaultRoute, autoI18n) : void 0;
|
|
150
|
+
const defaultPath = pageLocales[autoI18n.defaultLocale];
|
|
151
|
+
const fallbackPath = typeof defaultPath === "string" ? defaultPath : originalPath;
|
|
152
|
+
return [pageName, Object.fromEntries(autoI18n.locales.flatMap((locale) => {
|
|
153
|
+
const configuredPath = pageLocales[locale.code];
|
|
154
|
+
if (configuredPath !== void 0)
|
|
155
|
+
return [[locale.code, configuredPath]];
|
|
156
|
+
return fallbackPath === void 0 ? [] : [[locale.code, fallbackPath]];
|
|
157
|
+
}))];
|
|
158
|
+
}));
|
|
159
|
+
}
|
|
160
|
+
function mapPathForI18nPages(path, autoI18n, routes = []) {
|
|
72
161
|
const pages = autoI18n.pages;
|
|
73
162
|
if (!pages || !Object.keys(pages).length)
|
|
74
163
|
return false;
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
if (!pageLocales)
|
|
100
|
-
continue;
|
|
101
|
-
if (autoI18n.defaultLocale in pageLocales && pageLocales[autoI18n.defaultLocale] === path)
|
|
102
|
-
return resolveForAllLocales(pageName, pageLocales);
|
|
103
|
-
}
|
|
104
|
-
return false;
|
|
164
|
+
const materializedPages = routes.length ? materializeI18nPages(autoI18n, routes) : pages;
|
|
165
|
+
const resolvedRoutes = flattenResolvedI18nRoutes(routes, autoI18n);
|
|
166
|
+
const pagesForMapping = Object.fromEntries(Object.entries(materializedPages || {}).map(([pageName, pageLocales]) => {
|
|
167
|
+
if (pageLocales !== false)
|
|
168
|
+
return [pageName, pageLocales];
|
|
169
|
+
const normalizedPageName = normalizeI18nPageKey(pageName);
|
|
170
|
+
const route = resolvedRoutes.find((route2) => route2.name === pageName || route2.name === normalizedPageName);
|
|
171
|
+
if (!route)
|
|
172
|
+
return [pageName, false];
|
|
173
|
+
const originalPath = routeBasePath(route, autoI18n);
|
|
174
|
+
return [pageName, Object.fromEntries(autoI18n.locales.map((locale) => [
|
|
175
|
+
locale.code,
|
|
176
|
+
locale.code === autoI18n.defaultLocale ? originalPath : false
|
|
177
|
+
]))];
|
|
178
|
+
}));
|
|
179
|
+
const i18n = toRuntimeI18nConfig({
|
|
180
|
+
...autoI18n,
|
|
181
|
+
strategy: autoI18n.strategy === "prefix_and_default" ? "prefix_except_default" : autoI18n.strategy,
|
|
182
|
+
pages: pagesForMapping
|
|
183
|
+
});
|
|
184
|
+
const resolved = resolveLocaleAlternates(path, i18n, autoI18n.strategy === "no_prefix" ? { locale: autoI18n.defaultLocale } : {});
|
|
185
|
+
if (resolved._tag === "strategy")
|
|
186
|
+
return false;
|
|
187
|
+
return resolved.alternates.map((alternate) => alternate.domain ? withHttps(withBase(alternate.path, alternate.domain)) : alternate.path);
|
|
105
188
|
}
|
|
106
189
|
function resolveI18nModule() {
|
|
107
190
|
const found = I18N_MODULES.find((m) => hasNuxtModule(m));
|
|
@@ -117,7 +200,7 @@ function resolveI18nPages(config, isMicro) {
|
|
|
117
200
|
if (!routes)
|
|
118
201
|
return void 0;
|
|
119
202
|
return Object.fromEntries(
|
|
120
|
-
Object.entries(routes).filter((entry) => typeof entry[1] === "object" && entry[1] !== null && !Array.isArray(entry[1]))
|
|
203
|
+
Object.entries(routes).filter((entry) => entry[1] === false || typeof entry[1] === "object" && entry[1] !== null && !Array.isArray(entry[1]))
|
|
121
204
|
);
|
|
122
205
|
}
|
|
123
206
|
async function resolveI18nConfig(logger) {
|
|
@@ -134,17 +217,48 @@ async function resolveI18nConfig(logger) {
|
|
|
134
217
|
const normalisedLocales = normalizeLocales(nuxtI18nConfig);
|
|
135
218
|
const pages = resolveI18nPages(nuxtI18nConfig, i18n.isMicro);
|
|
136
219
|
const usingI18nPages = Object.keys(pages || {}).length;
|
|
137
|
-
const hasI18nConfigForAlternatives = nuxtI18nConfig.differentDomains || usingI18nPages || nuxtI18nConfig.strategy !== "no_prefix" && nuxtI18nConfig.locales;
|
|
220
|
+
const hasI18nConfigForAlternatives = nuxtI18nConfig.differentDomains || nuxtI18nConfig.multiDomainLocales || usingI18nPages || nuxtI18nConfig.strategy !== "no_prefix" && nuxtI18nConfig.locales;
|
|
138
221
|
if (!hasI18nConfigForAlternatives)
|
|
139
222
|
return false;
|
|
140
223
|
return {
|
|
141
224
|
differentDomains: nuxtI18nConfig.differentDomains,
|
|
225
|
+
multiDomainLocales: nuxtI18nConfig.multiDomainLocales,
|
|
226
|
+
routesNameSeparator: nuxtI18nConfig.routesNameSeparator,
|
|
227
|
+
defaultLocaleRouteNameSuffix: nuxtI18nConfig.defaultLocaleRouteNameSuffix,
|
|
142
228
|
defaultLocale: nuxtI18nConfig.defaultLocale,
|
|
143
229
|
locales: normalisedLocales,
|
|
144
230
|
strategy: nuxtI18nConfig.strategy,
|
|
145
231
|
pages
|
|
146
232
|
};
|
|
147
233
|
}
|
|
234
|
+
function toRuntimeI18nConfig(auto) {
|
|
235
|
+
const pages = auto.pages && Object.fromEntries(
|
|
236
|
+
Object.entries(auto.pages).filter((entry) => entry[1] !== false)
|
|
237
|
+
);
|
|
238
|
+
return {
|
|
239
|
+
defaultLocale: auto.defaultLocale,
|
|
240
|
+
strategy: auto.strategy,
|
|
241
|
+
...auto.differentDomains ? { differentDomains: true } : {},
|
|
242
|
+
...auto.multiDomainLocales ? { multiDomainLocales: true } : {},
|
|
243
|
+
// Translated route paths. Without these the runtime can only guess
|
|
244
|
+
// alternates by adding/removing a locale prefix, which is wrong for every
|
|
245
|
+
// page whose slug differs per locale.
|
|
246
|
+
...pages && Object.keys(pages).length ? { pages } : {},
|
|
247
|
+
locales: auto.locales.map((l) => {
|
|
248
|
+
const raw = l;
|
|
249
|
+
return {
|
|
250
|
+
code: l.code,
|
|
251
|
+
hreflang: l._hreflang || raw.language || l.code,
|
|
252
|
+
name: raw.name,
|
|
253
|
+
nativeName: raw.nativeName ?? raw.name,
|
|
254
|
+
...raw.language ? { language: raw.language } : {},
|
|
255
|
+
...raw.domain ? { domain: raw.domain } : {},
|
|
256
|
+
...raw.domains?.length ? { domains: [...raw.domains] } : {},
|
|
257
|
+
...raw.defaultForDomains?.length ? { defaultForDomains: [...raw.defaultForDomains] } : {}
|
|
258
|
+
};
|
|
259
|
+
})
|
|
260
|
+
};
|
|
261
|
+
}
|
|
148
262
|
function mergeOnKey(arr, key) {
|
|
149
263
|
const map = /* @__PURE__ */ new Map();
|
|
150
264
|
for (const item of arr) {
|
|
@@ -158,4 +272,4 @@ function mergeOnKey(arr, key) {
|
|
|
158
272
|
return [...map.values()];
|
|
159
273
|
}
|
|
160
274
|
|
|
161
|
-
export { expandCompactLocaleRoute, generatePathForI18nPages, isCompactLocaleRoute, mapPathForI18nPages, mergeOnKey, normalizeLocales, resolveI18nConfig, resolveI18nModule, splitPathForI18nLocales };
|
|
275
|
+
export { expandCompactLocaleRoute, generatePathForI18nPages, isCompactLocaleRoute, mapPathForI18nPages, materializeI18nPages, mergeOnKey, normalizeI18nPageKey, normalizeLocales, resolveI18nConfig, resolveI18nModule, splitPathForI18nLocales, toRuntimeI18nConfig };
|
package/dist/kit.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { pathToFileURL } from 'node:url';
|
|
2
|
-
import { getNuxtVersion, addTypeTemplate, useNuxt, resolveModule, createResolver, addTemplate, loadNuxtModuleInstance, tryUseNuxt, hasNuxtModule, hasNuxtModuleCompatibility
|
|
2
|
+
import { getNuxtVersion, addTypeTemplate, useNuxt, useLogger, resolveModule, directoryToURL, createResolver, addTemplate, loadNuxtModuleInstance, tryUseNuxt, hasNuxtModule, hasNuxtModuleCompatibility } from '@nuxt/kit';
|
|
3
3
|
import { relative, dirname } from 'pathe';
|
|
4
4
|
import { resolvePackageJSON, readPackageJSON } from 'pkg-types';
|
|
5
5
|
import { provider, env } from 'std-env';
|
|
@@ -125,12 +125,38 @@ ${indent(h3Runtime.trim(), 2)}
|
|
|
125
125
|
}
|
|
126
126
|
`;
|
|
127
127
|
}
|
|
128
|
-
function
|
|
128
|
+
function resolveRuntimeModule(nuxt, id) {
|
|
129
|
+
try {
|
|
130
|
+
return {
|
|
131
|
+
_tag: "resolved",
|
|
132
|
+
path: resolveModule(id, {
|
|
133
|
+
url: [...nuxt.options.modulesDir.map(directoryToURL), new URL(import.meta.url)]
|
|
134
|
+
})
|
|
135
|
+
};
|
|
136
|
+
} catch (cause) {
|
|
137
|
+
return { _tag: "unresolved", cause };
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
function applyNitroRuntimeCompatibility(nuxt, compatibility, reportResolutionFailure = false) {
|
|
129
141
|
const nuxtOptions = nuxt.options;
|
|
130
142
|
const nitroOptions = nuxtOptions.nitro ||= {};
|
|
131
143
|
nitroOptions.alias ||= {};
|
|
132
144
|
nitroOptions.virtual ||= {};
|
|
133
|
-
|
|
145
|
+
const h3RuntimeModule = compatibility._tag === "nitro-v3" ? "nitro/h3" : "h3";
|
|
146
|
+
nitroOptions.alias[H3_RUNTIME_MODULE] = h3RuntimeModule;
|
|
147
|
+
const h3Resolution = resolveRuntimeModule(nuxt, h3RuntimeModule);
|
|
148
|
+
if (h3Resolution._tag === "resolved") {
|
|
149
|
+
nitroOptions.typescript ||= {};
|
|
150
|
+
nitroOptions.typescript.tsConfig ||= {};
|
|
151
|
+
nitroOptions.typescript.tsConfig.compilerOptions ||= {};
|
|
152
|
+
nitroOptions.typescript.tsConfig.compilerOptions.paths ||= {};
|
|
153
|
+
nitroOptions.typescript.tsConfig.compilerOptions.paths[H3_RUNTIME_MODULE] = [h3Resolution.path];
|
|
154
|
+
} else if (reportResolutionFailure) {
|
|
155
|
+
useLogger("nuxtseo-shared").warn(
|
|
156
|
+
`Could not resolve Nitro runtime module '${h3RuntimeModule}'. Generated server types may be incomplete.`,
|
|
157
|
+
h3Resolution.cause
|
|
158
|
+
);
|
|
159
|
+
}
|
|
134
160
|
if (compatibility._tag === "nitro-v3")
|
|
135
161
|
nitroOptions.alias[OFETCH_RUNTIME_MODULE] = resolveModule("ofetch", { url: new URL(import.meta.url) });
|
|
136
162
|
nitroOptions.virtual[NITRO_RUNTIME_MODULE] = compatibility._tag === "nitro-v3" ? nitroV3Runtime : nitroV2Runtime;
|
|
@@ -165,7 +191,7 @@ function setupNitroRuntimeCompatibility(nuxt = useNuxt()) {
|
|
|
165
191
|
const nuxtWithRuntimeMarker = nuxt;
|
|
166
192
|
if (!nuxtWithRuntimeMarker[runtimeSetupMarker]) {
|
|
167
193
|
nuxtWithRuntimeMarker[runtimeSetupMarker] = true;
|
|
168
|
-
nuxt.hooks.hookOnce("modules:done", () => applyNitroRuntimeCompatibility(nuxt, compatibility));
|
|
194
|
+
nuxt.hooks.hookOnce("modules:done", () => applyNitroRuntimeCompatibility(nuxt, compatibility, true));
|
|
169
195
|
}
|
|
170
196
|
const nuxtWithTypeMarker = nuxt;
|
|
171
197
|
if (!nuxtWithTypeMarker[typeSetupMarker]) {
|
package/dist/module.json
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxtseo-shared",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "5.3.
|
|
4
|
+
"version": "5.3.12",
|
|
5
5
|
"description": "Shared utilities for Nuxt SEO modules.",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Harlan Wilton",
|
|
@@ -40,6 +40,10 @@
|
|
|
40
40
|
"types": "./dist/i18n.d.mts",
|
|
41
41
|
"default": "./dist/i18n.mjs"
|
|
42
42
|
},
|
|
43
|
+
"./i18n-runtime": {
|
|
44
|
+
"types": "./dist/i18n-runtime.d.mts",
|
|
45
|
+
"default": "./dist/i18n-runtime.mjs"
|
|
46
|
+
},
|
|
43
47
|
"./pro": {
|
|
44
48
|
"types": "./dist/pro.d.mts",
|
|
45
49
|
"default": "./dist/pro.mjs"
|
|
@@ -81,7 +85,7 @@
|
|
|
81
85
|
"dependencies": {
|
|
82
86
|
"@clack/prompts": "^1.7.0",
|
|
83
87
|
"@nuxt/devtools-kit": "4.0.0-alpha.7",
|
|
84
|
-
"@nuxt/kit": "^4.5.
|
|
88
|
+
"@nuxt/kit": "^4.5.2",
|
|
85
89
|
"birpc": "^4.0.0",
|
|
86
90
|
"consola": "^3.4.2",
|
|
87
91
|
"defu": "^6.1.7",
|
|
@@ -97,16 +101,16 @@
|
|
|
97
101
|
"devDependencies": {
|
|
98
102
|
"@arethetypeswrong/cli": "^0.18.5",
|
|
99
103
|
"@nuxt/module-builder": "^1.0.3",
|
|
100
|
-
"@nuxt/schema": "^4.5.
|
|
104
|
+
"@nuxt/schema": "^4.5.2",
|
|
101
105
|
"@nuxt/test-utils": "^4.1.0",
|
|
102
106
|
"@nuxtjs/i18n": "^10.6.0",
|
|
103
107
|
"h3": "^1.15.11",
|
|
104
108
|
"nitropack": "^2.13.4",
|
|
105
|
-
"nuxt": "^4.5.
|
|
106
|
-
"nuxt-site-config": "^4.
|
|
109
|
+
"nuxt": "^4.5.2",
|
|
110
|
+
"nuxt-site-config": "^4.2.0",
|
|
107
111
|
"typescript": "6.0.3",
|
|
108
112
|
"vitest": "^4.1.10",
|
|
109
|
-
"vue": "^3.5.
|
|
113
|
+
"vue": "^3.5.41",
|
|
110
114
|
"vue-tsc": "^3.3.9"
|
|
111
115
|
},
|
|
112
116
|
"scripts": {
|