@modern-js/plugin-i18n 2.69.5 → 3.0.0-alpha.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 (151) hide show
  1. package/README.md +6 -0
  2. package/dist/cjs/cli/index.cjs +154 -0
  3. package/dist/cjs/runtime/I18nLink.cjs +68 -0
  4. package/dist/cjs/runtime/context.cjs +138 -0
  5. package/dist/cjs/runtime/hooks.cjs +189 -0
  6. package/dist/cjs/runtime/i18n/backend/config.cjs +39 -0
  7. package/dist/cjs/runtime/i18n/backend/defaults.cjs +56 -0
  8. package/dist/cjs/runtime/i18n/backend/defaults.node.cjs +56 -0
  9. package/dist/cjs/runtime/i18n/backend/index.cjs +108 -0
  10. package/dist/cjs/runtime/i18n/backend/middleware.cjs +54 -0
  11. package/dist/cjs/runtime/i18n/backend/middleware.common.cjs +105 -0
  12. package/dist/cjs/runtime/i18n/backend/middleware.node.cjs +58 -0
  13. package/dist/cjs/runtime/i18n/backend/sdk-backend.cjs +171 -0
  14. package/dist/cjs/runtime/i18n/detection/config.cjs +63 -0
  15. package/dist/cjs/runtime/i18n/detection/index.cjs +309 -0
  16. package/dist/cjs/runtime/i18n/detection/middleware.cjs +185 -0
  17. package/dist/cjs/runtime/i18n/detection/middleware.node.cjs +74 -0
  18. package/dist/cjs/runtime/i18n/index.cjs +43 -0
  19. package/dist/cjs/runtime/i18n/instance.cjs +132 -0
  20. package/dist/cjs/runtime/i18n/utils.cjs +185 -0
  21. package/dist/cjs/runtime/index.cjs +162 -0
  22. package/dist/cjs/runtime/types.cjs +18 -0
  23. package/dist/cjs/runtime/utils.cjs +134 -0
  24. package/dist/cjs/server/index.cjs +178 -0
  25. package/dist/cjs/shared/deepMerge.cjs +54 -0
  26. package/dist/cjs/shared/detection.cjs +105 -0
  27. package/dist/cjs/shared/type.cjs +18 -0
  28. package/dist/cjs/shared/utils.cjs +78 -0
  29. package/dist/esm/cli/index.js +106 -0
  30. package/dist/esm/runtime/I18nLink.js +31 -0
  31. package/dist/esm/runtime/context.js +101 -0
  32. package/dist/esm/runtime/hooks.js +146 -0
  33. package/dist/esm/runtime/i18n/backend/config.js +5 -0
  34. package/dist/esm/runtime/i18n/backend/defaults.js +19 -0
  35. package/dist/esm/runtime/i18n/backend/defaults.node.js +19 -0
  36. package/dist/esm/runtime/i18n/backend/index.js +74 -0
  37. package/dist/esm/runtime/i18n/backend/middleware.common.js +61 -0
  38. package/dist/esm/runtime/i18n/backend/middleware.js +7 -0
  39. package/dist/esm/runtime/i18n/backend/middleware.node.js +8 -0
  40. package/dist/esm/runtime/i18n/backend/sdk-backend.js +137 -0
  41. package/dist/esm/runtime/i18n/detection/config.js +26 -0
  42. package/dist/esm/runtime/i18n/detection/index.js +260 -0
  43. package/dist/esm/runtime/i18n/detection/middleware.js +132 -0
  44. package/dist/esm/runtime/i18n/detection/middleware.node.js +31 -0
  45. package/dist/esm/runtime/i18n/index.js +3 -0
  46. package/dist/esm/runtime/i18n/instance.js +77 -0
  47. package/dist/esm/runtime/i18n/utils.js +136 -0
  48. package/dist/esm/runtime/index.js +119 -0
  49. package/dist/esm/runtime/types.js +0 -0
  50. package/dist/esm/runtime/utils.js +82 -0
  51. package/dist/esm/server/index.js +168 -0
  52. package/dist/esm/shared/deepMerge.js +20 -0
  53. package/dist/esm/shared/detection.js +71 -0
  54. package/dist/esm/shared/type.js +0 -0
  55. package/dist/esm/shared/utils.js +35 -0
  56. package/dist/esm-node/cli/index.js +106 -0
  57. package/dist/esm-node/runtime/I18nLink.js +31 -0
  58. package/dist/esm-node/runtime/context.js +101 -0
  59. package/dist/esm-node/runtime/hooks.js +146 -0
  60. package/dist/esm-node/runtime/i18n/backend/config.js +5 -0
  61. package/dist/esm-node/runtime/i18n/backend/defaults.js +19 -0
  62. package/dist/esm-node/runtime/i18n/backend/defaults.node.js +19 -0
  63. package/dist/esm-node/runtime/i18n/backend/index.js +74 -0
  64. package/dist/esm-node/runtime/i18n/backend/middleware.common.js +61 -0
  65. package/dist/esm-node/runtime/i18n/backend/middleware.js +7 -0
  66. package/dist/esm-node/runtime/i18n/backend/middleware.node.js +8 -0
  67. package/dist/esm-node/runtime/i18n/backend/sdk-backend.js +137 -0
  68. package/dist/esm-node/runtime/i18n/detection/config.js +26 -0
  69. package/dist/esm-node/runtime/i18n/detection/index.js +260 -0
  70. package/dist/esm-node/runtime/i18n/detection/middleware.js +132 -0
  71. package/dist/esm-node/runtime/i18n/detection/middleware.node.js +31 -0
  72. package/dist/esm-node/runtime/i18n/index.js +3 -0
  73. package/dist/esm-node/runtime/i18n/instance.js +77 -0
  74. package/dist/esm-node/runtime/i18n/utils.js +136 -0
  75. package/dist/esm-node/runtime/index.js +119 -0
  76. package/dist/esm-node/runtime/types.js +0 -0
  77. package/dist/esm-node/runtime/utils.js +82 -0
  78. package/dist/esm-node/server/index.js +168 -0
  79. package/dist/esm-node/shared/deepMerge.js +20 -0
  80. package/dist/esm-node/shared/detection.js +71 -0
  81. package/dist/esm-node/shared/type.js +0 -0
  82. package/dist/esm-node/shared/utils.js +35 -0
  83. package/dist/types/cli/index.d.ts +21 -0
  84. package/dist/types/runtime/I18nLink.d.ts +8 -0
  85. package/dist/types/runtime/context.d.ts +38 -0
  86. package/dist/types/runtime/hooks.d.ts +28 -0
  87. package/dist/types/runtime/i18n/backend/config.d.ts +2 -0
  88. package/dist/types/runtime/i18n/backend/defaults.d.ts +13 -0
  89. package/dist/types/runtime/i18n/backend/defaults.node.d.ts +8 -0
  90. package/dist/types/runtime/i18n/backend/index.d.ts +3 -0
  91. package/dist/types/runtime/i18n/backend/middleware.common.d.ts +14 -0
  92. package/dist/types/runtime/i18n/backend/middleware.d.ts +12 -0
  93. package/dist/types/runtime/i18n/backend/middleware.node.d.ts +13 -0
  94. package/dist/types/runtime/i18n/backend/sdk-backend.d.ts +52 -0
  95. package/dist/types/runtime/i18n/detection/config.d.ts +11 -0
  96. package/dist/types/runtime/i18n/detection/index.d.ts +50 -0
  97. package/dist/types/runtime/i18n/detection/middleware.d.ts +24 -0
  98. package/dist/types/runtime/i18n/detection/middleware.node.d.ts +17 -0
  99. package/dist/types/runtime/i18n/index.d.ts +3 -0
  100. package/dist/types/runtime/i18n/instance.d.ts +93 -0
  101. package/dist/types/runtime/i18n/utils.d.ts +29 -0
  102. package/dist/types/runtime/index.d.ts +19 -0
  103. package/dist/types/runtime/types.d.ts +15 -0
  104. package/dist/types/runtime/utils.d.ts +33 -0
  105. package/dist/types/server/index.d.ts +8 -0
  106. package/dist/types/shared/deepMerge.d.ts +1 -0
  107. package/dist/types/shared/detection.d.ts +11 -0
  108. package/dist/types/shared/type.d.ts +156 -0
  109. package/dist/types/shared/utils.d.ts +5 -0
  110. package/package.json +100 -34
  111. package/rslib.config.mts +4 -0
  112. package/src/cli/index.ts +245 -0
  113. package/src/runtime/I18nLink.tsx +76 -0
  114. package/src/runtime/context.tsx +256 -0
  115. package/src/runtime/hooks.ts +274 -0
  116. package/src/runtime/i18n/backend/config.ts +10 -0
  117. package/src/runtime/i18n/backend/defaults.node.ts +31 -0
  118. package/src/runtime/i18n/backend/defaults.ts +37 -0
  119. package/src/runtime/i18n/backend/index.ts +181 -0
  120. package/src/runtime/i18n/backend/middleware.common.ts +116 -0
  121. package/src/runtime/i18n/backend/middleware.node.ts +32 -0
  122. package/src/runtime/i18n/backend/middleware.ts +28 -0
  123. package/src/runtime/i18n/backend/sdk-backend.ts +292 -0
  124. package/src/runtime/i18n/detection/config.ts +32 -0
  125. package/src/runtime/i18n/detection/index.ts +641 -0
  126. package/src/runtime/i18n/detection/middleware.node.ts +84 -0
  127. package/src/runtime/i18n/detection/middleware.ts +251 -0
  128. package/src/runtime/i18n/index.ts +8 -0
  129. package/src/runtime/i18n/instance.ts +227 -0
  130. package/src/runtime/i18n/utils.ts +333 -0
  131. package/src/runtime/index.tsx +275 -0
  132. package/src/runtime/types.ts +17 -0
  133. package/src/runtime/utils.ts +151 -0
  134. package/src/server/index.ts +336 -0
  135. package/src/shared/deepMerge.ts +38 -0
  136. package/src/shared/detection.ts +131 -0
  137. package/src/shared/type.ts +170 -0
  138. package/src/shared/utils.ts +82 -0
  139. package/tsconfig.json +12 -0
  140. package/dist/cjs/index.js +0 -73
  141. package/dist/cjs/languageDetector.js +0 -51
  142. package/dist/cjs/utils/index.js +0 -39
  143. package/dist/esm/index.js +0 -61
  144. package/dist/esm/languageDetector.js +0 -33
  145. package/dist/esm/utils/index.js +0 -16
  146. package/dist/esm-node/index.js +0 -49
  147. package/dist/esm-node/languageDetector.js +0 -26
  148. package/dist/esm-node/utils/index.js +0 -15
  149. package/dist/types/index.d.ts +0 -34
  150. package/dist/types/languageDetector.d.ts +0 -6
  151. package/dist/types/utils/index.d.ts +0 -5
@@ -0,0 +1,132 @@
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
+ getActualI18nextInstance: ()=>getActualI18nextInstance,
28
+ getI18nInstance: ()=>getI18nInstance,
29
+ getI18nWrapperI18nextInstance: ()=>getI18nWrapperI18nextInstance,
30
+ getI18nextInstanceForProvider: ()=>getI18nextInstanceForProvider,
31
+ getI18nextProvider: ()=>getI18nextProvider,
32
+ getInitReactI18next: ()=>getInitReactI18next,
33
+ isI18nInstance: ()=>isI18nInstance,
34
+ isI18nWrapperInstance: ()=>isI18nWrapperInstance
35
+ });
36
+ function isI18nWrapperInstance(obj) {
37
+ if (!obj || 'object' != typeof obj) return false;
38
+ if (!obj.i18nInstance || 'object' != typeof obj.i18nInstance) return false;
39
+ if (!obj.i18nInstance.instance) return false;
40
+ if ('function' != typeof obj.init || 'function' != typeof obj.use) return false;
41
+ return true;
42
+ }
43
+ function getI18nWrapperI18nextInstance(wrapperInstance) {
44
+ if (isI18nWrapperInstance(wrapperInstance)) return wrapperInstance.i18nInstance?.instance;
45
+ return null;
46
+ }
47
+ function getActualI18nextInstance(instance) {
48
+ if (isI18nWrapperInstance(instance)) {
49
+ const i18nextInstance = getI18nWrapperI18nextInstance(instance);
50
+ return i18nextInstance || instance;
51
+ }
52
+ return instance;
53
+ }
54
+ function isI18nInstance(obj) {
55
+ if (!obj || 'object' != typeof obj) return false;
56
+ if (isI18nWrapperInstance(obj)) return true;
57
+ return 'function' == typeof obj.init && 'function' == typeof obj.use;
58
+ }
59
+ async function tryImportI18next() {
60
+ try {
61
+ const i18next = await import("i18next");
62
+ return i18next.default;
63
+ } catch (error) {
64
+ return null;
65
+ }
66
+ }
67
+ async function createI18nextInstance() {
68
+ try {
69
+ const i18next = await tryImportI18next();
70
+ if (!i18next) return null;
71
+ return i18next.createInstance({
72
+ initImmediate: false
73
+ });
74
+ } catch (error) {
75
+ return null;
76
+ }
77
+ }
78
+ async function tryImportReactI18next() {
79
+ try {
80
+ const reactI18next = await import("react-i18next");
81
+ return reactI18next;
82
+ } catch (error) {
83
+ return null;
84
+ }
85
+ }
86
+ function getI18nextInstanceForProvider(instance) {
87
+ if (isI18nWrapperInstance(instance)) {
88
+ const i18nextInstance = getI18nWrapperI18nextInstance(instance);
89
+ if (i18nextInstance) return i18nextInstance;
90
+ }
91
+ return instance;
92
+ }
93
+ async function getI18nInstance(userInstance) {
94
+ if (userInstance) {
95
+ if (isI18nWrapperInstance(userInstance)) return userInstance;
96
+ if (isI18nInstance(userInstance)) return userInstance;
97
+ }
98
+ const i18nextInstance = await createI18nextInstance();
99
+ if (i18nextInstance) return i18nextInstance;
100
+ throw new Error('No i18n instance found');
101
+ }
102
+ async function getInitReactI18next() {
103
+ const reactI18nextModule = await tryImportReactI18next();
104
+ if (reactI18nextModule) return reactI18nextModule.initReactI18next;
105
+ return null;
106
+ }
107
+ async function getI18nextProvider() {
108
+ const reactI18nextModule = await tryImportReactI18next();
109
+ if (reactI18nextModule) return reactI18nextModule.I18nextProvider;
110
+ return null;
111
+ }
112
+ exports.getActualI18nextInstance = __webpack_exports__.getActualI18nextInstance;
113
+ exports.getI18nInstance = __webpack_exports__.getI18nInstance;
114
+ exports.getI18nWrapperI18nextInstance = __webpack_exports__.getI18nWrapperI18nextInstance;
115
+ exports.getI18nextInstanceForProvider = __webpack_exports__.getI18nextInstanceForProvider;
116
+ exports.getI18nextProvider = __webpack_exports__.getI18nextProvider;
117
+ exports.getInitReactI18next = __webpack_exports__.getInitReactI18next;
118
+ exports.isI18nInstance = __webpack_exports__.isI18nInstance;
119
+ exports.isI18nWrapperInstance = __webpack_exports__.isI18nWrapperInstance;
120
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
121
+ "getActualI18nextInstance",
122
+ "getI18nInstance",
123
+ "getI18nWrapperI18nextInstance",
124
+ "getI18nextInstanceForProvider",
125
+ "getI18nextProvider",
126
+ "getInitReactI18next",
127
+ "isI18nInstance",
128
+ "isI18nWrapperInstance"
129
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
130
+ Object.defineProperty(exports, '__esModule', {
131
+ value: true
132
+ });
@@ -0,0 +1,185 @@
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
+ setupClonedInstance: ()=>setupClonedInstance,
28
+ buildInitOptions: ()=>buildInitOptions,
29
+ assertI18nInstance: ()=>assertI18nInstance,
30
+ initializeI18nInstance: ()=>initializeI18nInstance,
31
+ changeI18nLanguage: ()=>changeI18nLanguage,
32
+ ensureLanguageMatch: ()=>ensureLanguageMatch
33
+ });
34
+ const runtime_namespaceObject = require("@modern-js/runtime");
35
+ const index_cjs_namespaceObject = require("./backend/index.cjs");
36
+ const middleware_cjs_namespaceObject = require("./backend/middleware.cjs");
37
+ const sdk_backend_cjs_namespaceObject = require("./backend/sdk-backend.cjs");
38
+ const external_detection_index_cjs_namespaceObject = require("./detection/index.cjs");
39
+ const external_instance_cjs_namespaceObject = require("./instance.cjs");
40
+ function assertI18nInstance(obj) {
41
+ if (!(0, external_instance_cjs_namespaceObject.isI18nInstance)(obj)) throw new Error('Object does not implement I18nInstance interface');
42
+ }
43
+ const buildInitOptions = async (finalLanguage, fallbackLanguage, languages, mergedDetection, mergedBackend, userInitOptions, useSuspense, i18nInstance)=>{
44
+ const defaultUseSuspense = void 0 !== useSuspense ? useSuspense : (0, runtime_namespaceObject.isBrowser)() ? userInitOptions?.react?.useSuspense ?? true : false;
45
+ const isChainedBackend = !!mergedBackend?._useChainedBackend;
46
+ const sanitizedUserInitOptions = userInitOptions ? {
47
+ ...userInitOptions,
48
+ backend: void 0
49
+ } : void 0;
50
+ const { backend: _removedBackend, ...userOptionsWithoutBackend } = sanitizedUserInitOptions || {};
51
+ const initOptions = {
52
+ lng: finalLanguage,
53
+ fallbackLng: fallbackLanguage,
54
+ supportedLngs: languages,
55
+ detection: mergedDetection,
56
+ initImmediate: sanitizedUserInitOptions?.initImmediate ?? true,
57
+ interpolation: {
58
+ ...sanitizedUserInitOptions?.interpolation || {},
59
+ escapeValue: sanitizedUserInitOptions?.interpolation?.escapeValue ?? false
60
+ },
61
+ react: {
62
+ ...sanitizedUserInitOptions?.react || {},
63
+ useSuspense: defaultUseSuspense
64
+ },
65
+ ...userOptionsWithoutBackend
66
+ };
67
+ if (mergedBackend) if (isChainedBackend && mergedBackend._chainedBackendConfig) {
68
+ let HttpBackend;
69
+ let SdkBackendClass;
70
+ if (i18nInstance?.options?.backend?.backends && Array.isArray(i18nInstance.options.backend.backends) && i18nInstance.options.backend.backends.length >= 2) {
71
+ HttpBackend = i18nInstance.options.backend.backends[0];
72
+ SdkBackendClass = i18nInstance.options.backend.backends[1];
73
+ } else {
74
+ HttpBackend = middleware_cjs_namespaceObject.HttpBackendWithSave;
75
+ SdkBackendClass = sdk_backend_cjs_namespaceObject.SdkBackend;
76
+ }
77
+ initOptions.backend = {
78
+ backends: [
79
+ HttpBackend,
80
+ SdkBackendClass
81
+ ],
82
+ backendOptions: mergedBackend._chainedBackendConfig.backendOptions,
83
+ cacheHitMode: mergedBackend.cacheHitMode || 'refreshAndUpdateStore'
84
+ };
85
+ } else {
86
+ const { _useChainedBackend, _chainedBackendConfig, ...cleanBackend } = mergedBackend || {};
87
+ initOptions.backend = cleanBackend;
88
+ }
89
+ return initOptions;
90
+ };
91
+ const ensureLanguageMatch = async (i18nInstance, finalLanguage)=>{
92
+ if (i18nInstance.language !== finalLanguage) {
93
+ await i18nInstance.setLang?.(finalLanguage);
94
+ await i18nInstance.changeLanguage?.(finalLanguage);
95
+ }
96
+ };
97
+ const changeI18nLanguage = async (i18nInstance, newLang, options)=>{
98
+ if (!newLang || 'string' != typeof newLang) throw new Error('Language must be a non-empty string');
99
+ if (!i18nInstance) throw new Error('i18nInstance is required');
100
+ await i18nInstance.setLang?.(newLang);
101
+ await i18nInstance.changeLanguage?.(newLang);
102
+ if ((0, runtime_namespaceObject.isBrowser)()) {
103
+ const detectionOptions = options?.detectionOptions || i18nInstance.options?.detection;
104
+ (0, external_detection_index_cjs_namespaceObject.cacheUserLanguage)(i18nInstance, newLang, detectionOptions);
105
+ }
106
+ };
107
+ const initializeI18nInstance = async (i18nInstance, finalLanguage, fallbackLanguage, languages, mergedDetection, mergedBackend, userInitOptions, useSuspense)=>{
108
+ if (!i18nInstance.isInitialized) {
109
+ const initOptions = await buildInitOptions(finalLanguage, fallbackLanguage, languages, mergedDetection, mergedBackend, userInitOptions, useSuspense, i18nInstance);
110
+ const actualInstance = (0, external_instance_cjs_namespaceObject.getActualI18nextInstance)(i18nInstance);
111
+ const savedBackendConfig = actualInstance?.options?.backend || i18nInstance.options?.backend;
112
+ const isChainedBackendFromSaved = savedBackendConfig?.backends && Array.isArray(savedBackendConfig.backends);
113
+ await i18nInstance.init(initOptions);
114
+ if (mergedBackend) {
115
+ if ((0, external_instance_cjs_namespaceObject.isI18nWrapperInstance)(i18nInstance) && actualInstance?.options) {
116
+ if (isChainedBackendFromSaved && initOptions.backend) actualInstance.options.backend = {
117
+ ...initOptions.backend,
118
+ backends: savedBackendConfig.backends
119
+ };
120
+ else if (initOptions.backend) actualInstance.options.backend = {
121
+ ...actualInstance.options.backend,
122
+ ...initOptions.backend
123
+ };
124
+ }
125
+ if (hasOptions(i18nInstance)) {
126
+ if (isChainedBackendFromSaved && initOptions.backend) i18nInstance.options.backend = {
127
+ ...initOptions.backend,
128
+ backends: savedBackendConfig.backends
129
+ };
130
+ else if (initOptions.backend) i18nInstance.options.backend = {
131
+ ...i18nInstance.options.backend,
132
+ ...initOptions.backend
133
+ };
134
+ }
135
+ }
136
+ if (mergedBackend && hasOptions(i18nInstance)) {
137
+ const defaultNS = initOptions.defaultNS || initOptions.ns || 'translation';
138
+ const ns = Array.isArray(defaultNS) ? defaultNS[0] : defaultNS;
139
+ let retries = 20;
140
+ while(retries > 0){
141
+ const actualInstance = (0, external_instance_cjs_namespaceObject.getActualI18nextInstance)(i18nInstance);
142
+ const store = actualInstance.store;
143
+ if (store?.data?.[finalLanguage]?.[ns]) break;
144
+ await new Promise((resolve)=>setTimeout(resolve, 100));
145
+ retries--;
146
+ }
147
+ }
148
+ }
149
+ };
150
+ function hasOptions(instance) {
151
+ return void 0 !== instance.options && null !== instance.options;
152
+ }
153
+ const setupClonedInstance = async (i18nInstance, finalLanguage, fallbackLanguage, languages, backendEnabled, backend, i18nextDetector, detection, localePathRedirect, userInitOptions)=>{
154
+ const mergedBackend = (0, index_cjs_namespaceObject.mergeBackendOptions)(backend, userInitOptions);
155
+ const hasSdkConfig = 'function' == typeof userInitOptions?.backend?.sdk || mergedBackend?.sdk && 'function' == typeof mergedBackend.sdk;
156
+ if (backendEnabled || hasSdkConfig) {
157
+ (0, middleware_cjs_namespaceObject.useI18nextBackend)(i18nInstance, mergedBackend);
158
+ if (mergedBackend && hasOptions(i18nInstance)) i18nInstance.options.backend = {
159
+ ...i18nInstance.options.backend,
160
+ ...mergedBackend
161
+ };
162
+ if (i18nInstance.isInitialized) await ensureLanguageMatch(i18nInstance, finalLanguage);
163
+ else {
164
+ const mergedDetection = (0, external_detection_index_cjs_namespaceObject.mergeDetectionOptions)(i18nextDetector, detection, localePathRedirect, userInitOptions);
165
+ await initializeI18nInstance(i18nInstance, finalLanguage, fallbackLanguage, languages, mergedDetection, mergedBackend, userInitOptions, false);
166
+ }
167
+ } else await ensureLanguageMatch(i18nInstance, finalLanguage);
168
+ };
169
+ exports.assertI18nInstance = __webpack_exports__.assertI18nInstance;
170
+ exports.buildInitOptions = __webpack_exports__.buildInitOptions;
171
+ exports.changeI18nLanguage = __webpack_exports__.changeI18nLanguage;
172
+ exports.ensureLanguageMatch = __webpack_exports__.ensureLanguageMatch;
173
+ exports.initializeI18nInstance = __webpack_exports__.initializeI18nInstance;
174
+ exports.setupClonedInstance = __webpack_exports__.setupClonedInstance;
175
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
176
+ "assertI18nInstance",
177
+ "buildInitOptions",
178
+ "changeI18nLanguage",
179
+ "ensureLanguageMatch",
180
+ "initializeI18nInstance",
181
+ "setupClonedInstance"
182
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
183
+ Object.defineProperty(exports, '__esModule', {
184
+ value: true
185
+ });
@@ -0,0 +1,162 @@
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
+ I18nLink: ()=>external_I18nLink_cjs_namespaceObject.I18nLink,
28
+ default: ()=>runtime,
29
+ useModernI18n: ()=>external_context_cjs_namespaceObject.useModernI18n,
30
+ i18nPlugin: ()=>i18nPlugin
31
+ });
32
+ const jsx_runtime_namespaceObject = require("react/jsx-runtime");
33
+ const runtime_namespaceObject = require("@modern-js/runtime");
34
+ const merge_namespaceObject = require("@modern-js/runtime-utils/merge");
35
+ const external_react_namespaceObject = require("react");
36
+ const external_context_cjs_namespaceObject = require("./context.cjs");
37
+ const external_hooks_cjs_namespaceObject = require("./hooks.cjs");
38
+ const index_cjs_namespaceObject = require("./i18n/index.cjs");
39
+ const backend_index_cjs_namespaceObject = require("./i18n/backend/index.cjs");
40
+ const middleware_cjs_namespaceObject = require("./i18n/backend/middleware.cjs");
41
+ const detection_index_cjs_namespaceObject = require("./i18n/detection/index.cjs");
42
+ const detection_middleware_cjs_namespaceObject = require("./i18n/detection/middleware.cjs");
43
+ const instance_cjs_namespaceObject = require("./i18n/instance.cjs");
44
+ const utils_cjs_namespaceObject = require("./i18n/utils.cjs");
45
+ const external_utils_cjs_namespaceObject = require("./utils.cjs");
46
+ require("./types.cjs");
47
+ const external_I18nLink_cjs_namespaceObject = require("./I18nLink.cjs");
48
+ const i18nPlugin = (options)=>({
49
+ name: '@modern-js/plugin-i18n',
50
+ setup: (api)=>{
51
+ const { entryName, i18nInstance: userI18nInstance, initOptions, localeDetection, backend } = options;
52
+ const { localePathRedirect = false, i18nextDetector = true, languages = [], fallbackLanguage = 'en', detection, ignoreRedirectRoutes } = localeDetection || {};
53
+ const { enabled: backendEnabled = false } = backend || {};
54
+ let I18nextProvider;
55
+ api.onBeforeRender(async (context)=>{
56
+ let i18nInstance = await (0, index_cjs_namespaceObject.getI18nInstance)(userI18nInstance);
57
+ const { i18n: otherConfig } = api.getRuntimeConfig();
58
+ const { initOptions: otherInitOptions } = otherConfig || {};
59
+ const userInitOptions = (0, merge_namespaceObject.merge)(otherInitOptions || {}, initOptions || {});
60
+ const initReactI18next = await (0, instance_cjs_namespaceObject.getInitReactI18next)();
61
+ I18nextProvider = await (0, instance_cjs_namespaceObject.getI18nextProvider)();
62
+ if (initReactI18next) i18nInstance.use(initReactI18next);
63
+ const pathname = (0, external_utils_cjs_namespaceObject.getPathname)(context);
64
+ if (i18nextDetector) (0, detection_middleware_cjs_namespaceObject.useI18nextLanguageDetector)(i18nInstance);
65
+ const mergedDetection = (0, detection_index_cjs_namespaceObject.mergeDetectionOptions)(i18nextDetector, detection, localePathRedirect, userInitOptions);
66
+ const mergedBackend = (0, backend_index_cjs_namespaceObject.mergeBackendOptions)(backend, userInitOptions);
67
+ const hasSdkConfig = 'function' == typeof userInitOptions?.backend?.sdk || mergedBackend?.sdk && 'function' == typeof mergedBackend.sdk;
68
+ if (mergedBackend && (backendEnabled || hasSdkConfig)) (0, middleware_cjs_namespaceObject.useI18nextBackend)(i18nInstance, mergedBackend);
69
+ const { finalLanguage } = await (0, detection_index_cjs_namespaceObject.detectLanguageWithPriority)(i18nInstance, {
70
+ languages,
71
+ fallbackLanguage,
72
+ localePathRedirect,
73
+ i18nextDetector,
74
+ detection,
75
+ userInitOptions,
76
+ mergedBackend,
77
+ pathname,
78
+ ssrContext: context.ssrContext
79
+ });
80
+ await (0, utils_cjs_namespaceObject.initializeI18nInstance)(i18nInstance, finalLanguage, fallbackLanguage, languages, mergedDetection, mergedBackend, userInitOptions);
81
+ if (!(0, runtime_namespaceObject.isBrowser)() && i18nInstance.cloneInstance) {
82
+ i18nInstance = i18nInstance.cloneInstance();
83
+ await (0, utils_cjs_namespaceObject.setupClonedInstance)(i18nInstance, finalLanguage, fallbackLanguage, languages, backendEnabled, backend, i18nextDetector, detection, localePathRedirect, userInitOptions);
84
+ }
85
+ if (localePathRedirect) await (0, utils_cjs_namespaceObject.ensureLanguageMatch)(i18nInstance, finalLanguage);
86
+ if (!(0, runtime_namespaceObject.isBrowser)()) (0, detection_index_cjs_namespaceObject.exportServerLngToWindow)(context, finalLanguage);
87
+ context.i18nInstance = i18nInstance;
88
+ context.changeLanguage = async (newLang)=>{
89
+ await (0, utils_cjs_namespaceObject.changeI18nLanguage)(i18nInstance, newLang, {
90
+ detectionOptions: mergedDetection
91
+ });
92
+ };
93
+ });
94
+ api.wrapRoot((App)=>(props)=>{
95
+ const runtimeContext = (0, runtime_namespaceObject.useRuntimeContext)();
96
+ const i18nInstance = runtimeContext.i18nInstance;
97
+ const initialLang = (0, external_react_namespaceObject.useMemo)(()=>i18nInstance?.language || (localeDetection?.fallbackLanguage ?? 'en'), [
98
+ i18nInstance?.language,
99
+ localeDetection?.fallbackLanguage
100
+ ]);
101
+ const [lang, setLang] = (0, external_react_namespaceObject.useState)(initialLang);
102
+ const [forceUpdate, setForceUpdate] = (0, external_react_namespaceObject.useState)(0);
103
+ const prevLangRef = (0, external_react_namespaceObject.useRef)(lang);
104
+ const runtimeContextRef = (0, external_react_namespaceObject.useRef)(runtimeContext);
105
+ runtimeContextRef.current = runtimeContext;
106
+ (0, external_react_namespaceObject.useEffect)(()=>{
107
+ if (i18nInstance?.language) {
108
+ const translator = i18nInstance.translator;
109
+ if (translator) translator.language = i18nInstance.language;
110
+ }
111
+ }, [
112
+ i18nInstance?.language
113
+ ]);
114
+ (0, external_react_namespaceObject.useEffect)(()=>{
115
+ prevLangRef.current = lang;
116
+ }, [
117
+ lang
118
+ ]);
119
+ (0, external_hooks_cjs_namespaceObject.useSdkResourcesLoader)(i18nInstance, setForceUpdate);
120
+ (0, external_hooks_cjs_namespaceObject.useLanguageSync)(i18nInstance, localePathRedirect, languages, runtimeContextRef, prevLangRef, setLang);
121
+ (0, external_hooks_cjs_namespaceObject.useClientSideRedirect)(i18nInstance, localePathRedirect, languages, fallbackLanguage, ignoreRedirectRoutes);
122
+ const contextValue = (0, external_react_namespaceObject.useMemo)(()=>(0, external_hooks_cjs_namespaceObject.createContextValue)(lang, i18nInstance, entryName, languages, localePathRedirect, ignoreRedirectRoutes, setLang), [
123
+ lang,
124
+ i18nInstance,
125
+ entryName,
126
+ languages,
127
+ localePathRedirect,
128
+ ignoreRedirectRoutes,
129
+ forceUpdate
130
+ ]);
131
+ const appContent = /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_context_cjs_namespaceObject.ModernI18nProvider, {
132
+ value: contextValue,
133
+ children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(App, {
134
+ ...props
135
+ })
136
+ });
137
+ if (!i18nInstance) return appContent;
138
+ if (I18nextProvider) {
139
+ const i18nextInstanceForProvider = (0, instance_cjs_namespaceObject.getI18nextInstanceForProvider)(i18nInstance);
140
+ return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(I18nextProvider, {
141
+ i18n: i18nextInstanceForProvider,
142
+ children: appContent
143
+ });
144
+ }
145
+ return appContent;
146
+ });
147
+ }
148
+ });
149
+ const runtime = i18nPlugin;
150
+ exports.I18nLink = __webpack_exports__.I18nLink;
151
+ exports["default"] = __webpack_exports__["default"];
152
+ exports.i18nPlugin = __webpack_exports__.i18nPlugin;
153
+ exports.useModernI18n = __webpack_exports__.useModernI18n;
154
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
155
+ "I18nLink",
156
+ "default",
157
+ "i18nPlugin",
158
+ "useModernI18n"
159
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
160
+ Object.defineProperty(exports, '__esModule', {
161
+ value: true
162
+ });
@@ -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,134 @@
1
+ "use strict";
2
+ var __webpack_modules__ = {
3
+ "@modern-js/runtime/router" (module) {
4
+ module.exports = require("@modern-js/runtime/router");
5
+ }
6
+ };
7
+ var __webpack_module_cache__ = {};
8
+ function __webpack_require__(moduleId) {
9
+ var cachedModule = __webpack_module_cache__[moduleId];
10
+ if (void 0 !== cachedModule) return cachedModule.exports;
11
+ var module = __webpack_module_cache__[moduleId] = {
12
+ exports: {}
13
+ };
14
+ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
15
+ return module.exports;
16
+ }
17
+ (()=>{
18
+ __webpack_require__.d = (exports1, definition)=>{
19
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
20
+ enumerable: true,
21
+ get: definition[key]
22
+ });
23
+ };
24
+ })();
25
+ (()=>{
26
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
27
+ })();
28
+ (()=>{
29
+ __webpack_require__.r = (exports1)=>{
30
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
31
+ value: 'Module'
32
+ });
33
+ Object.defineProperty(exports1, '__esModule', {
34
+ value: true
35
+ });
36
+ };
37
+ })();
38
+ var __webpack_exports__ = {};
39
+ (()=>{
40
+ __webpack_require__.r(__webpack_exports__);
41
+ __webpack_require__.d(__webpack_exports__, {
42
+ detectLanguageFromPath: ()=>detectLanguageFromPath,
43
+ getLanguageFromPath: ()=>getLanguageFromPath,
44
+ buildLocalizedUrl: ()=>buildLocalizedUrl,
45
+ getPathname: ()=>getPathname,
46
+ shouldIgnoreRedirect: ()=>shouldIgnoreRedirect,
47
+ useRouterHooks: ()=>useRouterHooks,
48
+ getEntryPath: ()=>getEntryPath
49
+ });
50
+ const runtime_namespaceObject = require("@modern-js/runtime");
51
+ const context_namespaceObject = require("@modern-js/runtime/context");
52
+ const getPathname = (context)=>{
53
+ if ((0, runtime_namespaceObject.isBrowser)()) return window.location.pathname;
54
+ return context.ssrContext?.request?.pathname || '/';
55
+ };
56
+ const getEntryPath = ()=>{
57
+ const basename = (0, context_namespaceObject.getGlobalBasename)();
58
+ if (basename) return '/' === basename ? '' : basename;
59
+ return '';
60
+ };
61
+ const getLanguageFromPath = (pathname, languages, fallbackLanguage)=>{
62
+ const segments = pathname.split('/').filter(Boolean);
63
+ const firstSegment = segments[0];
64
+ if (languages.includes(firstSegment)) return firstSegment;
65
+ return fallbackLanguage;
66
+ };
67
+ const buildLocalizedUrl = (pathname, language, languages)=>{
68
+ const segments = pathname.split('/').filter(Boolean);
69
+ if (segments.length > 0 && languages.includes(segments[0])) segments[0] = language;
70
+ else segments.unshift(language);
71
+ return `/${segments.join('/')}`;
72
+ };
73
+ const detectLanguageFromPath = (pathname, languages, localePathRedirect)=>{
74
+ if (!localePathRedirect) return {
75
+ detected: false
76
+ };
77
+ const relativePath = pathname.replace(getEntryPath(), '');
78
+ const segments = relativePath.split('/').filter(Boolean);
79
+ const firstSegment = segments[0];
80
+ if (firstSegment && languages.includes(firstSegment)) return {
81
+ detected: true,
82
+ language: firstSegment
83
+ };
84
+ return {
85
+ detected: false
86
+ };
87
+ };
88
+ const shouldIgnoreRedirect = (pathname, languages, ignoreRedirectRoutes)=>{
89
+ if (!ignoreRedirectRoutes) return false;
90
+ const segments = pathname.split('/').filter(Boolean);
91
+ let pathWithoutLang = pathname;
92
+ if (segments.length > 0 && languages.includes(segments[0])) pathWithoutLang = `/${segments.slice(1).join('/')}`;
93
+ const normalizedPath = pathWithoutLang.startsWith('/') ? pathWithoutLang : `/${pathWithoutLang}`;
94
+ if ('function' == typeof ignoreRedirectRoutes) return ignoreRedirectRoutes(normalizedPath);
95
+ return ignoreRedirectRoutes.some((pattern)=>normalizedPath === pattern || normalizedPath.startsWith(`${pattern}/`));
96
+ };
97
+ const useRouterHooks = ()=>{
98
+ try {
99
+ const { useLocation, useNavigate, useParams } = __webpack_require__("@modern-js/runtime/router");
100
+ return {
101
+ navigate: useNavigate(),
102
+ location: useLocation(),
103
+ params: useParams(),
104
+ hasRouter: true
105
+ };
106
+ } catch (error) {
107
+ return {
108
+ navigate: null,
109
+ location: null,
110
+ params: {},
111
+ hasRouter: false
112
+ };
113
+ }
114
+ };
115
+ })();
116
+ exports.buildLocalizedUrl = __webpack_exports__.buildLocalizedUrl;
117
+ exports.detectLanguageFromPath = __webpack_exports__.detectLanguageFromPath;
118
+ exports.getEntryPath = __webpack_exports__.getEntryPath;
119
+ exports.getLanguageFromPath = __webpack_exports__.getLanguageFromPath;
120
+ exports.getPathname = __webpack_exports__.getPathname;
121
+ exports.shouldIgnoreRedirect = __webpack_exports__.shouldIgnoreRedirect;
122
+ exports.useRouterHooks = __webpack_exports__.useRouterHooks;
123
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
124
+ "buildLocalizedUrl",
125
+ "detectLanguageFromPath",
126
+ "getEntryPath",
127
+ "getLanguageFromPath",
128
+ "getPathname",
129
+ "shouldIgnoreRedirect",
130
+ "useRouterHooks"
131
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
132
+ Object.defineProperty(exports, '__esModule', {
133
+ value: true
134
+ });