@lingo.dev/compiler 0.1.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 (300) hide show
  1. package/LICENSE.md +201 -0
  2. package/README.md +192 -0
  3. package/build/_virtual/rolldown_runtime.cjs +29 -0
  4. package/build/_virtual/rolldown_runtime.mjs +7 -0
  5. package/build/index.cjs +0 -0
  6. package/build/index.d.cts +2 -0
  7. package/build/index.d.mts +2 -0
  8. package/build/index.mjs +1 -0
  9. package/build/metadata/manager.cjs +131 -0
  10. package/build/metadata/manager.mjs +123 -0
  11. package/build/metadata/manager.mjs.map +1 -0
  12. package/build/plugin/build-translator.cjs +198 -0
  13. package/build/plugin/build-translator.mjs +196 -0
  14. package/build/plugin/build-translator.mjs.map +1 -0
  15. package/build/plugin/cleanup.cjs +20 -0
  16. package/build/plugin/cleanup.mjs +20 -0
  17. package/build/plugin/cleanup.mjs.map +1 -0
  18. package/build/plugin/next-compiler-loader.cjs +41 -0
  19. package/build/plugin/next-compiler-loader.d.cts +12 -0
  20. package/build/plugin/next-compiler-loader.d.cts.map +1 -0
  21. package/build/plugin/next-compiler-loader.d.mts +13 -0
  22. package/build/plugin/next-compiler-loader.d.mts.map +1 -0
  23. package/build/plugin/next-compiler-loader.mjs +42 -0
  24. package/build/plugin/next-compiler-loader.mjs.map +1 -0
  25. package/build/plugin/next-config-loader.cjs +13 -0
  26. package/build/plugin/next-config-loader.d.cts +8 -0
  27. package/build/plugin/next-config-loader.d.cts.map +1 -0
  28. package/build/plugin/next-config-loader.d.mts +9 -0
  29. package/build/plugin/next-config-loader.d.mts.map +1 -0
  30. package/build/plugin/next-config-loader.mjs +14 -0
  31. package/build/plugin/next-config-loader.mjs.map +1 -0
  32. package/build/plugin/next-locale-client-loader.cjs +9 -0
  33. package/build/plugin/next-locale-client-loader.d.cts +8 -0
  34. package/build/plugin/next-locale-client-loader.d.cts.map +1 -0
  35. package/build/plugin/next-locale-client-loader.d.mts +9 -0
  36. package/build/plugin/next-locale-client-loader.d.mts.map +1 -0
  37. package/build/plugin/next-locale-client-loader.mjs +10 -0
  38. package/build/plugin/next-locale-client-loader.mjs.map +1 -0
  39. package/build/plugin/next-locale-server-loader.cjs +9 -0
  40. package/build/plugin/next-locale-server-loader.d.cts +8 -0
  41. package/build/plugin/next-locale-server-loader.d.cts.map +1 -0
  42. package/build/plugin/next-locale-server-loader.d.mts +9 -0
  43. package/build/plugin/next-locale-server-loader.d.mts.map +1 -0
  44. package/build/plugin/next-locale-server-loader.mjs +10 -0
  45. package/build/plugin/next-locale-server-loader.mjs.map +1 -0
  46. package/build/plugin/next.cjs +220 -0
  47. package/build/plugin/next.d.cts +9 -0
  48. package/build/plugin/next.d.cts.map +1 -0
  49. package/build/plugin/next.d.mts +9 -0
  50. package/build/plugin/next.d.mts.map +1 -0
  51. package/build/plugin/next.mjs +222 -0
  52. package/build/plugin/next.mjs.map +1 -0
  53. package/build/plugin/transform/babel-compat.cjs +13 -0
  54. package/build/plugin/transform/babel-compat.mjs +10 -0
  55. package/build/plugin/transform/babel-compat.mjs.map +1 -0
  56. package/build/plugin/transform/index.cjs +44 -0
  57. package/build/plugin/transform/index.mjs +42 -0
  58. package/build/plugin/transform/index.mjs.map +1 -0
  59. package/build/plugin/transform/metadata.cjs +142 -0
  60. package/build/plugin/transform/metadata.mjs +141 -0
  61. package/build/plugin/transform/metadata.mjs.map +1 -0
  62. package/build/plugin/transform/parse-override.cjs +145 -0
  63. package/build/plugin/transform/parse-override.mjs +144 -0
  64. package/build/plugin/transform/parse-override.mjs.map +1 -0
  65. package/build/plugin/transform/process-file.cjs +391 -0
  66. package/build/plugin/transform/process-file.mjs +390 -0
  67. package/build/plugin/transform/process-file.mjs.map +1 -0
  68. package/build/plugin/transform/use-i18n.cjs +8 -0
  69. package/build/plugin/transform/use-i18n.mjs +7 -0
  70. package/build/plugin/transform/use-i18n.mjs.map +1 -0
  71. package/build/plugin/transform/utils.cjs +205 -0
  72. package/build/plugin/transform/utils.mjs +192 -0
  73. package/build/plugin/transform/utils.mjs.map +1 -0
  74. package/build/plugin/unplugin.cjs +188 -0
  75. package/build/plugin/unplugin.d.cts +8 -0
  76. package/build/plugin/unplugin.d.cts.map +1 -0
  77. package/build/plugin/unplugin.d.mts +8 -0
  78. package/build/plugin/unplugin.d.mts.map +1 -0
  79. package/build/plugin/unplugin.mjs +186 -0
  80. package/build/plugin/unplugin.mjs.map +1 -0
  81. package/build/plugin/vite.cjs +28 -0
  82. package/build/plugin/vite.d.cts +9 -0
  83. package/build/plugin/vite.d.cts.map +1 -0
  84. package/build/plugin/vite.d.mts +9 -0
  85. package/build/plugin/vite.d.mts.map +1 -0
  86. package/build/plugin/vite.mjs +29 -0
  87. package/build/plugin/vite.mjs.map +1 -0
  88. package/build/plugin/webpack.cjs +27 -0
  89. package/build/plugin/webpack.d.cts +8 -0
  90. package/build/plugin/webpack.d.cts.map +1 -0
  91. package/build/plugin/webpack.d.mts +8 -0
  92. package/build/plugin/webpack.d.mts.map +1 -0
  93. package/build/plugin/webpack.mjs +28 -0
  94. package/build/plugin/webpack.mjs.map +1 -0
  95. package/build/react/client/index.cjs +9 -0
  96. package/build/react/client/index.d.cts +5 -0
  97. package/build/react/client/index.d.mts +5 -0
  98. package/build/react/client/index.mjs +6 -0
  99. package/build/react/client/useTranslation.cjs +71 -0
  100. package/build/react/client/useTranslation.d.cts +42 -0
  101. package/build/react/client/useTranslation.d.cts.map +1 -0
  102. package/build/react/client/useTranslation.d.mts +42 -0
  103. package/build/react/client/useTranslation.d.mts.map +1 -0
  104. package/build/react/client/useTranslation.mjs +71 -0
  105. package/build/react/client/useTranslation.mjs.map +1 -0
  106. package/build/react/next/client.cjs +25 -0
  107. package/build/react/next/client.d.cts +9 -0
  108. package/build/react/next/client.d.cts.map +1 -0
  109. package/build/react/next/client.d.mts +9 -0
  110. package/build/react/next/client.d.mts.map +1 -0
  111. package/build/react/next/client.mjs +24 -0
  112. package/build/react/next/client.mjs.map +1 -0
  113. package/build/react/next/cookie-locale-resolver.cjs +29 -0
  114. package/build/react/next/cookie-locale-resolver.d.cts +33 -0
  115. package/build/react/next/cookie-locale-resolver.d.cts.map +1 -0
  116. package/build/react/next/cookie-locale-resolver.d.mts +33 -0
  117. package/build/react/next/cookie-locale-resolver.d.mts.map +1 -0
  118. package/build/react/next/cookie-locale-resolver.mjs +29 -0
  119. package/build/react/next/cookie-locale-resolver.mjs.map +1 -0
  120. package/build/react/next/server.cjs +21 -0
  121. package/build/react/next/server.d.cts +13 -0
  122. package/build/react/next/server.d.cts.map +1 -0
  123. package/build/react/next/server.d.mts +14 -0
  124. package/build/react/next/server.d.mts.map +1 -0
  125. package/build/react/next/server.mjs +20 -0
  126. package/build/react/next/server.mjs.map +1 -0
  127. package/build/react/server/ServerLingoProvider.cjs +19 -0
  128. package/build/react/server/ServerLingoProvider.d.cts +12 -0
  129. package/build/react/server/ServerLingoProvider.d.cts.map +1 -0
  130. package/build/react/server/ServerLingoProvider.d.mts +12 -0
  131. package/build/react/server/ServerLingoProvider.d.mts.map +1 -0
  132. package/build/react/server/ServerLingoProvider.mjs +19 -0
  133. package/build/react/server/ServerLingoProvider.mjs.map +1 -0
  134. package/build/react/server/index.cjs +7 -0
  135. package/build/react/server/index.d.cts +4 -0
  136. package/build/react/server/index.d.mts +4 -0
  137. package/build/react/server/index.mjs +5 -0
  138. package/build/react/server/useTranslation.cjs +60 -0
  139. package/build/react/server/useTranslation.d.cts +36 -0
  140. package/build/react/server/useTranslation.d.cts.map +1 -0
  141. package/build/react/server/useTranslation.d.mts +36 -0
  142. package/build/react/server/useTranslation.d.mts.map +1 -0
  143. package/build/react/server/useTranslation.mjs +60 -0
  144. package/build/react/server/useTranslation.mjs.map +1 -0
  145. package/build/react/server-only/index.cjs +42 -0
  146. package/build/react/server-only/index.d.cts +38 -0
  147. package/build/react/server-only/index.d.cts.map +1 -0
  148. package/build/react/server-only/index.d.mts +38 -0
  149. package/build/react/server-only/index.d.mts.map +1 -0
  150. package/build/react/server-only/index.mjs +42 -0
  151. package/build/react/server-only/index.mjs.map +1 -0
  152. package/build/react/server-only/translations.cjs +85 -0
  153. package/build/react/server-only/translations.mjs +85 -0
  154. package/build/react/server-only/translations.mjs.map +1 -0
  155. package/build/react/shared/LingoContext.cjs +14 -0
  156. package/build/react/shared/LingoContext.d.cts +41 -0
  157. package/build/react/shared/LingoContext.d.cts.map +1 -0
  158. package/build/react/shared/LingoContext.d.mts +41 -0
  159. package/build/react/shared/LingoContext.d.mts.map +1 -0
  160. package/build/react/shared/LingoContext.mjs +13 -0
  161. package/build/react/shared/LingoContext.mjs.map +1 -0
  162. package/build/react/shared/LingoProvider.cjs +274 -0
  163. package/build/react/shared/LingoProvider.d.cts +76 -0
  164. package/build/react/shared/LingoProvider.d.cts.map +1 -0
  165. package/build/react/shared/LingoProvider.d.mts +76 -0
  166. package/build/react/shared/LingoProvider.d.mts.map +1 -0
  167. package/build/react/shared/LingoProvider.mjs +274 -0
  168. package/build/react/shared/LingoProvider.mjs.map +1 -0
  169. package/build/react/shared/LocaleSwitcher.cjs +61 -0
  170. package/build/react/shared/LocaleSwitcher.d.cts +71 -0
  171. package/build/react/shared/LocaleSwitcher.d.cts.map +1 -0
  172. package/build/react/shared/LocaleSwitcher.d.mts +71 -0
  173. package/build/react/shared/LocaleSwitcher.d.mts.map +1 -0
  174. package/build/react/shared/LocaleSwitcher.mjs +61 -0
  175. package/build/react/shared/LocaleSwitcher.mjs.map +1 -0
  176. package/build/react/shared/render-rich-text.cjs +55 -0
  177. package/build/react/shared/render-rich-text.d.cts +17 -0
  178. package/build/react/shared/render-rich-text.d.cts.map +1 -0
  179. package/build/react/shared/render-rich-text.d.mts +17 -0
  180. package/build/react/shared/render-rich-text.d.mts.map +1 -0
  181. package/build/react/shared/render-rich-text.mjs +54 -0
  182. package/build/react/shared/render-rich-text.mjs.map +1 -0
  183. package/build/react/shared/utils.cjs +34 -0
  184. package/build/react/shared/utils.mjs +35 -0
  185. package/build/react/shared/utils.mjs.map +1 -0
  186. package/build/react/types.d.cts +16 -0
  187. package/build/react/types.d.cts.map +1 -0
  188. package/build/react/types.d.mts +16 -0
  189. package/build/react/types.d.mts.map +1 -0
  190. package/build/translation-server/logger.cjs +37 -0
  191. package/build/translation-server/logger.mjs +37 -0
  192. package/build/translation-server/logger.mjs.map +1 -0
  193. package/build/translation-server/translation-server.cjs +547 -0
  194. package/build/translation-server/translation-server.mjs +544 -0
  195. package/build/translation-server/translation-server.mjs.map +1 -0
  196. package/build/translation-server/ws-events.cjs +15 -0
  197. package/build/translation-server/ws-events.mjs +15 -0
  198. package/build/translation-server/ws-events.mjs.map +1 -0
  199. package/build/translators/api.cjs +12 -0
  200. package/build/translators/api.mjs +12 -0
  201. package/build/translators/api.mjs.map +1 -0
  202. package/build/translators/cache-factory.cjs +26 -0
  203. package/build/translators/cache-factory.mjs +27 -0
  204. package/build/translators/cache-factory.mjs.map +1 -0
  205. package/build/translators/lingo/model-factory.cjs +179 -0
  206. package/build/translators/lingo/model-factory.mjs +174 -0
  207. package/build/translators/lingo/model-factory.mjs.map +1 -0
  208. package/build/translators/lingo/prompt.cjs +43 -0
  209. package/build/translators/lingo/prompt.mjs +43 -0
  210. package/build/translators/lingo/prompt.mjs.map +1 -0
  211. package/build/translators/lingo/service.cjs +152 -0
  212. package/build/translators/lingo/service.mjs +152 -0
  213. package/build/translators/lingo/service.mjs.map +1 -0
  214. package/build/translators/lingo/shots.cjs +28 -0
  215. package/build/translators/lingo/shots.mjs +28 -0
  216. package/build/translators/lingo/shots.mjs.map +1 -0
  217. package/build/translators/local-cache.cjs +115 -0
  218. package/build/translators/local-cache.mjs +113 -0
  219. package/build/translators/local-cache.mjs.map +1 -0
  220. package/build/translators/parse-xml.cjs +109 -0
  221. package/build/translators/parse-xml.mjs +108 -0
  222. package/build/translators/parse-xml.mjs.map +1 -0
  223. package/build/translators/pluralization/icu-validator.cjs +36 -0
  224. package/build/translators/pluralization/icu-validator.mjs +36 -0
  225. package/build/translators/pluralization/icu-validator.mjs.map +1 -0
  226. package/build/translators/pluralization/pattern-detector.cjs +25 -0
  227. package/build/translators/pluralization/pattern-detector.mjs +25 -0
  228. package/build/translators/pluralization/pattern-detector.mjs.map +1 -0
  229. package/build/translators/pluralization/prompt.cjs +98 -0
  230. package/build/translators/pluralization/prompt.mjs +98 -0
  231. package/build/translators/pluralization/prompt.mjs.map +1 -0
  232. package/build/translators/pluralization/service.cjs +247 -0
  233. package/build/translators/pluralization/service.mjs +247 -0
  234. package/build/translators/pluralization/service.mjs.map +1 -0
  235. package/build/translators/pluralization/shots.cjs +53 -0
  236. package/build/translators/pluralization/shots.mjs +53 -0
  237. package/build/translators/pluralization/shots.mjs.map +1 -0
  238. package/build/translators/pluralization/types.d.cts +17 -0
  239. package/build/translators/pluralization/types.d.cts.map +1 -0
  240. package/build/translators/pluralization/types.d.mts +17 -0
  241. package/build/translators/pluralization/types.d.mts.map +1 -0
  242. package/build/translators/pseudotranslator/index.cjs +129 -0
  243. package/build/translators/pseudotranslator/index.mjs +129 -0
  244. package/build/translators/pseudotranslator/index.mjs.map +1 -0
  245. package/build/translators/translation-service.cjs +182 -0
  246. package/build/translators/translation-service.mjs +183 -0
  247. package/build/translators/translation-service.mjs.map +1 -0
  248. package/build/translators/translator-factory.cjs +49 -0
  249. package/build/translators/translator-factory.mjs +50 -0
  250. package/build/translators/translator-factory.mjs.map +1 -0
  251. package/build/types.d.cts +161 -0
  252. package/build/types.d.cts.map +1 -0
  253. package/build/types.d.mts +161 -0
  254. package/build/types.d.mts.map +1 -0
  255. package/build/utils/config-factory.cjs +58 -0
  256. package/build/utils/config-factory.mjs +58 -0
  257. package/build/utils/config-factory.mjs.map +1 -0
  258. package/build/utils/hash.cjs +17 -0
  259. package/build/utils/hash.mjs +16 -0
  260. package/build/utils/hash.mjs.map +1 -0
  261. package/build/utils/is-valid-locale.cjs +14 -0
  262. package/build/utils/is-valid-locale.mjs +14 -0
  263. package/build/utils/is-valid-locale.mjs.map +1 -0
  264. package/build/utils/logger.cjs +51 -0
  265. package/build/utils/logger.mjs +50 -0
  266. package/build/utils/logger.mjs.map +1 -0
  267. package/build/utils/path-helpers.cjs +49 -0
  268. package/build/utils/path-helpers.mjs +47 -0
  269. package/build/utils/path-helpers.mjs.map +1 -0
  270. package/build/utils/timeout.cjs +42 -0
  271. package/build/utils/timeout.mjs +41 -0
  272. package/build/utils/timeout.mjs.map +1 -0
  273. package/build/virtual/code-generator.cjs +54 -0
  274. package/build/virtual/code-generator.mjs +53 -0
  275. package/build/virtual/code-generator.mjs.map +1 -0
  276. package/build/virtual/config.cjs +10 -0
  277. package/build/virtual/config.d.cts +9 -0
  278. package/build/virtual/config.d.cts.map +1 -0
  279. package/build/virtual/config.d.mts +9 -0
  280. package/build/virtual/config.d.mts.map +1 -0
  281. package/build/virtual/config.mjs +8 -0
  282. package/build/virtual/config.mjs.map +1 -0
  283. package/build/virtual/locale/client.cjs +23 -0
  284. package/build/virtual/locale/client.d.cts +19 -0
  285. package/build/virtual/locale/client.d.cts.map +1 -0
  286. package/build/virtual/locale/client.d.mts +19 -0
  287. package/build/virtual/locale/client.d.mts.map +1 -0
  288. package/build/virtual/locale/client.mjs +22 -0
  289. package/build/virtual/locale/client.mjs.map +1 -0
  290. package/build/virtual/locale/server.cjs +13 -0
  291. package/build/virtual/locale/server.d.cts +13 -0
  292. package/build/virtual/locale/server.d.cts.map +1 -0
  293. package/build/virtual/locale/server.d.mts +13 -0
  294. package/build/virtual/locale/server.d.mts.map +1 -0
  295. package/build/virtual/locale/server.mjs +13 -0
  296. package/build/virtual/locale/server.mjs.map +1 -0
  297. package/build/widget/lingo-dev-widget.cjs +228 -0
  298. package/build/widget/lingo-dev-widget.mjs +229 -0
  299. package/build/widget/lingo-dev-widget.mjs.map +1 -0
  300. package/package.json +189 -0
@@ -0,0 +1,186 @@
1
+ import { logger } from "../utils/logger.mjs";
2
+ import { createLingoConfig } from "../utils/config-factory.mjs";
3
+ import { MetadataManager, cleanupExistingMetadata, getMetadataPath } from "../metadata/manager.mjs";
4
+ import { startTranslationServer } from "../translation-server/translation-server.mjs";
5
+ import { processBuildTranslations } from "./build-translator.mjs";
6
+ import { registerCleanupOnCurrentProcess } from "./cleanup.mjs";
7
+ import { useI18nRegex } from "./transform/use-i18n.mjs";
8
+ import { transformComponent } from "./transform/index.mjs";
9
+ import { generateClientLocaleModule, generateConfigModule, generateServerLocaleModule } from "../virtual/code-generator.mjs";
10
+ import path from "path";
11
+ import fs from "fs";
12
+ import { createUnplugin } from "unplugin";
13
+
14
+ //#region src/plugin/unplugin.ts
15
+ let translationServer;
16
+ const PLUGIN_NAME = "lingo-compiler";
17
+ function tryLocalOrReturnVirtual(config, fileName, virtualName) {
18
+ const customPath = path.join(config.sourceRoot, config.lingoDir, fileName);
19
+ if (fs.existsSync(customPath)) return customPath;
20
+ return virtualName;
21
+ }
22
+ /**
23
+ * Single source of truth for virtual modules
24
+ * Each entry defines both resolver (import path → virtual ID) and loader (virtual ID → code)
25
+ *
26
+ * If customFileCheck is defined, the specified file will be first searched for, and if not found virtual module will be used.
27
+ */
28
+ const virtualModules = {
29
+ "@lingo.dev/compiler/virtual/config": {
30
+ virtualId: "\0virtual:lingo-config",
31
+ loader: (config) => generateConfigModule(config),
32
+ customFileCheck: void 0
33
+ },
34
+ "@lingo.dev/compiler/virtual/locale/server": {
35
+ virtualId: "\0virtual:locale-resolver.server",
36
+ loader: (config) => generateServerLocaleModule(config),
37
+ customFileCheck: "locale-resolver.server.ts"
38
+ },
39
+ "@lingo.dev/compiler/virtual/locale/client": {
40
+ virtualId: "\0virtual:locale-resolver.client",
41
+ loader: (config) => generateClientLocaleModule(config),
42
+ customFileCheck: "locale-resolver.client.ts"
43
+ }
44
+ };
45
+ const virtualModulesResolvers = Object.fromEntries(Object.entries(virtualModules).map(([importPath, module]) => [importPath, (config) => module.customFileCheck ? tryLocalOrReturnVirtual(config, module.customFileCheck, module.virtualId) : module.virtualId]));
46
+ const virtualModulesLoaders = Object.fromEntries(Object.values(virtualModules).map((value) => [value.virtualId, value.loader]));
47
+ /**
48
+ * Universal plugin for Lingo.dev compiler
49
+ * Supports Vite, Webpack
50
+ */
51
+ const lingoUnplugin = createUnplugin((options) => {
52
+ const config = createLingoConfig(options);
53
+ const isDev = config.environment === "development";
54
+ const startPort = config.dev.translationServerStartPort;
55
+ let webpackMode;
56
+ const getMetadataPath$1 = () => {
57
+ return getMetadataPath(webpackMode ? {
58
+ ...config,
59
+ environment: webpackMode
60
+ } : config);
61
+ };
62
+ async function startServer() {
63
+ const server = await startTranslationServer({
64
+ startPort,
65
+ onError: (err) => {
66
+ logger.error("Translation server error:", err);
67
+ },
68
+ onReady: (port) => {
69
+ logger.info(`Translation server started successfully on port: ${port}`);
70
+ },
71
+ config
72
+ });
73
+ config.dev.translationServerUrl = server.getUrl();
74
+ registerCleanupOnCurrentProcess({ asyncCleanup: async () => {
75
+ await translationServer.stop();
76
+ } });
77
+ return server;
78
+ }
79
+ return {
80
+ name: PLUGIN_NAME,
81
+ enforce: "pre",
82
+ vite: {
83
+ config() {
84
+ return { optimizeDeps: { exclude: ["@lingo.dev/compiler"] } };
85
+ },
86
+ async buildStart() {
87
+ const metadataFilePath = getMetadataPath$1();
88
+ cleanupExistingMetadata(metadataFilePath);
89
+ registerCleanupOnCurrentProcess({ cleanup: () => cleanupExistingMetadata(metadataFilePath) });
90
+ if (isDev && !translationServer) translationServer = await startServer();
91
+ },
92
+ async buildEnd() {
93
+ const metadataFilePath = getMetadataPath$1();
94
+ if (!isDev) try {
95
+ await processBuildTranslations({
96
+ config,
97
+ publicOutputPath: "public/translations",
98
+ metadataFilePath
99
+ });
100
+ } catch (error) {
101
+ logger.error("Build-time translation processing failed:", error);
102
+ }
103
+ }
104
+ },
105
+ webpack(compiler) {
106
+ webpackMode = compiler.options.mode === "development" ? "development" : "production";
107
+ const metadataFilePath = getMetadataPath$1();
108
+ config.environment = webpackMode;
109
+ compiler.hooks.initialize.tap(PLUGIN_NAME, () => {
110
+ cleanupExistingMetadata(metadataFilePath);
111
+ registerCleanupOnCurrentProcess({ cleanup: () => cleanupExistingMetadata(metadataFilePath) });
112
+ });
113
+ compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {
114
+ if (webpackMode === "development" && !translationServer) translationServer = await startServer();
115
+ });
116
+ compiler.hooks.additionalPass.tapPromise(PLUGIN_NAME, async () => {
117
+ if (webpackMode === "production") try {
118
+ await processBuildTranslations({
119
+ config,
120
+ publicOutputPath: "public/translations",
121
+ metadataFilePath
122
+ });
123
+ } catch (error) {
124
+ logger.error("Build-time translation processing failed:", error);
125
+ throw error;
126
+ }
127
+ });
128
+ compiler.hooks.shutdown.tapPromise(PLUGIN_NAME, async () => {
129
+ cleanupExistingMetadata(metadataFilePath);
130
+ await translationServer?.stop();
131
+ });
132
+ },
133
+ resolveId(id) {
134
+ const handler = virtualModulesResolvers[id];
135
+ if (handler) return handler(config);
136
+ return null;
137
+ },
138
+ load: {
139
+ filter: { id: /virtual:/ },
140
+ handler(id) {
141
+ const handler = virtualModulesLoaders[id];
142
+ if (handler) return handler(config);
143
+ return null;
144
+ }
145
+ },
146
+ transform: {
147
+ filter: {
148
+ id: {
149
+ include: [/\.[tj]sx$/],
150
+ exclude: /node_modules/
151
+ },
152
+ code: config.useDirective ? useI18nRegex : void 0
153
+ },
154
+ async handler(code, id) {
155
+ try {
156
+ const result = transformComponent({
157
+ code,
158
+ filePath: id,
159
+ config
160
+ });
161
+ if (!result.transformed) {
162
+ logger.debug(`No transformation needed for ${id}`);
163
+ return null;
164
+ }
165
+ const metadataManager = new MetadataManager(getMetadataPath$1());
166
+ if (result.newEntries && result.newEntries.length > 0) {
167
+ await metadataManager.saveMetadataWithEntries(result.newEntries);
168
+ logger.debug(`Found ${result.newEntries.length} translatable text(s) in ${id}`);
169
+ }
170
+ logger.debug(`Returning transformed code for ${id}`);
171
+ return {
172
+ code: result.code,
173
+ map: result.map
174
+ };
175
+ } catch (error) {
176
+ logger.error(`Transform error in ${id}:`, error);
177
+ return null;
178
+ }
179
+ }
180
+ }
181
+ };
182
+ });
183
+
184
+ //#endregion
185
+ export { lingoUnplugin };
186
+ //# sourceMappingURL=unplugin.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unplugin.mjs","names":["translationServer: TranslationServer","webpackMode: \"development\" | \"production\" | undefined","getMetadataPath","rawGetMetadataPath"],"sources":["../../src/plugin/unplugin.ts"],"sourcesContent":["import { createUnplugin } from \"unplugin\";\nimport { transformComponent } from \"./transform\";\nimport type {\n LingoConfig,\n LingoInternalFields,\n PartialLingoConfig,\n} from \"../types\";\nimport {\n startTranslationServer,\n type TranslationServer,\n} from \"../translation-server\";\nimport {\n cleanupExistingMetadata,\n getMetadataPath as rawGetMetadataPath,\n MetadataManager,\n} from \"../metadata/manager\";\nimport { createLingoConfig } from \"../utils/config-factory\";\nimport { logger } from \"../utils/logger\";\nimport { useI18nRegex } from \"./transform/use-i18n\";\nimport {\n generateClientLocaleModule,\n generateConfigModule,\n generateServerLocaleModule,\n} from \"../virtual/code-generator\";\nimport { processBuildTranslations } from \"./build-translator\";\nimport { registerCleanupOnCurrentProcess } from \"./cleanup\";\nimport path from \"path\";\nimport fs from \"fs\";\n\nexport type LingoPluginOptions = PartialLingoConfig;\n\nlet translationServer: TranslationServer;\n\nconst PLUGIN_NAME = \"lingo-compiler\";\n\nfunction tryLocalOrReturnVirtual(\n config: LingoConfig,\n fileName: string,\n virtualName: string,\n) {\n const customPath = path.join(config.sourceRoot, config.lingoDir, fileName);\n if (fs.existsSync(customPath)) {\n return customPath;\n }\n return virtualName;\n}\n\n/**\n * Single source of truth for virtual modules\n * Each entry defines both resolver (import path → virtual ID) and loader (virtual ID → code)\n *\n * If customFileCheck is defined, the specified file will be first searched for, and if not found virtual module will be used.\n */\nconst virtualModules = {\n \"@lingo.dev/compiler/virtual/config\": {\n virtualId: \"\\0virtual:lingo-config\",\n loader: (config: LingoConfig) => generateConfigModule(config),\n customFileCheck: undefined,\n },\n \"@lingo.dev/compiler/virtual/locale/server\": {\n virtualId: \"\\0virtual:locale-resolver.server\" as const,\n loader: (config: LingoConfig) => generateServerLocaleModule(config),\n customFileCheck: \"locale-resolver.server.ts\" as const,\n },\n \"@lingo.dev/compiler/virtual/locale/client\": {\n virtualId: \"\\0virtual:locale-resolver.client\" as const,\n loader: (config: LingoConfig) => generateClientLocaleModule(config),\n customFileCheck: \"locale-resolver.client.ts\" as const,\n },\n} as const;\n\n// Derive resolver and loader maps from the single source\nconst virtualModulesResolvers = Object.fromEntries(\n Object.entries(virtualModules).map(([importPath, module]) => [\n importPath,\n (config: LingoConfig) =>\n module.customFileCheck\n ? tryLocalOrReturnVirtual(\n config,\n module.customFileCheck,\n module.virtualId,\n )\n : module.virtualId,\n ]),\n);\n\nconst virtualModulesLoaders = Object.fromEntries(\n Object.values(virtualModules).map((value) => [value.virtualId, value.loader]),\n);\n\n/**\n * Universal plugin for Lingo.dev compiler\n * Supports Vite, Webpack\n */\nexport const lingoUnplugin = createUnplugin<\n LingoPluginOptions & Partial<Pick<LingoConfig, LingoInternalFields>>\n>((options) => {\n const config = createLingoConfig(options);\n\n // Won't work for webpack most likely. Use mode there to set correct environment in configs.\n const isDev = config.environment === \"development\";\n const startPort = config.dev.translationServerStartPort;\n\n // For webpack: store the actual mode and use it to compute the correct metadata path\n let webpackMode: \"development\" | \"production\" | undefined;\n // Should be dynamic, because webpack only tells us the mode inside the plugin, not inside the config.\n const getMetadataPath = () => {\n return rawGetMetadataPath(\n webpackMode ? { ...config, environment: webpackMode } : config,\n );\n };\n\n async function startServer() {\n const server = await startTranslationServer({\n startPort,\n onError: (err) => {\n logger.error(\"Translation server error:\", err);\n },\n onReady: (port) => {\n logger.info(`Translation server started successfully on port: ${port}`);\n },\n config,\n });\n // I don't like this quite a lot. But starting server inside the loader seems lame.\n config.dev.translationServerUrl = server.getUrl();\n registerCleanupOnCurrentProcess({\n asyncCleanup: async () => {\n await translationServer.stop();\n },\n });\n return server;\n }\n\n return {\n name: PLUGIN_NAME,\n enforce: \"pre\", // Run before other plugins (especially before React plugin)\n\n vite: {\n // Vite handles deep merge\n config() {\n // Required for custom virtual like modules to be resolved; otherwise they are bundled with raw source code.\n return {\n optimizeDeps: {\n exclude: [\"@lingo.dev/compiler\"],\n },\n };\n },\n async buildStart() {\n const metadataFilePath = getMetadataPath();\n\n cleanupExistingMetadata(metadataFilePath);\n registerCleanupOnCurrentProcess({\n cleanup: () => cleanupExistingMetadata(metadataFilePath),\n });\n\n if (isDev && !translationServer) {\n translationServer = await startServer();\n }\n },\n\n async buildEnd() {\n const metadataFilePath = getMetadataPath();\n if (!isDev) {\n try {\n await processBuildTranslations({\n config,\n publicOutputPath: \"public/translations\",\n metadataFilePath,\n });\n } catch (error) {\n logger.error(\"Build-time translation processing failed:\", error);\n }\n }\n },\n },\n\n webpack(compiler) {\n webpackMode =\n compiler.options.mode === \"development\" ? \"development\" : \"production\";\n const metadataFilePath = getMetadataPath();\n // Yes, this is dirty play, but webpack runs only for this plugin, and this way we save people from using wrong config\n config.environment = webpackMode;\n\n compiler.hooks.initialize.tap(PLUGIN_NAME, () => {\n cleanupExistingMetadata(metadataFilePath);\n registerCleanupOnCurrentProcess({\n cleanup: () => cleanupExistingMetadata(metadataFilePath),\n });\n });\n\n compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {\n if (webpackMode === \"development\" && !translationServer) {\n translationServer = await startServer();\n }\n });\n\n compiler.hooks.additionalPass.tapPromise(PLUGIN_NAME, async () => {\n if (webpackMode === \"production\") {\n try {\n await processBuildTranslations({\n config,\n publicOutputPath: \"public/translations\",\n metadataFilePath,\n });\n } catch (error) {\n logger.error(\"Build-time translation processing failed:\", error);\n throw error;\n }\n }\n });\n\n // Duplicates the cleanup process handlers does, but won't hurt since cleanup is idempotent.\n compiler.hooks.shutdown.tapPromise(PLUGIN_NAME, async () => {\n cleanupExistingMetadata(metadataFilePath);\n await translationServer?.stop();\n });\n },\n\n resolveId(id) {\n const handler = virtualModulesResolvers[id];\n if (handler) {\n return handler(config);\n }\n return null;\n },\n\n load: {\n filter: {\n // Without the filter webpack goes mad\n id: /virtual:/,\n },\n handler(id: string) {\n const handler = virtualModulesLoaders[id];\n if (handler) {\n return handler(config);\n }\n return null;\n },\n },\n\n transform: {\n filter: {\n id: {\n include: [/\\.[tj]sx$/],\n exclude: /node_modules/,\n },\n // If useDirective is enabled, only process files with \"use i18n\"\n // This is more efficient than checking in the handler\n code: config.useDirective ? useI18nRegex : undefined,\n },\n async handler(code, id) {\n try {\n // Transform the component\n const result = transformComponent({\n code,\n filePath: id,\n config,\n });\n\n // If no transformation occurred, return original code\n if (!result.transformed) {\n logger.debug(`No transformation needed for ${id}`);\n return null;\n }\n const metadataManager = new MetadataManager(getMetadataPath());\n\n // Update metadata with new entries (thread-safe)\n if (result.newEntries && result.newEntries.length > 0) {\n await metadataManager.saveMetadataWithEntries(result.newEntries);\n\n logger.debug(\n `Found ${result.newEntries.length} translatable text(s) in ${id}`,\n );\n }\n\n logger.debug(`Returning transformed code for ${id}`);\n return {\n code: result.code,\n map: result.map,\n };\n } catch (error) {\n logger.error(`Transform error in ${id}:`, error);\n return null;\n }\n },\n },\n };\n});\n"],"mappings":"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"}
@@ -0,0 +1,28 @@
1
+ const require_unplugin = require('./unplugin.cjs');
2
+
3
+ //#region src/plugin/vite.ts
4
+ /**
5
+ * Vite Plugin for Lingo.dev Compiler
6
+ *
7
+ * Usage:
8
+ * ```ts
9
+ * // vite.config.ts
10
+ * import { defineConfig } from 'vite';
11
+ * import { lingoCompilerPlugin } from '@lingo.dev/compiler/vite';
12
+ *
13
+ * export default defineConfig({
14
+ * plugins: [
15
+ * lingoCompilerPlugin({
16
+ * sourceLocale: 'en',
17
+ * targetLocales: ['es', 'fr'],
18
+ * }),
19
+ * ],
20
+ * });
21
+ * ```
22
+ */
23
+ function lingoCompilerPlugin(options) {
24
+ return require_unplugin.lingoUnplugin.vite(options);
25
+ }
26
+
27
+ //#endregion
28
+ exports.lingoCompilerPlugin = lingoCompilerPlugin;
@@ -0,0 +1,9 @@
1
+ import { LingoPluginOptions } from "./unplugin.cjs";
2
+ import * as unplugin0 from "unplugin";
3
+
4
+ //#region src/plugin/vite.d.ts
5
+
6
+ declare function lingoCompilerPlugin(options: LingoPluginOptions): unplugin0.VitePlugin<any> | unplugin0.VitePlugin<any>[];
7
+ //#endregion
8
+ export { type LingoPluginOptions, lingoCompilerPlugin };
9
+ //# sourceMappingURL=vite.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite.d.cts","names":[],"sources":["../../src/plugin/vite.ts"],"sourcesContent":[],"mappings":";;;;;iBAsBgB,mBAAA,UAA6B,qBAAkB,SAAA,CAAA,kBAAA,SAAA,CAAA"}
@@ -0,0 +1,9 @@
1
+ import { LingoPluginOptions } from "./unplugin.mjs";
2
+ import * as unplugin0 from "unplugin";
3
+
4
+ //#region src/plugin/vite.d.ts
5
+
6
+ declare function lingoCompilerPlugin(options: LingoPluginOptions): unplugin0.VitePlugin<any> | unplugin0.VitePlugin<any>[];
7
+ //#endregion
8
+ export { type LingoPluginOptions, lingoCompilerPlugin };
9
+ //# sourceMappingURL=vite.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite.d.mts","names":[],"sources":["../../src/plugin/vite.ts"],"sourcesContent":[],"mappings":";;;;;iBAsBgB,mBAAA,UAA6B,qBAAkB,SAAA,CAAA,kBAAA,SAAA,CAAA"}
@@ -0,0 +1,29 @@
1
+ import { lingoUnplugin } from "./unplugin.mjs";
2
+
3
+ //#region src/plugin/vite.ts
4
+ /**
5
+ * Vite Plugin for Lingo.dev Compiler
6
+ *
7
+ * Usage:
8
+ * ```ts
9
+ * // vite.config.ts
10
+ * import { defineConfig } from 'vite';
11
+ * import { lingoCompilerPlugin } from '@lingo.dev/compiler/vite';
12
+ *
13
+ * export default defineConfig({
14
+ * plugins: [
15
+ * lingoCompilerPlugin({
16
+ * sourceLocale: 'en',
17
+ * targetLocales: ['es', 'fr'],
18
+ * }),
19
+ * ],
20
+ * });
21
+ * ```
22
+ */
23
+ function lingoCompilerPlugin(options) {
24
+ return lingoUnplugin.vite(options);
25
+ }
26
+
27
+ //#endregion
28
+ export { lingoCompilerPlugin };
29
+ //# sourceMappingURL=vite.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite.mjs","names":[],"sources":["../../src/plugin/vite.ts"],"sourcesContent":["/**\n * Vite Plugin for Lingo.dev Compiler\n *\n * Usage:\n * ```ts\n * // vite.config.ts\n * import { defineConfig } from 'vite';\n * import { lingoCompilerPlugin } from '@lingo.dev/compiler/vite';\n *\n * export default defineConfig({\n * plugins: [\n * lingoCompilerPlugin({\n * sourceLocale: 'en',\n * targetLocales: ['es', 'fr'],\n * }),\n * ],\n * });\n * ```\n */\n\nimport { type LingoPluginOptions, lingoUnplugin } from \"./unplugin\";\n\nexport function lingoCompilerPlugin(options: LingoPluginOptions) {\n return lingoUnplugin.vite(options);\n}\n\nexport type { LingoPluginOptions };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,oBAAoB,SAA6B;AAC/D,QAAO,cAAc,KAAK,QAAQ"}
@@ -0,0 +1,27 @@
1
+ const require_unplugin = require('./unplugin.cjs');
2
+
3
+ //#region src/plugin/webpack.ts
4
+ /**
5
+ * Webpack Plugin for Lingo.dev Compiler
6
+ *
7
+ * Usage:
8
+ * ```js
9
+ * // webpack.config.js
10
+ * import { lingoCompilerPlugin } from '@lingo.dev/compiler/webpack';
11
+ *
12
+ * export default {
13
+ * plugins: [
14
+ * lingoCompilerPlugin({
15
+ * sourceLocale: 'en',
16
+ * targetLocales: ['es', 'fr'],
17
+ * }),
18
+ * ],
19
+ * };
20
+ * ```
21
+ */
22
+ function lingoCompilerPlugin(options) {
23
+ return require_unplugin.lingoUnplugin.webpack(options);
24
+ }
25
+
26
+ //#endregion
27
+ exports.lingoCompilerPlugin = lingoCompilerPlugin;
@@ -0,0 +1,8 @@
1
+ import { LingoPluginOptions } from "./unplugin.cjs";
2
+
3
+ //#region src/plugin/webpack.d.ts
4
+
5
+ declare function lingoCompilerPlugin(options: LingoPluginOptions): WebpackPluginInstance;
6
+ //#endregion
7
+ export { type LingoPluginOptions, lingoCompilerPlugin };
8
+ //# sourceMappingURL=webpack.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webpack.d.cts","names":[],"sources":["../../src/plugin/webpack.ts"],"sourcesContent":[],"mappings":";;;;iBAqBgB,mBAAA,UAA6B,qBAAkB"}
@@ -0,0 +1,8 @@
1
+ import { LingoPluginOptions } from "./unplugin.mjs";
2
+
3
+ //#region src/plugin/webpack.d.ts
4
+
5
+ declare function lingoCompilerPlugin(options: LingoPluginOptions): WebpackPluginInstance;
6
+ //#endregion
7
+ export { type LingoPluginOptions, lingoCompilerPlugin };
8
+ //# sourceMappingURL=webpack.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webpack.d.mts","names":[],"sources":["../../src/plugin/webpack.ts"],"sourcesContent":[],"mappings":";;;;iBAqBgB,mBAAA,UAA6B,qBAAkB"}
@@ -0,0 +1,28 @@
1
+ import { lingoUnplugin } from "./unplugin.mjs";
2
+
3
+ //#region src/plugin/webpack.ts
4
+ /**
5
+ * Webpack Plugin for Lingo.dev Compiler
6
+ *
7
+ * Usage:
8
+ * ```js
9
+ * // webpack.config.js
10
+ * import { lingoCompilerPlugin } from '@lingo.dev/compiler/webpack';
11
+ *
12
+ * export default {
13
+ * plugins: [
14
+ * lingoCompilerPlugin({
15
+ * sourceLocale: 'en',
16
+ * targetLocales: ['es', 'fr'],
17
+ * }),
18
+ * ],
19
+ * };
20
+ * ```
21
+ */
22
+ function lingoCompilerPlugin(options) {
23
+ return lingoUnplugin.webpack(options);
24
+ }
25
+
26
+ //#endregion
27
+ export { lingoCompilerPlugin };
28
+ //# sourceMappingURL=webpack.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webpack.mjs","names":[],"sources":["../../src/plugin/webpack.ts"],"sourcesContent":["/**\n * Webpack Plugin for Lingo.dev Compiler\n *\n * Usage:\n * ```js\n * // webpack.config.js\n * import { lingoCompilerPlugin } from '@lingo.dev/compiler/webpack';\n *\n * export default {\n * plugins: [\n * lingoCompilerPlugin({\n * sourceLocale: 'en',\n * targetLocales: ['es', 'fr'],\n * }),\n * ],\n * };\n * ```\n */\n\nimport { lingoUnplugin, type LingoPluginOptions } from \"./unplugin\";\n\nexport function lingoCompilerPlugin(options: LingoPluginOptions) {\n return lingoUnplugin.webpack(options);\n}\n\nexport type { LingoPluginOptions };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAqBA,SAAgB,oBAAoB,SAA6B;AAC/D,QAAO,cAAc,QAAQ,QAAQ"}
@@ -0,0 +1,9 @@
1
+ const require_LingoContext = require('../shared/LingoContext.cjs');
2
+ const require_LingoProvider = require('../shared/LingoProvider.cjs');
3
+ const require_LocaleSwitcher = require('../shared/LocaleSwitcher.cjs');
4
+ const require_useTranslation = require('./useTranslation.cjs');
5
+
6
+ exports.LingoProvider = require_LingoProvider.LingoProvider;
7
+ exports.LocaleSwitcher = require_LocaleSwitcher.LocaleSwitcher;
8
+ exports.useLingoContext = require_LingoContext.useLingoContext;
9
+ exports.useTranslation = require_useTranslation.useTranslation;
@@ -0,0 +1,5 @@
1
+ import { LingoProvider, LingoProviderProps } from "../shared/LingoProvider.cjs";
2
+ import { useLingoContext } from "../shared/LingoContext.cjs";
3
+ import { useTranslation } from "./useTranslation.cjs";
4
+ import { LocaleSwitcher } from "../shared/LocaleSwitcher.cjs";
5
+ export { LingoProvider, type LingoProviderProps, LocaleSwitcher, useLingoContext, useTranslation };
@@ -0,0 +1,5 @@
1
+ import { LingoProvider, LingoProviderProps } from "../shared/LingoProvider.mjs";
2
+ import { useLingoContext } from "../shared/LingoContext.mjs";
3
+ import { useTranslation } from "./useTranslation.mjs";
4
+ import { LocaleSwitcher } from "../shared/LocaleSwitcher.mjs";
5
+ export { LingoProvider, type LingoProviderProps, LocaleSwitcher, useLingoContext, useTranslation };
@@ -0,0 +1,6 @@
1
+ import { useLingoContext } from "../shared/LingoContext.mjs";
2
+ import { LingoProvider } from "../shared/LingoProvider.mjs";
3
+ import { LocaleSwitcher } from "../shared/LocaleSwitcher.mjs";
4
+ import { useTranslation } from "./useTranslation.mjs";
5
+
6
+ export { LingoProvider, LocaleSwitcher, useLingoContext, useTranslation };
@@ -0,0 +1,71 @@
1
+ "use client";
2
+
3
+ const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
4
+ const require_logger = require('../../utils/logger.cjs');
5
+ const require_LingoContext = require('../shared/LingoContext.cjs');
6
+ const require_render_rich_text = require('../shared/render-rich-text.cjs');
7
+ let react = require("react");
8
+
9
+ //#region src/react/client/useTranslation.ts
10
+ /**
11
+ * useTranslation Hook
12
+ *
13
+ * Returns a translation function `t(hash, source, params?)` that:
14
+ * - Returns original text for source locale
15
+ * - Returns translated text for target locales
16
+ * - Supports rich text with variable and component placeholders
17
+ * - Automatically requests missing translations
18
+ * - Falls back to source text while loading
19
+ *
20
+ * This hook is automatically injected by the Babel plugin.
21
+ * You typically don't need to call it manually.
22
+ *
23
+ * @param hashes - Static list of all translation hashes used in this component (injected at build time)
24
+ *
25
+ * @example
26
+ * ```tsx
27
+ * 'use client';
28
+ *
29
+ * export function Welcome() {
30
+ * const t = useTranslation(['hash_abc123', 'hash_def456']);
31
+ *
32
+ * return (
33
+ * <div>
34
+ * <h1>{t('hash_abc123', 'Welcome')}</h1>
35
+ * <p>{t('hash_def456', 'Hello {name}', { name: 'Alice' })}</p>
36
+ * <p>{t('hash_def789', 'Click <a0>here</a0>', {
37
+ * a0: (chunks) => <a href="/home">{chunks}</a>
38
+ * })}</p>
39
+ * </div>
40
+ * );
41
+ * }
42
+ * ```
43
+ */
44
+ const useTranslation = (hashes) => {
45
+ const { translations, registerHashes, locale, sourceLocale } = require_LingoContext.useLingoContext();
46
+ (0, react.useEffect)(() => {
47
+ require_logger.logger.debug(`Registering ${hashes.length} hashes for component`);
48
+ registerHashes(hashes);
49
+ }, [
50
+ hashes,
51
+ registerHashes,
52
+ locale,
53
+ sourceLocale
54
+ ]);
55
+ return {
56
+ t: (0, react.useCallback)((hash, source, params) => {
57
+ require_logger.logger.debug(`Client. The translations for locale ${locale} are: ${JSON.stringify(translations)}`);
58
+ const text = translations[hash] || source;
59
+ if (!params) return text;
60
+ return require_render_rich_text.renderRichText(text, params, locale);
61
+ }, [
62
+ translations,
63
+ locale,
64
+ sourceLocale
65
+ ]),
66
+ locale
67
+ };
68
+ };
69
+
70
+ //#endregion
71
+ exports.useTranslation = useTranslation;
@@ -0,0 +1,42 @@
1
+ import { TranslationHook } from "../types.cjs";
2
+
3
+ //#region src/react/client/useTranslation.d.ts
4
+
5
+ /**
6
+ * useTranslation Hook
7
+ *
8
+ * Returns a translation function `t(hash, source, params?)` that:
9
+ * - Returns original text for source locale
10
+ * - Returns translated text for target locales
11
+ * - Supports rich text with variable and component placeholders
12
+ * - Automatically requests missing translations
13
+ * - Falls back to source text while loading
14
+ *
15
+ * This hook is automatically injected by the Babel plugin.
16
+ * You typically don't need to call it manually.
17
+ *
18
+ * @param hashes - Static list of all translation hashes used in this component (injected at build time)
19
+ *
20
+ * @example
21
+ * ```tsx
22
+ * 'use client';
23
+ *
24
+ * export function Welcome() {
25
+ * const t = useTranslation(['hash_abc123', 'hash_def456']);
26
+ *
27
+ * return (
28
+ * <div>
29
+ * <h1>{t('hash_abc123', 'Welcome')}</h1>
30
+ * <p>{t('hash_def456', 'Hello {name}', { name: 'Alice' })}</p>
31
+ * <p>{t('hash_def789', 'Click <a0>here</a0>', {
32
+ * a0: (chunks) => <a href="/home">{chunks}</a>
33
+ * })}</p>
34
+ * </div>
35
+ * );
36
+ * }
37
+ * ```
38
+ */
39
+ declare const useTranslation: TranslationHook;
40
+ //#endregion
41
+ export { useTranslation };
42
+ //# sourceMappingURL=useTranslation.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useTranslation.d.cts","names":[],"sources":["../../../src/react/client/useTranslation.ts"],"sourcesContent":[],"mappings":";;;;;;AA6CA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAAa,gBAAgB"}
@@ -0,0 +1,42 @@
1
+ import { TranslationHook } from "../types.mjs";
2
+
3
+ //#region src/react/client/useTranslation.d.ts
4
+
5
+ /**
6
+ * useTranslation Hook
7
+ *
8
+ * Returns a translation function `t(hash, source, params?)` that:
9
+ * - Returns original text for source locale
10
+ * - Returns translated text for target locales
11
+ * - Supports rich text with variable and component placeholders
12
+ * - Automatically requests missing translations
13
+ * - Falls back to source text while loading
14
+ *
15
+ * This hook is automatically injected by the Babel plugin.
16
+ * You typically don't need to call it manually.
17
+ *
18
+ * @param hashes - Static list of all translation hashes used in this component (injected at build time)
19
+ *
20
+ * @example
21
+ * ```tsx
22
+ * 'use client';
23
+ *
24
+ * export function Welcome() {
25
+ * const t = useTranslation(['hash_abc123', 'hash_def456']);
26
+ *
27
+ * return (
28
+ * <div>
29
+ * <h1>{t('hash_abc123', 'Welcome')}</h1>
30
+ * <p>{t('hash_def456', 'Hello {name}', { name: 'Alice' })}</p>
31
+ * <p>{t('hash_def789', 'Click <a0>here</a0>', {
32
+ * a0: (chunks) => <a href="/home">{chunks}</a>
33
+ * })}</p>
34
+ * </div>
35
+ * );
36
+ * }
37
+ * ```
38
+ */
39
+ declare const useTranslation: TranslationHook;
40
+ //#endregion
41
+ export { useTranslation };
42
+ //# sourceMappingURL=useTranslation.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useTranslation.d.mts","names":[],"sources":["../../../src/react/client/useTranslation.ts"],"sourcesContent":[],"mappings":";;;;;;AA6CA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAAa,gBAAgB"}
@@ -0,0 +1,71 @@
1
+ "use client";
2
+
3
+ import { logger } from "../../utils/logger.mjs";
4
+ import { useLingoContext } from "../shared/LingoContext.mjs";
5
+ import { renderRichText } from "../shared/render-rich-text.mjs";
6
+ import { useCallback, useEffect } from "react";
7
+
8
+ //#region src/react/client/useTranslation.ts
9
+ /**
10
+ * useTranslation Hook
11
+ *
12
+ * Returns a translation function `t(hash, source, params?)` that:
13
+ * - Returns original text for source locale
14
+ * - Returns translated text for target locales
15
+ * - Supports rich text with variable and component placeholders
16
+ * - Automatically requests missing translations
17
+ * - Falls back to source text while loading
18
+ *
19
+ * This hook is automatically injected by the Babel plugin.
20
+ * You typically don't need to call it manually.
21
+ *
22
+ * @param hashes - Static list of all translation hashes used in this component (injected at build time)
23
+ *
24
+ * @example
25
+ * ```tsx
26
+ * 'use client';
27
+ *
28
+ * export function Welcome() {
29
+ * const t = useTranslation(['hash_abc123', 'hash_def456']);
30
+ *
31
+ * return (
32
+ * <div>
33
+ * <h1>{t('hash_abc123', 'Welcome')}</h1>
34
+ * <p>{t('hash_def456', 'Hello {name}', { name: 'Alice' })}</p>
35
+ * <p>{t('hash_def789', 'Click <a0>here</a0>', {
36
+ * a0: (chunks) => <a href="/home">{chunks}</a>
37
+ * })}</p>
38
+ * </div>
39
+ * );
40
+ * }
41
+ * ```
42
+ */
43
+ const useTranslation = (hashes) => {
44
+ const { translations, registerHashes, locale, sourceLocale } = useLingoContext();
45
+ useEffect(() => {
46
+ logger.debug(`Registering ${hashes.length} hashes for component`);
47
+ registerHashes(hashes);
48
+ }, [
49
+ hashes,
50
+ registerHashes,
51
+ locale,
52
+ sourceLocale
53
+ ]);
54
+ return {
55
+ t: useCallback((hash, source, params) => {
56
+ logger.debug(`Client. The translations for locale ${locale} are: ${JSON.stringify(translations)}`);
57
+ const text = translations[hash] || source;
58
+ if (!params) return text;
59
+ return renderRichText(text, params, locale);
60
+ }, [
61
+ translations,
62
+ locale,
63
+ sourceLocale
64
+ ]),
65
+ locale
66
+ };
67
+ };
68
+
69
+ //#endregion
70
+ export { useTranslation };
71
+ //# sourceMappingURL=useTranslation.mjs.map