@0xchain/i18n 1.1.0-beta.72 → 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 CHANGED
@@ -2,5 +2,5 @@ export * from './navigation';
2
2
  export * from './routing';
3
3
  export * from './request';
4
4
  export * from './middleware';
5
- export * from './localeCookie';
5
+ export { resolveIntlHref } from './lib/resolveIntlHref';
6
6
  //# sourceMappingURL=index.d.ts.map
@@ -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;AAC7B,cAAc,gBAAgB,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,19 +1,13 @@
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
- import { createI18nMiddleware, middleware } from "./middleware.js";
5
- import { LOCALE_COOKIE_MAX_AGE, LOCALE_COOKIE_NAME, localeCookieOptions, requestHostname, resolveLocaleCookieDomain } from "./localeCookie.js";
4
+ import { middleware } from "./middleware.js";
6
5
  export {
7
- LOCALE_COOKIE_MAX_AGE,
8
- LOCALE_COOKIE_NAME,
9
6
  Link,
10
- createI18nMiddleware,
11
7
  getPathname,
12
- localeCookieOptions,
13
8
  middleware,
14
9
  redirect,
15
- requestHostname,
16
- resolveLocaleCookieDomain,
10
+ resolveIntlHref,
17
11
  routing,
18
12
  usePathname,
19
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"}
@@ -1,4 +1,2 @@
1
- import { NextRequest } from 'next/server';
2
- export declare function createI18nMiddleware(request: NextRequest): import('next/server').NextResponse<unknown>;
3
- export declare function middleware(request: NextRequest): import('next/server').NextResponse<unknown>;
1
+ export declare function middleware(request: unknown): unknown;
4
2
  //# sourceMappingURL=middleware.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAS/C,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,WAAW,+CAmBxD;AAED,wBAAgB,UAAU,CAAC,OAAO,EAAE,WAAW,+CAE9C"}
1
+ {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAKA,wBAAgB,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAEpD"}
@@ -1,27 +1,9 @@
1
1
  import createMiddleware from "next-intl/middleware";
2
- import { requestHostname, LOCALE_COOKIE_NAME, localeCookieOptions, resolveLocaleCookieDomain } from "./localeCookie.js";
3
2
  import { routing } from "./routing.js";
4
- function createI18nMiddleware(request) {
5
- const hostname = requestHostname(request);
6
- const handle = createMiddleware({
7
- ...routing,
8
- localeCookie: {
9
- name: LOCALE_COOKIE_NAME,
10
- ...localeCookieOptions(hostname)
11
- }
12
- });
13
- const response = handle(request);
14
- const locale = request.nextUrl.pathname.split("/")[1];
15
- if (resolveLocaleCookieDomain(hostname) && routing.locales.includes(locale)) {
16
- response.cookies.set(LOCALE_COOKIE_NAME, "", { path: "/", maxAge: 0 });
17
- response.cookies.set(LOCALE_COOKIE_NAME, locale, localeCookieOptions(hostname));
18
- }
19
- return response;
20
- }
3
+ const i18n = createMiddleware(routing);
21
4
  function middleware(request) {
22
- return createI18nMiddleware(request);
5
+ return i18n(request);
23
6
  }
24
7
  export {
25
- createI18nMiddleware,
26
8
  middleware
27
9
  };
@@ -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 };
@@ -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
- const useRouter = navigation.useRouter;
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
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0xchain/i18n",
3
- "version": "1.1.0-beta.72",
3
+ "version": "1.1.0-beta.73",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -32,11 +32,6 @@
32
32
  "import": "./dist/middleware.js",
33
33
  "default": "./dist/middleware.js"
34
34
  },
35
- "./localeCookie": {
36
- "types": "./dist/localeCookie.d.ts",
37
- "import": "./dist/localeCookie.js",
38
- "default": "./dist/localeCookie.js"
39
- },
40
35
  "./redirectWithLanguage": {
41
36
  "types": "./dist/redirectWithLanguage.d.ts",
42
37
  "import": "./dist/redirectWithLanguage.js",
@@ -1,18 +0,0 @@
1
- export declare const LOCALE_COOKIE_NAME = "NEXT_LOCALE";
2
- export declare const LOCALE_COOKIE_MAX_AGE: number;
3
- export declare function resolveLocaleCookieDomain(hostname: string): string | undefined;
4
- export declare function localeCookieOptions(hostname: string): {
5
- domain?: string | undefined;
6
- path: string;
7
- sameSite: "lax";
8
- maxAge: number;
9
- };
10
- export declare function requestHostname(request: {
11
- headers: {
12
- get(name: string): string | null;
13
- };
14
- nextUrl: {
15
- hostname: string;
16
- };
17
- }): string;
18
- //# sourceMappingURL=localeCookie.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"localeCookie.d.ts","sourceRoot":"","sources":["../src/localeCookie.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,kBAAkB,gBAAgB,CAAC;AAChD,eAAO,MAAM,qBAAqB,QAAqB,CAAC;AAExD,wBAAgB,yBAAyB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAK9E;AAED,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM;;;;;EAQnD;AAED,wBAAgB,eAAe,CAAC,OAAO,EAAE;IACvC,OAAO,EAAE;QAAE,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAA;KAAE,CAAC;IAC9C,OAAO,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC;CAC/B,UAMA"}
@@ -1,27 +0,0 @@
1
- const LOCALE_COOKIE_NAME = "NEXT_LOCALE";
2
- const LOCALE_COOKIE_MAX_AGE = 60 * 60 * 24 * 365;
3
- function resolveLocaleCookieDomain(hostname) {
4
- const host = hostname.split(":")[0];
5
- if (/(^|\.)ichatgo\.ai$/i.test(host)) return ".ichatgo.ai";
6
- if (/(^|\.)chaindigg\.com$/i.test(host)) return ".chaindigg.com";
7
- return void 0;
8
- }
9
- function localeCookieOptions(hostname) {
10
- const domain = resolveLocaleCookieDomain(hostname);
11
- return {
12
- path: "/",
13
- sameSite: "lax",
14
- maxAge: LOCALE_COOKIE_MAX_AGE,
15
- ...domain ? { domain } : {}
16
- };
17
- }
18
- function requestHostname(request) {
19
- return (request.headers.get("x-forwarded-host") || request.headers.get("host") || request.nextUrl.hostname).split(":")[0];
20
- }
21
- export {
22
- LOCALE_COOKIE_MAX_AGE,
23
- LOCALE_COOKIE_NAME,
24
- localeCookieOptions,
25
- requestHostname,
26
- resolveLocaleCookieDomain
27
- };