@ubean/i18n 0.6.4 → 0.6.6
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/browser.d.ts +3 -3
- package/dist/browser.js +3 -3
- package/dist/{detect-BIURd4aL.js → detect-DgCzeC6u.js} +55 -19
- package/dist/{head-CT8nH37d.d.ts → head-BJtI_y8G.d.ts} +1 -1
- package/dist/{head-BzEFwnlD.js → head-DWOP7x2f.js} +4 -4
- package/dist/index.d.ts +6 -3
- package/dist/index.js +4 -4
- package/dist/paths-CnNW2Mah.d.ts +94 -0
- package/dist/{routing-Psxmai55.js → routing-CqXNfVlC.js} +7 -4
- package/dist/routing.d.ts +1 -1
- package/dist/routing.js +2 -2
- package/package.json +3 -3
- package/dist/paths-CJVBLGJS.d.ts +0 -59
package/dist/browser.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
import { a as parseLocaleCookie, i as detectLocaleFromAcceptLanguage, n as LocaleHeadTags, o as serializeLocaleCookie, r as buildLocaleHead, t as LocaleHeadInput } from "./head-
|
|
3
|
-
export { type CompiledLocalePath, type HonoLocalePath, type I18nLocaleMeta, type I18nRoutingStrategy, type LocaleHeadInput, type LocaleHeadTags, type LocaleRoutingConfig, buildLocaleHead, compileLocalePaths, detectLocaleFromAcceptLanguage, extractLocaleFromPath, getVueLocaleParam, localizePath, parseLocaleCookie, serializeLocaleCookie, switchLocalePath, toVueRouterLocalePath };
|
|
1
|
+
import { a as localeSegment, c as switchLocalePath, d as HonoLocalePath, f as I18nLocaleMeta, h as LocaleRoutingConfig, i as localeFromSegment, l as toVueRouterLocalePath, m as I18nRoutingStrategy, n as extractLocaleFromPath, o as localeSegments, r as getVueLocaleParam, s as localizePath, t as compileLocalePaths, u as CompiledLocalePath } from "./paths-CnNW2Mah.js";
|
|
2
|
+
import { a as parseLocaleCookie, i as detectLocaleFromAcceptLanguage, n as LocaleHeadTags, o as serializeLocaleCookie, r as buildLocaleHead, t as LocaleHeadInput } from "./head-BJtI_y8G.js";
|
|
3
|
+
export { type CompiledLocalePath, type HonoLocalePath, type I18nLocaleMeta, type I18nRoutingStrategy, type LocaleHeadInput, type LocaleHeadTags, type LocaleRoutingConfig, buildLocaleHead, compileLocalePaths, detectLocaleFromAcceptLanguage, extractLocaleFromPath, getVueLocaleParam, localeFromSegment, localeSegment, localeSegments, localizePath, parseLocaleCookie, serializeLocaleCookie, switchLocalePath, toVueRouterLocalePath };
|
package/dist/browser.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { a as extractLocaleFromPath, c as switchLocalePath, i as compileLocalePaths, l as
|
|
2
|
-
import { t as buildLocaleHead } from "./head-
|
|
3
|
-
export { buildLocaleHead, compileLocalePaths, detectLocaleFromAcceptLanguage, extractLocaleFromPath, getVueLocaleParam, localizePath, parseLocaleCookie, serializeLocaleCookie, switchLocalePath, toVueRouterLocalePath };
|
|
1
|
+
import { a as extractLocaleFromPath, c as localeSegment, d as switchLocalePath, f as toVueRouterLocalePath, i as compileLocalePaths, l as localeSegments, n as parseLocaleCookie, o as getVueLocaleParam, r as serializeLocaleCookie, s as localeFromSegment, t as detectLocaleFromAcceptLanguage, u as localizePath } from "./detect-DgCzeC6u.js";
|
|
2
|
+
import { t as buildLocaleHead } from "./head-DWOP7x2f.js";
|
|
3
|
+
export { buildLocaleHead, compileLocalePaths, detectLocaleFromAcceptLanguage, extractLocaleFromPath, getVueLocaleParam, localeFromSegment, localeSegment, localeSegments, localizePath, parseLocaleCookie, serializeLocaleCookie, switchLocalePath, toVueRouterLocalePath };
|
|
@@ -1,4 +1,26 @@
|
|
|
1
1
|
//#region src/paths.ts
|
|
2
|
+
/** URL segment for a locale code (`paths` override, else the code itself). */
|
|
3
|
+
function localeSegment(code, paths) {
|
|
4
|
+
return paths?.[code] ?? code;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Reverse of `localeSegment`: the locale code owning a URL segment.
|
|
8
|
+
* Returns `null` for a segment no configured locale claims.
|
|
9
|
+
*/
|
|
10
|
+
function localeFromSegment(segment, locales, paths) {
|
|
11
|
+
for (const code of locales) if (localeSegment(code, paths) === segment) return code;
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* URL segments for every configured locale, in config order.
|
|
16
|
+
*
|
|
17
|
+
* This — not the raw codes — is the set the router matches against: with
|
|
18
|
+
* `{ 'zh-CN': 'zh' }` the URL is `/zh/about`, so a param built from `zh-CN`
|
|
19
|
+
* would never match the path the server rewrites to.
|
|
20
|
+
*/
|
|
21
|
+
function localeSegments(cfg) {
|
|
22
|
+
return cfg.locales.map((code) => localeSegment(code, cfg.paths));
|
|
23
|
+
}
|
|
2
24
|
function escapeRegex(code) {
|
|
3
25
|
return code.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
4
26
|
}
|
|
@@ -20,17 +42,19 @@ function normalizePath(path) {
|
|
|
20
42
|
* - `no_prefix`: empty
|
|
21
43
|
*/
|
|
22
44
|
function getVueLocaleParam(cfg) {
|
|
23
|
-
const { strategy, defaultLocale
|
|
24
|
-
const
|
|
45
|
+
const { strategy, defaultLocale } = cfg;
|
|
46
|
+
const segments = localeSegments(cfg);
|
|
47
|
+
const defaultSegment = localeSegment(defaultLocale, cfg.paths);
|
|
48
|
+
const others = segments.filter((c) => c !== defaultSegment);
|
|
25
49
|
switch (strategy) {
|
|
26
50
|
case "no_prefix": return "";
|
|
27
|
-
case "prefix": return
|
|
51
|
+
case "prefix": return segments.length === 0 ? "" : `:locale(${joinCodes(segments)})`;
|
|
28
52
|
case "prefix_except_default":
|
|
29
53
|
if (others.length === 0) return "";
|
|
30
54
|
return `:locale(${joinCodes(others)})?`;
|
|
31
55
|
case "prefix_and_default":
|
|
32
|
-
if (
|
|
33
|
-
return `:locale(${joinCodes(
|
|
56
|
+
if (segments.length === 0) return "";
|
|
57
|
+
return `:locale(${joinCodes(segments)})?`;
|
|
34
58
|
default: return "";
|
|
35
59
|
}
|
|
36
60
|
}
|
|
@@ -41,16 +65,26 @@ function toVueRouterLocalePath(pagePath, localeParam) {
|
|
|
41
65
|
if (path === "/") return `/${localeParam}`;
|
|
42
66
|
return `/${localeParam}${path}`;
|
|
43
67
|
}
|
|
44
|
-
|
|
68
|
+
/**
|
|
69
|
+
* Split a URL path into its locale code and the rest.
|
|
70
|
+
*
|
|
71
|
+
* The returned `locale` is the **code**, not the segment — with
|
|
72
|
+
* `{ 'zh-CN': 'zh' }` requesting `/zh/about` yields `{ locale: 'zh-CN' }`,
|
|
73
|
+
* so callers keep using one vocabulary everywhere except in the URL itself.
|
|
74
|
+
*/
|
|
75
|
+
function extractLocaleFromPath(path, localeCodes, paths) {
|
|
45
76
|
const normalized = normalizePath(path);
|
|
46
77
|
const segments = normalized.split("/").filter(Boolean);
|
|
47
78
|
const first = segments[0];
|
|
48
|
-
if (first
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
79
|
+
if (first) {
|
|
80
|
+
const code = localeFromSegment(first, localeCodes, paths);
|
|
81
|
+
if (code) {
|
|
82
|
+
const rest = segments.slice(1).join("/");
|
|
83
|
+
return {
|
|
84
|
+
locale: code,
|
|
85
|
+
pathWithoutLocale: rest ? `/${rest}` : "/"
|
|
86
|
+
};
|
|
87
|
+
}
|
|
54
88
|
}
|
|
55
89
|
return {
|
|
56
90
|
locale: null,
|
|
@@ -58,16 +92,17 @@ function extractLocaleFromPath(path, localeCodes) {
|
|
|
58
92
|
};
|
|
59
93
|
}
|
|
60
94
|
function localizePath(path, locale, cfg) {
|
|
61
|
-
const { pathWithoutLocale } = extractLocaleFromPath(path, cfg.locales);
|
|
95
|
+
const { pathWithoutLocale } = extractLocaleFromPath(path, cfg.locales, cfg.paths);
|
|
62
96
|
const clean = pathWithoutLocale;
|
|
63
97
|
const suffix = clean === "/" ? "" : clean;
|
|
98
|
+
const segment = localeSegment(locale, cfg.paths);
|
|
64
99
|
switch (cfg.strategy) {
|
|
65
100
|
case "no_prefix": return clean;
|
|
66
|
-
case "prefix": return `/${
|
|
101
|
+
case "prefix": return `/${segment}${suffix}`;
|
|
67
102
|
case "prefix_except_default":
|
|
68
103
|
case "prefix_and_default":
|
|
69
104
|
if (locale === cfg.defaultLocale) return clean;
|
|
70
|
-
return `/${
|
|
105
|
+
return `/${segment}${suffix}`;
|
|
71
106
|
default: return clean;
|
|
72
107
|
}
|
|
73
108
|
}
|
|
@@ -86,23 +121,24 @@ function honoPathsFor(pagePath, cfg) {
|
|
|
86
121
|
isDefault
|
|
87
122
|
});
|
|
88
123
|
};
|
|
124
|
+
const segment = (code) => localeSegment(code, cfg.paths);
|
|
89
125
|
switch (strategy) {
|
|
90
126
|
case "no_prefix":
|
|
91
127
|
push(path, defaultLocale, true);
|
|
92
128
|
break;
|
|
93
129
|
case "prefix":
|
|
94
|
-
for (const locale of locales) push(`/${locale}${suffix}`, locale, locale === defaultLocale);
|
|
130
|
+
for (const locale of locales) push(`/${segment(locale)}${suffix}`, locale, locale === defaultLocale);
|
|
95
131
|
break;
|
|
96
132
|
case "prefix_except_default":
|
|
97
133
|
push(path, defaultLocale, true);
|
|
98
134
|
for (const locale of locales) {
|
|
99
135
|
if (locale === defaultLocale) continue;
|
|
100
|
-
push(`/${locale}${suffix}`, locale, false);
|
|
136
|
+
push(`/${segment(locale)}${suffix}`, locale, false);
|
|
101
137
|
}
|
|
102
138
|
break;
|
|
103
139
|
case "prefix_and_default":
|
|
104
140
|
push(path, defaultLocale, true);
|
|
105
|
-
for (const locale of locales) push(`/${locale}${suffix}`, locale, locale === defaultLocale);
|
|
141
|
+
for (const locale of locales) push(`/${segment(locale)}${suffix}`, locale, locale === defaultLocale);
|
|
106
142
|
break;
|
|
107
143
|
default: push(path, defaultLocale, true);
|
|
108
144
|
}
|
|
@@ -142,4 +178,4 @@ function serializeLocaleCookie(cookieName, locale) {
|
|
|
142
178
|
return `${cookieName}=${encodeURIComponent(locale)}; Path=/; SameSite=Lax`;
|
|
143
179
|
}
|
|
144
180
|
//#endregion
|
|
145
|
-
export { extractLocaleFromPath as a,
|
|
181
|
+
export { extractLocaleFromPath as a, localeSegment as c, switchLocalePath as d, toVueRouterLocalePath as f, compileLocalePaths as i, localeSegments as l, parseLocaleCookie as n, getVueLocaleParam as o, serializeLocaleCookie as r, localeFromSegment as s, detectLocaleFromAcceptLanguage as t, localizePath as u };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { f as
|
|
1
|
+
import { f as I18nLocaleMeta, h as LocaleRoutingConfig } from "./paths-CnNW2Mah.js";
|
|
2
2
|
//#region src/detect.d.ts
|
|
3
3
|
declare function detectLocaleFromAcceptLanguage(header: string | undefined, locales: string[], fallback: string): string;
|
|
4
4
|
declare function parseLocaleCookie(cookieHeader: string | undefined, cookieName: string): string | null;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as extractLocaleFromPath,
|
|
1
|
+
import { a as extractLocaleFromPath, u as localizePath } from "./detect-DgCzeC6u.js";
|
|
2
2
|
//#region src/head.ts
|
|
3
3
|
function abs(baseUrl, path) {
|
|
4
4
|
if (!baseUrl) return path;
|
|
@@ -7,8 +7,8 @@ function abs(baseUrl, path) {
|
|
|
7
7
|
function ogLocale(languageOrCode) {
|
|
8
8
|
return languageOrCode.replace(/-/g, "_");
|
|
9
9
|
}
|
|
10
|
-
function pagePathWithoutLocale(path,
|
|
11
|
-
return extractLocaleFromPath(path, locales).pathWithoutLocale;
|
|
10
|
+
function pagePathWithoutLocale(path, routing) {
|
|
11
|
+
return extractLocaleFromPath(path, routing.locales, routing.paths).pathWithoutLocale;
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
14
|
* Build hreflang / canonical / og:locale tags.
|
|
@@ -18,7 +18,7 @@ function buildLocaleHead(input) {
|
|
|
18
18
|
const current = input.locales.find((l) => l.code === input.locale);
|
|
19
19
|
const lang = current?.language || input.locale;
|
|
20
20
|
const dir = current?.dir || "ltr";
|
|
21
|
-
const clean = pagePathWithoutLocale(input.path, input.routing
|
|
21
|
+
const clean = pagePathWithoutLocale(input.path, input.routing);
|
|
22
22
|
const codes = input.routing.locales;
|
|
23
23
|
const link = [];
|
|
24
24
|
const meta = [];
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
import { a as parseLocaleCookie, i as detectLocaleFromAcceptLanguage, n as LocaleHeadTags, o as serializeLocaleCookie, r as buildLocaleHead, t as LocaleHeadInput } from "./head-
|
|
1
|
+
import { a as localeSegment, c as switchLocalePath, d as HonoLocalePath, f as I18nLocaleMeta, h as LocaleRoutingConfig, i as localeFromSegment, l as toVueRouterLocalePath, m as I18nRoutingStrategy, n as extractLocaleFromPath, o as localeSegments, p as I18nMiddlewareOptions, r as getVueLocaleParam, s as localizePath, t as compileLocalePaths, u as CompiledLocalePath } from "./paths-CnNW2Mah.js";
|
|
2
|
+
import { a as parseLocaleCookie, i as detectLocaleFromAcceptLanguage, n as LocaleHeadTags, o as serializeLocaleCookie, r as buildLocaleHead, t as LocaleHeadInput } from "./head-BJtI_y8G.js";
|
|
3
3
|
import { createI18nMiddleware, getPathWithoutLocale, getRequestLocale as getRequestLocale$1 } from "./routing.js";
|
|
4
4
|
import { LocaleMessage } from "@intlify/core";
|
|
5
5
|
//#region src/context.d.ts
|
|
@@ -22,12 +22,14 @@ export declare function getLocaleMessages(code: string): LocaleMessages | undefi
|
|
|
22
22
|
export declare function mergeLocaleMessages(code: string, messages: LocaleMessages): LocaleMessages;
|
|
23
23
|
export declare function setLocaleMeta(code: string, meta: {
|
|
24
24
|
name?: string;
|
|
25
|
+
path?: string;
|
|
25
26
|
dir?: 'ltr' | 'rtl';
|
|
26
27
|
language?: string;
|
|
27
28
|
isDefault?: boolean;
|
|
28
29
|
}): void;
|
|
29
30
|
export declare function getLocaleMeta(code: string): {
|
|
30
31
|
name?: string;
|
|
32
|
+
path?: string;
|
|
31
33
|
dir: "ltr" | "rtl";
|
|
32
34
|
language?: string;
|
|
33
35
|
isDefault?: boolean;
|
|
@@ -35,6 +37,7 @@ export declare function getLocaleMeta(code: string): {
|
|
|
35
37
|
export declare function listLocaleCodes(): string[];
|
|
36
38
|
export declare function getRegisteredLocalesMeta(): Array<{
|
|
37
39
|
code: string;
|
|
40
|
+
path?: string;
|
|
38
41
|
name?: string;
|
|
39
42
|
dir: 'ltr' | 'rtl';
|
|
40
43
|
language?: string;
|
|
@@ -56,4 +59,4 @@ export declare function t(key: string, ...args: unknown[]): string;
|
|
|
56
59
|
export declare function d(value: Date | number | string, ...args: unknown[]): string;
|
|
57
60
|
export declare function n(value: number, ...args: unknown[]): string;
|
|
58
61
|
//#endregion
|
|
59
|
-
export { type CompiledLocalePath, type HonoLocalePath, type I18nCoreContext, type I18nLocaleMeta, type I18nMiddlewareOptions, type I18nRequestScope, type I18nRoutingStrategy, type LocaleHeadInput, type LocaleHeadTags, type LocaleMessages, type LocaleRoutingConfig, buildLocaleHead, compileLocalePaths, createI18nMiddleware, detectLocaleFromAcceptLanguage, extractLocaleFromPath, getRequestLocale as getAlsLocale, getPathWithoutLocale, getRequestLocale$1 as getRequestLocale, getVueLocaleParam, localizePath, parseLocaleCookie, serializeLocaleCookie, switchLocalePath, toVueRouterLocalePath };
|
|
62
|
+
export { type CompiledLocalePath, type HonoLocalePath, type I18nCoreContext, type I18nLocaleMeta, type I18nMiddlewareOptions, type I18nRequestScope, type I18nRoutingStrategy, type LocaleHeadInput, type LocaleHeadTags, type LocaleMessages, type LocaleRoutingConfig, buildLocaleHead, compileLocalePaths, createI18nMiddleware, detectLocaleFromAcceptLanguage, extractLocaleFromPath, getRequestLocale as getAlsLocale, getPathWithoutLocale, getRequestLocale$1 as getRequestLocale, getVueLocaleParam, localeFromSegment, localeSegment, localeSegments, localizePath, parseLocaleCookie, serializeLocaleCookie, switchLocalePath, toVueRouterLocalePath };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as extractLocaleFromPath, c as switchLocalePath, i as compileLocalePaths, l as
|
|
2
|
-
import { C as t, S as setLocaleMeta, _ as n, a as d, b as setFallbackLocale, c as getI18nScope, d as getLocaleMeta, f as getLocaleName, g as mergeLocaleMessages, h as listLocaleCodes, i as createRequestContext, l as getLocaleDir, m as getRequestLocale, n as getPathWithoutLocale, o as ensureLocaleMessages, p as getRegisteredLocalesMeta, r as getRequestLocale$1, s as getFallbackLocale, t as createI18nMiddleware, u as getLocaleMessages, v as registerLocaleLoader, x as setLocaleMessages, y as runWithI18n } from "./routing-
|
|
3
|
-
import { t as buildLocaleHead } from "./head-
|
|
4
|
-
export { buildLocaleHead, compileLocalePaths, createI18nMiddleware, createRequestContext, d, detectLocaleFromAcceptLanguage, ensureLocaleMessages, extractLocaleFromPath, getRequestLocale as getAlsLocale, getFallbackLocale, getI18nScope, getLocaleDir, getLocaleMessages, getLocaleMeta, getLocaleName, getPathWithoutLocale, getRegisteredLocalesMeta, getRequestLocale$1 as getRequestLocale, getVueLocaleParam, listLocaleCodes, localizePath, mergeLocaleMessages, n, parseLocaleCookie, registerLocaleLoader, runWithI18n, serializeLocaleCookie, setFallbackLocale, setLocaleMessages, setLocaleMeta, switchLocalePath, t, toVueRouterLocalePath };
|
|
1
|
+
import { a as extractLocaleFromPath, c as localeSegment, d as switchLocalePath, f as toVueRouterLocalePath, i as compileLocalePaths, l as localeSegments, n as parseLocaleCookie, o as getVueLocaleParam, r as serializeLocaleCookie, s as localeFromSegment, t as detectLocaleFromAcceptLanguage, u as localizePath } from "./detect-DgCzeC6u.js";
|
|
2
|
+
import { C as t, S as setLocaleMeta, _ as n, a as d, b as setFallbackLocale, c as getI18nScope, d as getLocaleMeta, f as getLocaleName, g as mergeLocaleMessages, h as listLocaleCodes, i as createRequestContext, l as getLocaleDir, m as getRequestLocale, n as getPathWithoutLocale, o as ensureLocaleMessages, p as getRegisteredLocalesMeta, r as getRequestLocale$1, s as getFallbackLocale, t as createI18nMiddleware, u as getLocaleMessages, v as registerLocaleLoader, x as setLocaleMessages, y as runWithI18n } from "./routing-CqXNfVlC.js";
|
|
3
|
+
import { t as buildLocaleHead } from "./head-DWOP7x2f.js";
|
|
4
|
+
export { buildLocaleHead, compileLocalePaths, createI18nMiddleware, createRequestContext, d, detectLocaleFromAcceptLanguage, ensureLocaleMessages, extractLocaleFromPath, getRequestLocale as getAlsLocale, getFallbackLocale, getI18nScope, getLocaleDir, getLocaleMessages, getLocaleMeta, getLocaleName, getPathWithoutLocale, getRegisteredLocalesMeta, getRequestLocale$1 as getRequestLocale, getVueLocaleParam, listLocaleCodes, localeFromSegment, localeSegment, localeSegments, localizePath, mergeLocaleMessages, n, parseLocaleCookie, registerLocaleLoader, runWithI18n, serializeLocaleCookie, setFallbackLocale, setLocaleMessages, setLocaleMeta, switchLocalePath, t, toVueRouterLocalePath };
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
type I18nRoutingStrategy = 'prefix' | 'prefix_except_default' | 'prefix_and_default' | 'no_prefix';
|
|
3
|
+
interface LocaleRoutingConfig {
|
|
4
|
+
defaultLocale: string;
|
|
5
|
+
locales: string[];
|
|
6
|
+
strategy: I18nRoutingStrategy;
|
|
7
|
+
/**
|
|
8
|
+
* Locale code → URL segment. Missing entries fall back to the code itself.
|
|
9
|
+
*
|
|
10
|
+
* `code` is the identity (BCP-47, used for cookies / messages / `<html lang>`),
|
|
11
|
+
* `path` is only the URL segment, so `{ 'zh-CN': 'zh' }` gives `/zh/about`
|
|
12
|
+
* while every other surface still speaks `zh-CN`.
|
|
13
|
+
*/
|
|
14
|
+
paths?: Record<string, string>;
|
|
15
|
+
}
|
|
16
|
+
interface HonoLocalePath {
|
|
17
|
+
path: string;
|
|
18
|
+
locale: string;
|
|
19
|
+
isDefault: boolean;
|
|
20
|
+
}
|
|
21
|
+
interface CompiledLocalePath {
|
|
22
|
+
/** vue-router path, may contain `:locale(zh)?` */
|
|
23
|
+
vuePath: string;
|
|
24
|
+
/** Concrete Hono mount paths */
|
|
25
|
+
hono: HonoLocalePath[];
|
|
26
|
+
}
|
|
27
|
+
interface I18nDetectOptions {
|
|
28
|
+
cookieName: string;
|
|
29
|
+
redirectOn: 'root' | 'all';
|
|
30
|
+
alwaysRedirect: boolean;
|
|
31
|
+
}
|
|
32
|
+
interface I18nMiddlewareOptions {
|
|
33
|
+
defaultLocale: string;
|
|
34
|
+
locales: string[];
|
|
35
|
+
strategy: I18nRoutingStrategy;
|
|
36
|
+
/** Locale code → URL segment override. See `LocaleRoutingConfig.paths`. */
|
|
37
|
+
paths?: Record<string, string>;
|
|
38
|
+
detectBrowserLanguage?: false | Partial<I18nDetectOptions>;
|
|
39
|
+
loadMessages?: (locale: string, fallback: string) => Promise<void>;
|
|
40
|
+
}
|
|
41
|
+
interface I18nLocaleMeta {
|
|
42
|
+
/** Locale identity, BCP-47 when configured that way (`zh-CN`). */
|
|
43
|
+
code: string;
|
|
44
|
+
/** URL segment when it differs from `code`; `undefined` means "same as code". */
|
|
45
|
+
path?: string;
|
|
46
|
+
language?: string;
|
|
47
|
+
name?: string;
|
|
48
|
+
dir?: 'ltr' | 'rtl';
|
|
49
|
+
isDefault?: boolean;
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/paths.d.ts
|
|
53
|
+
/** URL segment for a locale code (`paths` override, else the code itself). */
|
|
54
|
+
declare function localeSegment(code: string, paths?: Record<string, string>): string;
|
|
55
|
+
/**
|
|
56
|
+
* Reverse of `localeSegment`: the locale code owning a URL segment.
|
|
57
|
+
* Returns `null` for a segment no configured locale claims.
|
|
58
|
+
*/
|
|
59
|
+
declare function localeFromSegment(segment: string, locales: string[], paths?: Record<string, string>): string | null;
|
|
60
|
+
/**
|
|
61
|
+
* URL segments for every configured locale, in config order.
|
|
62
|
+
*
|
|
63
|
+
* This — not the raw codes — is the set the router matches against: with
|
|
64
|
+
* `{ 'zh-CN': 'zh' }` the URL is `/zh/about`, so a param built from `zh-CN`
|
|
65
|
+
* would never match the path the server rewrites to.
|
|
66
|
+
*/
|
|
67
|
+
declare function localeSegments(cfg: LocaleRoutingConfig): string[];
|
|
68
|
+
/**
|
|
69
|
+
* vue-router locale param segment for the given strategy.
|
|
70
|
+
*
|
|
71
|
+
* - `prefix_except_default`: `:locale(zh)?` (default unprefixed; `/en/about` 不匹配)
|
|
72
|
+
* - `prefix`: `:locale(en|zh)` (required)
|
|
73
|
+
* - `prefix_and_default`: `:locale(en|zh)?` (`/about` 与 `/en/about` 都匹配)
|
|
74
|
+
* - `no_prefix`: empty
|
|
75
|
+
*/
|
|
76
|
+
declare function getVueLocaleParam(cfg: LocaleRoutingConfig): string;
|
|
77
|
+
/** Apply a vue-router locale param to a page path (`/` / `/about` / catch-all). */
|
|
78
|
+
declare function toVueRouterLocalePath(pagePath: string, localeParam: string): string;
|
|
79
|
+
/**
|
|
80
|
+
* Split a URL path into its locale code and the rest.
|
|
81
|
+
*
|
|
82
|
+
* The returned `locale` is the **code**, not the segment — with
|
|
83
|
+
* `{ 'zh-CN': 'zh' }` requesting `/zh/about` yields `{ locale: 'zh-CN' }`,
|
|
84
|
+
* so callers keep using one vocabulary everywhere except in the URL itself.
|
|
85
|
+
*/
|
|
86
|
+
declare function extractLocaleFromPath(path: string, localeCodes: string[], paths?: Record<string, string>): {
|
|
87
|
+
locale: string | null;
|
|
88
|
+
pathWithoutLocale: string;
|
|
89
|
+
};
|
|
90
|
+
declare function localizePath(path: string, locale: string, cfg: LocaleRoutingConfig): string;
|
|
91
|
+
declare function switchLocalePath(locale: string, currentPath: string, cfg: LocaleRoutingConfig): string;
|
|
92
|
+
declare function compileLocalePaths(pagePath: string, cfg: LocaleRoutingConfig): CompiledLocalePath;
|
|
93
|
+
//#endregion
|
|
94
|
+
export { localeSegment as a, switchLocalePath as c, HonoLocalePath as d, I18nLocaleMeta as f, LocaleRoutingConfig as h, localeFromSegment as i, toVueRouterLocalePath as l, I18nRoutingStrategy as m, extractLocaleFromPath as n, localeSegments as o, I18nMiddlewareOptions as p, getVueLocaleParam as r, localizePath as s, compileLocalePaths as t, CompiledLocalePath as u };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as extractLocaleFromPath, n as parseLocaleCookie, r as serializeLocaleCookie,
|
|
1
|
+
import { a as extractLocaleFromPath, n as parseLocaleCookie, r as serializeLocaleCookie, t as detectLocaleFromAcceptLanguage, u as localizePath } from "./detect-DgCzeC6u.js";
|
|
2
2
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
3
3
|
import { createCoreContext, datetime, number, translate } from "@intlify/core";
|
|
4
4
|
//#region src/context.ts
|
|
@@ -65,6 +65,7 @@ function setLocaleMeta(code, meta) {
|
|
|
65
65
|
const prev = catalogMeta.get(code);
|
|
66
66
|
catalogMeta.set(code, {
|
|
67
67
|
name: meta.name ?? prev?.name,
|
|
68
|
+
path: meta.path ?? prev?.path,
|
|
68
69
|
dir: meta.dir ?? prev?.dir ?? "ltr",
|
|
69
70
|
language: meta.language ?? prev?.language,
|
|
70
71
|
isDefault: meta.isDefault ?? prev?.isDefault
|
|
@@ -83,6 +84,7 @@ function getRegisteredLocalesMeta() {
|
|
|
83
84
|
const meta = catalogMeta.get(code);
|
|
84
85
|
return {
|
|
85
86
|
code,
|
|
87
|
+
...meta?.path ? { path: meta.path } : {},
|
|
86
88
|
name: meta?.name,
|
|
87
89
|
dir: meta?.dir || "ltr",
|
|
88
90
|
language: meta?.language,
|
|
@@ -217,7 +219,8 @@ function routingFrom(options) {
|
|
|
217
219
|
return {
|
|
218
220
|
defaultLocale: options.defaultLocale,
|
|
219
221
|
locales: options.locales,
|
|
220
|
-
strategy: options.strategy
|
|
222
|
+
strategy: options.strategy,
|
|
223
|
+
paths: options.paths
|
|
221
224
|
};
|
|
222
225
|
}
|
|
223
226
|
function isRootPath(path) {
|
|
@@ -257,9 +260,9 @@ function createI18nMiddleware(options) {
|
|
|
257
260
|
}, () => next());
|
|
258
261
|
return;
|
|
259
262
|
}
|
|
260
|
-
const extracted = extractLocaleFromPath(path, locales);
|
|
263
|
+
const extracted = extractLocaleFromPath(path, locales, options.paths);
|
|
261
264
|
let detectedLocale = defaultLocale;
|
|
262
|
-
if (extracted.locale
|
|
265
|
+
if (extracted.locale) {
|
|
263
266
|
if (strategy === "prefix_except_default" && extracted.locale === defaultLocale) {
|
|
264
267
|
writeCookie(c, defaultLocale);
|
|
265
268
|
return c.redirect(extracted.pathWithoutLocale + url.search, 302);
|
package/dist/routing.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { c as switchLocalePath, m as I18nRoutingStrategy, n as extractLocaleFromPath, p as I18nMiddlewareOptions, s as localizePath, t as compileLocalePaths } from "./paths-CnNW2Mah.js";
|
|
2
2
|
import { UbeanEnv } from "@ubean/shared";
|
|
3
3
|
import { Context, MiddlewareHandler } from "hono";
|
|
4
4
|
//#region src/routing.d.ts
|
package/dist/routing.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { a as extractLocaleFromPath,
|
|
2
|
-
import { n as getPathWithoutLocale, r as getRequestLocale, t as createI18nMiddleware } from "./routing-
|
|
1
|
+
import { a as extractLocaleFromPath, d as switchLocalePath, i as compileLocalePaths, u as localizePath } from "./detect-DgCzeC6u.js";
|
|
2
|
+
import { n as getPathWithoutLocale, r as getRequestLocale, t as createI18nMiddleware } from "./routing-CqXNfVlC.js";
|
|
3
3
|
export { compileLocalePaths, createI18nMiddleware, extractLocaleFromPath, getPathWithoutLocale, getRequestLocale, localizePath, switchLocalePath };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ubean/i18n",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.6",
|
|
4
4
|
"description": "i18n for ubean (vue-i18n / @intlify/core, compact locale routing, ALS)",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -24,8 +24,8 @@
|
|
|
24
24
|
}
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@intlify/core": "11.4.
|
|
28
|
-
"@ubean/shared": "0.6.
|
|
27
|
+
"@intlify/core": "11.4.16",
|
|
28
|
+
"@ubean/shared": "0.6.6",
|
|
29
29
|
"hono": "4.13.13"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
package/dist/paths-CJVBLGJS.d.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
//#region src/types.d.ts
|
|
2
|
-
type I18nRoutingStrategy = 'prefix' | 'prefix_except_default' | 'prefix_and_default' | 'no_prefix';
|
|
3
|
-
interface LocaleRoutingConfig {
|
|
4
|
-
defaultLocale: string;
|
|
5
|
-
locales: string[];
|
|
6
|
-
strategy: I18nRoutingStrategy;
|
|
7
|
-
}
|
|
8
|
-
interface HonoLocalePath {
|
|
9
|
-
path: string;
|
|
10
|
-
locale: string;
|
|
11
|
-
isDefault: boolean;
|
|
12
|
-
}
|
|
13
|
-
interface CompiledLocalePath {
|
|
14
|
-
/** vue-router path, may contain `:locale(zh)?` */
|
|
15
|
-
vuePath: string;
|
|
16
|
-
/** Concrete Hono mount paths */
|
|
17
|
-
hono: HonoLocalePath[];
|
|
18
|
-
}
|
|
19
|
-
interface I18nDetectOptions {
|
|
20
|
-
cookieName: string;
|
|
21
|
-
redirectOn: 'root' | 'all';
|
|
22
|
-
alwaysRedirect: boolean;
|
|
23
|
-
}
|
|
24
|
-
interface I18nMiddlewareOptions {
|
|
25
|
-
defaultLocale: string;
|
|
26
|
-
locales: string[];
|
|
27
|
-
strategy: I18nRoutingStrategy;
|
|
28
|
-
detectBrowserLanguage?: false | Partial<I18nDetectOptions>;
|
|
29
|
-
loadMessages?: (locale: string, fallback: string) => Promise<void>;
|
|
30
|
-
}
|
|
31
|
-
interface I18nLocaleMeta {
|
|
32
|
-
code: string;
|
|
33
|
-
language?: string;
|
|
34
|
-
name?: string;
|
|
35
|
-
dir?: 'ltr' | 'rtl';
|
|
36
|
-
isDefault?: boolean;
|
|
37
|
-
}
|
|
38
|
-
//#endregion
|
|
39
|
-
//#region src/paths.d.ts
|
|
40
|
-
/**
|
|
41
|
-
* vue-router locale param segment for the given strategy.
|
|
42
|
-
*
|
|
43
|
-
* - `prefix_except_default`: `:locale(zh)?` (default unprefixed; `/en/about` 不匹配)
|
|
44
|
-
* - `prefix`: `:locale(en|zh)` (required)
|
|
45
|
-
* - `prefix_and_default`: `:locale(en|zh)?` (`/about` 与 `/en/about` 都匹配)
|
|
46
|
-
* - `no_prefix`: empty
|
|
47
|
-
*/
|
|
48
|
-
declare function getVueLocaleParam(cfg: LocaleRoutingConfig): string;
|
|
49
|
-
/** Apply a vue-router locale param to a page path (`/` / `/about` / catch-all). */
|
|
50
|
-
declare function toVueRouterLocalePath(pagePath: string, localeParam: string): string;
|
|
51
|
-
declare function extractLocaleFromPath(path: string, localeCodes: string[]): {
|
|
52
|
-
locale: string | null;
|
|
53
|
-
pathWithoutLocale: string;
|
|
54
|
-
};
|
|
55
|
-
declare function localizePath(path: string, locale: string, cfg: LocaleRoutingConfig): string;
|
|
56
|
-
declare function switchLocalePath(locale: string, currentPath: string, cfg: LocaleRoutingConfig): string;
|
|
57
|
-
declare function compileLocalePaths(pagePath: string, cfg: LocaleRoutingConfig): CompiledLocalePath;
|
|
58
|
-
//#endregion
|
|
59
|
-
export { switchLocalePath as a, HonoLocalePath as c, I18nRoutingStrategy as d, LocaleRoutingConfig as f, localizePath as i, I18nLocaleMeta as l, extractLocaleFromPath as n, toVueRouterLocalePath as o, getVueLocaleParam as r, CompiledLocalePath as s, compileLocalePaths as t, I18nMiddlewareOptions as u };
|