@bleedingdev/modern-js-i18n-runtime-extensions 3.8.3-ultramodern.6 → 3.9.0-ultramodern.10

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.
Files changed (56) hide show
  1. package/dist/cjs/index.js +9 -2
  2. package/dist/cjs/localeRedirectPolicy.js +96 -0
  3. package/dist/cjs/localisedUrls/index.js +7 -0
  4. package/dist/cjs/localisedUrls/redirect.js +3 -6
  5. package/dist/cjs/localisedUrls/routeIdentity.js +118 -0
  6. package/dist/cjs/localisedUrls/routes.js +8 -0
  7. package/dist/cjs/paths.js +56 -0
  8. package/dist/cjs/router-navigation/adapter.js +235 -0
  9. package/dist/cjs/router-navigation/index.js +40 -0
  10. package/dist/esm/index.mjs +1 -0
  11. package/dist/esm/localeRedirectPolicy.mjs +46 -0
  12. package/dist/esm/localisedUrls/index.mjs +1 -0
  13. package/dist/esm/localisedUrls/redirect.mjs +3 -6
  14. package/dist/esm/localisedUrls/routeIdentity.mjs +77 -0
  15. package/dist/esm/localisedUrls/routes.mjs +8 -0
  16. package/dist/esm/paths.mjs +12 -0
  17. package/dist/esm/router-navigation/adapter.mjs +197 -0
  18. package/dist/esm/router-navigation/index.mjs +1 -0
  19. package/dist/esm-node/index.mjs +1 -0
  20. package/dist/esm-node/localeRedirectPolicy.mjs +47 -0
  21. package/dist/esm-node/localisedUrls/index.mjs +1 -0
  22. package/dist/esm-node/localisedUrls/redirect.mjs +3 -6
  23. package/dist/esm-node/localisedUrls/routeIdentity.mjs +78 -0
  24. package/dist/esm-node/localisedUrls/routes.mjs +8 -0
  25. package/dist/esm-node/paths.mjs +13 -0
  26. package/dist/esm-node/router-navigation/adapter.mjs +198 -0
  27. package/dist/esm-node/router-navigation/index.mjs +2 -0
  28. package/dist/types/index.d.ts +4 -3
  29. package/dist/types/language-sync/react.d.ts +1 -1
  30. package/dist/types/localeRedirectPolicy.d.ts +22 -0
  31. package/dist/types/localisedUrls/config.d.ts +1 -1
  32. package/dist/types/localisedUrls/index.d.ts +10 -9
  33. package/dist/types/localisedUrls/pathname.d.ts +1 -1
  34. package/dist/types/localisedUrls/redirect.d.ts +8 -0
  35. package/dist/types/localisedUrls/resolve.d.ts +1 -1
  36. package/dist/types/localisedUrls/routeIdentity.d.ts +24 -0
  37. package/dist/types/localisedUrls/routes.d.ts +1 -1
  38. package/dist/types/paths.d.ts +9 -0
  39. package/dist/types/router-navigation/adapter.d.ts +71 -0
  40. package/dist/types/router-navigation/index.d.ts +1 -0
  41. package/package.json +30 -6
  42. package/src/index.ts +1 -0
  43. package/src/localeRedirectPolicy.ts +127 -0
  44. package/src/localisedUrls/index.ts +4 -0
  45. package/src/localisedUrls/redirect.ts +16 -2
  46. package/src/localisedUrls/routeIdentity.ts +107 -0
  47. package/src/localisedUrls/routes.ts +10 -0
  48. package/src/paths.ts +37 -0
  49. package/src/router-navigation/adapter.tsx +536 -0
  50. package/src/router-navigation/index.ts +6 -0
  51. package/tests/languageSyncController.test.ts +0 -77
  52. package/tests/localeRedirectPolicy.test.ts +173 -0
  53. package/tests/localisedUrls.test.ts +0 -141
  54. package/tests/paths.test.ts +24 -0
  55. package/tests/reactLanguageSync.test.tsx +20 -1
  56. package/tests/clientRedirectIntegration.test.tsx +0 -74
@@ -0,0 +1,198 @@
1
+ import "node:module";
2
+ import { Fragment, jsx } from "react/jsx-runtime";
3
+ import { RuntimeContext, isBrowser } from "@modern-js/runtime";
4
+ import { InternalRuntimeContext } from "@modern-js/runtime/context";
5
+ import { getRouterRuntimeState, subscribeRouterRuntimeState } from "@modern-js/runtime-extensions/router-state";
6
+ import { useCallback, useContext, useSyncExternalStore } from "react";
7
+ const normalizeUrlPart = (value, prefix)=>{
8
+ if ('string' != typeof value || !value) return '';
9
+ return value.startsWith(prefix) ? value : `${prefix}${value}`;
10
+ };
11
+ const normalizeLocation = (location)=>{
12
+ if (!location || 'object' != typeof location) return null;
13
+ const locationValue = location;
14
+ if ('string' != typeof locationValue.pathname) return null;
15
+ return {
16
+ pathname: locationValue.pathname,
17
+ search: normalizeUrlPart('string' == typeof locationValue.search ? locationValue.search : locationValue.searchStr, '?'),
18
+ hash: normalizeUrlPart(locationValue.hash, '#')
19
+ };
20
+ };
21
+ const getWindowLocation = ()=>{
22
+ if (!isBrowser()) return null;
23
+ return {
24
+ pathname: window.location.pathname,
25
+ search: window.location.search,
26
+ hash: window.location.hash
27
+ };
28
+ };
29
+ const getRouterFramework = (runtimeContext, internalContext, inReactRouter)=>{
30
+ const framework = getRouterRuntimeState(internalContext)?.framework || getRouterRuntimeState(runtimeContext)?.framework;
31
+ if (framework) return framework;
32
+ if (internalContext.router?.useRouter || runtimeContext.router?.useRouter) return 'tanstack';
33
+ if (internalContext.router?.useLocation || internalContext.router?.useHref || runtimeContext.router?.useLocation || runtimeContext.router?.useHref) return 'react-router';
34
+ if (inReactRouter) return 'react-router';
35
+ };
36
+ const getRouterInstance = (internalContext, contextRouter)=>{
37
+ if (contextRouter) return contextRouter;
38
+ const router = getRouterRuntimeState(internalContext)?.instance;
39
+ if (!router || 'object' != typeof router) return null;
40
+ return router;
41
+ };
42
+ const getRouterStateLocation = (internalContext, contextRouter)=>{
43
+ const router = getRouterInstance(internalContext, contextRouter);
44
+ return normalizeLocation(router?.stores?.location?.get?.()) || normalizeLocation(router?.state?.location);
45
+ };
46
+ const getRouterParams = (internalContext, contextRouter)=>{
47
+ const router = getRouterInstance(internalContext, contextRouter);
48
+ const matches = router?.stores?.matches?.get?.() || router?.state?.matches;
49
+ if (!Array.isArray(matches)) return {};
50
+ return matches.reduce((params, match)=>{
51
+ if (match?.params) Object.assign(params, match.params);
52
+ return params;
53
+ }, {});
54
+ };
55
+ const getRouterSnapshot = (internalContext, contextRouter)=>{
56
+ const location = getRouterStateLocation(internalContext, contextRouter);
57
+ const params = getRouterParams(internalContext, contextRouter);
58
+ return JSON.stringify([
59
+ location?.pathname ?? '',
60
+ location?.search ?? '',
61
+ location?.hash ?? '',
62
+ Object.entries(params).sort(([left], [right])=>left.localeCompare(right))
63
+ ]);
64
+ };
65
+ const createI18nRouterNavigation = ({ I18nNavigationProvider, useNativeI18nRouterAdapter })=>{
66
+ const useIntegratedRouterAdapter = ()=>{
67
+ const runtimeContext = useContext(RuntimeContext);
68
+ const internalContext = useContext(InternalRuntimeContext);
69
+ const routerApi = internalContext.router || runtimeContext.router;
70
+ const native = useNativeI18nRouterAdapter();
71
+ const inReactRouter = native.hasRouter;
72
+ const reactRouterNavigate = native.navigate;
73
+ const reactRouterLocation = inReactRouter ? native.location : null;
74
+ const reactRouterParams = native.params;
75
+ const framework = getRouterFramework(runtimeContext, internalContext, inReactRouter);
76
+ const contextUseRouter = inReactRouter || 'tanstack' !== framework ? void 0 : internalContext.router?.useRouter || runtimeContext.router?.useRouter;
77
+ const contextRouter = contextUseRouter ? contextUseRouter({
78
+ warn: false
79
+ }) : null;
80
+ const slotRouter = getRouterInstance(internalContext, contextRouter);
81
+ const drivableSlotRouter = ('tanstack' === framework || 'react-router' === framework) && 'function' == typeof slotRouter?.navigate;
82
+ const hasRouter = Boolean(reactRouterNavigate) || drivableSlotRouter;
83
+ const subscribeToRouter = useCallback((update)=>{
84
+ const unsubscribers = [];
85
+ const unsubscribeFromSlot = subscribeRouterRuntimeState(internalContext, update);
86
+ if ('function' == typeof unsubscribeFromSlot) unsubscribers.push(unsubscribeFromSlot);
87
+ const router = getRouterInstance(internalContext, contextRouter);
88
+ if (!router) return ()=>{
89
+ for (const unsubscribe of unsubscribers)unsubscribe();
90
+ };
91
+ if ('react-router' === framework && !inReactRouter && 'function' == typeof router.subscribe) {
92
+ const subscribe = router.subscribe;
93
+ const unsubscribe = subscribe.call(router, update);
94
+ if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
95
+ }
96
+ if ('tanstack' === framework && 'function' == typeof router.stores?.location?.subscribe) {
97
+ const unsubscribe = router.stores.location.subscribe(update);
98
+ if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
99
+ }
100
+ if ('tanstack' === framework && 'function' == typeof router.subscribe) {
101
+ const subscribe = router.subscribe;
102
+ for (const eventType of [
103
+ 'onBeforeNavigate',
104
+ 'onBeforeLoad'
105
+ ]){
106
+ const unsubscribe = subscribe.call(router, eventType, update);
107
+ if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
108
+ }
109
+ }
110
+ return ()=>{
111
+ for (const unsubscribe of unsubscribers)unsubscribe();
112
+ };
113
+ }, [
114
+ contextRouter,
115
+ framework,
116
+ inReactRouter,
117
+ internalContext
118
+ ]);
119
+ const getSnapshot = useCallback(()=>getRouterSnapshot(internalContext, contextRouter), [
120
+ contextRouter,
121
+ internalContext
122
+ ]);
123
+ useSyncExternalStore(subscribeToRouter, getSnapshot, getSnapshot);
124
+ const navigate = useCallback((href, options)=>{
125
+ const router = getRouterInstance(internalContext, contextRouter);
126
+ const activeFramework = getRouterFramework(runtimeContext, internalContext, inReactRouter);
127
+ if ('tanstack' === activeFramework) {
128
+ if ('function' == typeof router?.navigate) return router.navigate({
129
+ to: href,
130
+ replace: options?.replace,
131
+ ...options?.state === void 0 ? {} : {
132
+ state: options.state
133
+ }
134
+ });
135
+ throw new Error('TanStack router instance is not available.');
136
+ }
137
+ if (reactRouterNavigate) return reactRouterNavigate(href, options);
138
+ if ('react-router' === activeFramework) {
139
+ if ('function' == typeof router?.navigate) return router.navigate(href, options);
140
+ throw new Error('React Router instance is not available.');
141
+ }
142
+ }, [
143
+ contextRouter,
144
+ internalContext,
145
+ inReactRouter,
146
+ reactRouterNavigate,
147
+ runtimeContext
148
+ ]);
149
+ const location = (reactRouterLocation ? normalizeLocation(reactRouterLocation) : getRouterStateLocation(internalContext, contextRouter)) || getWindowLocation();
150
+ const params = inReactRouter ? reactRouterParams : getRouterParams(internalContext, contextRouter);
151
+ const Link = 'tanstack' === framework ? internalContext.router?.Link || runtimeContext.router?.Link || null : 'react-router' === framework || inReactRouter ? routerApi?.Link ?? null : null;
152
+ return {
153
+ framework,
154
+ hasRouter,
155
+ location,
156
+ navigate: hasRouter ? navigate : null,
157
+ Link,
158
+ params,
159
+ ...'tanstack' === framework ? {
160
+ createLinkProps: createTanstackLinkProps
161
+ } : {}
162
+ };
163
+ };
164
+ const createTanstackLinkProps = (target)=>{
165
+ const preload = void 0 !== target.preload ? target.preload : 'none' === target.prefetch ? false : target.prefetch;
166
+ return {
167
+ to: target.pathname,
168
+ ...target.search ? {
169
+ search: target.search
170
+ } : {},
171
+ ...target.hash ? {
172
+ hash: target.hash
173
+ } : {},
174
+ ...void 0 === target.hashScrollIntoView ? {} : {
175
+ hashScrollIntoView: target.hashScrollIntoView
176
+ },
177
+ ...void 0 === preload ? {} : {
178
+ preload
179
+ }
180
+ };
181
+ };
182
+ const I18nRouterNavigationProvider = ({ children })=>{
183
+ const value = useIntegratedRouterAdapter();
184
+ const native = useNativeI18nRouterAdapter();
185
+ if (!value.hasRouter || native.hasRouter) return /*#__PURE__*/ jsx(Fragment, {
186
+ children: children
187
+ });
188
+ return /*#__PURE__*/ jsx(I18nNavigationProvider, {
189
+ value: value,
190
+ children: children
191
+ });
192
+ };
193
+ return {
194
+ useIntegratedRouterAdapter,
195
+ I18nRouterNavigationProvider
196
+ };
197
+ };
198
+ export { createI18nRouterNavigation };
@@ -0,0 +1,2 @@
1
+ import "node:module";
2
+ export { createI18nRouterNavigation } from "./adapter.mjs";
@@ -1,3 +1,4 @@
1
- export * from './language-sync/controller';
2
- export * from './language-sync/react';
3
- export * from './localisedUrls/index';
1
+ export * from './language-sync/controller.js';
2
+ export * from './language-sync/react.js';
3
+ export * from './localeRedirectPolicy.js';
4
+ export * from './localisedUrls/index.js';
@@ -1,5 +1,5 @@
1
1
  import type React from 'react';
2
- import { type LanguageSyncFailure, type LanguageSyncPolicy } from './controller';
2
+ import { type LanguageSyncFailure, type LanguageSyncPolicy } from './controller.js';
3
3
  export interface LatestLanguageSyncOptions<TTarget extends object> {
4
4
  changeLanguage: (target: TTarget, language: string) => PromiseLike<unknown> | unknown;
5
5
  commitLanguage: (target: TTarget, language: string) => void;
@@ -0,0 +1,22 @@
1
+ import type { LocalisedUrlsOption } from './localisedUrls/index.js';
2
+ interface LocaleRedirectRequest {
3
+ url: string;
4
+ header: () => {
5
+ host?: string;
6
+ };
7
+ }
8
+ /**
9
+ * Check if pathname should ignore automatic locale redirect.
10
+ */
11
+ export declare const shouldIgnoreRedirect: (pathname: string, urlPath: string, ignoreRedirectRoutes?: string[] | ((pathname: string) => boolean)) => boolean;
12
+ /**
13
+ * Check if pathname is a static or federation resource request.
14
+ *
15
+ * This includes configured staticRoutePrefixes, the shared default skip policy
16
+ * from ADR-0002, and language-prefixed variants such as /en/static/app.js.
17
+ */
18
+ export declare const isStaticResourceRequest: (pathname: string, staticRoutePrefixes: string[], languages?: string[]) => boolean;
19
+ export declare const getLanguageFromPath: (req: LocaleRedirectRequest, urlPath: string, languages: string[]) => string | null;
20
+ export declare const buildLocalizedUrl: (req: LocaleRedirectRequest, urlPath: string, language: string, languages: string[], localisedUrls?: LocalisedUrlsOption) => string;
21
+ export declare const createLocaleRedirectResponse: (location: string) => Response;
22
+ export {};
@@ -1,4 +1,4 @@
1
- import type { LocalisedUrlsOption, ResolvedLocalisedUrlsConfig } from './types';
1
+ import type { LocalisedUrlsOption, ResolvedLocalisedUrlsConfig } from './types.js';
2
2
  /**
3
3
  * Localised URLs are strictly opt-in: only an explicit, non-empty map enables
4
4
  * route expansion and validation. `true`, `false`, an empty map and absence
@@ -1,9 +1,10 @@
1
- export { resolveLocalisedUrlsConfig } from './config';
2
- export { normalisePathname, normalisePathPattern } from './normalise';
3
- export { canonicalTargetPathname, localiseTargetPathname, stripLanguagePrefix, } from './pathname';
4
- export { buildPathFromPattern, matchPathPattern } from './patterns';
5
- export type { LocaleRedirectSkipRule } from './redirect';
6
- export { DEFAULT_LOCALE_REDIRECT_SKIP_RULES, isDefaultLocaleRedirectSkipPath, matchesPathPrefix, shouldSkipLocaleRedirect, } from './redirect';
7
- export { resolveCanonicalLocalisedPath, resolveLocalisedPath } from './resolve';
8
- export { applyLocalisedUrlsToRoutes, validateLocalisedUrls } from './routes';
9
- export type { LocalisedRoute, LocalisedUrlPathMap, LocalisedUrlsMap, LocalisedUrlsOption, ResolvedLocalisedUrlsConfig, } from './types';
1
+ export { resolveLocalisedUrlsConfig } from './config.js';
2
+ export { normalisePathname, normalisePathPattern } from './normalise.js';
3
+ export { canonicalTargetPathname, localiseTargetPathname, stripLanguagePrefix, } from './pathname.js';
4
+ export { buildPathFromPattern, matchPathPattern } from './patterns.js';
5
+ export type { LocaleRedirectSkipRule } from './redirect.js';
6
+ export { DEFAULT_LOCALE_REDIRECT_SKIP_RULES, isDefaultLocaleRedirectSkipPath, matchesPathPrefix, shouldSkipLocaleRedirect, } from './redirect.js';
7
+ export { resolveCanonicalLocalisedPath, resolveLocalisedPath } from './resolve.js';
8
+ export { canonicaliseLocalisedRoutes, createLocalisedRouteRewrite, } from './routeIdentity.js';
9
+ export { applyLocalisedUrlsToRoutes, validateLocalisedUrls } from './routes.js';
10
+ export type { LocalisedRoute, LocalisedUrlPathMap, LocalisedUrlsMap, LocalisedUrlsOption, ResolvedLocalisedUrlsConfig, } from './types.js';
@@ -1,4 +1,4 @@
1
- import type { LocalisedUrlsOption } from './types';
1
+ import type { LocalisedUrlsOption } from './types.js';
2
2
  export declare const stripLanguagePrefix: (pathname: string, languages: string[]) => string;
3
3
  export declare const localiseTargetPathname: (pathname: string, language: string, languages: string[], localisedUrls?: LocalisedUrlsOption) => string;
4
4
  export declare const canonicalTargetPathname: (pathname: string, languages: string[], localisedUrls?: LocalisedUrlsOption) => string;
@@ -4,7 +4,15 @@ export type LocaleRedirectSkipRule = {
4
4
  } | {
5
5
  type: 'prefix';
6
6
  path: string;
7
+ } | {
8
+ type: 'pattern';
9
+ path: RegExp;
7
10
  };
11
+ /**
12
+ * Federation artifacts are never pages. The remote entries match the same
13
+ * shapes the federation layer itself recognises and serves (`mfCache.ts`):
14
+ * a manifest may name a root entry such as `remoteEntry.catalog.js`.
15
+ */
8
16
  export declare const DEFAULT_LOCALE_REDIRECT_SKIP_RULES: readonly LocaleRedirectSkipRule[];
9
17
  export declare const matchesPathPrefix: (pathname: string, prefix: string) => boolean;
10
18
  export declare const isDefaultLocaleRedirectSkipPath: (pathname: string, languages?: string[]) => boolean;
@@ -1,4 +1,4 @@
1
- import type { LocalisedUrlsMap } from './types';
1
+ import type { LocalisedUrlsMap } from './types.js';
2
2
  export declare const resolveLocalisedPath: (pathname: string, targetLanguage: string, languages: string[], localisedUrls: LocalisedUrlsMap) => string;
3
3
  /**
4
4
  * Reverse-map language-specific pathnames back to language-agnostic
@@ -0,0 +1,24 @@
1
+ interface RouteIdentity {
2
+ id?: string;
3
+ path?: string;
4
+ canonicalPath: string;
5
+ paths?: Record<string, string>;
6
+ }
7
+ interface RouteNode {
8
+ id?: string;
9
+ path?: string;
10
+ children?: RouteNode[];
11
+ modernLocalisedRoute?: RouteIdentity;
12
+ }
13
+ /** Restore one source route identity for routers with strict route-ID hooks. */
14
+ export declare function canonicaliseLocalisedRoutes<T extends RouteNode>(routes: T[]): T[];
15
+ /** Use the router's URL rewrite seam; browser URLs retain their locale spelling. */
16
+ export declare function createLocalisedRouteRewrite(routes: RouteNode[]): {
17
+ input: ({ url }: {
18
+ url: URL;
19
+ }) => URL;
20
+ output: ({ url }: {
21
+ url: URL;
22
+ }) => URL;
23
+ } | undefined;
24
+ export {};
@@ -1,3 +1,3 @@
1
- import type { LocalisedRoute, LocalisedUrlsMap } from './types';
1
+ import type { LocalisedRoute, LocalisedUrlsMap } from './types.js';
2
2
  export declare const validateLocalisedUrls: (routes: LocalisedRoute[], languages: string[], localisedUrls: LocalisedUrlsMap) => void;
3
3
  export declare const applyLocalisedUrlsToRoutes: (routes: LocalisedRoute[], languages: string[], localisedUrls: LocalisedUrlsMap) => LocalisedRoute[];
@@ -0,0 +1,9 @@
1
+ import { type LocalisedUrlsOption } from './localisedUrls/index.js';
2
+ export interface LocalizedPathsConfig {
3
+ languages: string[];
4
+ localisedUrls?: LocalisedUrlsOption;
5
+ }
6
+ /** Synchronous mapped URL construction, independent of React/plugin setup. */
7
+ export declare const buildLocalizedUrl: (target: string, language: string, languages: string[], localisedUrls?: LocalisedUrlsOption) => string;
8
+ export declare const localizePath: (target: string, language: string, config: LocalizedPathsConfig) => string;
9
+ export declare const canonicalPath: (target: string, config: LocalizedPathsConfig) => string;
@@ -0,0 +1,71 @@
1
+ import type React from 'react';
2
+ /**
3
+ * Fork-owned: the selected-router navigation adapter.
4
+ *
5
+ * `useNativeI18nRouterAdapter` only knows react-router's own hooks, so a bare
6
+ * `appTools() + i18nPlugin()` app running the TanStack router would fall back
7
+ * to plain `<a>` links and a non-reactive `window.location`. That breaks
8
+ * language-invariant active state and, worse, leaves the i18n language stuck
9
+ * after a client-side navigation to a localized URL.
10
+ *
11
+ * The subsystem lives in this fork-owned package rather than inside
12
+ * upstream-owned `@modern-js/plugin-i18n`: that package only wires it in
13
+ * through its existing `NavigationProvider` extension point, so the upstream
14
+ * package keeps a seam instead of gaining a fork subsystem. The primitives the
15
+ * adapter composes with - the navigation context provider, the native
16
+ * react-router adapter and the adapter shape - are injected by that seam, so
17
+ * nothing here imports `@modern-js/plugin-i18n` and no dependency cycle is
18
+ * created.
19
+ *
20
+ * The adapter reads the actual selected router out of the fork-owned router
21
+ * runtime-state slot, so it serves both routers without either plugin having
22
+ * to know about i18n.
23
+ */
24
+ /** The primitives the host plugin injects; see `createI18nRouterNavigation`. */
25
+ export interface I18nRouterNavigationDependencies<Adapter extends I18nRouterAdapterShape> {
26
+ I18nNavigationProvider: React.ComponentType<React.PropsWithChildren<{
27
+ value: Adapter;
28
+ }>>;
29
+ useNativeI18nRouterAdapter: () => Adapter;
30
+ }
31
+ export interface I18nRouterLinkTarget {
32
+ pathname: string;
33
+ href: string;
34
+ search?: Record<string, unknown>;
35
+ hash?: string;
36
+ hashScrollIntoView?: boolean | ScrollIntoViewOptions;
37
+ prefetch?: 'intent' | 'render' | 'viewport' | 'none';
38
+ preload?: unknown;
39
+ }
40
+ export interface I18nRouterAdapterShape {
41
+ framework?: string;
42
+ hasRouter: boolean;
43
+ location: {
44
+ pathname: string;
45
+ search: string;
46
+ hash: string;
47
+ } | null;
48
+ navigate: ((href: string, options?: {
49
+ replace?: boolean;
50
+ state?: unknown;
51
+ }) => void | Promise<void>) | null;
52
+ Link: React.ComponentType<{
53
+ to: string;
54
+ children?: React.ReactNode;
55
+ [key: string]: unknown;
56
+ }> | null;
57
+ params: Record<string, string>;
58
+ createLinkProps?: (target: I18nRouterLinkTarget) => {
59
+ to: string;
60
+ [key: string]: unknown;
61
+ };
62
+ }
63
+ /**
64
+ * Build the selected-router navigation hook and provider from the host
65
+ * plugin's own primitives. Injecting them is what keeps this fork-owned
66
+ * subsystem free of any import edge back into the plugin that installs it.
67
+ */
68
+ export declare const createI18nRouterNavigation: <Adapter extends I18nRouterAdapterShape>({ I18nNavigationProvider, useNativeI18nRouterAdapter, }: I18nRouterNavigationDependencies<Adapter>) => {
69
+ useIntegratedRouterAdapter: () => Adapter;
70
+ I18nRouterNavigationProvider: ({ children, }: React.PropsWithChildren) => React.JSX.Element;
71
+ };
@@ -0,0 +1 @@
1
+ export { createI18nRouterNavigation, type I18nRouterAdapterShape, type I18nRouterLinkTarget, type I18nRouterNavigationDependencies, } from './adapter.js';
package/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  "modern.js",
18
18
  "i18n"
19
19
  ],
20
- "version": "3.8.3-ultramodern.6",
20
+ "version": "3.9.0-ultramodern.10",
21
21
  "types": "./dist/types/index.d.ts",
22
22
  "main": "./dist/cjs/index.js",
23
23
  "module": "./dist/esm/index.mjs",
@@ -30,13 +30,35 @@
30
30
  },
31
31
  "default": "./dist/esm/index.mjs"
32
32
  },
33
- "./package.json": "./package.json"
33
+ "./router-navigation": {
34
+ "types": "./dist/types/router-navigation/index.d.ts",
35
+ "node": {
36
+ "import": "./dist/esm-node/router-navigation/index.mjs",
37
+ "require": "./dist/cjs/router-navigation/index.js"
38
+ },
39
+ "default": "./dist/esm/router-navigation/index.mjs"
40
+ },
41
+ "./package.json": "./package.json",
42
+ "./paths": {
43
+ "types": "./dist/types/paths.d.ts",
44
+ "node": {
45
+ "import": "./dist/esm-node/paths.mjs",
46
+ "require": "./dist/cjs/paths.js"
47
+ },
48
+ "default": "./dist/esm/paths.mjs"
49
+ }
34
50
  },
35
51
  "typesVersions": {
36
52
  "*": {
37
53
  ".": [
38
54
  "./dist/types/index.d.ts",
39
55
  "./src/index.ts"
56
+ ],
57
+ "paths": [
58
+ "./dist/types/paths.d.ts"
59
+ ],
60
+ "router-navigation": [
61
+ "./dist/types/router-navigation/index.d.ts"
40
62
  ]
41
63
  }
42
64
  },
@@ -44,17 +66,19 @@
44
66
  "node": ">=26.7.0"
45
67
  },
46
68
  "dependencies": {
69
+ "@modern-js/runtime-extensions": "npm:@bleedingdev/modern-js-runtime-extensions@3.9.0-ultramodern.10",
70
+ "@modern-js/runtime-utils": "npm:@bleedingdev/modern-js-runtime-utils@3.9.0-ultramodern.10",
47
71
  "i18next-browser-languagedetector": "^8.2.1"
48
72
  },
49
73
  "peerDependencies": {
50
- "@modern-js/runtime": "3.8.3-ultramodern.6",
74
+ "@modern-js/runtime": "3.9.0-ultramodern.10",
51
75
  "react": "^19.2.8"
52
76
  },
53
77
  "devDependencies": {
54
- "@modern-js/runtime": "npm:@bleedingdev/modern-js-runtime@3.8.3-ultramodern.6",
55
- "@rslib/core": "0.23.2",
78
+ "@modern-js/runtime": "npm:@bleedingdev/modern-js-runtime@3.9.0-ultramodern.10",
79
+ "@rslib/core": "1.0.0",
56
80
  "@scripts/rstest-config": "2.66.0",
57
- "@types/node": "^26.2.0",
81
+ "@types/node": "^26.4.1",
58
82
  "@typescript/native-preview": "7.0.0-dev.20260707.2",
59
83
  "react": "^19.2.8",
60
84
  "react-dom": "^19.2.8"
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './language-sync/controller';
2
2
  export * from './language-sync/react';
3
+ export * from './localeRedirectPolicy';
3
4
  export * from './localisedUrls/index';
@@ -0,0 +1,127 @@
1
+ import type { LocalisedUrlsOption } from './localisedUrls/index';
2
+ import {
3
+ isDefaultLocaleRedirectSkipPath,
4
+ localiseTargetPathname,
5
+ matchesPathPrefix,
6
+ shouldSkipLocaleRedirect,
7
+ stripLanguagePrefix,
8
+ } from './localisedUrls/index';
9
+
10
+ interface LocaleRedirectRequest {
11
+ url: string;
12
+ header: () => {
13
+ host?: string;
14
+ };
15
+ }
16
+
17
+ const stripUrlPathPrefix = (pathname: string, urlPath: string): string => {
18
+ const basePath = urlPath.replace('/*', '');
19
+
20
+ if (!basePath || basePath === '/') {
21
+ return pathname;
22
+ }
23
+
24
+ const remainingPath = pathname.startsWith(basePath)
25
+ ? pathname.slice(basePath.length)
26
+ : pathname;
27
+
28
+ return remainingPath || '/';
29
+ };
30
+
31
+ /**
32
+ * Check if pathname should ignore automatic locale redirect.
33
+ */
34
+ export const shouldIgnoreRedirect = (
35
+ pathname: string,
36
+ urlPath: string,
37
+ ignoreRedirectRoutes?: string[] | ((pathname: string) => boolean),
38
+ ): boolean => {
39
+ const remainingPath = stripUrlPathPrefix(pathname, urlPath);
40
+
41
+ return shouldSkipLocaleRedirect(remainingPath, [], ignoreRedirectRoutes);
42
+ };
43
+
44
+ /**
45
+ * Check if pathname is a static or federation resource request.
46
+ *
47
+ * This includes configured staticRoutePrefixes, the shared default skip policy
48
+ * from ADR-0002, and language-prefixed variants such as /en/static/app.js.
49
+ */
50
+ export const isStaticResourceRequest = (
51
+ pathname: string,
52
+ staticRoutePrefixes: string[],
53
+ languages: string[] = [],
54
+ ): boolean => {
55
+ if (isDefaultLocaleRedirectSkipPath(pathname, languages)) {
56
+ return true;
57
+ }
58
+
59
+ const matchesStaticRoutePrefix = (targetPathname: string): boolean =>
60
+ staticRoutePrefixes.some(prefix =>
61
+ matchesPathPrefix(targetPathname, prefix),
62
+ );
63
+
64
+ if (matchesStaticRoutePrefix(pathname)) {
65
+ return true;
66
+ }
67
+
68
+ const pathWithoutLanguage = stripLanguagePrefix(pathname, languages);
69
+
70
+ return (
71
+ pathWithoutLanguage !== pathname &&
72
+ matchesStaticRoutePrefix(pathWithoutLanguage)
73
+ );
74
+ };
75
+
76
+ export const getLanguageFromPath = (
77
+ req: LocaleRedirectRequest,
78
+ urlPath: string,
79
+ languages: string[],
80
+ ): string | null => {
81
+ const url = new URL(req.url, `http://${req.header().host}`);
82
+ const pathname = url.pathname;
83
+ const remainingPath = stripUrlPathPrefix(pathname, urlPath);
84
+ const segments = remainingPath.split('/').filter(Boolean);
85
+ const firstSegment = segments[0];
86
+
87
+ if (languages.includes(firstSegment)) {
88
+ return firstSegment;
89
+ }
90
+
91
+ return null;
92
+ };
93
+
94
+ export const buildLocalizedUrl = (
95
+ req: LocaleRedirectRequest,
96
+ urlPath: string,
97
+ language: string,
98
+ languages: string[],
99
+ localisedUrls?: LocalisedUrlsOption,
100
+ ): string => {
101
+ const url = new URL(req.url);
102
+ const pathname = url.pathname;
103
+ const basePath = urlPath.replace('/*', '');
104
+ const remainingPath = stripUrlPathPrefix(pathname, urlPath);
105
+
106
+ const newPathname = localiseTargetPathname(
107
+ remainingPath,
108
+ language,
109
+ languages,
110
+ localisedUrls,
111
+ );
112
+ const suffix = `${url.search}${url.hash}`;
113
+ const localizedUrl =
114
+ basePath === '/' ? newPathname + suffix : basePath + newPathname + suffix;
115
+
116
+ return localizedUrl;
117
+ };
118
+
119
+ export const createLocaleRedirectResponse = (location: string): Response =>
120
+ new Response(null, {
121
+ status: 302,
122
+ headers: {
123
+ 'Cache-Control': 'private, no-store',
124
+ Location: location,
125
+ Vary: 'Accept-Language, Cookie',
126
+ },
127
+ });
@@ -14,6 +14,10 @@ export {
14
14
  shouldSkipLocaleRedirect,
15
15
  } from './redirect';
16
16
  export { resolveCanonicalLocalisedPath, resolveLocalisedPath } from './resolve';
17
+ export {
18
+ canonicaliseLocalisedRoutes,
19
+ createLocalisedRouteRewrite,
20
+ } from './routeIdentity';
17
21
  export { applyLocalisedUrlsToRoutes, validateLocalisedUrls } from './routes';
18
22
  export type {
19
23
  LocalisedRoute,