@0xchain/i18n 1.1.0-beta.71 → 1.1.0-beta.73
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/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/lib/resolveIntlHref.d.ts +11 -0
- package/dist/lib/resolveIntlHref.d.ts.map +1 -0
- package/dist/navigation.d.ts +4 -0
- package/dist/navigation.js +71 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Link, getPathname, redirect, usePathname, useRouter } from "./navigation.js";
|
|
1
|
+
import { Link, getPathname, redirect, resolveIntlHref, usePathname, useRouter } from "./navigation.js";
|
|
2
2
|
import { routing } from "./routing.js";
|
|
3
3
|
import "./request.js";
|
|
4
4
|
import { middleware } from "./middleware.js";
|
|
@@ -7,6 +7,7 @@ export {
|
|
|
7
7
|
getPathname,
|
|
8
8
|
middleware,
|
|
9
9
|
redirect,
|
|
10
|
+
resolveIntlHref,
|
|
10
11
|
routing,
|
|
11
12
|
usePathname,
|
|
12
13
|
useRouter
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type ResolvedIntlHref<THref> = {
|
|
2
|
+
href: THref;
|
|
3
|
+
locale?: string;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* 兼容 next-intl `useRouter` 与 `next/navigation` 两种 href:
|
|
7
|
+
* - `/conversation` → 原样交给 next-intl(补当前语言)
|
|
8
|
+
* - `/ko/conversation` 或 `https://host/en/query` → 拆出语言,用 `{ locale }` 导航,避免 `/ko/ko`
|
|
9
|
+
*/
|
|
10
|
+
export declare function resolveIntlHref<THref>(href: THref, locales: readonly string[]): ResolvedIntlHref<THref>;
|
|
11
|
+
//# sourceMappingURL=resolveIntlHref.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolveIntlHref.d.ts","sourceRoot":"","sources":["../../src/lib/resolveIntlHref.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,gBAAgB,CAAC,KAAK,IAAI;IACpC,IAAI,EAAE,KAAK,CAAC;IACZ,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAmCF;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,KAAK,EACnC,IAAI,EAAE,KAAK,EACX,OAAO,EAAE,SAAS,MAAM,EAAE,GACzB,gBAAgB,CAAC,KAAK,CAAC,CA0BzB"}
|
package/dist/navigation.d.ts
CHANGED
|
@@ -7,3 +7,7 @@ export declare const usePathname: IntlNavigation['usePathname'];
|
|
|
7
7
|
export declare const getPathname: IntlNavigation['getPathname'];
|
|
8
8
|
export declare const redirect: IntlNavigation['redirect'];
|
|
9
9
|
export declare const useRouter: IntlNavigation['useRouter'];
|
|
10
|
+
export declare function resolveIntlHref<THref>(
|
|
11
|
+
href: THref,
|
|
12
|
+
locales: readonly string[],
|
|
13
|
+
): { href: THref; locale?: string };
|
package/dist/navigation.js
CHANGED
|
@@ -1,15 +1,85 @@
|
|
|
1
|
+
import { useMemo } from "react";
|
|
1
2
|
import { createNavigation } from "next-intl/navigation";
|
|
2
3
|
import { routing } from "./routing.js";
|
|
4
|
+
function pathnameAndSearch(href) {
|
|
5
|
+
try {
|
|
6
|
+
if (/^https?:\/\//i.test(href) || href.startsWith("//")) {
|
|
7
|
+
const url = new URL(href, "http://local.invalid");
|
|
8
|
+
return `${url.pathname}${url.search}`;
|
|
9
|
+
}
|
|
10
|
+
} catch {
|
|
11
|
+
}
|
|
12
|
+
return href.startsWith("/") || href.startsWith("?") ? href : `/${href}`;
|
|
13
|
+
}
|
|
14
|
+
function matchLocalePrefix(path, locales) {
|
|
15
|
+
const normalized = path.startsWith("/") ? path : `/${path}`;
|
|
16
|
+
const sorted = [...locales].sort((a, b) => b.length - a.length);
|
|
17
|
+
for (const locale of sorted) {
|
|
18
|
+
const prefix = `/${locale}`;
|
|
19
|
+
if (normalized === prefix || normalized === `${prefix}/`) {
|
|
20
|
+
return { locale, rest: "/" };
|
|
21
|
+
}
|
|
22
|
+
if (normalized.startsWith(`${prefix}/`)) {
|
|
23
|
+
return { locale, rest: normalized.slice(prefix.length) || "/" };
|
|
24
|
+
}
|
|
25
|
+
if (normalized.startsWith(`${prefix}?`)) {
|
|
26
|
+
return { locale, rest: `/${normalized.slice(prefix.length)}` };
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
function resolveIntlHref(href, locales) {
|
|
32
|
+
if (typeof href === "string") {
|
|
33
|
+
const path = pathnameAndSearch(href);
|
|
34
|
+
const matched = matchLocalePrefix(path, locales);
|
|
35
|
+
if (!matched) {
|
|
36
|
+
return { href: path };
|
|
37
|
+
}
|
|
38
|
+
return { href: matched.rest, locale: matched.locale };
|
|
39
|
+
}
|
|
40
|
+
if (href && typeof href === "object" && "pathname" in href) {
|
|
41
|
+
const record = href;
|
|
42
|
+
if (typeof record.pathname !== "string") {
|
|
43
|
+
return { href };
|
|
44
|
+
}
|
|
45
|
+
const matched = matchLocalePrefix(record.pathname, locales);
|
|
46
|
+
if (!matched) {
|
|
47
|
+
return { href };
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
href: { ...record, pathname: matched.rest },
|
|
51
|
+
locale: matched.locale
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
return { href };
|
|
55
|
+
}
|
|
3
56
|
const navigation = createNavigation(routing);
|
|
4
57
|
const Link = navigation.Link;
|
|
5
58
|
const usePathname = navigation.usePathname;
|
|
6
59
|
const getPathname = navigation.getPathname;
|
|
7
60
|
const redirect = navigation.redirect;
|
|
8
|
-
|
|
61
|
+
function withCompatibleHref(navigate, href, options) {
|
|
62
|
+
const resolved = resolveIntlHref(href, routing.locales);
|
|
63
|
+
const locale = options?.locale ?? resolved.locale;
|
|
64
|
+
navigate(resolved.href, locale ? { ...options, locale } : options);
|
|
65
|
+
}
|
|
66
|
+
function useRouter() {
|
|
67
|
+
const router = navigation.useRouter();
|
|
68
|
+
return useMemo(
|
|
69
|
+
() => ({
|
|
70
|
+
...router,
|
|
71
|
+
push: (href, options) => withCompatibleHref(router.push, href, options),
|
|
72
|
+
replace: (href, options) => withCompatibleHref(router.replace, href, options),
|
|
73
|
+
prefetch: (href, options) => withCompatibleHref(router.prefetch, href, options)
|
|
74
|
+
}),
|
|
75
|
+
[router]
|
|
76
|
+
);
|
|
77
|
+
}
|
|
9
78
|
export {
|
|
10
79
|
Link,
|
|
11
80
|
getPathname,
|
|
12
81
|
redirect,
|
|
82
|
+
resolveIntlHref,
|
|
13
83
|
usePathname,
|
|
14
84
|
useRouter
|
|
15
85
|
};
|