@bleedingdev/modern-js-plugin-i18n 3.2.0-ultramodern.0

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 (147) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +32 -0
  3. package/dist/cjs/cli/index.js +154 -0
  4. package/dist/cjs/runtime/I18nLink.js +68 -0
  5. package/dist/cjs/runtime/context.js +142 -0
  6. package/dist/cjs/runtime/hooks.js +194 -0
  7. package/dist/cjs/runtime/i18n/backend/config.js +39 -0
  8. package/dist/cjs/runtime/i18n/backend/defaults.js +56 -0
  9. package/dist/cjs/runtime/i18n/backend/defaults.node.js +56 -0
  10. package/dist/cjs/runtime/i18n/backend/index.js +108 -0
  11. package/dist/cjs/runtime/i18n/backend/middleware.common.js +105 -0
  12. package/dist/cjs/runtime/i18n/backend/middleware.js +54 -0
  13. package/dist/cjs/runtime/i18n/backend/middleware.node.js +58 -0
  14. package/dist/cjs/runtime/i18n/backend/sdk-backend.js +175 -0
  15. package/dist/cjs/runtime/i18n/backend/sdk-event.js +64 -0
  16. package/dist/cjs/runtime/i18n/detection/config.js +63 -0
  17. package/dist/cjs/runtime/i18n/detection/index.js +309 -0
  18. package/dist/cjs/runtime/i18n/detection/middleware.js +185 -0
  19. package/dist/cjs/runtime/i18n/detection/middleware.node.js +74 -0
  20. package/dist/cjs/runtime/i18n/index.js +43 -0
  21. package/dist/cjs/runtime/i18n/instance.js +132 -0
  22. package/dist/cjs/runtime/i18n/utils.js +189 -0
  23. package/dist/cjs/runtime/index.js +174 -0
  24. package/dist/cjs/runtime/types.js +18 -0
  25. package/dist/cjs/runtime/utils.js +136 -0
  26. package/dist/cjs/server/index.js +218 -0
  27. package/dist/cjs/shared/deepMerge.js +54 -0
  28. package/dist/cjs/shared/detection.js +105 -0
  29. package/dist/cjs/shared/type.js +18 -0
  30. package/dist/cjs/shared/utils.js +78 -0
  31. package/dist/esm/cli/index.mjs +107 -0
  32. package/dist/esm/rslib-runtime.mjs +18 -0
  33. package/dist/esm/runtime/I18nLink.mjs +32 -0
  34. package/dist/esm/runtime/context.mjs +105 -0
  35. package/dist/esm/runtime/hooks.mjs +151 -0
  36. package/dist/esm/runtime/i18n/backend/config.mjs +5 -0
  37. package/dist/esm/runtime/i18n/backend/defaults.mjs +19 -0
  38. package/dist/esm/runtime/i18n/backend/defaults.node.mjs +19 -0
  39. package/dist/esm/runtime/i18n/backend/index.mjs +74 -0
  40. package/dist/esm/runtime/i18n/backend/middleware.common.mjs +61 -0
  41. package/dist/esm/runtime/i18n/backend/middleware.mjs +7 -0
  42. package/dist/esm/runtime/i18n/backend/middleware.node.mjs +8 -0
  43. package/dist/esm/runtime/i18n/backend/sdk-backend.mjs +141 -0
  44. package/dist/esm/runtime/i18n/backend/sdk-event.mjs +21 -0
  45. package/dist/esm/runtime/i18n/detection/config.mjs +26 -0
  46. package/dist/esm/runtime/i18n/detection/index.mjs +260 -0
  47. package/dist/esm/runtime/i18n/detection/middleware.mjs +132 -0
  48. package/dist/esm/runtime/i18n/detection/middleware.node.mjs +31 -0
  49. package/dist/esm/runtime/i18n/index.mjs +2 -0
  50. package/dist/esm/runtime/i18n/instance.mjs +77 -0
  51. package/dist/esm/runtime/i18n/utils.mjs +140 -0
  52. package/dist/esm/runtime/index.mjs +132 -0
  53. package/dist/esm/runtime/types.mjs +0 -0
  54. package/dist/esm/runtime/utils.mjs +75 -0
  55. package/dist/esm/server/index.mjs +182 -0
  56. package/dist/esm/shared/deepMerge.mjs +20 -0
  57. package/dist/esm/shared/detection.mjs +71 -0
  58. package/dist/esm/shared/type.mjs +0 -0
  59. package/dist/esm/shared/utils.mjs +35 -0
  60. package/dist/esm-node/cli/index.mjs +108 -0
  61. package/dist/esm-node/rslib-runtime.mjs +19 -0
  62. package/dist/esm-node/runtime/I18nLink.mjs +33 -0
  63. package/dist/esm-node/runtime/context.mjs +106 -0
  64. package/dist/esm-node/runtime/hooks.mjs +152 -0
  65. package/dist/esm-node/runtime/i18n/backend/config.mjs +6 -0
  66. package/dist/esm-node/runtime/i18n/backend/defaults.mjs +20 -0
  67. package/dist/esm-node/runtime/i18n/backend/defaults.node.mjs +20 -0
  68. package/dist/esm-node/runtime/i18n/backend/index.mjs +75 -0
  69. package/dist/esm-node/runtime/i18n/backend/middleware.common.mjs +62 -0
  70. package/dist/esm-node/runtime/i18n/backend/middleware.mjs +8 -0
  71. package/dist/esm-node/runtime/i18n/backend/middleware.node.mjs +9 -0
  72. package/dist/esm-node/runtime/i18n/backend/sdk-backend.mjs +142 -0
  73. package/dist/esm-node/runtime/i18n/backend/sdk-event.mjs +22 -0
  74. package/dist/esm-node/runtime/i18n/detection/config.mjs +27 -0
  75. package/dist/esm-node/runtime/i18n/detection/index.mjs +261 -0
  76. package/dist/esm-node/runtime/i18n/detection/middleware.mjs +133 -0
  77. package/dist/esm-node/runtime/i18n/detection/middleware.node.mjs +32 -0
  78. package/dist/esm-node/runtime/i18n/index.mjs +3 -0
  79. package/dist/esm-node/runtime/i18n/instance.mjs +78 -0
  80. package/dist/esm-node/runtime/i18n/utils.mjs +141 -0
  81. package/dist/esm-node/runtime/index.mjs +133 -0
  82. package/dist/esm-node/runtime/types.mjs +1 -0
  83. package/dist/esm-node/runtime/utils.mjs +76 -0
  84. package/dist/esm-node/server/index.mjs +183 -0
  85. package/dist/esm-node/shared/deepMerge.mjs +21 -0
  86. package/dist/esm-node/shared/detection.mjs +72 -0
  87. package/dist/esm-node/shared/type.mjs +1 -0
  88. package/dist/esm-node/shared/utils.mjs +36 -0
  89. package/dist/types/cli/index.d.ts +21 -0
  90. package/dist/types/runtime/I18nLink.d.ts +8 -0
  91. package/dist/types/runtime/context.d.ts +38 -0
  92. package/dist/types/runtime/hooks.d.ts +28 -0
  93. package/dist/types/runtime/i18n/backend/config.d.ts +2 -0
  94. package/dist/types/runtime/i18n/backend/defaults.d.ts +13 -0
  95. package/dist/types/runtime/i18n/backend/defaults.node.d.ts +8 -0
  96. package/dist/types/runtime/i18n/backend/index.d.ts +3 -0
  97. package/dist/types/runtime/i18n/backend/middleware.common.d.ts +14 -0
  98. package/dist/types/runtime/i18n/backend/middleware.d.ts +12 -0
  99. package/dist/types/runtime/i18n/backend/middleware.node.d.ts +13 -0
  100. package/dist/types/runtime/i18n/backend/sdk-backend.d.ts +53 -0
  101. package/dist/types/runtime/i18n/backend/sdk-event.d.ts +9 -0
  102. package/dist/types/runtime/i18n/detection/config.d.ts +11 -0
  103. package/dist/types/runtime/i18n/detection/index.d.ts +50 -0
  104. package/dist/types/runtime/i18n/detection/middleware.d.ts +24 -0
  105. package/dist/types/runtime/i18n/detection/middleware.node.d.ts +17 -0
  106. package/dist/types/runtime/i18n/index.d.ts +3 -0
  107. package/dist/types/runtime/i18n/instance.d.ts +93 -0
  108. package/dist/types/runtime/i18n/utils.d.ts +29 -0
  109. package/dist/types/runtime/index.d.ts +20 -0
  110. package/dist/types/runtime/types.d.ts +15 -0
  111. package/dist/types/runtime/utils.d.ts +33 -0
  112. package/dist/types/server/index.d.ts +8 -0
  113. package/dist/types/shared/deepMerge.d.ts +1 -0
  114. package/dist/types/shared/detection.d.ts +11 -0
  115. package/dist/types/shared/type.d.ts +156 -0
  116. package/dist/types/shared/utils.d.ts +5 -0
  117. package/package.json +136 -0
  118. package/rslib.config.mts +4 -0
  119. package/src/cli/index.ts +245 -0
  120. package/src/runtime/I18nLink.tsx +76 -0
  121. package/src/runtime/context.tsx +281 -0
  122. package/src/runtime/hooks.ts +298 -0
  123. package/src/runtime/i18n/backend/config.ts +10 -0
  124. package/src/runtime/i18n/backend/defaults.node.ts +31 -0
  125. package/src/runtime/i18n/backend/defaults.ts +37 -0
  126. package/src/runtime/i18n/backend/index.ts +181 -0
  127. package/src/runtime/i18n/backend/middleware.common.ts +116 -0
  128. package/src/runtime/i18n/backend/middleware.node.ts +32 -0
  129. package/src/runtime/i18n/backend/middleware.ts +28 -0
  130. package/src/runtime/i18n/backend/sdk-backend.ts +306 -0
  131. package/src/runtime/i18n/backend/sdk-event.ts +39 -0
  132. package/src/runtime/i18n/detection/config.ts +32 -0
  133. package/src/runtime/i18n/detection/index.ts +641 -0
  134. package/src/runtime/i18n/detection/middleware.node.ts +84 -0
  135. package/src/runtime/i18n/detection/middleware.ts +251 -0
  136. package/src/runtime/i18n/index.ts +8 -0
  137. package/src/runtime/i18n/instance.ts +227 -0
  138. package/src/runtime/i18n/utils.ts +351 -0
  139. package/src/runtime/index.tsx +285 -0
  140. package/src/runtime/types.ts +17 -0
  141. package/src/runtime/utils.ts +163 -0
  142. package/src/server/index.ts +406 -0
  143. package/src/shared/deepMerge.ts +38 -0
  144. package/src/shared/detection.ts +131 -0
  145. package/src/shared/type.ts +170 -0
  146. package/src/shared/utils.ts +82 -0
  147. package/tsconfig.json +13 -0
@@ -0,0 +1,218 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ default: ()=>server,
28
+ i18nServerPlugin: ()=>i18nServerPlugin
29
+ });
30
+ const hono_namespaceObject = require("@modern-js/server-core/hono");
31
+ const config_js_namespaceObject = require("../runtime/i18n/detection/config.js");
32
+ const utils_js_namespaceObject = require("../shared/utils.js");
33
+ const { languageDetector } = hono_namespaceObject;
34
+ const convertToHonoLanguageDetectorOptions = (languages, fallbackLanguage, detectionOptions)=>{
35
+ const mergedDetection = detectionOptions ? (0, config_js_namespaceObject.mergeDetectionOptions)(detectionOptions) : config_js_namespaceObject.DEFAULT_I18NEXT_DETECTION_OPTIONS;
36
+ const order = (mergedDetection.order || []).filter((item)=>![
37
+ 'path',
38
+ 'localStorage',
39
+ 'navigator',
40
+ 'htmlTag',
41
+ 'subdomain'
42
+ ].includes(item));
43
+ const detectionOrder = order.length > 0 ? order : [
44
+ 'querystring',
45
+ 'cookie',
46
+ 'header'
47
+ ];
48
+ const honoOrder = detectionOrder.map((item)=>{
49
+ if ('querystring' === item) return 'querystring';
50
+ if ('cookie' === item) return 'cookie';
51
+ if ('header' === item) return 'header';
52
+ return item;
53
+ });
54
+ const caches = false === mergedDetection.caches ? false : Array.isArray(mergedDetection.caches) && !mergedDetection.caches.includes('cookie') ? false : [
55
+ 'cookie'
56
+ ];
57
+ const cookieMinutes = mergedDetection.cookieMinutes;
58
+ const cookieMaxAge = 'number' == typeof cookieMinutes && Number.isFinite(cookieMinutes) ? Math.max(0, Math.floor(60 * cookieMinutes)) : 60 * config_js_namespaceObject.DEFAULT_I18NEXT_DETECTION_OPTIONS.cookieMinutes;
59
+ const cookieDomain = mergedDetection.cookieDomain;
60
+ const cookieSecure = mergedDetection.cookieSecure;
61
+ const cookieHttpOnly = mergedDetection.cookieHttpOnly;
62
+ const cookieSameSite = mergedDetection.cookieSameSite;
63
+ const normalizedCookieDomain = 'string' == typeof cookieDomain ? cookieDomain : void 0;
64
+ const cookieOptions = {
65
+ maxAge: cookieMaxAge,
66
+ sameSite: 'None' === cookieSameSite || 'none' === cookieSameSite ? 'None' : 'Lax' === cookieSameSite || 'lax' === cookieSameSite ? 'Lax' : 'Strict',
67
+ secure: 'boolean' == typeof cookieSecure ? cookieSecure : false,
68
+ httpOnly: 'boolean' == typeof cookieHttpOnly ? cookieHttpOnly : false,
69
+ ...normalizedCookieDomain ? {
70
+ domain: normalizedCookieDomain
71
+ } : {}
72
+ };
73
+ return {
74
+ supportedLanguages: languages.length > 0 ? languages : [
75
+ fallbackLanguage
76
+ ],
77
+ fallbackLanguage,
78
+ order: honoOrder,
79
+ lookupQueryString: mergedDetection.lookupQuerystring || config_js_namespaceObject.DEFAULT_I18NEXT_DETECTION_OPTIONS.lookupQuerystring || 'lng',
80
+ lookupCookie: mergedDetection.lookupCookie || config_js_namespaceObject.DEFAULT_I18NEXT_DETECTION_OPTIONS.lookupCookie || 'i18next',
81
+ lookupFromHeaderKey: mergedDetection.lookupHeader || config_js_namespaceObject.DEFAULT_I18NEXT_DETECTION_OPTIONS.lookupHeader || 'accept-language',
82
+ ...void 0 !== caches && {
83
+ caches
84
+ },
85
+ ...false !== caches && {
86
+ cookieOptions
87
+ },
88
+ ignoreCase: true
89
+ };
90
+ };
91
+ const shouldIgnoreRedirect = (pathname, urlPath, ignoreRedirectRoutes)=>{
92
+ if (!ignoreRedirectRoutes) return false;
93
+ const basePath = urlPath.replace('/*', '');
94
+ const remainingPath = pathname.startsWith(basePath) ? pathname.slice(basePath.length) : pathname;
95
+ const normalizedPath = remainingPath.startsWith('/') ? remainingPath : `/${remainingPath}`;
96
+ if ('function' == typeof ignoreRedirectRoutes) return ignoreRedirectRoutes(normalizedPath);
97
+ return ignoreRedirectRoutes.some((pattern)=>normalizedPath === pattern || normalizedPath.startsWith(`${pattern}/`));
98
+ };
99
+ const isStaticResourceRequest = (pathname, staticRoutePrefixes, languages = [])=>{
100
+ if (staticRoutePrefixes.some((prefix)=>pathname.startsWith(`${prefix}/`) || pathname === prefix)) return true;
101
+ const standardStaticPrefixes = [
102
+ '/static/',
103
+ '/upload/'
104
+ ];
105
+ if (standardStaticPrefixes.some((prefix)=>pathname.startsWith(prefix))) return true;
106
+ const pathSegments = pathname.split('/').filter(Boolean);
107
+ if (pathSegments.length > 0 && languages.includes(pathSegments[0])) {
108
+ const pathWithoutLang = '/' + pathSegments.slice(1).join('/');
109
+ if (standardStaticPrefixes.some((prefix)=>pathWithoutLang.startsWith(prefix)) || staticRoutePrefixes.some((prefix)=>pathWithoutLang.startsWith(`${prefix}/`) || pathWithoutLang === prefix)) return true;
110
+ }
111
+ return false;
112
+ };
113
+ const getLanguageFromPath = (req, urlPath, languages)=>{
114
+ const url = new URL(req.url, `http://${req.header().host}`);
115
+ const pathname = url.pathname;
116
+ const basePath = urlPath.replace('/*', '');
117
+ const remainingPath = pathname.startsWith(basePath) ? pathname.slice(basePath.length) : pathname;
118
+ const segments = remainingPath.split('/').filter(Boolean);
119
+ const firstSegment = segments[0];
120
+ if (languages.includes(firstSegment)) return firstSegment;
121
+ return null;
122
+ };
123
+ const buildLocalizedUrl = (req, urlPath, language, languages)=>{
124
+ const url = new URL(req.url);
125
+ const pathname = url.pathname;
126
+ const basePath = urlPath.replace('/*', '');
127
+ const remainingPath = pathname.startsWith(basePath) ? pathname.slice(basePath.length) : pathname;
128
+ const segments = remainingPath.split('/').filter(Boolean);
129
+ if (segments.length > 0 && languages.includes(segments[0])) segments[0] = language;
130
+ else segments.unshift(language);
131
+ const newPathname = `/${segments.join('/')}`;
132
+ const suffix = `${url.search}${url.hash}`;
133
+ const localizedUrl = '/' === basePath ? newPathname + suffix : basePath + newPathname + suffix;
134
+ return localizedUrl;
135
+ };
136
+ const i18nServerPlugin = (options)=>({
137
+ name: '@modern-js/plugin-i18n/server',
138
+ setup: (api)=>{
139
+ api.onPrepare(()=>{
140
+ const { middlewares, routes } = api.getServerContext();
141
+ const entryPaths = new Set();
142
+ routes.forEach((route)=>{
143
+ if (route.entryName && route.urlPath && '/' !== route.urlPath) {
144
+ const pathSegments = route.urlPath.split('/').filter(Boolean);
145
+ if (pathSegments.length > 0) entryPaths.add(`/${pathSegments[0]}`);
146
+ }
147
+ });
148
+ routes.map((route)=>{
149
+ const { entryName } = route;
150
+ if (!entryName) return;
151
+ if (!options.localeDetection) return;
152
+ const { localePathRedirect, i18nextDetector = true, languages = [], fallbackLanguage = 'en', detection, ignoreRedirectRoutes } = (0, utils_js_namespaceObject.getLocaleDetectionOptions)(entryName, options.localeDetection);
153
+ const staticRoutePrefixes = options.staticRoutePrefixes;
154
+ const originUrlPath = route.urlPath;
155
+ const urlPath = originUrlPath.endsWith('/') ? `${originUrlPath}*` : `${originUrlPath}/*`;
156
+ if (localePathRedirect) {
157
+ if (i18nextDetector) {
158
+ const detectorOptions = convertToHonoLanguageDetectorOptions(languages, fallbackLanguage, detection);
159
+ const detectorHandler = languageDetector(detectorOptions);
160
+ middlewares.push({
161
+ name: 'i18n-language-detector',
162
+ path: urlPath,
163
+ handler: async (c, next)=>{
164
+ const url = new URL(c.req.url);
165
+ const pathname = url.pathname;
166
+ if (isStaticResourceRequest(pathname, staticRoutePrefixes, languages)) return await next();
167
+ if ('/' === originUrlPath) {
168
+ const pathSegments = pathname.split('/').filter(Boolean);
169
+ if (pathSegments.length > 0) {
170
+ const firstSegment = `/${pathSegments[0]}`;
171
+ if (entryPaths.has(firstSegment)) return await next();
172
+ }
173
+ }
174
+ return detectorHandler(c, next);
175
+ }
176
+ });
177
+ }
178
+ middlewares.push({
179
+ name: 'i18n-server-middleware',
180
+ path: urlPath,
181
+ handler: async (c, next)=>{
182
+ const url = new URL(c.req.url);
183
+ const pathname = url.pathname;
184
+ if (isStaticResourceRequest(pathname, staticRoutePrefixes, languages)) return await next();
185
+ if (shouldIgnoreRedirect(pathname, urlPath, ignoreRedirectRoutes)) return await next();
186
+ if ('/' === originUrlPath) {
187
+ const pathSegments = pathname.split('/').filter(Boolean);
188
+ if (pathSegments.length > 0) {
189
+ const firstSegment = `/${pathSegments[0]}`;
190
+ if (entryPaths.has(firstSegment)) return await next();
191
+ }
192
+ }
193
+ const language = getLanguageFromPath(c.req, urlPath, languages);
194
+ if (!language) {
195
+ let detectedLanguage = null;
196
+ if (i18nextDetector) detectedLanguage = c.get('language') || null;
197
+ const targetLanguage = detectedLanguage || fallbackLanguage;
198
+ const localizedUrl = buildLocalizedUrl(c.req, originUrlPath, targetLanguage, languages);
199
+ return c.redirect(localizedUrl);
200
+ }
201
+ await next();
202
+ }
203
+ });
204
+ }
205
+ });
206
+ });
207
+ }
208
+ });
209
+ const server = i18nServerPlugin;
210
+ exports["default"] = __webpack_exports__["default"];
211
+ exports.i18nServerPlugin = __webpack_exports__.i18nServerPlugin;
212
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
213
+ "default",
214
+ "i18nServerPlugin"
215
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
216
+ Object.defineProperty(exports, '__esModule', {
217
+ value: true
218
+ });
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ deepMerge: ()=>deepMerge
28
+ });
29
+ function isPlainObject(value) {
30
+ return null !== value && 'object' == typeof value && !Array.isArray(value) && !(value instanceof Date);
31
+ }
32
+ function deepMerge(defaultOptions, userOptions) {
33
+ if (!userOptions) return defaultOptions;
34
+ const merged = {
35
+ ...defaultOptions
36
+ };
37
+ for(const key in userOptions){
38
+ const userValue = userOptions[key];
39
+ if (void 0 === userValue) continue;
40
+ const defaultValue = merged[key];
41
+ const isUserValueObject = isPlainObject(userValue);
42
+ const isDefaultValueObject = isPlainObject(defaultValue);
43
+ if (isUserValueObject && isDefaultValueObject) merged[key] = deepMerge(defaultValue, userValue);
44
+ else merged[key] = userValue;
45
+ }
46
+ return merged;
47
+ }
48
+ exports.deepMerge = __webpack_exports__.deepMerge;
49
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
50
+ "deepMerge"
51
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
52
+ Object.defineProperty(exports, '__esModule', {
53
+ value: true
54
+ });
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ detectLanguageFromRequest: ()=>detectLanguageFromRequest
28
+ });
29
+ const config_js_namespaceObject = require("../runtime/i18n/detection/config.js");
30
+ function detectLanguageFromRequest(req, languages, detectionOptions) {
31
+ try {
32
+ const mergedDetection = detectionOptions ? (0, config_js_namespaceObject.mergeDetectionOptions)(detectionOptions) : config_js_namespaceObject.DEFAULT_I18NEXT_DETECTION_OPTIONS;
33
+ const order = (mergedDetection.order || []).filter((item)=>![
34
+ 'path',
35
+ 'localStorage',
36
+ 'navigator',
37
+ 'htmlTag',
38
+ 'subdomain'
39
+ ].includes(item));
40
+ const detectionOrder = order.length > 0 ? order : [
41
+ 'querystring',
42
+ 'cookie',
43
+ 'header'
44
+ ];
45
+ const getHeader = (name)=>{
46
+ req.headers, Headers;
47
+ return req.headers.get(name);
48
+ };
49
+ for (const method of detectionOrder){
50
+ let detectedLang = null;
51
+ switch(method){
52
+ case 'querystring':
53
+ {
54
+ const lookupKey = mergedDetection.lookupQuerystring || config_js_namespaceObject.DEFAULT_I18NEXT_DETECTION_OPTIONS.lookupQuerystring || 'lng';
55
+ const host = getHeader('host') || 'localhost';
56
+ const url = new URL(req.url, `http://${host}`);
57
+ detectedLang = url.searchParams.get(lookupKey);
58
+ break;
59
+ }
60
+ case 'cookie':
61
+ {
62
+ const lookupKey = mergedDetection.lookupCookie || config_js_namespaceObject.DEFAULT_I18NEXT_DETECTION_OPTIONS.lookupCookie || 'i18next';
63
+ const cookieHeader = getHeader('Cookie');
64
+ if (cookieHeader) {
65
+ const cookies = cookieHeader.split(';').reduce((acc, item)=>{
66
+ const [key, value] = item.trim().split('=');
67
+ if (key && value) acc[key] = value;
68
+ return acc;
69
+ }, {});
70
+ detectedLang = cookies[lookupKey] || null;
71
+ }
72
+ break;
73
+ }
74
+ case 'header':
75
+ {
76
+ const lookupKey = mergedDetection.lookupHeader || config_js_namespaceObject.DEFAULT_I18NEXT_DETECTION_OPTIONS.lookupHeader || 'accept-language';
77
+ const acceptLanguage = getHeader(lookupKey);
78
+ if (acceptLanguage) {
79
+ const languagesList = acceptLanguage.split(',').map((lang)=>{
80
+ const [code, q] = lang.trim().split(';');
81
+ return {
82
+ code: code.split('-')[0],
83
+ quality: q ? parseFloat(q.split('=')[1]) : 1.0
84
+ };
85
+ }).sort((a, b)=>b.quality - a.quality);
86
+ for (const lang of languagesList)if (0 === languages.length || languages.includes(lang.code)) {
87
+ detectedLang = lang.code;
88
+ break;
89
+ }
90
+ }
91
+ break;
92
+ }
93
+ }
94
+ if (detectedLang && (0 === languages.length || languages.includes(detectedLang))) return detectedLang;
95
+ }
96
+ } catch (error) {}
97
+ return null;
98
+ }
99
+ exports.detectLanguageFromRequest = __webpack_exports__.detectLanguageFromRequest;
100
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
101
+ "detectLanguageFromRequest"
102
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
103
+ Object.defineProperty(exports, '__esModule', {
104
+ value: true
105
+ });
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.r = (exports1)=>{
5
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
6
+ value: 'Module'
7
+ });
8
+ Object.defineProperty(exports1, '__esModule', {
9
+ value: true
10
+ });
11
+ };
12
+ })();
13
+ var __webpack_exports__ = {};
14
+ __webpack_require__.r(__webpack_exports__);
15
+ for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
16
+ Object.defineProperty(exports, '__esModule', {
17
+ value: true
18
+ });
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ getBackendOptions: ()=>getBackendOptions,
28
+ getEntryConfig: ()=>getEntryConfig,
29
+ getLocaleDetectionOptions: ()=>getLocaleDetectionOptions,
30
+ removeEntryConfigKey: ()=>removeEntryConfigKey
31
+ });
32
+ function getEntryConfig(entryName, config, entryKey) {
33
+ const entryConfigMap = config[entryKey];
34
+ return entryConfigMap?.[entryName];
35
+ }
36
+ function removeEntryConfigKey(config, entryKey) {
37
+ const { [entryKey]: _, ...rest } = config;
38
+ return rest;
39
+ }
40
+ function getLocaleDetectionOptions(entryName, localeDetection) {
41
+ const fullConfig = localeDetection;
42
+ const entryConfig = getEntryConfig(entryName, fullConfig, 'localeDetectionByEntry');
43
+ if (entryConfig) {
44
+ const globalConfig = removeEntryConfigKey(fullConfig, 'localeDetectionByEntry');
45
+ return {
46
+ ...globalConfig,
47
+ ...entryConfig
48
+ };
49
+ }
50
+ if ('localeDetectionByEntry' in fullConfig) return removeEntryConfigKey(fullConfig, 'localeDetectionByEntry');
51
+ return localeDetection;
52
+ }
53
+ function getBackendOptions(entryName, backend) {
54
+ const fullConfig = backend;
55
+ const entryConfig = getEntryConfig(entryName, fullConfig, 'backendOptionsByEntry');
56
+ if (entryConfig) {
57
+ const globalConfig = removeEntryConfigKey(fullConfig, 'backendOptionsByEntry');
58
+ return {
59
+ ...globalConfig,
60
+ ...entryConfig
61
+ };
62
+ }
63
+ if ('backendOptionsByEntry' in fullConfig) return removeEntryConfigKey(fullConfig, 'backendOptionsByEntry');
64
+ return backend;
65
+ }
66
+ exports.getBackendOptions = __webpack_exports__.getBackendOptions;
67
+ exports.getEntryConfig = __webpack_exports__.getEntryConfig;
68
+ exports.getLocaleDetectionOptions = __webpack_exports__.getLocaleDetectionOptions;
69
+ exports.removeEntryConfigKey = __webpack_exports__.removeEntryConfigKey;
70
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
71
+ "getBackendOptions",
72
+ "getEntryConfig",
73
+ "getLocaleDetectionOptions",
74
+ "removeEntryConfigKey"
75
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
76
+ Object.defineProperty(exports, '__esModule', {
77
+ value: true
78
+ });
@@ -0,0 +1,107 @@
1
+ import { getPublicDirRoutePrefixes } from "@modern-js/server-core";
2
+ import fs from "fs";
3
+ import path from "path";
4
+ import { getBackendOptions, getLocaleDetectionOptions } from "../shared/utils.mjs";
5
+ function hasJsonFiles(dirPath) {
6
+ try {
7
+ if (!fs.existsSync(dirPath) || !fs.statSync(dirPath).isDirectory()) return false;
8
+ const entries = fs.readdirSync(dirPath);
9
+ for (const entry of entries){
10
+ const entryPath = path.join(dirPath, entry);
11
+ const stat = fs.statSync(entryPath);
12
+ if (stat.isFile() && entry.endsWith('.json')) return true;
13
+ if (stat.isDirectory()) {
14
+ if (hasJsonFiles(entryPath)) return true;
15
+ }
16
+ }
17
+ return false;
18
+ } catch {
19
+ return false;
20
+ }
21
+ }
22
+ function detectLocalesDirectory(appDirectory, normalizedConfig) {
23
+ const rootLocalesPath = path.join(appDirectory, 'locales');
24
+ if (hasJsonFiles(rootLocalesPath)) return true;
25
+ const configPublicPath = path.join(appDirectory, 'config', 'public', 'locales');
26
+ if (hasJsonFiles(configPublicPath)) return true;
27
+ const publicDir = normalizedConfig?.server?.publicDir;
28
+ if (publicDir) {
29
+ const publicDirPath = Array.isArray(publicDir) ? publicDir[0] : publicDir;
30
+ const localesPath = path.isAbsolute(publicDirPath) ? path.join(publicDirPath, 'locales') : path.join(appDirectory, publicDirPath, 'locales');
31
+ if (hasJsonFiles(localesPath)) return true;
32
+ }
33
+ return false;
34
+ }
35
+ const i18nPlugin = (options = {})=>({
36
+ name: '@modern-js/plugin-i18n',
37
+ setup: (api)=>{
38
+ const { localeDetection, backend, transformRuntimeConfig, customPlugin, ...restOptions } = options;
39
+ api._internalRuntimePlugins(({ entrypoint, plugins })=>{
40
+ const localeDetectionOptions = localeDetection ? getLocaleDetectionOptions(entrypoint.entryName, localeDetection) : void 0;
41
+ let backendOptions;
42
+ const { appDirectory } = api.getAppContext();
43
+ const normalizedConfig = api.getNormalizedConfig();
44
+ if (backend) {
45
+ const entryBackendOptions = getBackendOptions(entrypoint.entryName, backend);
46
+ if (entryBackendOptions?.enabled === false) backendOptions = entryBackendOptions;
47
+ else if (entryBackendOptions?.loadPath || entryBackendOptions?.addPath) backendOptions = {
48
+ ...entryBackendOptions,
49
+ enabled: true
50
+ };
51
+ else if (entryBackendOptions?.enabled !== true) {
52
+ const hasLocales = detectLocalesDirectory(appDirectory, normalizedConfig);
53
+ backendOptions = hasLocales ? {
54
+ ...entryBackendOptions,
55
+ enabled: true
56
+ } : entryBackendOptions;
57
+ } else backendOptions = entryBackendOptions;
58
+ } else {
59
+ const hasLocales = detectLocalesDirectory(appDirectory, normalizedConfig);
60
+ if (hasLocales) backendOptions = getBackendOptions(entrypoint.entryName, {
61
+ enabled: true
62
+ });
63
+ }
64
+ const { metaName } = api.getAppContext();
65
+ let extendedConfig = restOptions;
66
+ if (transformRuntimeConfig) extendedConfig = transformRuntimeConfig(restOptions, entrypoint);
67
+ const config = {
68
+ entryName: entrypoint.entryName,
69
+ localeDetection: localeDetectionOptions,
70
+ backend: backendOptions,
71
+ ...extendedConfig
72
+ };
73
+ plugins.push({
74
+ name: customPlugin?.runtime?.name || 'i18n',
75
+ path: customPlugin?.runtime?.path || `@${metaName}/plugin-i18n/runtime`,
76
+ config
77
+ });
78
+ return {
79
+ entrypoint,
80
+ plugins
81
+ };
82
+ });
83
+ api._internalServerPlugins(({ plugins })=>{
84
+ const { serverRoutes, metaName } = api.getAppContext();
85
+ const normalizedConfig = api.getNormalizedConfig();
86
+ let staticRoutePrefixes = [];
87
+ if (serverRoutes && Array.isArray(serverRoutes)) staticRoutePrefixes = serverRoutes.filter((route)=>!route.entryName && route.entryPath.startsWith('public')).map((route)=>route.urlPath).filter(Boolean);
88
+ const publicDirPrefixes = getPublicDirRoutePrefixes(normalizedConfig?.server?.publicDir);
89
+ publicDirPrefixes.forEach((prefix)=>{
90
+ if (!staticRoutePrefixes.includes(prefix)) staticRoutePrefixes.push(prefix);
91
+ });
92
+ plugins.push({
93
+ name: customPlugin?.server?.name || `@${metaName}/plugin-i18n/server`,
94
+ options: {
95
+ localeDetection,
96
+ staticRoutePrefixes
97
+ }
98
+ });
99
+ return {
100
+ plugins
101
+ };
102
+ });
103
+ }
104
+ });
105
+ const cli = i18nPlugin;
106
+ export default cli;
107
+ export { i18nPlugin };
@@ -0,0 +1,18 @@
1
+ var __webpack_modules__ = {};
2
+ var __webpack_module_cache__ = {};
3
+ function __webpack_require__(moduleId) {
4
+ var cachedModule = __webpack_module_cache__[moduleId];
5
+ if (void 0 !== cachedModule) return cachedModule.exports;
6
+ var module = __webpack_module_cache__[moduleId] = {
7
+ exports: {}
8
+ };
9
+ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
10
+ return module.exports;
11
+ }
12
+ __webpack_require__.m = __webpack_modules__;
13
+ (()=>{
14
+ __webpack_require__.add = function(modules) {
15
+ Object.assign(__webpack_require__.m, modules);
16
+ };
17
+ })();
18
+ export { __webpack_require__ };
@@ -0,0 +1,32 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { Link as router_Link, useInRouterContext, useParams } from "@modern-js/runtime/router";
3
+ import { useModernI18n } from "./context.mjs";
4
+ import { buildLocalizedUrl } from "./utils.mjs";
5
+ const useRouterHooks = ()=>{
6
+ const inRouter = useInRouterContext();
7
+ return {
8
+ Link: inRouter ? router_Link : null,
9
+ params: inRouter ? useParams() : {},
10
+ hasRouter: inRouter
11
+ };
12
+ };
13
+ const I18nLink = ({ to, children, ...props })=>{
14
+ const { Link, params, hasRouter } = useRouterHooks();
15
+ const { language, supportedLanguages } = useModernI18n();
16
+ const currentLang = language;
17
+ const localizedTo = buildLocalizedUrl(to, currentLang, supportedLanguages);
18
+ if ('development' === process.env.NODE_ENV && hasRouter && !params.lang) console.warn("I18nLink is being used outside of a :lang dynamic route context. This may cause unexpected behavior. Please ensure I18nLink is used within a route that has a :lang parameter.");
19
+ if (!hasRouter || !Link) return /*#__PURE__*/ jsx("a", {
20
+ href: localizedTo,
21
+ ...props,
22
+ children: children
23
+ });
24
+ return /*#__PURE__*/ jsx(Link, {
25
+ to: localizedTo,
26
+ ...props,
27
+ children: children
28
+ });
29
+ };
30
+ const runtime_I18nLink = I18nLink;
31
+ export default runtime_I18nLink;
32
+ export { I18nLink };