@bleedingdev/modern-js-plugin-i18n 3.2.0-ultramodern.12 → 3.2.0-ultramodern.121

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 (119) hide show
  1. package/README.md +221 -11
  2. package/dist/cjs/cli/index.js +32 -5
  3. package/dist/cjs/runtime/I18nLink.js +17 -28
  4. package/dist/cjs/runtime/Link.js +264 -0
  5. package/dist/cjs/runtime/canonicalRoutes.js +18 -0
  6. package/dist/cjs/runtime/context.js +41 -10
  7. package/dist/cjs/runtime/hooks.js +17 -10
  8. package/dist/cjs/runtime/i18n/backend/config.js +9 -5
  9. package/dist/cjs/runtime/i18n/backend/defaults.js +15 -10
  10. package/dist/cjs/runtime/i18n/backend/defaults.node.js +47 -8
  11. package/dist/cjs/runtime/i18n/backend/index.js +9 -5
  12. package/dist/cjs/runtime/i18n/backend/middleware.common.js +9 -5
  13. package/dist/cjs/runtime/i18n/backend/middleware.js +9 -5
  14. package/dist/cjs/runtime/i18n/backend/middleware.node.js +13 -9
  15. package/dist/cjs/runtime/i18n/backend/sdk-backend.js +9 -5
  16. package/dist/cjs/runtime/i18n/backend/sdk-event.js +16 -11
  17. package/dist/cjs/runtime/i18n/detection/config.js +9 -5
  18. package/dist/cjs/runtime/i18n/detection/index.js +9 -5
  19. package/dist/cjs/runtime/i18n/detection/middleware.js +9 -5
  20. package/dist/cjs/runtime/i18n/detection/middleware.node.js +9 -5
  21. package/dist/cjs/runtime/i18n/index.js +9 -5
  22. package/dist/cjs/runtime/i18n/instance.js +17 -37
  23. package/dist/cjs/runtime/i18n/react-i18next.js +53 -0
  24. package/dist/cjs/runtime/i18n/utils.js +9 -17
  25. package/dist/cjs/runtime/index.js +50 -15
  26. package/dist/cjs/runtime/localizedPaths.js +102 -0
  27. package/dist/cjs/runtime/routerAdapter.js +167 -0
  28. package/dist/cjs/runtime/utils.js +80 -97
  29. package/dist/cjs/server/index.js +62 -14
  30. package/dist/cjs/shared/deepMerge.js +12 -8
  31. package/dist/cjs/shared/detection.js +9 -5
  32. package/dist/cjs/shared/localisedUrls.js +351 -0
  33. package/dist/cjs/shared/utils.js +15 -11
  34. package/dist/esm/cli/index.mjs +23 -0
  35. package/dist/esm/runtime/I18nLink.mjs +7 -22
  36. package/dist/esm/runtime/Link.mjs +221 -0
  37. package/dist/esm/runtime/canonicalRoutes.mjs +0 -0
  38. package/dist/esm/runtime/context.mjs +34 -7
  39. package/dist/esm/runtime/hooks.mjs +9 -6
  40. package/dist/esm/runtime/i18n/backend/defaults.mjs +1 -1
  41. package/dist/esm/runtime/i18n/backend/defaults.node.mjs +24 -3
  42. package/dist/esm/runtime/i18n/backend/middleware.node.mjs +3 -3
  43. package/dist/esm/runtime/i18n/instance.mjs +1 -19
  44. package/dist/esm/runtime/i18n/react-i18next.mjs +15 -0
  45. package/dist/esm/runtime/i18n/utils.mjs +0 -12
  46. package/dist/esm/runtime/index.mjs +23 -13
  47. package/dist/esm/runtime/localizedPaths.mjs +55 -0
  48. package/dist/esm/runtime/routerAdapter.mjs +129 -0
  49. package/dist/esm/runtime/utils.mjs +19 -31
  50. package/dist/esm/server/index.mjs +46 -8
  51. package/dist/esm/shared/localisedUrls.mjs +283 -0
  52. package/dist/esm-node/cli/index.mjs +23 -0
  53. package/dist/esm-node/runtime/I18nLink.mjs +7 -22
  54. package/dist/esm-node/runtime/Link.mjs +222 -0
  55. package/dist/esm-node/runtime/canonicalRoutes.mjs +1 -0
  56. package/dist/esm-node/runtime/context.mjs +34 -7
  57. package/dist/esm-node/runtime/hooks.mjs +9 -6
  58. package/dist/esm-node/runtime/i18n/backend/defaults.mjs +1 -1
  59. package/dist/esm-node/runtime/i18n/backend/defaults.node.mjs +24 -3
  60. package/dist/esm-node/runtime/i18n/backend/middleware.node.mjs +3 -3
  61. package/dist/esm-node/runtime/i18n/instance.mjs +1 -19
  62. package/dist/esm-node/runtime/i18n/react-i18next.mjs +16 -0
  63. package/dist/esm-node/runtime/i18n/utils.mjs +0 -12
  64. package/dist/esm-node/runtime/index.mjs +23 -13
  65. package/dist/esm-node/runtime/localizedPaths.mjs +56 -0
  66. package/dist/esm-node/runtime/routerAdapter.mjs +130 -0
  67. package/dist/esm-node/runtime/utils.mjs +19 -31
  68. package/dist/esm-node/server/index.mjs +46 -8
  69. package/dist/esm-node/shared/localisedUrls.mjs +284 -0
  70. package/dist/types/cli/index.d.ts +1 -0
  71. package/dist/types/runtime/I18nLink.d.ts +6 -0
  72. package/dist/types/runtime/Link.d.ts +66 -0
  73. package/dist/types/runtime/canonicalRoutes.d.ts +60 -0
  74. package/dist/types/runtime/context.d.ts +3 -0
  75. package/dist/types/runtime/hooks.d.ts +4 -2
  76. package/dist/types/runtime/i18n/backend/defaults.node.d.ts +3 -2
  77. package/dist/types/runtime/i18n/backend/middleware.node.d.ts +1 -1
  78. package/dist/types/runtime/i18n/instance.d.ts +4 -6
  79. package/dist/types/runtime/i18n/react-i18next.d.ts +7 -0
  80. package/dist/types/runtime/index.d.ts +6 -1
  81. package/dist/types/runtime/localizedPaths.d.ts +39 -0
  82. package/dist/types/runtime/routerAdapter.d.ts +26 -0
  83. package/dist/types/runtime/types.d.ts +1 -1
  84. package/dist/types/runtime/utils.d.ts +13 -9
  85. package/dist/types/server/index.d.ts +6 -0
  86. package/dist/types/shared/localisedUrls.d.ts +36 -0
  87. package/dist/types/shared/type.d.ts +14 -0
  88. package/package.json +24 -24
  89. package/rstest.config.mts +44 -0
  90. package/src/cli/index.ts +44 -1
  91. package/src/runtime/I18nLink.tsx +14 -51
  92. package/src/runtime/Link.tsx +430 -0
  93. package/src/runtime/canonicalRoutes.ts +93 -0
  94. package/src/runtime/context.tsx +45 -7
  95. package/src/runtime/hooks.ts +13 -4
  96. package/src/runtime/i18n/backend/defaults.node.ts +40 -2
  97. package/src/runtime/i18n/backend/defaults.ts +3 -1
  98. package/src/runtime/i18n/backend/middleware.node.ts +1 -1
  99. package/src/runtime/i18n/instance.ts +3 -30
  100. package/src/runtime/i18n/react-i18next.ts +25 -0
  101. package/src/runtime/i18n/utils.ts +4 -26
  102. package/src/runtime/index.tsx +47 -12
  103. package/src/runtime/localizedPaths.ts +107 -0
  104. package/src/runtime/routerAdapter.tsx +332 -0
  105. package/src/runtime/types.ts +1 -1
  106. package/src/runtime/utils.ts +33 -38
  107. package/src/server/index.ts +108 -11
  108. package/src/shared/localisedUrls.ts +623 -0
  109. package/src/shared/type.ts +14 -0
  110. package/tests/backendDefaults.test.ts +51 -0
  111. package/tests/i18nUtils.test.ts +59 -0
  112. package/tests/link.test.tsx +525 -0
  113. package/tests/linkTypes.test.ts +28 -0
  114. package/tests/localisedUrls.test.ts +536 -0
  115. package/tests/routerAdapter.test.tsx +456 -0
  116. package/tests/type-fixture/linkTypes.fixture.tsx +51 -0
  117. package/tests/type-fixture/tsconfig.json +15 -0
  118. package/dist/esm/rslib-runtime.mjs +0 -18
  119. package/dist/esm-node/rslib-runtime.mjs +0 -19
@@ -0,0 +1,221 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { useMemo } from "react";
3
+ import { useModernI18n } from "./context.mjs";
4
+ import { canonicalPath } from "./localizedPaths.mjs";
5
+ import { useI18nRouterAdapter } from "./routerAdapter.mjs";
6
+ import { buildLocalizedUrl, splitUrlTarget } from "./utils.mjs";
7
+ const EXTERNAL_TARGET_RE = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i;
8
+ const warnedTargets = new Set();
9
+ const warnOnce = (key, message)=>{
10
+ if ('development' !== process.env.NODE_ENV || warnedTargets.has(key)) return;
11
+ warnedTargets.add(key);
12
+ console.warn(message);
13
+ };
14
+ const interpolateRouteParams = (pathname, params)=>{
15
+ if (!/[$:*{]/.test(pathname)) return pathname;
16
+ const resolveParam = (name)=>{
17
+ const value = params?.[name];
18
+ return void 0 === value ? void 0 : String(value);
19
+ };
20
+ const segments = pathname.split('/').map((segment)=>{
21
+ if (!segment) return segment;
22
+ if (segment.startsWith('{-$') && segment.endsWith('}')) {
23
+ const value = resolveParam(segment.slice(3, -1));
24
+ return void 0 === value ? null : encodeURIComponent(value);
25
+ }
26
+ if ('$' === segment || '*' === segment) {
27
+ const value = resolveParam('_splat') ?? resolveParam('*');
28
+ return void 0 === value ? null : value.split('/').map(encodeURIComponent).join('/');
29
+ }
30
+ if (segment.startsWith('$')) {
31
+ const value = resolveParam(segment.slice(1));
32
+ if (void 0 === value) {
33
+ warnOnce(`missing-param:${pathname}:${segment}`, `[plugin-i18n] <Link to="${pathname}"> is missing required param "${segment.slice(1)}".`);
34
+ return segment;
35
+ }
36
+ return encodeURIComponent(value);
37
+ }
38
+ if (segment.startsWith(':')) {
39
+ const optional = segment.endsWith('?');
40
+ const name = segment.slice(1, optional ? -1 : void 0);
41
+ const value = resolveParam(name);
42
+ if (void 0 === value) {
43
+ if (optional) return null;
44
+ warnOnce(`missing-param:${pathname}:${segment}`, `[plugin-i18n] <Link to="${pathname}"> is missing required param "${name}".`);
45
+ return segment;
46
+ }
47
+ return encodeURIComponent(value);
48
+ }
49
+ return segment;
50
+ }).filter((segment)=>null !== segment);
51
+ return segments.join('/') || '/';
52
+ };
53
+ const normalizeSearch = (search, searchFromTo)=>{
54
+ if (search && 'object' == typeof search) {
55
+ const entries = Object.entries(search).filter(([, value])=>null != value);
56
+ const searchObject = Object.fromEntries(entries.map(([key, value])=>[
57
+ key,
58
+ String(value)
59
+ ]));
60
+ const params = new URLSearchParams(searchObject);
61
+ const serialized = params.toString();
62
+ return {
63
+ searchString: serialized ? `?${serialized}` : '',
64
+ searchObject
65
+ };
66
+ }
67
+ const raw = 'string' == typeof search && search ? search : searchFromTo;
68
+ if (!raw) return {
69
+ searchString: '',
70
+ searchObject: void 0
71
+ };
72
+ const searchString = raw.startsWith('?') ? raw : `?${raw}`;
73
+ const searchObject = {};
74
+ new URLSearchParams(searchString).forEach((value, key)=>{
75
+ searchObject[key] = value;
76
+ });
77
+ return {
78
+ searchString,
79
+ searchObject
80
+ };
81
+ };
82
+ const splitActiveProps = (active, activeProps)=>{
83
+ if (!active || !activeProps) return {};
84
+ return activeProps;
85
+ };
86
+ const mergeClassNames = (...values)=>{
87
+ const classNames = values.filter((value)=>'string' == typeof value && value.length > 0);
88
+ return classNames.length > 0 ? classNames.join(' ') : void 0;
89
+ };
90
+ const Link = (props)=>{
91
+ const { to, params, children, hash: hashProp, search: searchProp, hashScrollIntoView, activeOptions, activeProps, prefetch, preload, ...rest } = props;
92
+ const adapter = useI18nRouterAdapter();
93
+ const { language, supportedLanguages, localisedUrls } = useModernI18n();
94
+ const config = {
95
+ languages: supportedLanguages,
96
+ localisedUrls
97
+ };
98
+ const isExternal = EXTERNAL_TARGET_RE.test(to);
99
+ const isBareHash = to.startsWith('#');
100
+ const target = useMemo(()=>{
101
+ if (isExternal || isBareHash) return null;
102
+ const { pathname, search: searchFromTo, hash: hashFromTo } = splitUrlTarget(to);
103
+ const interpolated = interpolateRouteParams(pathname || '/', params);
104
+ const firstSegment = interpolated.split('/').filter(Boolean)[0];
105
+ if (firstSegment && supportedLanguages.includes(firstSegment)) warnOnce(`lang-prefix:${to}`, `[plugin-i18n] <Link to="${to}"> starts with a language prefix. Write language-agnostic canonical paths; the Link localizes them automatically.`);
106
+ const localizedPathname = buildLocalizedUrl(interpolated, language, supportedLanguages, localisedUrls);
107
+ const hash = hashProp ?? (hashFromTo ? hashFromTo.slice(1) : '');
108
+ const { searchString, searchObject } = normalizeSearch(searchProp, searchFromTo);
109
+ return {
110
+ canonicalPathname: interpolated,
111
+ localizedPathname,
112
+ hash,
113
+ searchString,
114
+ searchObject,
115
+ href: `${localizedPathname}${searchString}${hash ? `#${hash}` : ''}`
116
+ };
117
+ }, [
118
+ to,
119
+ params,
120
+ hashProp,
121
+ searchProp,
122
+ isExternal,
123
+ isBareHash,
124
+ language,
125
+ supportedLanguages,
126
+ localisedUrls
127
+ ]);
128
+ const isActive = useMemo(()=>{
129
+ if (!target || !adapter.location) return false;
130
+ const current = canonicalPath(adapter.location.pathname, config);
131
+ const targetCanonical = canonicalPath(target.canonicalPathname, config);
132
+ const exact = activeOptions?.exact ?? '/' === targetCanonical;
133
+ if (current === targetCanonical) return true;
134
+ if (exact) return false;
135
+ return current.startsWith('/' === targetCanonical ? '/' : `${targetCanonical}/`);
136
+ }, [
137
+ target,
138
+ adapter.location,
139
+ activeOptions?.exact,
140
+ supportedLanguages,
141
+ localisedUrls
142
+ ]);
143
+ const resolvedActiveProps = splitActiveProps(isActive, activeProps);
144
+ const activeAttributes = isActive ? {
145
+ 'data-status': 'active',
146
+ 'aria-current': rest['aria-current'] ?? resolvedActiveProps['aria-current'] ?? 'page'
147
+ } : {};
148
+ if (!target) {
149
+ const { replace: _replace, ...anchorProps } = rest;
150
+ return /*#__PURE__*/ jsx("a", {
151
+ href: to,
152
+ ...anchorProps,
153
+ children: children
154
+ });
155
+ }
156
+ const { Link: RouterLink, hasRouter, framework } = adapter;
157
+ if (!hasRouter || !RouterLink) {
158
+ const { replace: _replace, ...anchorProps } = rest;
159
+ const { className: activeClassName, style: activeStyle, ...activeRest } = resolvedActiveProps;
160
+ return /*#__PURE__*/ jsx("a", {
161
+ href: target.href,
162
+ ...anchorProps,
163
+ ...activeRest,
164
+ ...activeAttributes,
165
+ className: mergeClassNames(rest.className, activeClassName),
166
+ style: {
167
+ ...rest.style,
168
+ ...activeStyle
169
+ },
170
+ children: children
171
+ });
172
+ }
173
+ const { className: activeClassName, style: activeStyle, ...activeRest } = resolvedActiveProps;
174
+ const mergedClassName = mergeClassNames(rest.className, activeClassName);
175
+ const mergedStyle = {
176
+ ...rest.style,
177
+ ...activeStyle
178
+ };
179
+ if ('tanstack' === framework) {
180
+ const tanstackPreload = void 0 !== preload ? preload : void 0 === prefetch ? void 0 : 'none' === prefetch ? false : prefetch;
181
+ return /*#__PURE__*/ jsx(RouterLink, {
182
+ to: target.localizedPathname,
183
+ ...target.searchObject ? {
184
+ search: target.searchObject
185
+ } : {},
186
+ ...target.hash ? {
187
+ hash: target.hash
188
+ } : {},
189
+ ...void 0 === hashScrollIntoView ? {} : {
190
+ hashScrollIntoView
191
+ },
192
+ ...void 0 === tanstackPreload ? {} : {
193
+ preload: tanstackPreload
194
+ },
195
+ ...rest,
196
+ ...activeRest,
197
+ ...activeAttributes,
198
+ className: mergedClassName,
199
+ style: mergedStyle,
200
+ children: children
201
+ });
202
+ }
203
+ return /*#__PURE__*/ jsx(RouterLink, {
204
+ to: target.href,
205
+ ...void 0 === prefetch ? {} : {
206
+ prefetch
207
+ },
208
+ ...void 0 === preload ? {} : {
209
+ preload
210
+ },
211
+ ...rest,
212
+ ...activeRest,
213
+ ...activeAttributes,
214
+ className: mergedClassName,
215
+ style: mergedStyle,
216
+ children: children
217
+ });
218
+ };
219
+ const runtime_Link = Link;
220
+ export default runtime_Link;
221
+ export { Link, interpolateRouteParams };
File without changes
@@ -1,8 +1,9 @@
1
1
  import { jsx } from "react/jsx-runtime";
2
2
  import { isBrowser } from "@modern-js/runtime";
3
- import { createContext, useCallback, useContext, useMemo } from "react";
3
+ import { createContext, useCallback, useContext, useEffect, useMemo } from "react";
4
4
  import { cacheUserLanguage } from "./i18n/detection/index.mjs";
5
- import { buildLocalizedUrl, detectLanguageFromPath, getEntryPath, shouldIgnoreRedirect, useRouterHooks } from "./utils.mjs";
5
+ import { useI18nRouterAdapter } from "./routerAdapter.mjs";
6
+ import { buildLocalizedUrl, detectLanguageFromPath, getEntryPath, shouldIgnoreRedirect } from "./utils.mjs";
6
7
  const ModernI18nContext = /*#__PURE__*/ createContext(null);
7
8
  const ModernI18nProvider = ({ children, value })=>/*#__PURE__*/ jsx(ModernI18nContext.Provider, {
8
9
  value: value,
@@ -11,9 +12,33 @@ const ModernI18nProvider = ({ children, value })=>/*#__PURE__*/ jsx(ModernI18nCo
11
12
  const useModernI18n = ()=>{
12
13
  const context = useContext(ModernI18nContext);
13
14
  if (!context) throw new Error('useModernI18n must be used within a ModernI18nProvider');
14
- const { language: contextLanguage, i18nInstance, languages, localePathRedirect, ignoreRedirectRoutes, updateLanguage } = context;
15
- const { navigate, location, hasRouter } = useRouterHooks();
16
- const currentLanguage = contextLanguage;
15
+ const { language: contextLanguage, i18nInstance, languages, localePathRedirect, ignoreRedirectRoutes, localisedUrls, updateLanguage } = context;
16
+ const { navigate, location, hasRouter } = useI18nRouterAdapter();
17
+ const pathLanguage = useMemo(()=>{
18
+ if (!localePathRedirect || !location?.pathname) return;
19
+ const detected = detectLanguageFromPath(location.pathname, languages || [], localePathRedirect);
20
+ return detected.detected ? detected.language : void 0;
21
+ }, [
22
+ languages,
23
+ localePathRedirect,
24
+ location?.pathname
25
+ ]);
26
+ const currentLanguage = pathLanguage || contextLanguage;
27
+ useEffect(()=>{
28
+ if (!pathLanguage || pathLanguage === contextLanguage) return;
29
+ updateLanguage?.(pathLanguage);
30
+ i18nInstance?.setLang?.(pathLanguage);
31
+ i18nInstance?.changeLanguage?.(pathLanguage);
32
+ if (isBrowser()) {
33
+ const detectionOptions = i18nInstance.options?.detection;
34
+ cacheUserLanguage(i18nInstance, pathLanguage, detectionOptions);
35
+ }
36
+ }, [
37
+ contextLanguage,
38
+ i18nInstance,
39
+ pathLanguage,
40
+ updateLanguage
41
+ ]);
17
42
  const changeLanguage = useCallback(async (newLang)=>{
18
43
  try {
19
44
  if (!newLang || 'string' != typeof newLang) throw new Error('Language must be a non-empty string');
@@ -30,7 +55,7 @@ const useModernI18n = ()=>{
30
55
  const pathLanguage = detectLanguageFromPath(currentPath, languages || [], localePathRedirect);
31
56
  if (pathLanguage.detected && pathLanguage.language === newLang) return;
32
57
  if (!shouldIgnoreRedirect(relativePath, languages || [], ignoreRedirectRoutes)) {
33
- const newPath = buildLocalizedUrl(relativePath, newLang, languages || []);
58
+ const newPath = buildLocalizedUrl(relativePath, newLang, languages || [], localisedUrls);
34
59
  const newUrl = entryPath + newPath + location.search + location.hash;
35
60
  await navigate(newUrl, {
36
61
  replace: true
@@ -43,7 +68,7 @@ const useModernI18n = ()=>{
43
68
  const pathLanguage = detectLanguageFromPath(currentPath, languages || [], localePathRedirect);
44
69
  if (pathLanguage.detected && pathLanguage.language === newLang) return;
45
70
  if (!shouldIgnoreRedirect(relativePath, languages || [], ignoreRedirectRoutes)) {
46
- const newPath = buildLocalizedUrl(relativePath, newLang, languages || []);
71
+ const newPath = buildLocalizedUrl(relativePath, newLang, languages || [], localisedUrls);
47
72
  const newUrl = entryPath + newPath + window.location.search + window.location.hash;
48
73
  window.history.pushState(null, '', newUrl);
49
74
  }
@@ -58,6 +83,7 @@ const useModernI18n = ()=>{
58
83
  updateLanguage,
59
84
  localePathRedirect,
60
85
  ignoreRedirectRoutes,
86
+ localisedUrls,
61
87
  languages,
62
88
  hasRouter,
63
89
  navigate,
@@ -98,6 +124,7 @@ const useModernI18n = ()=>{
98
124
  changeLanguage,
99
125
  i18nInstance,
100
126
  supportedLanguages: languages || [],
127
+ localisedUrls,
101
128
  isLanguageSupported,
102
129
  isResourcesReady
103
130
  };
@@ -2,7 +2,8 @@ import { isBrowser } from "@modern-js/runtime";
2
2
  import { useEffect, useRef } from "react";
3
3
  import { I18N_SDK_RESOURCES_LOADED_EVENT, getI18nSdkBackendId } from "./i18n/backend/sdk-event.mjs";
4
4
  import { cacheUserLanguage } from "./i18n/detection/index.mjs";
5
- import { buildLocalizedUrl, detectLanguageFromPath, getEntryPath, getPathname, shouldIgnoreRedirect, useRouterHooks } from "./utils.mjs";
5
+ import { useI18nRouterAdapter } from "./routerAdapter.mjs";
6
+ import { buildLocalizedUrl, detectLanguageFromPath, getEntryPath, getPathname, shouldIgnoreRedirect } from "./utils.mjs";
6
7
  function createMinimalI18nInstance(language) {
7
8
  const minimalInstance = {
8
9
  language,
@@ -14,7 +15,7 @@ function createMinimalI18nInstance(language) {
14
15
  };
15
16
  return minimalInstance;
16
17
  }
17
- function createContextValue(lang, i18nInstance, entryName, languages, localePathRedirect, ignoreRedirectRoutes, setLang) {
18
+ function createContextValue(lang, i18nInstance, entryName, languages, localePathRedirect, ignoreRedirectRoutes, localisedUrls, setLang) {
18
19
  const instance = i18nInstance || createMinimalI18nInstance(lang);
19
20
  return {
20
21
  language: lang,
@@ -23,6 +24,7 @@ function createContextValue(lang, i18nInstance, entryName, languages, localePath
23
24
  languages,
24
25
  localePathRedirect,
25
26
  ignoreRedirectRoutes,
27
+ localisedUrls,
26
28
  updateLanguage: setLang
27
29
  };
28
30
  }
@@ -72,9 +74,9 @@ function useSdkResourcesLoader(i18nInstance, setForceUpdate) {
72
74
  setForceUpdate
73
75
  ]);
74
76
  }
75
- function useClientSideRedirect(i18nInstance, localePathRedirect, languages, fallbackLanguage, ignoreRedirectRoutes) {
77
+ function useClientSideRedirect(i18nInstance, localePathRedirect, languages, fallbackLanguage, ignoreRedirectRoutes, localisedUrls) {
76
78
  const hasRedirectedRef = useRef(false);
77
- const { navigate, location, hasRouter } = useRouterHooks();
79
+ const { navigate, location, hasRouter } = useI18nRouterAdapter();
78
80
  useEffect(()=>{
79
81
  if ('browser' !== process.env.MODERN_TARGET) return;
80
82
  if (!localePathRedirect || !i18nInstance) return;
@@ -93,7 +95,7 @@ function useClientSideRedirect(i18nInstance, localePathRedirect, languages, fall
93
95
  const pathDetection = detectLanguageFromPath(currentPathname, languages, localePathRedirect);
94
96
  if (pathDetection.detected) return;
95
97
  const targetLanguage = i18nInstance.language || fallbackLanguage || languages[0] || 'en';
96
- const newPath = buildLocalizedUrl(relativePath, targetLanguage, languages);
98
+ const newPath = buildLocalizedUrl(relativePath, targetLanguage, languages, localisedUrls);
97
99
  const newUrl = entryPath + newPath + currentSearch + currentHash;
98
100
  if (newUrl !== currentPathname + currentSearch + currentHash) {
99
101
  hasRedirectedRef.current = true;
@@ -110,7 +112,8 @@ function useClientSideRedirect(i18nInstance, localePathRedirect, languages, fall
110
112
  i18nInstance,
111
113
  languages,
112
114
  fallbackLanguage,
113
- ignoreRedirectRoutes
115
+ ignoreRedirectRoutes,
116
+ localisedUrls
114
117
  ]);
115
118
  }
116
119
  function useLanguageSync(i18nInstance, localePathRedirect, languages, runtimeContextRef, prevLangRef, setLang) {
@@ -4,7 +4,7 @@ const DEFAULT_I18NEXT_BACKEND_OPTIONS = {
4
4
  };
5
5
  function convertPath(path) {
6
6
  if (!path) return path;
7
- if (path.startsWith('/')) return `${window.__assetPrefix__ || ''}${path}`;
7
+ if (path.startsWith('/')) return "u" < typeof window ? path : `${window.__assetPrefix__ || ''}${path}`;
8
8
  return path;
9
9
  }
10
10
  function convertBackendOptions(options) {
@@ -1,6 +1,27 @@
1
+ import fs from "fs";
2
+ import path_0 from "path";
3
+ const CONVENTIONAL_LOCALES_DIRS = [
4
+ './locales',
5
+ './config/public/locales'
6
+ ];
7
+ const isDirectory = (dirPath)=>{
8
+ try {
9
+ return fs.statSync(dirPath).isDirectory();
10
+ } catch {
11
+ return false;
12
+ }
13
+ };
14
+ const resolveDefaultLocalesDir = (cwd = process.cwd())=>{
15
+ for (const dir of CONVENTIONAL_LOCALES_DIRS)if (isDirectory(path_0.resolve(cwd, dir))) return dir;
16
+ return CONVENTIONAL_LOCALES_DIRS[0];
17
+ };
1
18
  const DEFAULT_I18NEXT_BACKEND_OPTIONS = {
2
- loadPath: './locales/{{lng}}/{{ns}}.json',
3
- addPath: './locales/{{lng}}/{{ns}}.json'
19
+ get loadPath () {
20
+ return `${resolveDefaultLocalesDir()}/{{lng}}/{{ns}}.json`;
21
+ },
22
+ get addPath () {
23
+ return `${resolveDefaultLocalesDir()}/{{lng}}/{{ns}}.json`;
24
+ }
4
25
  };
5
26
  function convertPath(path) {
6
27
  if (!path) return path;
@@ -16,4 +37,4 @@ function convertBackendOptions(options) {
16
37
  if (converted.addPath) converted.addPath = convertPath(converted.addPath);
17
38
  return converted;
18
39
  }
19
- export { DEFAULT_I18NEXT_BACKEND_OPTIONS, convertBackendOptions };
40
+ export { DEFAULT_I18NEXT_BACKEND_OPTIONS, convertBackendOptions, resolveDefaultLocalesDir };
@@ -1,8 +1,8 @@
1
- import i18next_fs_backend from "i18next-fs-backend";
1
+ import cjs from "i18next-fs-backend/cjs";
2
2
  import { useI18nextBackendCommon } from "./middleware.common.mjs";
3
- class FsBackendWithSave extends i18next_fs_backend {
3
+ class FsBackendWithSave extends cjs {
4
4
  save(_language, _namespace, _data) {}
5
5
  }
6
6
  const HttpBackendWithSave = FsBackendWithSave;
7
- const useI18nextBackend = (i18nInstance, backend)=>useI18nextBackendCommon(i18nInstance, FsBackendWithSave, i18next_fs_backend, backend);
7
+ const useI18nextBackend = (i18nInstance, backend)=>useI18nextBackendCommon(i18nInstance, FsBackendWithSave, cjs, backend);
8
8
  export { FsBackendWithSave, HttpBackendWithSave, useI18nextBackend };
@@ -40,14 +40,6 @@ async function createI18nextInstance() {
40
40
  return null;
41
41
  }
42
42
  }
43
- async function tryImportReactI18next() {
44
- try {
45
- const reactI18next = await import("react-i18next");
46
- return reactI18next;
47
- } catch (error) {
48
- return null;
49
- }
50
- }
51
43
  function getI18nextInstanceForProvider(instance) {
52
44
  if (isI18nWrapperInstance(instance)) {
53
45
  const i18nextInstance = getI18nWrapperI18nextInstance(instance);
@@ -64,14 +56,4 @@ async function getI18nInstance(userInstance) {
64
56
  if (i18nextInstance) return i18nextInstance;
65
57
  throw new Error('No i18n instance found');
66
58
  }
67
- async function getInitReactI18next() {
68
- const reactI18nextModule = await tryImportReactI18next();
69
- if (reactI18nextModule) return reactI18nextModule.initReactI18next;
70
- return null;
71
- }
72
- async function getI18nextProvider() {
73
- const reactI18nextModule = await tryImportReactI18next();
74
- if (reactI18nextModule) return reactI18nextModule.I18nextProvider;
75
- return null;
76
- }
77
- export { getActualI18nextInstance, getI18nInstance, getI18nWrapperI18nextInstance, getI18nextInstanceForProvider, getI18nextProvider, getInitReactI18next, isI18nInstance, isI18nWrapperInstance };
59
+ export { getActualI18nextInstance, getI18nInstance, getI18nWrapperI18nextInstance, getI18nextInstanceForProvider, isI18nInstance, isI18nWrapperInstance };
@@ -0,0 +1,15 @@
1
+ async function tryImportReactI18next() {
2
+ try {
3
+ return await import("react-i18next");
4
+ } catch (error) {
5
+ return null;
6
+ }
7
+ }
8
+ async function getReactI18nextIntegration() {
9
+ const reactI18nextModule = await tryImportReactI18next();
10
+ return {
11
+ I18nextProvider: reactI18nextModule?.I18nextProvider ?? null,
12
+ initReactI18next: reactI18nextModule?.initReactI18next ?? null
13
+ };
14
+ }
15
+ export { getReactI18nextIntegration };
@@ -100,18 +100,6 @@ const initializeI18nInstance = async (i18nInstance, finalLanguage, fallbackLangu
100
100
  };
101
101
  }
102
102
  }
103
- if (mergedBackend && hasOptions(i18nInstance)) {
104
- const defaultNS = initOptions.defaultNS || initOptions.ns || 'translation';
105
- const ns = Array.isArray(defaultNS) ? defaultNS[0] : defaultNS;
106
- let retries = 20;
107
- while(retries > 0){
108
- const actualInstance = getActualI18nextInstance(i18nInstance);
109
- const store = actualInstance.store;
110
- if (store?.data?.[finalLanguage]?.[ns]) break;
111
- await new Promise((resolve)=>setTimeout(resolve, 100));
112
- retries--;
113
- }
114
- }
115
103
  }
116
104
  };
117
105
  function hasOptions(instance) {
@@ -10,26 +10,31 @@ import { mergeBackendOptions } from "./i18n/backend/index.mjs";
10
10
  import { useI18nextBackend } from "./i18n/backend/middleware.mjs";
11
11
  import { detectLanguageWithPriority, exportServerLngToWindow, mergeDetectionOptions } from "./i18n/detection/index.mjs";
12
12
  import { useI18nextLanguageDetector } from "./i18n/detection/middleware.mjs";
13
- import { getI18nextInstanceForProvider, getI18nextProvider, getInitReactI18next } from "./i18n/instance.mjs";
13
+ import { getI18nextInstanceForProvider } from "./i18n/instance.mjs";
14
14
  import { changeI18nLanguage, ensureLanguageMatch, initializeI18nInstance, setupClonedInstance } from "./i18n/utils.mjs";
15
- import { getPathname } from "./utils.mjs";
15
+ import { buildLocalizedUrl, getPathname, splitUrlTarget } from "./utils.mjs";
16
16
  import "./types.mjs";
17
17
  const i18nPlugin = (options)=>({
18
18
  name: '@modern-js/plugin-i18n',
19
19
  setup: (api)=>{
20
- const { entryName, i18nInstance: userI18nInstance, initOptions, localeDetection, backend, htmlLangAttr = false } = options;
21
- const { localePathRedirect = false, i18nextDetector = true, languages = [], fallbackLanguage = 'en', detection, ignoreRedirectRoutes } = localeDetection || {};
20
+ const { entryName, i18nInstance: userI18nInstance, initOptions, localeDetection, backend, htmlLangAttr = false, reactI18next = true } = options;
21
+ const { localePathRedirect = false, i18nextDetector = true, languages = [], fallbackLanguage = 'en', detection, ignoreRedirectRoutes, localisedUrls } = localeDetection || {};
22
22
  const { enabled: backendEnabled = false } = backend || {};
23
23
  let latestI18nInstance;
24
24
  let I18nextProvider;
25
+ const loadReactI18nextIntegration = async ()=>{
26
+ if (!reactI18next) return null;
27
+ const { getReactI18nextIntegration } = await import("./i18n/react-i18next.mjs");
28
+ return getReactI18nextIntegration();
29
+ };
25
30
  api.onBeforeRender(async (context)=>{
26
31
  let i18nInstance = await getI18nInstance(userI18nInstance);
27
32
  const { i18n: otherConfig } = api.getRuntimeConfig();
28
33
  const { initOptions: otherInitOptions } = otherConfig || {};
29
34
  const userInitOptions = merge(otherInitOptions || {}, initOptions || {});
30
- const initReactI18next = await getInitReactI18next();
31
- I18nextProvider = await getI18nextProvider();
32
- if (initReactI18next) i18nInstance.use(initReactI18next);
35
+ const reactI18nextIntegration = await loadReactI18nextIntegration();
36
+ I18nextProvider = reactI18nextIntegration?.I18nextProvider ?? null;
37
+ if (reactI18nextIntegration?.initReactI18next) i18nInstance.use(reactI18nextIntegration.initReactI18next);
33
38
  const pathname = getPathname(context);
34
39
  if (i18nextDetector) useI18nextLanguageDetector(i18nInstance);
35
40
  const mergedDetection = mergeDetectionOptions(i18nextDetector, detection, localePathRedirect, userInitOptions);
@@ -89,16 +94,18 @@ const i18nPlugin = (options)=>({
89
94
  ]);
90
95
  useSdkResourcesLoader(i18nInstance, setForceUpdate);
91
96
  useLanguageSync(i18nInstance, localePathRedirect, languages, runtimeContextRef, prevLangRef, setLang);
92
- useClientSideRedirect(i18nInstance, localePathRedirect, languages, fallbackLanguage, ignoreRedirectRoutes);
93
- const contextValue = useMemo(()=>createContextValue(lang, i18nInstance, entryName, languages, localePathRedirect, ignoreRedirectRoutes, setLang), [
97
+ useClientSideRedirect(i18nInstance, localePathRedirect, languages, fallbackLanguage, ignoreRedirectRoutes, localisedUrls);
98
+ const contextValue = useMemo(()=>createContextValue(lang, i18nInstance, entryName, languages, localePathRedirect, ignoreRedirectRoutes, localisedUrls, setLang), [
94
99
  lang,
95
100
  i18nInstance,
96
101
  entryName,
97
102
  languages,
98
103
  localePathRedirect,
99
104
  ignoreRedirectRoutes,
105
+ localisedUrls,
100
106
  forceUpdate
101
107
  ]);
108
+ const children = props.children;
102
109
  const appContent = /*#__PURE__*/ jsxs(Fragment, {
103
110
  children: [
104
111
  Boolean(htmlLangAttr) && /*#__PURE__*/ jsx(Helmet, {
@@ -108,9 +115,10 @@ const i18nPlugin = (options)=>({
108
115
  }),
109
116
  /*#__PURE__*/ jsx(ModernI18nProvider, {
110
117
  value: contextValue,
111
- children: /*#__PURE__*/ jsx(App, {
112
- ...props
113
- })
118
+ children: App ? /*#__PURE__*/ jsx(App, {
119
+ ...props,
120
+ children: children
121
+ }) : children
114
122
  })
115
123
  ]
116
124
  });
@@ -128,5 +136,7 @@ const i18nPlugin = (options)=>({
128
136
  });
129
137
  const runtime = i18nPlugin;
130
138
  export { I18nLink } from "./I18nLink.mjs";
139
+ export { Link } from "./Link.mjs";
140
+ export { canonicalPath, localizePath, useLocalizedLocation, useLocalizedPaths } from "./localizedPaths.mjs";
131
141
  export default runtime;
132
- export { i18nPlugin, useModernI18n };
142
+ export { buildLocalizedUrl, i18nPlugin, splitUrlTarget, useModernI18n };
@@ -0,0 +1,55 @@
1
+ import { useMemo } from "react";
2
+ import { canonicalTargetPathname } from "../shared/localisedUrls.mjs";
3
+ import { useModernI18n } from "./context.mjs";
4
+ import { useI18nRouterAdapter } from "./routerAdapter.mjs";
5
+ import { buildLocalizedUrl, splitUrlTarget } from "./utils.mjs";
6
+ const localizePath = (pathname, language, config)=>buildLocalizedUrl(pathname, language, config.languages, config.localisedUrls);
7
+ const canonicalPath = (target, config)=>{
8
+ const { pathname, search, hash } = splitUrlTarget(target);
9
+ const resolvedPath = canonicalTargetPathname(pathname, config.languages, config.localisedUrls);
10
+ return `${resolvedPath}${search}${hash}`;
11
+ };
12
+ const useLocalizedPaths = ()=>{
13
+ const { supportedLanguages, localisedUrls } = useModernI18n();
14
+ return useMemo(()=>{
15
+ const config = {
16
+ languages: supportedLanguages,
17
+ localisedUrls
18
+ };
19
+ return {
20
+ localizePath: (pathname, language)=>localizePath(pathname, language, config),
21
+ canonicalPath: (pathname)=>canonicalPath(pathname, config)
22
+ };
23
+ }, [
24
+ supportedLanguages,
25
+ localisedUrls
26
+ ]);
27
+ };
28
+ const useLocalizedLocation = ()=>{
29
+ const { language, supportedLanguages, localisedUrls } = useModernI18n();
30
+ const { location } = useI18nRouterAdapter();
31
+ const pathname = location?.pathname ?? '/';
32
+ const search = location?.search ?? '';
33
+ const hash = location?.hash ?? '';
34
+ return useMemo(()=>{
35
+ const config = {
36
+ languages: supportedLanguages,
37
+ localisedUrls
38
+ };
39
+ const alternates = {};
40
+ for (const supportedLanguage of supportedLanguages)alternates[supportedLanguage] = `${localizePath(pathname, supportedLanguage, config)}${search}${hash}`;
41
+ return {
42
+ language,
43
+ canonical: canonicalPath(pathname, config),
44
+ alternates
45
+ };
46
+ }, [
47
+ language,
48
+ supportedLanguages,
49
+ localisedUrls,
50
+ pathname,
51
+ search,
52
+ hash
53
+ ]);
54
+ };
55
+ export { canonicalPath, localizePath, useLocalizedLocation, useLocalizedPaths };