gt-next 11.4.12 → 11.4.13
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/CHANGELOG.md +13 -0
- package/dist/config-dir/props/withGTConfigProps.d.ts +2 -1
- package/dist/config-dir/props/withGTConfigProps.d.ts.map +1 -1
- package/dist/config-dir/props/withGTConfigProps.js.map +1 -1
- package/dist/config-dir/props/withGTConfigProps.mjs.map +1 -1
- package/dist/config.d.ts +7 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +11 -1
- package/dist/config.js.map +1 -1
- package/dist/config.mjs +11 -1
- package/dist/config.mjs.map +1 -1
- package/dist/errors/middleware.d.ts +2 -0
- package/dist/errors/middleware.d.ts.map +1 -1
- package/dist/errors/middleware.js +17 -0
- package/dist/errors/middleware.js.map +1 -1
- package/dist/errors/middleware.mjs +15 -1
- package/dist/errors/middleware.mjs.map +1 -1
- package/dist/gt_swc_plugin.wasm +0 -0
- package/dist/index.client.d.ts +1 -0
- package/dist/index.client.d.ts.map +1 -1
- package/dist/index.client.js +2 -0
- package/dist/index.client.js.map +1 -1
- package/dist/index.client.mjs +2 -1
- package/dist/index.client.mjs.map +1 -1
- package/dist/index.rsc.d.ts +1 -0
- package/dist/index.rsc.d.ts.map +1 -1
- package/dist/index.rsc.js +2 -0
- package/dist/index.rsc.js.map +1 -1
- package/dist/index.rsc.mjs +2 -1
- package/dist/index.rsc.mjs.map +1 -1
- package/dist/index.server.d.ts +1 -0
- package/dist/index.server.d.ts.map +1 -1
- package/dist/index.server.js +2 -0
- package/dist/index.server.js.map +1 -1
- package/dist/index.server.mjs +2 -1
- package/dist/index.server.mjs.map +1 -1
- package/dist/index.types.d.ts +11 -0
- package/dist/index.types.d.ts.map +1 -1
- package/dist/link/Link.d.ts +7 -4
- package/dist/link/Link.d.ts.map +1 -1
- package/dist/link/Link.js +19 -41
- package/dist/link/Link.js.map +1 -1
- package/dist/link/Link.mjs +19 -41
- package/dist/link/Link.mjs.map +1 -1
- package/dist/link/getLocalizeHref.d.ts +43 -0
- package/dist/link/getLocalizeHref.d.ts.map +1 -0
- package/dist/link/getLocalizeHref.js +59 -0
- package/dist/link/getLocalizeHref.js.map +1 -0
- package/dist/link/getLocalizeHref.mjs +56 -0
- package/dist/link/getLocalizeHref.mjs.map +1 -0
- package/dist/link/localize-utils.d.ts +15 -0
- package/dist/link/localize-utils.d.ts.map +1 -0
- package/dist/link/localize-utils.js +49 -0
- package/dist/link/localize-utils.js.map +1 -0
- package/dist/link/localize-utils.mjs +48 -0
- package/dist/link/localize-utils.mjs.map +1 -0
- package/dist/link/useLocalizeHref.d.ts +34 -0
- package/dist/link/useLocalizeHref.d.ts.map +1 -0
- package/dist/link/useLocalizeHref.js +46 -0
- package/dist/link/useLocalizeHref.js.map +1 -0
- package/dist/link/useLocalizeHref.mjs +44 -0
- package/dist/link/useLocalizeHref.mjs.map +1 -0
- package/dist/link.d.ts +2 -0
- package/dist/link.d.ts.map +1 -1
- package/dist/link.js +2 -0
- package/dist/link.mjs +2 -1
- package/dist/middleware-dir/createMiddlewareRouting.d.ts +7 -0
- package/dist/middleware-dir/createMiddlewareRouting.d.ts.map +1 -0
- package/dist/middleware-dir/createMiddlewareRouting.js +79 -0
- package/dist/middleware-dir/createMiddlewareRouting.js.map +1 -0
- package/dist/middleware-dir/createMiddlewareRouting.mjs +77 -0
- package/dist/middleware-dir/createMiddlewareRouting.mjs.map +1 -0
- package/dist/middleware-dir/createNextMiddleware.d.ts +5 -12
- package/dist/middleware-dir/createNextMiddleware.d.ts.map +1 -1
- package/dist/middleware-dir/createNextMiddleware.js +14 -55
- package/dist/middleware-dir/createNextMiddleware.js.map +1 -1
- package/dist/middleware-dir/createNextMiddleware.mjs +15 -56
- package/dist/middleware-dir/createNextMiddleware.mjs.map +1 -1
- package/dist/middleware-dir/defaultMiddlewareConfig.d.ts +11 -0
- package/dist/middleware-dir/defaultMiddlewareConfig.d.ts.map +1 -0
- package/dist/middleware-dir/defaultMiddlewareConfig.js +33 -0
- package/dist/middleware-dir/defaultMiddlewareConfig.js.map +1 -0
- package/dist/middleware-dir/defaultMiddlewareConfig.mjs +31 -0
- package/dist/middleware-dir/defaultMiddlewareConfig.mjs.map +1 -0
- package/dist/middleware-dir/resolveMiddlewareConfig.d.ts +9 -0
- package/dist/middleware-dir/resolveMiddlewareConfig.d.ts.map +1 -0
- package/dist/middleware-dir/resolveMiddlewareConfig.js +32 -0
- package/dist/middleware-dir/resolveMiddlewareConfig.js.map +1 -0
- package/dist/middleware-dir/resolveMiddlewareConfig.mjs +30 -0
- package/dist/middleware-dir/resolveMiddlewareConfig.mjs.map +1 -0
- package/dist/middleware-dir/types.d.ts +56 -0
- package/dist/middleware-dir/types.d.ts.map +1 -0
- package/dist/middleware-dir/types.js +1 -0
- package/dist/middleware-dir/types.mjs +1 -0
- package/dist/server.d.ts +1 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +2 -0
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +2 -1
- package/dist/server.mjs.map +1 -1
- package/dist/utils/client-boundary.js +1 -1
- package/dist/utils/client-boundary.mjs +1 -1
- package/package.json +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.server.mjs","names":[],"sources":["../src/index.server.ts"],"sourcesContent":["'use client';\n\nimport { initializeGT } from './setup/initGT';\n/**\n * No initializeGTServer() here because we do not want to\n * initialize the AsyncConditionStore on the server side.\n *\n * No initializeGTClient() here because we do want to enforce\n * expiry times here.\n */\ninitializeGT();\n\n// ===== Pages Router ===== //\nexport { parseLocale } from './pages-dir/parseLocale';\nexport { withGTServerSideProps } from './pages-dir/withGTServerSideProps';\nexport type { WithGTServerSideProps } from './pages-dir/withGTServerSideProps';\nexport { withGTStaticProps } from './pages-dir/withGTStaticProps';\nexport type { WithGTStaticProps } from './pages-dir/withGTStaticProps';\n\n// ===== Components ===== //\nexport {\n Branch,\n Currency,\n DateTime,\n Derive,\n GTProvider,\n GtInternalTranslateJsx,\n GtInternalVar,\n T,\n LocaleSelector,\n Num,\n Plural,\n RegionSelector,\n RelativeTime,\n Var,\n} from 'gt-react';\n\n// ===== Hooks ===== //\nexport {\n useDefaultLocale,\n useGT,\n useLocaleDirection,\n useLocales,\n useLocaleProperties,\n useLocaleSelector,\n useLocale,\n useMessages,\n useRegion,\n useSetLocale,\n useTranslations,\n} from 'gt-react';\n\n// ===== Functions ===== //\nexport {\n decodeMsg,\n decodeOptions,\n decodeVars,\n declareVar,\n derive,\n getDefaultLocale,\n getLocaleProperties,\n getLocales,\n resolveCanonicalLocale,\n getTranslationsSnapshot,\n getVersionId,\n gtFallback,\n mFallback,\n msg,\n} from 'gt-react';\nexport { isLocaleSupported } from './request/localeValidation';\n\n// ===== Types ===== //\nexport type { GTTranslationOptions, RuntimeTranslationOptions } from 'gt-react';\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.server.mjs","names":[],"sources":["../src/index.server.ts"],"sourcesContent":["'use client';\n\nimport { initializeGT } from './setup/initGT';\n/**\n * No initializeGTServer() here because we do not want to\n * initialize the AsyncConditionStore on the server side.\n *\n * No initializeGTClient() here because we do want to enforce\n * expiry times here.\n */\ninitializeGT();\n\n// ===== Pages Router ===== //\nexport { parseLocale } from './pages-dir/parseLocale';\nexport { withGTServerSideProps } from './pages-dir/withGTServerSideProps';\nexport type { WithGTServerSideProps } from './pages-dir/withGTServerSideProps';\nexport { withGTStaticProps } from './pages-dir/withGTStaticProps';\nexport type { WithGTStaticProps } from './pages-dir/withGTStaticProps';\n\n// ===== Components ===== //\nexport {\n Branch,\n Currency,\n DateTime,\n Derive,\n GTProvider,\n GtInternalTranslateJsx,\n GtInternalVar,\n T,\n LocaleSelector,\n Num,\n Plural,\n RegionSelector,\n RelativeTime,\n Var,\n} from 'gt-react';\n\n// ===== Hooks ===== //\nexport {\n useDefaultLocale,\n useGT,\n useLocaleDirection,\n useLocales,\n useLocaleProperties,\n useLocaleSelector,\n useLocale,\n useMessages,\n useRegion,\n useSetLocale,\n useTranslations,\n} from 'gt-react';\n\n// ===== Functions ===== //\nexport {\n decodeMsg,\n decodeOptions,\n decodeVars,\n declareVar,\n derive,\n getDefaultLocale,\n getLocaleProperties,\n getLocales,\n resolveCanonicalLocale,\n getTranslationsSnapshot,\n getVersionId,\n gtFallback,\n mFallback,\n msg,\n} from 'gt-react';\nexport { useLocalizeHref } from './link/useLocalizeHref';\nexport { isLocaleSupported } from './request/localeValidation';\n\n// ===== Types ===== //\nexport type { GTTranslationOptions, RuntimeTranslationOptions } from 'gt-react';\n"],"mappings":";;;;;;;;;;;;;;;;AAUA,cAAc"}
|
package/dist/index.types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { T as _T } from './server-dir/buildtime/T';
|
|
2
|
+
import { useLocalizeHref as _useLocalizeHref } from './link/useLocalizeHref';
|
|
2
3
|
import type { GetServerSideProps, GetServerSidePropsContext, PreviewData } from 'next';
|
|
3
4
|
import type { ParsedUrlQuery } from 'querystring';
|
|
4
5
|
import type { WithGTServerSideProps } from './pages-dir/withGTServerSideProps';
|
|
@@ -316,6 +317,16 @@ export declare const useTranslations: typeof _useTranslations;
|
|
|
316
317
|
* console.log(locale); // 'en-US'
|
|
317
318
|
*/
|
|
318
319
|
export declare const useLocale: typeof _useLocale;
|
|
320
|
+
/**
|
|
321
|
+
* Returns a function that localizes hrefs the same way `gt-next/link` does,
|
|
322
|
+
* for navigation that does not render a `<Link>`, such as `router.push()` or
|
|
323
|
+
* a Button `href`. Works in Server and Client Components.
|
|
324
|
+
*
|
|
325
|
+
* @example
|
|
326
|
+
* const localizeHref = useLocalizeHref();
|
|
327
|
+
* localizeHref('/checkout'); // '/fr/checkout', or '/checkout' for the default locale
|
|
328
|
+
*/
|
|
329
|
+
export declare const useLocalizeHref: typeof _useLocalizeHref;
|
|
319
330
|
export declare const useSetLocale: typeof _useSetLocale;
|
|
320
331
|
export declare const useLocaleSelector: typeof _useLocaleSelector;
|
|
321
332
|
/**
|
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UAAU,EACV,UAAU,EACV,uBAAuB,EACvB,gBAAgB,EAChB,mBAAmB,EACnB,UAAU,EACV,sBAAsB,EACtB,YAAY,GACb,CAAC"}
|
package/dist/link/Link.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import NextLink from 'next/link';
|
|
2
2
|
import { type ComponentProps } from 'react';
|
|
3
|
-
type
|
|
4
|
-
export type LinkProps = NextLinkProps;
|
|
3
|
+
export type LinkProps = ComponentProps<typeof NextLink>;
|
|
5
4
|
/**
|
|
6
5
|
* Locale-aware wrapper around Next.js's `<Link>` component.
|
|
7
6
|
*
|
|
@@ -9,12 +8,17 @@ export type LinkProps = NextLinkProps;
|
|
|
9
8
|
* locale string to use a specific locale, or pass `locale={false}` to leave the
|
|
10
9
|
* href unchanged. External URLs are never prefixed.
|
|
11
10
|
*
|
|
11
|
+
* With `prefixDefaultLocale: false` (the default), current default-locale links
|
|
12
|
+
* are left unprefixed. Explicit default-locale links are left unprefixed only
|
|
13
|
+
* when `enableSmartRouting` is `false`. Set these options in `withGTConfig()`
|
|
14
|
+
* so Link can read them. Use `useLocalizeHref()` to localize hrefs outside Link.
|
|
15
|
+
*
|
|
12
16
|
* @example
|
|
13
17
|
* ```tsx
|
|
14
18
|
* import Link from 'gt-next/link';
|
|
15
19
|
*
|
|
16
20
|
* export function Navigation() {
|
|
17
|
-
* return <Link href="/home">Home</Link>; // /
|
|
21
|
+
* return <Link href="/home">Home</Link>; // /fr/home, or /home for the default locale
|
|
18
22
|
* }
|
|
19
23
|
* ```
|
|
20
24
|
*
|
|
@@ -51,5 +55,4 @@ export type LinkProps = NextLinkProps;
|
|
|
51
55
|
* @returns The rendered Next.js link.
|
|
52
56
|
*/
|
|
53
57
|
export declare const Link: import("react").ForwardRefExoticComponent<any>;
|
|
54
|
-
export {};
|
|
55
58
|
//# sourceMappingURL=Link.d.ts.map
|
package/dist/link/Link.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Link.d.ts","sourceRoot":"","sources":["../../src/link/Link.tsx"],"names":[],"mappings":"AAEA,OAAO,QAAQ,MAAM,WAAW,CAAC;AACjC,OAAO,
|
|
1
|
+
{"version":3,"file":"Link.d.ts","sourceRoot":"","sources":["../../src/link/Link.tsx"],"names":[],"mappings":"AAEA,OAAO,QAAQ,MAAM,WAAW,CAAC;AACjC,OAAO,EAAc,KAAK,cAAc,EAAqB,MAAM,OAAO,CAAC;AAK3E,MAAM,MAAM,SAAS,GAAG,cAAc,CAAC,OAAO,QAAQ,CAAC,CAAC;AAExD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AACH,eAAO,MAAM,IAAI,gDAef,CAAC"}
|
package/dist/link/Link.js
CHANGED
|
@@ -1,20 +1,12 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const require_runtime = require("../_virtual/_rolldown/runtime.js");
|
|
4
|
-
|
|
4
|
+
const require_link_useLocalizeHref = require("./useLocalizeHref.js");
|
|
5
5
|
let react = require("react");
|
|
6
6
|
let react_jsx_runtime = require("react/jsx-runtime");
|
|
7
7
|
let next_link_js = require("next/link.js");
|
|
8
8
|
next_link_js = require_runtime.__toESM(next_link_js);
|
|
9
9
|
//#region src/link/Link.tsx
|
|
10
|
-
const LinkWithLocale = (0, react.forwardRef)(function LinkWithLocale({ href, ...props }, ref) {
|
|
11
|
-
const locale = (0, gt_react.useLocale)();
|
|
12
|
-
return renderLink({
|
|
13
|
-
...props,
|
|
14
|
-
href,
|
|
15
|
-
locale
|
|
16
|
-
}, ref);
|
|
17
|
-
});
|
|
18
10
|
/**
|
|
19
11
|
* Locale-aware wrapper around Next.js's `<Link>` component.
|
|
20
12
|
*
|
|
@@ -22,12 +14,17 @@ const LinkWithLocale = (0, react.forwardRef)(function LinkWithLocale({ href, ...
|
|
|
22
14
|
* locale string to use a specific locale, or pass `locale={false}` to leave the
|
|
23
15
|
* href unchanged. External URLs are never prefixed.
|
|
24
16
|
*
|
|
17
|
+
* With `prefixDefaultLocale: false` (the default), current default-locale links
|
|
18
|
+
* are left unprefixed. Explicit default-locale links are left unprefixed only
|
|
19
|
+
* when `enableSmartRouting` is `false`. Set these options in `withGTConfig()`
|
|
20
|
+
* so Link can read them. Use `useLocalizeHref()` to localize hrefs outside Link.
|
|
21
|
+
*
|
|
25
22
|
* @example
|
|
26
23
|
* ```tsx
|
|
27
24
|
* import Link from 'gt-next/link';
|
|
28
25
|
*
|
|
29
26
|
* export function Navigation() {
|
|
30
|
-
* return <Link href="/home">Home</Link>; // /
|
|
27
|
+
* return <Link href="/home">Home</Link>; // /fr/home, or /home for the default locale
|
|
31
28
|
* }
|
|
32
29
|
* ```
|
|
33
30
|
*
|
|
@@ -64,41 +61,22 @@ const LinkWithLocale = (0, react.forwardRef)(function LinkWithLocale({ href, ...
|
|
|
64
61
|
* @returns The rendered Next.js link.
|
|
65
62
|
*/
|
|
66
63
|
const Link = (0, react.forwardRef)(function Link({ href, locale, ...props }, ref) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
function renderLink({ href, locale, ...props }, ref) {
|
|
64
|
+
const localizeHref = require_link_useLocalizeHref.useLocalizeHref();
|
|
65
|
+
let localizedHref = href;
|
|
66
|
+
if (typeof href === "string") localizedHref = localizeHref(href, locale);
|
|
67
|
+
else if (typeof href.pathname === "string") {
|
|
68
|
+
const pathname = localizeHref(href.pathname, locale);
|
|
69
|
+
if (pathname !== href.pathname) localizedHref = {
|
|
70
|
+
...href,
|
|
71
|
+
pathname
|
|
72
|
+
};
|
|
73
|
+
}
|
|
79
74
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(next_link_js.default, {
|
|
80
75
|
...props,
|
|
81
|
-
href:
|
|
76
|
+
href: localizedHref,
|
|
82
77
|
ref
|
|
83
78
|
});
|
|
84
|
-
}
|
|
85
|
-
function localizeHref(href, locale) {
|
|
86
|
-
if (!locale) return href;
|
|
87
|
-
if (typeof href === "string") return localizePath(href, locale);
|
|
88
|
-
if (typeof href.pathname !== "string") return href;
|
|
89
|
-
const localizedPathname = localizePath(href.pathname, locale);
|
|
90
|
-
if (localizedPathname === href.pathname) return href;
|
|
91
|
-
return {
|
|
92
|
-
...href,
|
|
93
|
-
pathname: localizedPathname
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
function localizePath(href, locale) {
|
|
97
|
-
if (!href.startsWith("/") || href.startsWith("//")) return href;
|
|
98
|
-
const url = new URL(href, "http://localhost");
|
|
99
|
-
url.pathname = url.pathname === "/" ? `/${encodeURIComponent(locale)}` : `/${encodeURIComponent(locale)}${url.pathname}`;
|
|
100
|
-
return `${url.pathname}${url.search}${url.hash}`;
|
|
101
|
-
}
|
|
79
|
+
});
|
|
102
80
|
//#endregion
|
|
103
81
|
exports.Link = Link;
|
|
104
82
|
|
package/dist/link/Link.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Link.js","names":["NextLink"],"sources":["../../src/link/Link.tsx"],"sourcesContent":["'use client';\n\nimport NextLink from 'next/link';\nimport {
|
|
1
|
+
{"version":3,"file":"Link.js","names":["useLocalizeHref","NextLink"],"sources":["../../src/link/Link.tsx"],"sourcesContent":["'use client';\n\nimport NextLink from 'next/link';\nimport { forwardRef, type ComponentProps, type ComponentRef } from 'react';\nimport { useLocalizeHref } from './useLocalizeHref';\n\ntype LinkRef = ComponentRef<typeof NextLink>;\n\nexport type LinkProps = ComponentProps<typeof NextLink>;\n\n/**\n * Locale-aware wrapper around Next.js's `<Link>` component.\n *\n * Internal hrefs are prefixed with the current GT locale by default. Pass a\n * locale string to use a specific locale, or pass `locale={false}` to leave the\n * href unchanged. External URLs are never prefixed.\n *\n * With `prefixDefaultLocale: false` (the default), current default-locale links\n * are left unprefixed. Explicit default-locale links are left unprefixed only\n * when `enableSmartRouting` is `false`. Set these options in `withGTConfig()`\n * so Link can read them. Use `useLocalizeHref()` to localize hrefs outside Link.\n *\n * @example\n * ```tsx\n * import Link from 'gt-next/link';\n *\n * export function Navigation() {\n * return <Link href=\"/home\">Home</Link>; // /fr/home, or /home for the default locale\n * }\n * ```\n *\n * @example\n * ```tsx\n * import Link from 'gt-next/link';\n *\n * export function LocaleSwitcher() {\n * return <Link href=\"/home\" locale=\"fr\">French</Link>; // /fr/home\n * }\n * ```\n *\n * @example\n * ```tsx\n * import Link from 'gt-next/link';\n *\n * export function UnlocalizedLink() {\n * return <Link href=\"/legal\" locale={false}>Legal</Link>; // /legal\n * }\n * ```\n *\n * @example\n * ```tsx\n * import Link from 'gt-next/link';\n *\n * export function ExternalLink() {\n * return <Link href=\"https://example.com\">Example</Link>; // https://example.com\n * }\n * ```\n *\n * @see https://nextjs.org/docs/app/api-reference/components/link\n * @param href - The destination URL or URL object. Internal paths are localized.\n * @param locale - Locale to prefix, or `false` to disable GT locale prefixing.\n * @returns The rendered Next.js link.\n */\nexport const Link = forwardRef<LinkRef, LinkProps>(function Link(\n { href, locale, ...props },\n ref\n) {\n const localizeHref = useLocalizeHref();\n\n let localizedHref = href;\n if (typeof href === 'string') {\n localizedHref = localizeHref(href, locale);\n } else if (typeof href.pathname === 'string') {\n const pathname = localizeHref(href.pathname, locale);\n if (pathname !== href.pathname) localizedHref = { ...href, pathname };\n }\n\n return <NextLink {...props} href={localizedHref} ref={ref} />;\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA,MAAa,QAAA,GAAA,MAAA,YAAsC,SAAS,KAC1D,EAAE,MAAM,QAAQ,GAAG,SACnB,KACA;CACA,MAAM,eAAeA,6BAAAA,iBAAiB;CAEtC,IAAI,gBAAgB;AACpB,KAAI,OAAO,SAAS,SAClB,iBAAgB,aAAa,MAAM,OAAO;UACjC,OAAO,KAAK,aAAa,UAAU;EAC5C,MAAM,WAAW,aAAa,KAAK,UAAU,OAAO;AACpD,MAAI,aAAa,KAAK,SAAU,iBAAgB;GAAE,GAAG;GAAM;GAAU;;AAGvE,QAAO,iBAAA,GAAA,kBAAA,KAACC,aAAAA,SAAD;EAAU,GAAI;EAAO,MAAM;EAAoB;EAAO,CAAA;EAC7D"}
|
package/dist/link/Link.mjs
CHANGED
|
@@ -1,17 +1,9 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import {
|
|
2
|
+
import { useLocalizeHref } from "./useLocalizeHref.mjs";
|
|
3
3
|
import { forwardRef } from "react";
|
|
4
4
|
import { jsx } from "react/jsx-runtime";
|
|
5
5
|
import NextLink from "next/link.js";
|
|
6
6
|
//#region src/link/Link.tsx
|
|
7
|
-
const LinkWithLocale = forwardRef(function LinkWithLocale({ href, ...props }, ref) {
|
|
8
|
-
const locale = useLocale();
|
|
9
|
-
return renderLink({
|
|
10
|
-
...props,
|
|
11
|
-
href,
|
|
12
|
-
locale
|
|
13
|
-
}, ref);
|
|
14
|
-
});
|
|
15
7
|
/**
|
|
16
8
|
* Locale-aware wrapper around Next.js's `<Link>` component.
|
|
17
9
|
*
|
|
@@ -19,12 +11,17 @@ const LinkWithLocale = forwardRef(function LinkWithLocale({ href, ...props }, re
|
|
|
19
11
|
* locale string to use a specific locale, or pass `locale={false}` to leave the
|
|
20
12
|
* href unchanged. External URLs are never prefixed.
|
|
21
13
|
*
|
|
14
|
+
* With `prefixDefaultLocale: false` (the default), current default-locale links
|
|
15
|
+
* are left unprefixed. Explicit default-locale links are left unprefixed only
|
|
16
|
+
* when `enableSmartRouting` is `false`. Set these options in `withGTConfig()`
|
|
17
|
+
* so Link can read them. Use `useLocalizeHref()` to localize hrefs outside Link.
|
|
18
|
+
*
|
|
22
19
|
* @example
|
|
23
20
|
* ```tsx
|
|
24
21
|
* import Link from 'gt-next/link';
|
|
25
22
|
*
|
|
26
23
|
* export function Navigation() {
|
|
27
|
-
* return <Link href="/home">Home</Link>; // /
|
|
24
|
+
* return <Link href="/home">Home</Link>; // /fr/home, or /home for the default locale
|
|
28
25
|
* }
|
|
29
26
|
* ```
|
|
30
27
|
*
|
|
@@ -61,41 +58,22 @@ const LinkWithLocale = forwardRef(function LinkWithLocale({ href, ...props }, re
|
|
|
61
58
|
* @returns The rendered Next.js link.
|
|
62
59
|
*/
|
|
63
60
|
const Link = forwardRef(function Link({ href, locale, ...props }, ref) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
function renderLink({ href, locale, ...props }, ref) {
|
|
61
|
+
const localizeHref = useLocalizeHref();
|
|
62
|
+
let localizedHref = href;
|
|
63
|
+
if (typeof href === "string") localizedHref = localizeHref(href, locale);
|
|
64
|
+
else if (typeof href.pathname === "string") {
|
|
65
|
+
const pathname = localizeHref(href.pathname, locale);
|
|
66
|
+
if (pathname !== href.pathname) localizedHref = {
|
|
67
|
+
...href,
|
|
68
|
+
pathname
|
|
69
|
+
};
|
|
70
|
+
}
|
|
76
71
|
return /* @__PURE__ */ jsx(NextLink, {
|
|
77
72
|
...props,
|
|
78
|
-
href:
|
|
73
|
+
href: localizedHref,
|
|
79
74
|
ref
|
|
80
75
|
});
|
|
81
|
-
}
|
|
82
|
-
function localizeHref(href, locale) {
|
|
83
|
-
if (!locale) return href;
|
|
84
|
-
if (typeof href === "string") return localizePath(href, locale);
|
|
85
|
-
if (typeof href.pathname !== "string") return href;
|
|
86
|
-
const localizedPathname = localizePath(href.pathname, locale);
|
|
87
|
-
if (localizedPathname === href.pathname) return href;
|
|
88
|
-
return {
|
|
89
|
-
...href,
|
|
90
|
-
pathname: localizedPathname
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
function localizePath(href, locale) {
|
|
94
|
-
if (!href.startsWith("/") || href.startsWith("//")) return href;
|
|
95
|
-
const url = new URL(href, "http://localhost");
|
|
96
|
-
url.pathname = url.pathname === "/" ? `/${encodeURIComponent(locale)}` : `/${encodeURIComponent(locale)}${url.pathname}`;
|
|
97
|
-
return `${url.pathname}${url.search}${url.hash}`;
|
|
98
|
-
}
|
|
76
|
+
});
|
|
99
77
|
//#endregion
|
|
100
78
|
export { Link };
|
|
101
79
|
|
package/dist/link/Link.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Link.mjs","names":[],"sources":["../../src/link/Link.tsx"],"sourcesContent":["'use client';\n\nimport NextLink from 'next/link';\nimport {
|
|
1
|
+
{"version":3,"file":"Link.mjs","names":[],"sources":["../../src/link/Link.tsx"],"sourcesContent":["'use client';\n\nimport NextLink from 'next/link';\nimport { forwardRef, type ComponentProps, type ComponentRef } from 'react';\nimport { useLocalizeHref } from './useLocalizeHref';\n\ntype LinkRef = ComponentRef<typeof NextLink>;\n\nexport type LinkProps = ComponentProps<typeof NextLink>;\n\n/**\n * Locale-aware wrapper around Next.js's `<Link>` component.\n *\n * Internal hrefs are prefixed with the current GT locale by default. Pass a\n * locale string to use a specific locale, or pass `locale={false}` to leave the\n * href unchanged. External URLs are never prefixed.\n *\n * With `prefixDefaultLocale: false` (the default), current default-locale links\n * are left unprefixed. Explicit default-locale links are left unprefixed only\n * when `enableSmartRouting` is `false`. Set these options in `withGTConfig()`\n * so Link can read them. Use `useLocalizeHref()` to localize hrefs outside Link.\n *\n * @example\n * ```tsx\n * import Link from 'gt-next/link';\n *\n * export function Navigation() {\n * return <Link href=\"/home\">Home</Link>; // /fr/home, or /home for the default locale\n * }\n * ```\n *\n * @example\n * ```tsx\n * import Link from 'gt-next/link';\n *\n * export function LocaleSwitcher() {\n * return <Link href=\"/home\" locale=\"fr\">French</Link>; // /fr/home\n * }\n * ```\n *\n * @example\n * ```tsx\n * import Link from 'gt-next/link';\n *\n * export function UnlocalizedLink() {\n * return <Link href=\"/legal\" locale={false}>Legal</Link>; // /legal\n * }\n * ```\n *\n * @example\n * ```tsx\n * import Link from 'gt-next/link';\n *\n * export function ExternalLink() {\n * return <Link href=\"https://example.com\">Example</Link>; // https://example.com\n * }\n * ```\n *\n * @see https://nextjs.org/docs/app/api-reference/components/link\n * @param href - The destination URL or URL object. Internal paths are localized.\n * @param locale - Locale to prefix, or `false` to disable GT locale prefixing.\n * @returns The rendered Next.js link.\n */\nexport const Link = forwardRef<LinkRef, LinkProps>(function Link(\n { href, locale, ...props },\n ref\n) {\n const localizeHref = useLocalizeHref();\n\n let localizedHref = href;\n if (typeof href === 'string') {\n localizedHref = localizeHref(href, locale);\n } else if (typeof href.pathname === 'string') {\n const pathname = localizeHref(href.pathname, locale);\n if (pathname !== href.pathname) localizedHref = { ...href, pathname };\n }\n\n return <NextLink {...props} href={localizedHref} ref={ref} />;\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA,MAAa,OAAO,WAA+B,SAAS,KAC1D,EAAE,MAAM,QAAQ,GAAG,SACnB,KACA;CACA,MAAM,eAAe,iBAAiB;CAEtC,IAAI,gBAAgB;AACpB,KAAI,OAAO,SAAS,SAClB,iBAAgB,aAAa,MAAM,OAAO;UACjC,OAAO,KAAK,aAAa,UAAU;EAC5C,MAAM,WAAW,aAAa,KAAK,UAAU,OAAO;AACpD,MAAI,aAAa,KAAK,SAAU,iBAAgB;GAAE,GAAG;GAAM;GAAU;;AAGvE,QAAO,oBAAC,UAAD;EAAU,GAAI;EAAO,MAAM;EAAoB;EAAO,CAAA;EAC7D"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { type LinkLocale } from './localize-utils';
|
|
2
|
+
/**
|
|
3
|
+
* Returns a function that localizes hrefs the same way `gt-next/link` does,
|
|
4
|
+
* for server code such as Server Components, route handlers, and Server
|
|
5
|
+
* Actions.
|
|
6
|
+
*
|
|
7
|
+
* Internal paths are prefixed with the target locale, except default-locale
|
|
8
|
+
* paths when `prefixDefaultLocale` is `false` (an explicit default `locale`
|
|
9
|
+
* keeps its prefix while `enableSmartRouting` is on). Localized path aliases
|
|
10
|
+
* and locale availability are still resolved by the middleware. External URLs
|
|
11
|
+
* are never prefixed.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* import { getLocalizeHref } from 'gt-next/server';
|
|
16
|
+
*
|
|
17
|
+
* export async function CheckoutButton() {
|
|
18
|
+
* const localizeHref = await getLocalizeHref();
|
|
19
|
+
* return <Button href={localizeHref('/checkout')}>Checkout</Button>;
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @returns A promise of a function taking an href and an optional locale (a
|
|
24
|
+
* locale string, or `false` to leave the href unchanged) that returns the
|
|
25
|
+
* localized href.
|
|
26
|
+
*/
|
|
27
|
+
export declare function getLocalizeHref(): Promise<(href: string, locale?: LinkLocale) => string>;
|
|
28
|
+
/**
|
|
29
|
+
* Server Component version of `useLocalizeHref()`: localizes hrefs the same
|
|
30
|
+
* way `gt-next/link` does, using the request locale.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```tsx
|
|
34
|
+
* import { useLocalizeHref } from 'gt-next';
|
|
35
|
+
*
|
|
36
|
+
* export function CheckoutButton() {
|
|
37
|
+
* const localizeHref = useLocalizeHref();
|
|
38
|
+
* return <Button href={localizeHref('/checkout')}>Checkout</Button>;
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare function useLocalizeHref(): (href: string, locale?: LinkLocale) => string;
|
|
43
|
+
//# sourceMappingURL=getLocalizeHref.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getLocalizeHref.d.ts","sourceRoot":"","sources":["../../src/link/getLocalizeHref.ts"],"names":[],"mappings":"AAGA,OAAO,EAAgB,KAAK,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEjE;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAsB,eAAe,IAAI,OAAO,CAC9C,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,UAAU,KAAK,MAAM,CAC9C,CAKA;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,eAAe,WAtBtB,MAAM,WAAW,UAAU,KAAK,MAAM,CAwB9C"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
require("../_virtual/_rolldown/runtime.js");
|
|
3
|
+
const require_link_localize_utils = require("./localize-utils.js");
|
|
4
|
+
const require_utils_use = require("../utils/use.js");
|
|
5
|
+
const require_request_getLocale = require("../request/getLocale.js");
|
|
6
|
+
let gt_i18n_internal = require("gt-i18n/internal");
|
|
7
|
+
//#region src/link/getLocalizeHref.ts
|
|
8
|
+
/**
|
|
9
|
+
* Returns a function that localizes hrefs the same way `gt-next/link` does,
|
|
10
|
+
* for server code such as Server Components, route handlers, and Server
|
|
11
|
+
* Actions.
|
|
12
|
+
*
|
|
13
|
+
* Internal paths are prefixed with the target locale, except default-locale
|
|
14
|
+
* paths when `prefixDefaultLocale` is `false` (an explicit default `locale`
|
|
15
|
+
* keeps its prefix while `enableSmartRouting` is on). Localized path aliases
|
|
16
|
+
* and locale availability are still resolved by the middleware. External URLs
|
|
17
|
+
* are never prefixed.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* import { getLocalizeHref } from 'gt-next/server';
|
|
22
|
+
*
|
|
23
|
+
* export async function CheckoutButton() {
|
|
24
|
+
* const localizeHref = await getLocalizeHref();
|
|
25
|
+
* return <Button href={localizeHref('/checkout')}>Checkout</Button>;
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @returns A promise of a function taking an href and an optional locale (a
|
|
30
|
+
* locale string, or `false` to leave the href unchanged) that returns the
|
|
31
|
+
* localized href.
|
|
32
|
+
*/
|
|
33
|
+
async function getLocalizeHref() {
|
|
34
|
+
const currentLocale = await require_request_getLocale.getLocale();
|
|
35
|
+
const defaultLocale = (0, gt_i18n_internal.getI18nConfig)().getDefaultLocale();
|
|
36
|
+
return (href, locale) => require_link_localize_utils.localizeHref(href, locale, currentLocale, defaultLocale);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Server Component version of `useLocalizeHref()`: localizes hrefs the same
|
|
40
|
+
* way `gt-next/link` does, using the request locale.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```tsx
|
|
44
|
+
* import { useLocalizeHref } from 'gt-next';
|
|
45
|
+
*
|
|
46
|
+
* export function CheckoutButton() {
|
|
47
|
+
* const localizeHref = useLocalizeHref();
|
|
48
|
+
* return <Button href={localizeHref('/checkout')}>Checkout</Button>;
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
function useLocalizeHref() {
|
|
53
|
+
return require_utils_use.use(getLocalizeHref());
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
exports.getLocalizeHref = getLocalizeHref;
|
|
57
|
+
exports.useLocalizeHref = useLocalizeHref;
|
|
58
|
+
|
|
59
|
+
//# sourceMappingURL=getLocalizeHref.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getLocalizeHref.js","names":["getLocale","localizeHref","use"],"sources":["../../src/link/getLocalizeHref.ts"],"sourcesContent":["import { getI18nConfig } from 'gt-i18n/internal';\nimport { getLocale } from '../request/getLocale';\nimport { use } from '../utils/use';\nimport { localizeHref, type LinkLocale } from './localize-utils';\n\n/**\n * Returns a function that localizes hrefs the same way `gt-next/link` does,\n * for server code such as Server Components, route handlers, and Server\n * Actions.\n *\n * Internal paths are prefixed with the target locale, except default-locale\n * paths when `prefixDefaultLocale` is `false` (an explicit default `locale`\n * keeps its prefix while `enableSmartRouting` is on). Localized path aliases\n * and locale availability are still resolved by the middleware. External URLs\n * are never prefixed.\n *\n * @example\n * ```tsx\n * import { getLocalizeHref } from 'gt-next/server';\n *\n * export async function CheckoutButton() {\n * const localizeHref = await getLocalizeHref();\n * return <Button href={localizeHref('/checkout')}>Checkout</Button>;\n * }\n * ```\n *\n * @returns A promise of a function taking an href and an optional locale (a\n * locale string, or `false` to leave the href unchanged) that returns the\n * localized href.\n */\nexport async function getLocalizeHref(): Promise<\n (href: string, locale?: LinkLocale) => string\n> {\n const currentLocale = await getLocale();\n const defaultLocale = getI18nConfig().getDefaultLocale();\n return (href, locale) =>\n localizeHref(href, locale, currentLocale, defaultLocale);\n}\n\n/**\n * Server Component version of `useLocalizeHref()`: localizes hrefs the same\n * way `gt-next/link` does, using the request locale.\n *\n * @example\n * ```tsx\n * import { useLocalizeHref } from 'gt-next';\n *\n * export function CheckoutButton() {\n * const localizeHref = useLocalizeHref();\n * return <Button href={localizeHref('/checkout')}>Checkout</Button>;\n * }\n * ```\n */\nexport function useLocalizeHref() {\n return use(getLocalizeHref());\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BA,eAAsB,kBAEpB;CACA,MAAM,gBAAgB,MAAMA,0BAAAA,WAAW;CACvC,MAAM,iBAAA,GAAA,iBAAA,gBAA+B,CAAC,kBAAkB;AACxD,SAAQ,MAAM,WACZC,4BAAAA,aAAa,MAAM,QAAQ,eAAe,cAAc;;;;;;;;;;;;;;;;AAiB5D,SAAgB,kBAAkB;AAChC,QAAOC,kBAAAA,IAAI,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { localizeHref } from "./localize-utils.mjs";
|
|
2
|
+
import { use } from "../utils/use.mjs";
|
|
3
|
+
import { getLocale } from "../request/getLocale.mjs";
|
|
4
|
+
import { getI18nConfig } from "gt-i18n/internal";
|
|
5
|
+
//#region src/link/getLocalizeHref.ts
|
|
6
|
+
/**
|
|
7
|
+
* Returns a function that localizes hrefs the same way `gt-next/link` does,
|
|
8
|
+
* for server code such as Server Components, route handlers, and Server
|
|
9
|
+
* Actions.
|
|
10
|
+
*
|
|
11
|
+
* Internal paths are prefixed with the target locale, except default-locale
|
|
12
|
+
* paths when `prefixDefaultLocale` is `false` (an explicit default `locale`
|
|
13
|
+
* keeps its prefix while `enableSmartRouting` is on). Localized path aliases
|
|
14
|
+
* and locale availability are still resolved by the middleware. External URLs
|
|
15
|
+
* are never prefixed.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```tsx
|
|
19
|
+
* import { getLocalizeHref } from 'gt-next/server';
|
|
20
|
+
*
|
|
21
|
+
* export async function CheckoutButton() {
|
|
22
|
+
* const localizeHref = await getLocalizeHref();
|
|
23
|
+
* return <Button href={localizeHref('/checkout')}>Checkout</Button>;
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @returns A promise of a function taking an href and an optional locale (a
|
|
28
|
+
* locale string, or `false` to leave the href unchanged) that returns the
|
|
29
|
+
* localized href.
|
|
30
|
+
*/
|
|
31
|
+
async function getLocalizeHref() {
|
|
32
|
+
const currentLocale = await getLocale();
|
|
33
|
+
const defaultLocale = getI18nConfig().getDefaultLocale();
|
|
34
|
+
return (href, locale) => localizeHref(href, locale, currentLocale, defaultLocale);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Server Component version of `useLocalizeHref()`: localizes hrefs the same
|
|
38
|
+
* way `gt-next/link` does, using the request locale.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```tsx
|
|
42
|
+
* import { useLocalizeHref } from 'gt-next';
|
|
43
|
+
*
|
|
44
|
+
* export function CheckoutButton() {
|
|
45
|
+
* const localizeHref = useLocalizeHref();
|
|
46
|
+
* return <Button href={localizeHref('/checkout')}>Checkout</Button>;
|
|
47
|
+
* }
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
function useLocalizeHref() {
|
|
51
|
+
return use(getLocalizeHref());
|
|
52
|
+
}
|
|
53
|
+
//#endregion
|
|
54
|
+
export { getLocalizeHref, useLocalizeHref };
|
|
55
|
+
|
|
56
|
+
//# sourceMappingURL=getLocalizeHref.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getLocalizeHref.mjs","names":[],"sources":["../../src/link/getLocalizeHref.ts"],"sourcesContent":["import { getI18nConfig } from 'gt-i18n/internal';\nimport { getLocale } from '../request/getLocale';\nimport { use } from '../utils/use';\nimport { localizeHref, type LinkLocale } from './localize-utils';\n\n/**\n * Returns a function that localizes hrefs the same way `gt-next/link` does,\n * for server code such as Server Components, route handlers, and Server\n * Actions.\n *\n * Internal paths are prefixed with the target locale, except default-locale\n * paths when `prefixDefaultLocale` is `false` (an explicit default `locale`\n * keeps its prefix while `enableSmartRouting` is on). Localized path aliases\n * and locale availability are still resolved by the middleware. External URLs\n * are never prefixed.\n *\n * @example\n * ```tsx\n * import { getLocalizeHref } from 'gt-next/server';\n *\n * export async function CheckoutButton() {\n * const localizeHref = await getLocalizeHref();\n * return <Button href={localizeHref('/checkout')}>Checkout</Button>;\n * }\n * ```\n *\n * @returns A promise of a function taking an href and an optional locale (a\n * locale string, or `false` to leave the href unchanged) that returns the\n * localized href.\n */\nexport async function getLocalizeHref(): Promise<\n (href: string, locale?: LinkLocale) => string\n> {\n const currentLocale = await getLocale();\n const defaultLocale = getI18nConfig().getDefaultLocale();\n return (href, locale) =>\n localizeHref(href, locale, currentLocale, defaultLocale);\n}\n\n/**\n * Server Component version of `useLocalizeHref()`: localizes hrefs the same\n * way `gt-next/link` does, using the request locale.\n *\n * @example\n * ```tsx\n * import { useLocalizeHref } from 'gt-next';\n *\n * export function CheckoutButton() {\n * const localizeHref = useLocalizeHref();\n * return <Button href={localizeHref('/checkout')}>Checkout</Button>;\n * }\n * ```\n */\nexport function useLocalizeHref() {\n return use(getLocalizeHref());\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BA,eAAsB,kBAEpB;CACA,MAAM,gBAAgB,MAAM,WAAW;CACvC,MAAM,gBAAgB,eAAe,CAAC,kBAAkB;AACxD,SAAQ,MAAM,WACZ,aAAa,MAAM,QAAQ,eAAe,cAAc;;;;;;;;;;;;;;;;AAiB5D,SAAgB,kBAAkB;AAChC,QAAO,IAAI,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type NextLink from 'next/link';
|
|
2
|
+
import type { ComponentProps } from 'react';
|
|
3
|
+
/** A locale to prefix, `undefined` for the current locale, or `false` for none. */
|
|
4
|
+
export type LinkLocale = ComponentProps<typeof NextLink>['locale'];
|
|
5
|
+
/**
|
|
6
|
+
* Localizes an href the way `gt-next/link` does.
|
|
7
|
+
*
|
|
8
|
+
* Internal paths are prefixed with the target locale, except default-locale
|
|
9
|
+
* paths when `prefixDefaultLocale` is `false` (an explicit default `locale`
|
|
10
|
+
* keeps its prefix while `enableSmartRouting` is on). Localized path aliases
|
|
11
|
+
* and locale availability are still resolved by the middleware. External URLs
|
|
12
|
+
* are never prefixed.
|
|
13
|
+
*/
|
|
14
|
+
export declare function localizeHref(href: string, locale: LinkLocale, currentLocale: string, defaultLocale: string): string;
|
|
15
|
+
//# sourceMappingURL=localize-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"localize-utils.d.ts","sourceRoot":"","sources":["../../src/link/localize-utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,QAAQ,MAAM,WAAW,CAAC;AACtC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,OAAO,CAAC;AAG5C,mFAAmF;AACnF,MAAM,MAAM,UAAU,GAAG,cAAc,CAAC,OAAO,QAAQ,CAAC,CAAC,QAAQ,CAAC,CAAC;AA6CnE;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAC1B,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,UAAU,EAClB,aAAa,EAAE,MAAM,EACrB,aAAa,EAAE,MAAM,GACpB,MAAM,CAGR"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_middleware_dir_resolveMiddlewareConfig = require("../middleware-dir/resolveMiddlewareConfig.js");
|
|
3
|
+
//#region src/link/localize-utils.ts
|
|
4
|
+
let routingConfig;
|
|
5
|
+
/**
|
|
6
|
+
* Reads the routing options passed to withGTConfig() on first use, so
|
|
7
|
+
* importing gt-next/link does no work.
|
|
8
|
+
*/
|
|
9
|
+
function getRoutingConfig() {
|
|
10
|
+
routingConfig ??= require_middleware_dir_resolveMiddlewareConfig.resolveMiddlewareConfig();
|
|
11
|
+
return routingConfig;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Returns the locale to prefix, or `false` when the middleware would not
|
|
15
|
+
* redirect the unprefixed path. Unprefixed default-locale paths are rewritten
|
|
16
|
+
* internally unless `prefixDefaultLocale` is set. An explicit default-locale
|
|
17
|
+
* switch keeps its prefix while smart routing would otherwise route to the
|
|
18
|
+
* locale cookie.
|
|
19
|
+
*/
|
|
20
|
+
function getPrefixLocale(locale, currentLocale, defaultLocale) {
|
|
21
|
+
if (locale === false) return false;
|
|
22
|
+
const targetLocale = locale ?? currentLocale;
|
|
23
|
+
const { prefixDefaultLocale, enableSmartRouting } = getRoutingConfig();
|
|
24
|
+
if (prefixDefaultLocale || targetLocale !== defaultLocale) return targetLocale;
|
|
25
|
+
return locale !== void 0 && enableSmartRouting ? targetLocale : false;
|
|
26
|
+
}
|
|
27
|
+
function prefixPath(path, locale) {
|
|
28
|
+
if (!path.startsWith("/") || path.startsWith("//")) return path;
|
|
29
|
+
const url = new URL(path, "http://localhost");
|
|
30
|
+
url.pathname = url.pathname === "/" ? `/${encodeURIComponent(locale)}` : `/${encodeURIComponent(locale)}${url.pathname}`;
|
|
31
|
+
return `${url.pathname}${url.search}${url.hash}`;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Localizes an href the way `gt-next/link` does.
|
|
35
|
+
*
|
|
36
|
+
* Internal paths are prefixed with the target locale, except default-locale
|
|
37
|
+
* paths when `prefixDefaultLocale` is `false` (an explicit default `locale`
|
|
38
|
+
* keeps its prefix while `enableSmartRouting` is on). Localized path aliases
|
|
39
|
+
* and locale availability are still resolved by the middleware. External URLs
|
|
40
|
+
* are never prefixed.
|
|
41
|
+
*/
|
|
42
|
+
function localizeHref(href, locale, currentLocale, defaultLocale) {
|
|
43
|
+
const prefixLocale = getPrefixLocale(locale, currentLocale, defaultLocale);
|
|
44
|
+
return prefixLocale ? prefixPath(href, prefixLocale) : href;
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
exports.localizeHref = localizeHref;
|
|
48
|
+
|
|
49
|
+
//# sourceMappingURL=localize-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"localize-utils.js","names":["resolveMiddlewareConfig"],"sources":["../../src/link/localize-utils.ts"],"sourcesContent":["import type NextLink from 'next/link';\nimport type { ComponentProps } from 'react';\nimport { resolveMiddlewareConfig } from '../middleware-dir/resolveMiddlewareConfig';\n\n/** A locale to prefix, `undefined` for the current locale, or `false` for none. */\nexport type LinkLocale = ComponentProps<typeof NextLink>['locale'];\n\nlet routingConfig: ReturnType<typeof resolveMiddlewareConfig> | undefined;\n\n/**\n * Reads the routing options passed to withGTConfig() on first use, so\n * importing gt-next/link does no work.\n */\nfunction getRoutingConfig() {\n routingConfig ??= resolveMiddlewareConfig();\n return routingConfig;\n}\n\n/**\n * Returns the locale to prefix, or `false` when the middleware would not\n * redirect the unprefixed path. Unprefixed default-locale paths are rewritten\n * internally unless `prefixDefaultLocale` is set. An explicit default-locale\n * switch keeps its prefix while smart routing would otherwise route to the\n * locale cookie.\n */\nfunction getPrefixLocale(\n locale: LinkLocale,\n currentLocale: string,\n defaultLocale: string\n): string | false {\n if (locale === false) return false;\n const targetLocale = locale ?? currentLocale;\n const { prefixDefaultLocale, enableSmartRouting } = getRoutingConfig();\n if (prefixDefaultLocale || targetLocale !== defaultLocale)\n return targetLocale;\n return locale !== undefined && enableSmartRouting ? targetLocale : false;\n}\n\nfunction prefixPath(path: string, locale: string): string {\n if (!path.startsWith('/') || path.startsWith('//')) return path;\n\n const url = new URL(path, 'http://localhost');\n url.pathname =\n url.pathname === '/'\n ? `/${encodeURIComponent(locale)}`\n : `/${encodeURIComponent(locale)}${url.pathname}`;\n\n return `${url.pathname}${url.search}${url.hash}`;\n}\n\n/**\n * Localizes an href the way `gt-next/link` does.\n *\n * Internal paths are prefixed with the target locale, except default-locale\n * paths when `prefixDefaultLocale` is `false` (an explicit default `locale`\n * keeps its prefix while `enableSmartRouting` is on). Localized path aliases\n * and locale availability are still resolved by the middleware. External URLs\n * are never prefixed.\n */\nexport function localizeHref(\n href: string,\n locale: LinkLocale,\n currentLocale: string,\n defaultLocale: string\n): string {\n const prefixLocale = getPrefixLocale(locale, currentLocale, defaultLocale);\n return prefixLocale ? prefixPath(href, prefixLocale) : href;\n}\n"],"mappings":";;;AAOA,IAAI;;;;;AAMJ,SAAS,mBAAmB;AAC1B,mBAAkBA,+CAAAA,yBAAyB;AAC3C,QAAO;;;;;;;;;AAUT,SAAS,gBACP,QACA,eACA,eACgB;AAChB,KAAI,WAAW,MAAO,QAAO;CAC7B,MAAM,eAAe,UAAU;CAC/B,MAAM,EAAE,qBAAqB,uBAAuB,kBAAkB;AACtE,KAAI,uBAAuB,iBAAiB,cAC1C,QAAO;AACT,QAAO,WAAW,KAAA,KAAa,qBAAqB,eAAe;;AAGrE,SAAS,WAAW,MAAc,QAAwB;AACxD,KAAI,CAAC,KAAK,WAAW,IAAI,IAAI,KAAK,WAAW,KAAK,CAAE,QAAO;CAE3D,MAAM,MAAM,IAAI,IAAI,MAAM,mBAAmB;AAC7C,KAAI,WACF,IAAI,aAAa,MACb,IAAI,mBAAmB,OAAO,KAC9B,IAAI,mBAAmB,OAAO,GAAG,IAAI;AAE3C,QAAO,GAAG,IAAI,WAAW,IAAI,SAAS,IAAI;;;;;;;;;;;AAY5C,SAAgB,aACd,MACA,QACA,eACA,eACQ;CACR,MAAM,eAAe,gBAAgB,QAAQ,eAAe,cAAc;AAC1E,QAAO,eAAe,WAAW,MAAM,aAAa,GAAG"}
|