@weglot/translate-react 0.0.0-stage → 0.2.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 (292) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +6 -0
  3. package/README.md +199 -2
  4. package/build/_virtual/rolldown_runtime.cjs +29 -0
  5. package/build/_virtual/rolldown_runtime.mjs +7 -0
  6. package/build/index.cjs +0 -0
  7. package/build/index.d.cts +2 -0
  8. package/build/index.d.mts +2 -0
  9. package/build/index.mjs +1 -0
  10. package/build/locale.d.cts +6 -0
  11. package/build/locale.d.cts.map +1 -0
  12. package/build/locale.d.mts +6 -0
  13. package/build/locale.d.mts.map +1 -0
  14. package/build/metadata/manager.cjs +119 -0
  15. package/build/metadata/manager.mjs +114 -0
  16. package/build/metadata/manager.mjs.map +1 -0
  17. package/build/plugin/build-translator.cjs +210 -0
  18. package/build/plugin/build-translator.mjs +208 -0
  19. package/build/plugin/build-translator.mjs.map +1 -0
  20. package/build/plugin/cleanup.cjs +20 -0
  21. package/build/plugin/cleanup.mjs +20 -0
  22. package/build/plugin/cleanup.mjs.map +1 -0
  23. package/build/plugin/index.cjs +0 -0
  24. package/build/plugin/index.d.cts +2 -0
  25. package/build/plugin/index.d.mts +2 -0
  26. package/build/plugin/index.mjs +1 -0
  27. package/build/plugin/next-compiler-loader.cjs +40 -0
  28. package/build/plugin/next-compiler-loader.d.cts +12 -0
  29. package/build/plugin/next-compiler-loader.d.cts.map +1 -0
  30. package/build/plugin/next-compiler-loader.d.mts +13 -0
  31. package/build/plugin/next-compiler-loader.d.mts.map +1 -0
  32. package/build/plugin/next-compiler-loader.mjs +41 -0
  33. package/build/plugin/next-compiler-loader.mjs.map +1 -0
  34. package/build/plugin/next-config-loader.cjs +13 -0
  35. package/build/plugin/next-config-loader.d.cts +8 -0
  36. package/build/plugin/next-config-loader.d.cts.map +1 -0
  37. package/build/plugin/next-config-loader.d.mts +9 -0
  38. package/build/plugin/next-config-loader.d.mts.map +1 -0
  39. package/build/plugin/next-config-loader.mjs +14 -0
  40. package/build/plugin/next-config-loader.mjs.map +1 -0
  41. package/build/plugin/next-locale-client-loader.cjs +9 -0
  42. package/build/plugin/next-locale-client-loader.d.cts +8 -0
  43. package/build/plugin/next-locale-client-loader.d.cts.map +1 -0
  44. package/build/plugin/next-locale-client-loader.d.mts +9 -0
  45. package/build/plugin/next-locale-client-loader.d.mts.map +1 -0
  46. package/build/plugin/next-locale-client-loader.mjs +10 -0
  47. package/build/plugin/next-locale-client-loader.mjs.map +1 -0
  48. package/build/plugin/next-locale-server-loader.cjs +9 -0
  49. package/build/plugin/next-locale-server-loader.d.cts +8 -0
  50. package/build/plugin/next-locale-server-loader.d.cts.map +1 -0
  51. package/build/plugin/next-locale-server-loader.d.mts +9 -0
  52. package/build/plugin/next-locale-server-loader.d.mts.map +1 -0
  53. package/build/plugin/next-locale-server-loader.mjs +10 -0
  54. package/build/plugin/next-locale-server-loader.mjs.map +1 -0
  55. package/build/plugin/next.cjs +227 -0
  56. package/build/plugin/next.d.cts +9 -0
  57. package/build/plugin/next.d.cts.map +1 -0
  58. package/build/plugin/next.d.mts +9 -0
  59. package/build/plugin/next.d.mts.map +1 -0
  60. package/build/plugin/next.mjs +229 -0
  61. package/build/plugin/next.mjs.map +1 -0
  62. package/build/plugin/transform/babel-compat.cjs +13 -0
  63. package/build/plugin/transform/babel-compat.mjs +10 -0
  64. package/build/plugin/transform/babel-compat.mjs.map +1 -0
  65. package/build/plugin/transform/expression-strings.cjs +145 -0
  66. package/build/plugin/transform/expression-strings.mjs +140 -0
  67. package/build/plugin/transform/expression-strings.mjs.map +1 -0
  68. package/build/plugin/transform/index.cjs +45 -0
  69. package/build/plugin/transform/index.mjs +43 -0
  70. package/build/plugin/transform/index.mjs.map +1 -0
  71. package/build/plugin/transform/metadata.cjs +142 -0
  72. package/build/plugin/transform/metadata.mjs +141 -0
  73. package/build/plugin/transform/metadata.mjs.map +1 -0
  74. package/build/plugin/transform/process-file.cjs +525 -0
  75. package/build/plugin/transform/process-file.mjs +524 -0
  76. package/build/plugin/transform/process-file.mjs.map +1 -0
  77. package/build/plugin/transform/use-i18n.cjs +8 -0
  78. package/build/plugin/transform/use-i18n.mjs +7 -0
  79. package/build/plugin/transform/use-i18n.mjs.map +1 -0
  80. package/build/plugin/transform/utils.cjs +237 -0
  81. package/build/plugin/transform/utils.mjs +219 -0
  82. package/build/plugin/transform/utils.mjs.map +1 -0
  83. package/build/plugin/unplugin.cjs +188 -0
  84. package/build/plugin/unplugin.d.cts +8 -0
  85. package/build/plugin/unplugin.d.cts.map +1 -0
  86. package/build/plugin/unplugin.d.mts +8 -0
  87. package/build/plugin/unplugin.d.mts.map +1 -0
  88. package/build/plugin/unplugin.mjs +188 -0
  89. package/build/plugin/unplugin.mjs.map +1 -0
  90. package/build/plugin/vite.cjs +27 -0
  91. package/build/plugin/vite.d.cts +9 -0
  92. package/build/plugin/vite.d.cts.map +1 -0
  93. package/build/plugin/vite.d.mts +9 -0
  94. package/build/plugin/vite.d.mts.map +1 -0
  95. package/build/plugin/vite.mjs +28 -0
  96. package/build/plugin/vite.mjs.map +1 -0
  97. package/build/plugin/webpack.cjs +26 -0
  98. package/build/plugin/webpack.d.cts +8 -0
  99. package/build/plugin/webpack.d.cts.map +1 -0
  100. package/build/plugin/webpack.d.mts +8 -0
  101. package/build/plugin/webpack.d.mts.map +1 -0
  102. package/build/plugin/webpack.mjs +27 -0
  103. package/build/plugin/webpack.mjs.map +1 -0
  104. package/build/react/client/index.cjs +9 -0
  105. package/build/react/client/index.d.cts +5 -0
  106. package/build/react/client/index.d.mts +5 -0
  107. package/build/react/client/index.mjs +6 -0
  108. package/build/react/client/useTranslation.cjs +71 -0
  109. package/build/react/client/useTranslation.d.cts +42 -0
  110. package/build/react/client/useTranslation.d.cts.map +1 -0
  111. package/build/react/client/useTranslation.d.mts +42 -0
  112. package/build/react/client/useTranslation.d.mts.map +1 -0
  113. package/build/react/client/useTranslation.mjs +71 -0
  114. package/build/react/client/useTranslation.mjs.map +1 -0
  115. package/build/react/next/client.cjs +25 -0
  116. package/build/react/next/client.d.cts +9 -0
  117. package/build/react/next/client.d.cts.map +1 -0
  118. package/build/react/next/client.d.mts +9 -0
  119. package/build/react/next/client.d.mts.map +1 -0
  120. package/build/react/next/client.mjs +24 -0
  121. package/build/react/next/client.mjs.map +1 -0
  122. package/build/react/next/cookie-locale-resolver.cjs +29 -0
  123. package/build/react/next/cookie-locale-resolver.d.cts +33 -0
  124. package/build/react/next/cookie-locale-resolver.d.cts.map +1 -0
  125. package/build/react/next/cookie-locale-resolver.d.mts +33 -0
  126. package/build/react/next/cookie-locale-resolver.d.mts.map +1 -0
  127. package/build/react/next/cookie-locale-resolver.mjs +29 -0
  128. package/build/react/next/cookie-locale-resolver.mjs.map +1 -0
  129. package/build/react/next/server.cjs +21 -0
  130. package/build/react/next/server.d.cts +13 -0
  131. package/build/react/next/server.d.cts.map +1 -0
  132. package/build/react/next/server.d.mts +14 -0
  133. package/build/react/next/server.d.mts.map +1 -0
  134. package/build/react/next/server.mjs +20 -0
  135. package/build/react/next/server.mjs.map +1 -0
  136. package/build/react/server/ServerWeglotProvider.cjs +19 -0
  137. package/build/react/server/ServerWeglotProvider.d.cts +12 -0
  138. package/build/react/server/ServerWeglotProvider.d.cts.map +1 -0
  139. package/build/react/server/ServerWeglotProvider.d.mts +12 -0
  140. package/build/react/server/ServerWeglotProvider.d.mts.map +1 -0
  141. package/build/react/server/ServerWeglotProvider.mjs +19 -0
  142. package/build/react/server/ServerWeglotProvider.mjs.map +1 -0
  143. package/build/react/server/index.cjs +7 -0
  144. package/build/react/server/index.d.cts +4 -0
  145. package/build/react/server/index.d.mts +4 -0
  146. package/build/react/server/index.mjs +5 -0
  147. package/build/react/server/useTranslation.cjs +60 -0
  148. package/build/react/server/useTranslation.d.cts +36 -0
  149. package/build/react/server/useTranslation.d.cts.map +1 -0
  150. package/build/react/server/useTranslation.d.mts +36 -0
  151. package/build/react/server/useTranslation.d.mts.map +1 -0
  152. package/build/react/server/useTranslation.mjs +60 -0
  153. package/build/react/server/useTranslation.mjs.map +1 -0
  154. package/build/react/server-only/index.cjs +42 -0
  155. package/build/react/server-only/index.d.cts +38 -0
  156. package/build/react/server-only/index.d.cts.map +1 -0
  157. package/build/react/server-only/index.d.mts +38 -0
  158. package/build/react/server-only/index.d.mts.map +1 -0
  159. package/build/react/server-only/index.mjs +42 -0
  160. package/build/react/server-only/index.mjs.map +1 -0
  161. package/build/react/server-only/translations.cjs +86 -0
  162. package/build/react/server-only/translations.mjs +86 -0
  163. package/build/react/server-only/translations.mjs.map +1 -0
  164. package/build/react/shared/LocaleSwitcher.cjs +61 -0
  165. package/build/react/shared/LocaleSwitcher.d.cts +71 -0
  166. package/build/react/shared/LocaleSwitcher.d.cts.map +1 -0
  167. package/build/react/shared/LocaleSwitcher.d.mts +71 -0
  168. package/build/react/shared/LocaleSwitcher.d.mts.map +1 -0
  169. package/build/react/shared/LocaleSwitcher.mjs +61 -0
  170. package/build/react/shared/LocaleSwitcher.mjs.map +1 -0
  171. package/build/react/shared/WeglotContext.cjs +14 -0
  172. package/build/react/shared/WeglotContext.d.cts +34 -0
  173. package/build/react/shared/WeglotContext.d.cts.map +1 -0
  174. package/build/react/shared/WeglotContext.d.mts +34 -0
  175. package/build/react/shared/WeglotContext.d.mts.map +1 -0
  176. package/build/react/shared/WeglotContext.mjs +13 -0
  177. package/build/react/shared/WeglotContext.mjs.map +1 -0
  178. package/build/react/shared/WeglotProvider.cjs +250 -0
  179. package/build/react/shared/WeglotProvider.d.cts +60 -0
  180. package/build/react/shared/WeglotProvider.d.cts.map +1 -0
  181. package/build/react/shared/WeglotProvider.d.mts +60 -0
  182. package/build/react/shared/WeglotProvider.d.mts.map +1 -0
  183. package/build/react/shared/WeglotProvider.mjs +250 -0
  184. package/build/react/shared/WeglotProvider.mjs.map +1 -0
  185. package/build/react/shared/render-rich-text.cjs +56 -0
  186. package/build/react/shared/render-rich-text.d.cts +16 -0
  187. package/build/react/shared/render-rich-text.d.cts.map +1 -0
  188. package/build/react/shared/render-rich-text.d.mts +16 -0
  189. package/build/react/shared/render-rich-text.d.mts.map +1 -0
  190. package/build/react/shared/render-rich-text.mjs +55 -0
  191. package/build/react/shared/render-rich-text.mjs.map +1 -0
  192. package/build/react/shared/utils.cjs +34 -0
  193. package/build/react/shared/utils.mjs +35 -0
  194. package/build/react/shared/utils.mjs.map +1 -0
  195. package/build/react/types.d.cts +16 -0
  196. package/build/react/types.d.cts.map +1 -0
  197. package/build/react/types.d.mts +16 -0
  198. package/build/react/types.d.mts.map +1 -0
  199. package/build/runtime/html/segmenter.cjs +226 -0
  200. package/build/runtime/html/segmenter.mjs +225 -0
  201. package/build/runtime/html/segmenter.mjs.map +1 -0
  202. package/build/runtime/index.cjs +6 -0
  203. package/build/runtime/index.d.cts +2 -0
  204. package/build/runtime/index.d.mts +2 -0
  205. package/build/runtime/index.mjs +3 -0
  206. package/build/runtime/translate-dynamic.cjs +144 -0
  207. package/build/runtime/translate-dynamic.d.cts +29 -0
  208. package/build/runtime/translate-dynamic.d.cts.map +1 -0
  209. package/build/runtime/translate-dynamic.d.mts +29 -0
  210. package/build/runtime/translate-dynamic.d.mts.map +1 -0
  211. package/build/runtime/translate-dynamic.mjs +141 -0
  212. package/build/runtime/translate-dynamic.mjs.map +1 -0
  213. package/build/translation-server/logger.cjs +37 -0
  214. package/build/translation-server/logger.mjs +37 -0
  215. package/build/translation-server/logger.mjs.map +1 -0
  216. package/build/translation-server/translation-server.cjs +451 -0
  217. package/build/translation-server/translation-server.mjs +448 -0
  218. package/build/translation-server/translation-server.mjs.map +1 -0
  219. package/build/translators/api.cjs +12 -0
  220. package/build/translators/api.mjs +12 -0
  221. package/build/translators/api.mjs.map +1 -0
  222. package/build/translators/cache-factory.cjs +26 -0
  223. package/build/translators/cache-factory.mjs +27 -0
  224. package/build/translators/cache-factory.mjs.map +1 -0
  225. package/build/translators/local-cache.cjs +132 -0
  226. package/build/translators/local-cache.mjs +130 -0
  227. package/build/translators/local-cache.mjs.map +1 -0
  228. package/build/translators/translation-service.cjs +139 -0
  229. package/build/translators/translation-service.mjs +140 -0
  230. package/build/translators/translation-service.mjs.map +1 -0
  231. package/build/translators/weglot/api-client.cjs +131 -0
  232. package/build/translators/weglot/api-client.mjs +130 -0
  233. package/build/translators/weglot/api-client.mjs.map +1 -0
  234. package/build/translators/weglot/definitions/definitions.generated.cjs +53 -0
  235. package/build/translators/weglot/definitions/definitions.generated.mjs +52 -0
  236. package/build/translators/weglot/definitions/definitions.generated.mjs.map +1 -0
  237. package/build/translators/weglot/engine.cjs +43 -0
  238. package/build/translators/weglot/engine.mjs +40 -0
  239. package/build/translators/weglot/engine.mjs.map +1 -0
  240. package/build/translators/weglot/translator.cjs +67 -0
  241. package/build/translators/weglot/translator.mjs +68 -0
  242. package/build/translators/weglot/translator.mjs.map +1 -0
  243. package/build/translators/weglot/wire-format.cjs +128 -0
  244. package/build/translators/weglot/wire-format.mjs +129 -0
  245. package/build/translators/weglot/wire-format.mjs.map +1 -0
  246. package/build/types.d.cts +153 -0
  247. package/build/types.d.cts.map +1 -0
  248. package/build/types.d.mts +153 -0
  249. package/build/types.d.mts.map +1 -0
  250. package/build/utils/config-factory.cjs +100 -0
  251. package/build/utils/config-factory.mjs +97 -0
  252. package/build/utils/config-factory.mjs.map +1 -0
  253. package/build/utils/hash.cjs +17 -0
  254. package/build/utils/hash.mjs +16 -0
  255. package/build/utils/hash.mjs.map +1 -0
  256. package/build/utils/is-valid-locale.cjs +13 -0
  257. package/build/utils/is-valid-locale.mjs +13 -0
  258. package/build/utils/is-valid-locale.mjs.map +1 -0
  259. package/build/utils/logger.cjs +51 -0
  260. package/build/utils/logger.mjs +50 -0
  261. package/build/utils/logger.mjs.map +1 -0
  262. package/build/utils/path-helpers.cjs +49 -0
  263. package/build/utils/path-helpers.mjs +47 -0
  264. package/build/utils/path-helpers.mjs.map +1 -0
  265. package/build/utils/timeout.cjs +42 -0
  266. package/build/utils/timeout.mjs +41 -0
  267. package/build/utils/timeout.mjs.map +1 -0
  268. package/build/virtual/code-generator.cjs +58 -0
  269. package/build/virtual/code-generator.mjs +57 -0
  270. package/build/virtual/code-generator.mjs.map +1 -0
  271. package/build/virtual/config.cjs +19 -0
  272. package/build/virtual/config.d.cts +15 -0
  273. package/build/virtual/config.d.cts.map +1 -0
  274. package/build/virtual/config.d.mts +15 -0
  275. package/build/virtual/config.d.mts.map +1 -0
  276. package/build/virtual/config.mjs +14 -0
  277. package/build/virtual/config.mjs.map +1 -0
  278. package/build/virtual/locale/client.cjs +23 -0
  279. package/build/virtual/locale/client.d.cts +19 -0
  280. package/build/virtual/locale/client.d.cts.map +1 -0
  281. package/build/virtual/locale/client.d.mts +19 -0
  282. package/build/virtual/locale/client.d.mts.map +1 -0
  283. package/build/virtual/locale/client.mjs +22 -0
  284. package/build/virtual/locale/client.mjs.map +1 -0
  285. package/build/virtual/locale/server.cjs +13 -0
  286. package/build/virtual/locale/server.d.cts +13 -0
  287. package/build/virtual/locale/server.d.cts.map +1 -0
  288. package/build/virtual/locale/server.d.mts +13 -0
  289. package/build/virtual/locale/server.d.mts.map +1 -0
  290. package/build/virtual/locale/server.mjs +13 -0
  291. package/build/virtual/locale/server.mjs.map +1 -0
  292. package/package.json +260 -4
@@ -0,0 +1,525 @@
1
+ const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
2
+ const require_logger = require('../../utils/logger.cjs');
3
+ const require_engine = require('../../translators/weglot/engine.cjs');
4
+ const require_utils = require('./utils.cjs');
5
+ const require_metadata = require('./metadata.cjs');
6
+ const require_babel_compat = require('./babel-compat.cjs');
7
+ const require_expression_strings = require('./expression-strings.cjs');
8
+ let _babel_types = require("@babel/types");
9
+ _babel_types = require_rolldown_runtime.__toESM(_babel_types);
10
+
11
+ //#region src/plugin/transform/process-file.ts
12
+ /**
13
+ * Keep mutations to the current node and children. Do not mutate program inserting imports when processing the component.
14
+ * This should be done in the end.
15
+ * The overall idea is that state is used for side effect tracking. When we exit relevant points, we check if we need to make extra changes at this level.
16
+ * e.g. when we transform elements to insert translations, we record their hashes and add the translation hook call when leaving the component.
17
+ */
18
+ function updateWithMetadataState(state, newState) {
19
+ state.newEntries = [...state.newEntries, ...newState.newEntries];
20
+ state.needsAsyncImport = state.needsAsyncImport || newState.needsAsyncImport;
21
+ return state;
22
+ }
23
+ const SKIP_ATTRIBUTE = "data-weglot-skip";
24
+ /**
25
+ * Elements whose content should not be translated by default.
26
+ * These are typically code-related or technical elements where translation
27
+ * would break functionality or meaning.
28
+ */
29
+ const NON_TRANSLATABLE_ELEMENTS = new Set([
30
+ "code",
31
+ "pre",
32
+ "script",
33
+ "style",
34
+ "kbd",
35
+ "samp",
36
+ "var"
37
+ ]);
38
+ const TRANSLATABLE_ATTRIBUTES = new Set([
39
+ "title",
40
+ "aria-label",
41
+ "aria-description",
42
+ "alt",
43
+ "label",
44
+ "description",
45
+ "placeholder",
46
+ "content",
47
+ "subtitle"
48
+ ]);
49
+ /** The key in `translatableAttributes` whose deltas apply to every element. */
50
+ const ANY_ELEMENT = "*";
51
+ /** Component props are camelCase, HTML attributes lowercase; both mean the same prop. */
52
+ function normalizeAttributeName(name) {
53
+ return name.toLowerCase();
54
+ }
55
+ function collectImportAliases(ast) {
56
+ const aliases = {};
57
+ const body = ast.type === "File" ? ast.program.body : [];
58
+ for (const statement of body) {
59
+ if (statement.type !== "ImportDeclaration") continue;
60
+ for (const specifier of statement.specifiers) if (specifier.type === "ImportSpecifier" && specifier.imported.type === "Identifier" && specifier.imported.name !== specifier.local.name) aliases[specifier.local.name] = specifier.imported.name;
61
+ }
62
+ return aliases;
63
+ }
64
+ /** Keys an element answers to, least specific first so a later match wins:
65
+ * `<Select.Option>` answers to `Option` then `Select.Option`. */
66
+ function elementKeysFor(name, aliases) {
67
+ const canonical = (local) => aliases[local] ?? local;
68
+ if (name.type === "JSXIdentifier") return [...new Set([canonical(name.name), name.name])];
69
+ if (name.type === "JSXMemberExpression") {
70
+ const property = name.property.name;
71
+ const object = name.object.type === "JSXIdentifier" ? name.object.name : void 0;
72
+ if (!object) return [property];
73
+ return [...new Set([
74
+ property,
75
+ `${canonical(object)}.${property}`,
76
+ `${object}.${property}`
77
+ ])];
78
+ }
79
+ return [];
80
+ }
81
+ const reportedCasingMismatches = /* @__PURE__ */ new Set();
82
+ /** A mis-cased key is silently inert, and nothing else tells it apart from a
83
+ * key for a component this file never renders. */
84
+ function warnOnCasingMismatch(elementKeys, configured) {
85
+ for (const key of elementKeys) {
86
+ if (configured[key]) continue;
87
+ for (const configuredKey of Object.keys(configured)) if (configuredKey !== ANY_ELEMENT && configuredKey.toLowerCase() === key.toLowerCase() && !reportedCasingMismatches.has(configuredKey)) {
88
+ reportedCasingMismatches.add(configuredKey);
89
+ require_logger.logger.warn(`translatableAttributes key "${configuredKey}" does not match the element <${key}>; element keys are case-sensitive.`);
90
+ }
91
+ }
92
+ }
93
+ /** `"*"` deltas apply first, then the element's own, so a component can re-add
94
+ * what `"*"` removed. */
95
+ function translatableAttributesFor(elementKeys, configured) {
96
+ const deltas = [...configured[ANY_ELEMENT] ?? [], ...elementKeys.flatMap((key) => configured[key] ?? [])];
97
+ if (deltas.length === 0) {
98
+ warnOnCasingMismatch(elementKeys, configured);
99
+ return TRANSLATABLE_ATTRIBUTES;
100
+ }
101
+ const effective = new Set(TRANSLATABLE_ATTRIBUTES);
102
+ for (const delta of deltas) {
103
+ const removed = delta.startsWith("!");
104
+ const name = normalizeAttributeName(removed ? delta.slice(1) : delta);
105
+ if (removed) effective.delete(name);
106
+ else effective.add(name);
107
+ }
108
+ return effective;
109
+ }
110
+ /**
111
+ * Check if a JSX element should skip translation based on:
112
+ * 1. Element type (code, pre, script, style, etc.)
113
+ * 2. translate="no" attribute (HTML standard)
114
+ * 3. data-weglot-skip attribute
115
+ */
116
+ /** Every branch is registered at build time, so a component asks for each label
117
+ * it might render, not only the first. */
118
+ function translateExpressionStrings(path, state) {
119
+ const component = state.componentsStack.at(-1);
120
+ if (!component) return;
121
+ const enclosing = path.parent.type === "JSXAttribute" ? path.parentPath?.parentPath?.node : void 0;
122
+ if (!require_expression_strings.isTranslatablePosition(path, translatableAttributesFor(enclosing && enclosing.type === "JSXOpeningElement" ? elementKeysFor(enclosing.name, state.importAliases) : [], state.translatableAttributes))) return;
123
+ if ((path.parent.type === "JSXElement" || path.parent.type === "JSXFragment") && shouldSkipTranslationForElement(path.parent)) return;
124
+ const expression = path.get("expression");
125
+ if (expression.node.type === "JSXEmptyExpression") return;
126
+ for (const reference of require_expression_strings.collectConstantReferences(expression)) reference.path.replaceWith(_babel_types.cloneNode(reference.replacement, true));
127
+ for (const literalPath of require_expression_strings.collectLiteralPaths(expression)) {
128
+ const message = literalPath.isTemplateLiteral() ? require_expression_strings.templateToMessage(literalPath.node) : null;
129
+ let text;
130
+ if (literalPath.isTemplateLiteral()) {
131
+ if (!message) continue;
132
+ text = require_utils.normalizeWhitespace(message.text);
133
+ } else {
134
+ text = require_utils.normalizeWhitespace(literalPath.node.value);
135
+ if (!require_expression_strings.isTranslatableText(text)) continue;
136
+ }
137
+ const entry = require_utils.createTranslationEntry("content", text, { componentName: component.name }, state.filePath, literalPath.node.loc?.start.line, literalPath.node.loc?.start.column);
138
+ registerEntry(entry, state, component.name);
139
+ literalPath.replaceWith(require_utils.buildTranslationCall(entry.hash, text, component.translator, message ?? void 0));
140
+ }
141
+ }
142
+ function shouldSkipTranslationForElement(element) {
143
+ if (element.type === "JSXFragment") return false;
144
+ const openingElement = element.openingElement;
145
+ if (openingElement.name.type === "JSXIdentifier") {
146
+ if (NON_TRANSLATABLE_ELEMENTS.has(openingElement.name.name.toLowerCase())) return true;
147
+ }
148
+ for (const attr of openingElement.attributes) if (attr.type === "JSXAttribute" && attr.name.type === "JSXIdentifier") {
149
+ if (attr.name.name === "translate") {
150
+ if (attr.value?.type === "StringLiteral" && attr.value.value === "no") return true;
151
+ }
152
+ if (attr.name.name === SKIP_ATTRIBUTE) return true;
153
+ }
154
+ return false;
155
+ }
156
+ function translateAttributes(node, state) {
157
+ const openingElement = node.openingElement;
158
+ const component = state.componentsStack.at(-1);
159
+ if (!component) return;
160
+ const translatable = translatableAttributesFor(elementKeysFor(openingElement.name, state.importAliases), state.translatableAttributes);
161
+ for (const attr of openingElement.attributes) if (attr.type === "JSXAttribute" && attr.name.type === "JSXIdentifier" && translatable.has(normalizeAttributeName(attr.name.name)) && attr.value?.type === "StringLiteral") {
162
+ const text = require_utils.normalizeWhitespace(attr.value.value);
163
+ if (!require_expression_strings.isTranslatableText(text)) continue;
164
+ const entry = require_utils.createTranslationEntry("attribute", text, {
165
+ attributeName: attr.name.name,
166
+ componentName: component.name
167
+ }, state.filePath, node.loc?.start.line, node.loc?.start.column);
168
+ registerEntry(entry, state, component.name);
169
+ attr.value = require_utils.constructTranslationCall(entry.hash, text, void 0, component.translator);
170
+ }
171
+ }
172
+ /**
173
+ * Check if a JSX element has mixed content that needs rich text translation
174
+ * Mixed content = text nodes + expressions + nested JSX elements (excluding void elements)
175
+ */
176
+ function hasMixedContent(element) {
177
+ const children = element.children;
178
+ if (children.length === 0) return false;
179
+ let state = 0;
180
+ for (const child of children) if (child.type === "JSXText") {
181
+ if (child.value.trim().length > 0) switch (state) {
182
+ case 0:
183
+ state = 1;
184
+ break;
185
+ case 1: break;
186
+ case 2: return true;
187
+ case 3: return true;
188
+ }
189
+ } else if (child.type === "JSXExpressionContainer") switch (state) {
190
+ case 0:
191
+ state = 2;
192
+ break;
193
+ case 1: return true;
194
+ case 2: break;
195
+ case 3: return true;
196
+ }
197
+ else if (child.type === "JSXElement") if (!require_utils.isVoidElement(child)) switch (state) {
198
+ case 0:
199
+ state = 2;
200
+ break;
201
+ case 1: return true;
202
+ case 2: break;
203
+ case 3: return true;
204
+ }
205
+ else switch (state) {
206
+ case 0: break;
207
+ case 1:
208
+ state = 3;
209
+ break;
210
+ case 2: break;
211
+ case 3: break;
212
+ }
213
+ return hasMergeableSiblings(children);
214
+ }
215
+ function hostElementName(child) {
216
+ const name = child.openingElement.name;
217
+ return name.type === "JSXIdentifier" ? name.name.toLowerCase() : null;
218
+ }
219
+ function holdsLiteralText(element) {
220
+ return element.children.some((child) => child.type === "JSXText" && child.value.trim().length > 0 || child.type === "JSXElement" && holdsLiteralText(child));
221
+ }
222
+ /** `<span>Hello </span><span>world</span>` is one sentence, and the state machine
223
+ * above only sees text interleaved with elements. The HTML segmenter merges it too. */
224
+ function hasMergeableSiblings(children) {
225
+ let withText = 0;
226
+ for (const child of children) {
227
+ if (child.type === "JSXText") {
228
+ if (child.value.trim().length > 0) return false;
229
+ continue;
230
+ }
231
+ if (child.type === "JSXExpressionContainer") continue;
232
+ if (child.type !== "JSXElement") return false;
233
+ const name = hostElementName(child);
234
+ if (name === null || !require_engine.INLINE_TAGS.has(name)) return false;
235
+ if (holdsLiteralText(child)) withText++;
236
+ }
237
+ return withText > 1;
238
+ }
239
+ /**
240
+ * Serialize JSX children to a translation string with placeholders
241
+ */
242
+ function serializeJSXChildren(children, state, elementCounts = /* @__PURE__ */ new Map()) {
243
+ let text = "";
244
+ const variables = [];
245
+ const expressions = /* @__PURE__ */ new Map();
246
+ const components = /* @__PURE__ */ new Map();
247
+ for (const child of children) if (child.type === "JSXText") {
248
+ let normalized = require_utils.normalizeWhitespace(child.value);
249
+ const keepsBoundary = (whitespace) => whitespace.length > 0 && !whitespace.includes("\n");
250
+ const leading = /^\s*/.exec(child.value)?.[0] ?? "";
251
+ const trailing = /\s*$/.exec(child.value)?.[0] ?? "";
252
+ if (text.length > 0 && keepsBoundary(leading) && normalized.length > 0 && !text.endsWith(" ")) normalized = ` ${normalized}`;
253
+ if (normalized.length > 0 && keepsBoundary(trailing)) normalized = `${normalized} `;
254
+ text += require_utils.escapeTextForICU(normalized);
255
+ } else if (child.type === "JSXExpressionContainer") {
256
+ const expr = child.expression;
257
+ if (expr.type === "Identifier") {
258
+ text += `{${expr.name}}`;
259
+ if (!variables.includes(expr.name)) variables.push(expr.name);
260
+ } else if (expr.type === "StringLiteral") text += require_utils.escapeTextForICU(expr.value);
261
+ else if (expr.type !== "JSXEmptyExpression") {
262
+ const name = `expression${expressions.size}`;
263
+ text += `{${name}}`;
264
+ expressions.set(name, expr);
265
+ }
266
+ } else if (child.type === "JSXElement") {
267
+ let shouldTranslate = true;
268
+ if (require_utils.isVoidElement(child)) shouldTranslate = false;
269
+ if (shouldSkipTranslationForElement(child)) shouldTranslate = false;
270
+ const openingElement = child.openingElement;
271
+ let elementName = "";
272
+ if (openingElement.name.type === "JSXIdentifier") elementName = openingElement.name.name;
273
+ else elementName = "element";
274
+ const count = elementCounts.get(elementName) || 0;
275
+ elementCounts.set(elementName, count + 1);
276
+ const tagName = `${elementName}${count}`;
277
+ translateAttributes(child, state);
278
+ if (shouldTranslate) {
279
+ const nested = serializeJSXChildren(child.children, state, elementCounts);
280
+ text += `<${tagName}>${nested.text}</${tagName}>`;
281
+ for (const v of nested.variables) if (!variables.includes(v)) variables.push(v);
282
+ for (const [nestedTag, nestedElement] of nested.components) components.set(nestedTag, nestedElement);
283
+ } else {
284
+ text += `<${tagName}></${tagName}>`;
285
+ child.extra = {
286
+ ...child.extra,
287
+ shouldTranslate: false
288
+ };
289
+ }
290
+ components.set(tagName, child);
291
+ }
292
+ return {
293
+ text,
294
+ variables,
295
+ expressions,
296
+ components
297
+ };
298
+ }
299
+ function transformVoidElement(node, state) {
300
+ translateAttributes(node, state);
301
+ }
302
+ function getTranslationScope(node, state) {
303
+ if (hasMixedContent(node)) {
304
+ const serialized = serializeJSXChildren(node.children, state);
305
+ const text$1 = serialized.text.trim();
306
+ if (text$1.length === 0) return null;
307
+ return {
308
+ kind: "mixed",
309
+ text: text$1,
310
+ args: {
311
+ variables: serialized.variables,
312
+ expressions: serialized.expressions,
313
+ components: serialized.components
314
+ }
315
+ };
316
+ }
317
+ const textNodeIndex = node.children.findIndex((child) => child.type === "JSXText" && child.value.trim().length > 0);
318
+ if (textNodeIndex === -1) return null;
319
+ if (!node.children.every((child) => child.type === "JSXText" || child.type === "JSXElement" && require_utils.isVoidElement(child))) return null;
320
+ const textNode = node.children[textNodeIndex];
321
+ const text = require_utils.normalizeWhitespace(textNode.value);
322
+ if (text.length === 0) return null;
323
+ return {
324
+ kind: "text",
325
+ text,
326
+ textNodeIndex
327
+ };
328
+ }
329
+ function registerEntry(entry, state, componentName) {
330
+ state.newEntries.push(entry);
331
+ const hashes = state.componentHashes.get(componentName) ?? [];
332
+ hashes.push(entry.hash);
333
+ state.componentHashes.set(componentName, hashes);
334
+ }
335
+ function rewriteChildren(path, state, translationScope, entryHash) {
336
+ const translator = state.componentsStack.at(-1)?.translator ?? require_utils.DEFAULT_TRANSLATOR;
337
+ if (translationScope.kind === "mixed") {
338
+ path.node.children = [require_utils.constructTranslationCall(entryHash, translationScope.text, translationScope.args, translator)];
339
+ return;
340
+ }
341
+ const { textNodeIndex, text } = translationScope;
342
+ path.node.children = path.node.children.map((child, index) => {
343
+ if (index === textNodeIndex) return require_utils.constructTranslationCall(entryHash, text, void 0, translator);
344
+ if (child.type === "JSXElement") transformVoidElement(child, state);
345
+ return child;
346
+ });
347
+ }
348
+ function processJSXElement(path, state) {
349
+ const component = state.componentsStack.at(-1);
350
+ if (!component) return;
351
+ const scope = getTranslationScope(path.node, state);
352
+ if (!scope) return;
353
+ const entry = require_utils.createTranslationEntry("content", scope.text, { componentName: component.name }, state.filePath, path.node.loc?.start.line, path.node.loc?.start.column);
354
+ registerEntry(entry, state, component.name);
355
+ rewriteChildren(path, state, scope, entry.hash);
356
+ path.skip();
357
+ }
358
+ /**
359
+ * Inject dynamic locale attribute into <html> elements
360
+ * Transforms: <html> → <html lang={locale}>
361
+ *
362
+ * Only injects if:
363
+ * 1. Element is <html>
364
+ * 2. No existing lang/language attribute
365
+ * 3. We're inside a React component (has locale context)
366
+ */
367
+ function injectHtmlLangAttribute(node, state) {
368
+ const openingElement = node.openingElement;
369
+ if (openingElement.name.type !== "JSXIdentifier" || openingElement.name.name !== "html") return;
370
+ if (openingElement.attributes.some((attr) => {
371
+ return attr.type === "JSXAttribute" && attr.name.type === "JSXIdentifier" && (attr.name.name === "lang" || attr.name.name === "language");
372
+ })) return;
373
+ const component = state.componentsStack.at(-1);
374
+ if (!component) return;
375
+ const langAttr = _babel_types.jsxAttribute(_babel_types.jsxIdentifier("lang"), _babel_types.jsxExpressionContainer(_babel_types.identifier("locale")));
376
+ openingElement.attributes.push(langAttr);
377
+ require_logger.logger.debug(`Injected locale attribute into <html> element in component: ${component.name}`);
378
+ state.componentsNeedingLocale.add(component.name);
379
+ }
380
+ /**
381
+ * Inject translation hook into a component when it's done processing
382
+ * Called when popping component from stack
383
+ */
384
+ function injectTranslationHook(path, component, state) {
385
+ const needsLocale = state.componentsNeedingLocale.has(component.name);
386
+ const collected = state.componentHashes.get(component.name);
387
+ const hashes = collected && [...new Set(collected)];
388
+ if ((!hashes || hashes.length === 0) && !needsLocale) return;
389
+ if (component.isAsync) {
390
+ require_utils.injectServerHook(path, hashes ?? [], needsLocale, component.translator);
391
+ state.needsAsyncImport = true;
392
+ } else {
393
+ require_utils.injectUnifiedHook(path, hashes ?? [], needsLocale, component.translator);
394
+ state.needsUnifiedImport = true;
395
+ }
396
+ }
397
+ /**
398
+ * Add translation imports at the top of the file
399
+ * Called on Program exit after all components have been processed
400
+ */
401
+ function addTranslationImports(programPath, state) {
402
+ if (state.needsAsyncImport) programPath.node.body.unshift(require_utils.constructServerImport());
403
+ if (state.needsUnifiedImport) programPath.node.body.unshift(require_utils.comstructUnifiedImport());
404
+ }
405
+ /**
406
+ * Handle component function detection and state update
407
+ */
408
+ function processComponentFunction(path, state) {
409
+ if (!require_utils.isReactComponent(path)) {
410
+ path.skip();
411
+ require_logger.logger.debug(`Skipping non-React component: ${path.node.type}`);
412
+ return;
413
+ }
414
+ const componentName = require_utils.inferComponentName(path);
415
+ if (!componentName) {
416
+ path.skip();
417
+ return;
418
+ }
419
+ if (state.componentsStack.length > 0 && !require_utils.isComponentName(componentName)) {
420
+ require_logger.logger.debug(`Folding helper ${componentName} into its component`);
421
+ return;
422
+ }
423
+ if (!state.componentHashes.has(componentName)) state.componentHashes.set(componentName, []);
424
+ const componentEntry = {
425
+ name: componentName,
426
+ isAsync: path.node.async,
427
+ translator: require_utils.translatorNameFor(path)
428
+ };
429
+ state.componentsStack.push(componentEntry);
430
+ require_logger.logger.debug(`Component ${JSON.stringify(componentEntry)} entered`);
431
+ path.setData("componentName", componentName);
432
+ path.traverse(expressionStringVisitors, { visitorState: state });
433
+ path.traverse(componentVisitors, { visitorState: state });
434
+ const component = state.componentsStack.pop();
435
+ if (component) injectTranslationHook(path, component, state);
436
+ path.skip();
437
+ }
438
+ const expressionStringVisitors = {
439
+ JSXExpressionContainer(path) {
440
+ translateExpressionStrings(path, this.visitorState);
441
+ },
442
+ FunctionDeclaration(path) {
443
+ if (require_utils.isNestedComponent(path)) path.skip();
444
+ },
445
+ ArrowFunctionExpression(path) {
446
+ if (require_utils.isNestedComponent(path)) path.skip();
447
+ },
448
+ FunctionExpression(path) {
449
+ if (require_utils.isNestedComponent(path)) path.skip();
450
+ }
451
+ };
452
+ const componentVisitors = {
453
+ FunctionDeclaration(path) {
454
+ processComponentFunction(path, this.visitorState);
455
+ },
456
+ ArrowFunctionExpression(path) {
457
+ processComponentFunction(path, this.visitorState);
458
+ },
459
+ FunctionExpression(path) {
460
+ processComponentFunction(path, this.visitorState);
461
+ },
462
+ JSXFragment(path) {
463
+ processJSXElement(path, this.visitorState);
464
+ },
465
+ JSXElement(path) {
466
+ translateAttributes(path.node, this.visitorState);
467
+ injectHtmlLangAttribute(path.node, this.visitorState);
468
+ if (shouldSkipTranslationForElement(path.node)) {
469
+ path.skip();
470
+ return;
471
+ }
472
+ processJSXElement(path, this.visitorState);
473
+ }
474
+ };
475
+ const fileVisitors = {
476
+ ExportNamedDeclaration(path) {
477
+ const metadataState = require_metadata.processNextStaticMetadata(path, { filePath: this.visitorState.filePath });
478
+ if (metadataState) updateWithMetadataState(this.visitorState, metadataState);
479
+ },
480
+ FunctionDeclaration(path) {
481
+ const metadataState = require_metadata.processNextDynamicMetadata(path, { filePath: this.visitorState.filePath });
482
+ if (metadataState) updateWithMetadataState(this.visitorState, metadataState);
483
+ processComponentFunction(path, this.visitorState);
484
+ },
485
+ ArrowFunctionExpression(path) {
486
+ processComponentFunction(path, this.visitorState);
487
+ },
488
+ FunctionExpression(path) {
489
+ processComponentFunction(path, this.visitorState);
490
+ }
491
+ };
492
+ /**
493
+ * Create Babel visitors for auto-translation (single-pass transformation)
494
+ */
495
+ const programVisitor = { Program: {
496
+ enter(path) {
497
+ if (this.visitorState.needsDirective && !require_utils.hasUseI18nDirective(path)) {
498
+ path.skip();
499
+ return;
500
+ }
501
+ path.traverse(fileVisitors, { visitorState: this.visitorState });
502
+ },
503
+ exit(path) {
504
+ if (this.visitorState.needsUnifiedImport || this.visitorState.needsAsyncImport) addTranslationImports(path, this.visitorState);
505
+ }
506
+ } };
507
+ function processFile(ast, options) {
508
+ const state = {
509
+ needsDirective: options.needsDirective,
510
+ filePath: options.relativeFilePath,
511
+ newEntries: [],
512
+ componentsStack: [],
513
+ componentHashes: /* @__PURE__ */ new Map(),
514
+ needsUnifiedImport: false,
515
+ needsAsyncImport: false,
516
+ componentsNeedingLocale: /* @__PURE__ */ new Set(),
517
+ translatableAttributes: options.translatableAttributes ?? {},
518
+ importAliases: collectImportAliases(ast)
519
+ };
520
+ require_babel_compat.traverse(ast, programVisitor, void 0, { visitorState: state });
521
+ return state.newEntries;
522
+ }
523
+
524
+ //#endregion
525
+ exports.processFile = processFile;