@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,274 @@
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_utils = require('./utils.cjs');
6
+ const require_LingoContext = require('./LingoContext.cjs');
7
+ let react = require("react");
8
+ let _lingo_dev_compiler_virtual_config = require("@lingo.dev/compiler/virtual/config");
9
+ let _lingo_dev_compiler_virtual_locale_client = require("@lingo.dev/compiler/virtual/locale/client");
10
+ let react_jsx_runtime = require("react/jsx-runtime");
11
+
12
+ //#region src/react/shared/LingoProvider.tsx
13
+ const noop = () => {};
14
+ const IS_DEV = process.env.NODE_ENV === "development";
15
+ const BATCH_DELAY = 200;
16
+ /**
17
+ * Translation Provider Component
18
+ *
19
+ * Wraps your app to provide translation context to all components.
20
+ * Handles locale switching and on-demand translation loading.
21
+ *
22
+ * @example
23
+ * ```tsx
24
+ * // In your root layout
25
+ * import { LingoProvider } from '@lingo.dev/compiler-beta/react';
26
+ *
27
+ * export default function RootLayout({ children }) {
28
+ * return (
29
+ * <html>
30
+ * <body>
31
+ * <LingoProvider initialLocale="en">
32
+ * {children}
33
+ * </LingoProvider>
34
+ * </body>
35
+ * </html>
36
+ * );
37
+ * }
38
+ * ```
39
+ */
40
+ const LingoProvider = IS_DEV ? LingoProvider__Dev : LingoProvider__Prod;
41
+ function LingoProvider__Prod({ initialLocale, initialTranslations = {}, router, children }) {
42
+ const [locale, setLocaleState] = (0, react.useState)(() => {
43
+ if (initialLocale) return initialLocale;
44
+ if (typeof window !== "undefined") return (0, _lingo_dev_compiler_virtual_locale_client.getClientLocale)();
45
+ return _lingo_dev_compiler_virtual_config.sourceLocale;
46
+ });
47
+ const [translations, setTranslations] = (0, react.useState)(initialTranslations);
48
+ const [isLoading, setIsLoading] = (0, react.useState)(false);
49
+ require_logger.logger.debug(`LingoProvider initialized with locale: ${locale}`, initialTranslations);
50
+ /**
51
+ * Update HTML lang attribute when locale changes
52
+ * This ensures screen readers and SEO understand the page language
53
+ */
54
+ (0, react.useEffect)(() => {
55
+ if (typeof document !== "undefined") document.documentElement.lang = locale;
56
+ }, [locale]);
57
+ /**
58
+ * Load translations from public/translations/{locale}.json
59
+ * Lazy loads on-demand for SPAs
60
+ */
61
+ const loadTranslations = (0, react.useCallback)(async (targetLocale) => {
62
+ if (Object.keys(initialTranslations).length > 0) return;
63
+ setIsLoading(true);
64
+ try {
65
+ const response = await fetch(`/translations/${targetLocale}.json`);
66
+ if (!response.ok) throw new Error(`Failed to load translations for ${targetLocale}: ${response.statusText}`);
67
+ const data = await response.json();
68
+ setTranslations(data.entries || data);
69
+ require_logger.logger.debug(`Loaded translations for ${targetLocale}:`, Object.keys(data.entries || data).length);
70
+ } catch (error) {
71
+ require_logger.logger.error(`Failed to load translations for ${targetLocale}:`, error);
72
+ setTranslations({});
73
+ } finally {
74
+ setIsLoading(false);
75
+ }
76
+ }, [initialTranslations]);
77
+ (0, react.useEffect)(() => {
78
+ if (Object.keys(initialTranslations).length === 0) loadTranslations(locale);
79
+ }, []);
80
+ (0, react.useEffect)(() => {
81
+ if (router) setTranslations(initialTranslations);
82
+ }, [initialTranslations, router]);
83
+ /**
84
+ * Change locale
85
+ * - For Next.js SSR: triggers server re-render via router.refresh()
86
+ * - For SPAs: lazy loads translations from /translations/{locale}.json
87
+ */
88
+ const setLocale = (0, react.useCallback)(async (newLocale) => {
89
+ (0, _lingo_dev_compiler_virtual_locale_client.persistLocale)(newLocale);
90
+ setLocaleState(newLocale);
91
+ if (router) router.refresh();
92
+ else await loadTranslations(newLocale);
93
+ }, [router, loadTranslations]);
94
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_LingoContext.LingoContext.Provider, {
95
+ value: {
96
+ locale,
97
+ setLocale,
98
+ translations,
99
+ registerHashes: noop,
100
+ isLoading,
101
+ sourceLocale: _lingo_dev_compiler_virtual_config.sourceLocale
102
+ },
103
+ children
104
+ });
105
+ }
106
+ function LingoProvider__Dev({ initialLocale, initialTranslations = {}, router, devWidget, children }) {
107
+ const [locale, setLocaleState] = (0, react.useState)(() => {
108
+ if (initialLocale) return initialLocale;
109
+ return (0, _lingo_dev_compiler_virtual_locale_client.getClientLocale)();
110
+ });
111
+ const [translations, setTranslations] = (0, react.useState)(initialTranslations);
112
+ const [isLoading, setIsLoading] = (0, react.useState)(false);
113
+ const [allSeenHashes, setAllSeenHashes] = (0, react.useState)(/* @__PURE__ */ new Set());
114
+ const registeredHashesRef = (0, react.useRef)(/* @__PURE__ */ new Set());
115
+ const pendingHashesRef = (0, react.useRef)(/* @__PURE__ */ new Set());
116
+ const erroredHashesRef = (0, react.useRef)(/* @__PURE__ */ new Set());
117
+ const batchTimerRef = (0, react.useRef)(null);
118
+ const translationsRef = (0, react.useRef)(initialTranslations);
119
+ const localeRef = (0, react.useRef)(locale);
120
+ (0, react.useEffect)(() => {
121
+ translationsRef.current = translations;
122
+ }, [translations]);
123
+ (0, react.useEffect)(() => {
124
+ localeRef.current = locale;
125
+ }, [locale]);
126
+ /**
127
+ * Update HTML lang attribute when locale changes
128
+ * This ensures screen readers and SEO understand the page language
129
+ */
130
+ (0, react.useEffect)(() => {
131
+ if (typeof document !== "undefined") document.documentElement.lang = locale;
132
+ }, [locale]);
133
+ /**
134
+ * Register a hash as being used in a component
135
+ * Called during render - must not trigger state updates immediately
136
+ */
137
+ const registerHashes = (0, react.useCallback)((hashes) => {
138
+ let wasNew = false;
139
+ hashes.forEach((hash) => {
140
+ wasNew = wasNew || !registeredHashesRef.current.has(hash);
141
+ registeredHashesRef.current.add(hash);
142
+ });
143
+ require_logger.logger.debug(`Registering hashes: ${hashes.join(", ")}. Registered hashes: ${registeredHashesRef.current.values()}. wasNew: ${wasNew}`);
144
+ if (wasNew) setAllSeenHashes((prev) => {
145
+ const next = prev.union(new Set(hashes));
146
+ require_logger.logger.debug(`New allSeenHashes: ${[...next.values()]}`);
147
+ return next;
148
+ });
149
+ }, []);
150
+ /**
151
+ * Check for missing translations and request them (batched)
152
+ * This runs when allSeenHashes changes (hot reload or new components mount)
153
+ */
154
+ (0, react.useEffect)(() => {
155
+ require_logger.logger.debug(`LingoProvider checking translations for locale ${locale}, seen hashes: ${allSeenHashes.size}`);
156
+ const missingHashes = [];
157
+ require_logger.logger.debug("allSeenHashes: ", [...allSeenHashes.values()], [...pendingHashesRef.current.values()]);
158
+ for (const hash of allSeenHashes) if (!translations[hash] && !pendingHashesRef.current.has(hash) && !erroredHashesRef.current.has(hash)) {
159
+ missingHashes.push(hash);
160
+ pendingHashesRef.current.add(hash);
161
+ }
162
+ require_logger.logger.debug("Missing hashes: ", missingHashes.join(","));
163
+ if (missingHashes.length === 0 && localeRef.current == locale) return;
164
+ require_logger.logger.debug(`Requesting translations for ${missingHashes.length} hashes in locale ${locale}`);
165
+ if (batchTimerRef.current) clearTimeout(batchTimerRef.current);
166
+ batchTimerRef.current = setTimeout(async () => {
167
+ const hashesToFetch = Array.from(pendingHashesRef.current);
168
+ pendingHashesRef.current.clear();
169
+ require_logger.logger.debug(`Fetching translations for ${hashesToFetch.length} hashes`);
170
+ if (hashesToFetch.length === 0) return;
171
+ setIsLoading(true);
172
+ try {
173
+ const newTranslations = await require_utils.fetchTranslations(localeRef.current, hashesToFetch, _lingo_dev_compiler_virtual_config.serverUrl);
174
+ require_logger.logger.debug(`Fetched translations for ${hashesToFetch.length} hashes:`, newTranslations);
175
+ const receivedHashes = new Set(Object.keys(newTranslations));
176
+ const missingHashes$1 = hashesToFetch.filter((hash) => !receivedHashes.has(hash));
177
+ if (missingHashes$1.length > 0) {
178
+ require_logger.logger.warn(`Server did not return translations for ${missingHashes$1.length} hashes: ${missingHashes$1.join(", ")}`);
179
+ for (const hash of missingHashes$1) erroredHashesRef.current.add(hash);
180
+ }
181
+ setTranslations((prev) => ({
182
+ ...prev,
183
+ ...newTranslations
184
+ }));
185
+ for (const hash of hashesToFetch) registeredHashesRef.current.add(hash);
186
+ } catch (error) {
187
+ require_logger.logger.warn(`Failed to fetch translations from translation server: ${error}.`);
188
+ for (const hash of hashesToFetch) {
189
+ pendingHashesRef.current.delete(hash);
190
+ erroredHashesRef.current.add(hash);
191
+ }
192
+ } finally {
193
+ setIsLoading(false);
194
+ }
195
+ }, BATCH_DELAY);
196
+ }, [
197
+ allSeenHashes,
198
+ locale,
199
+ translations
200
+ ]);
201
+ /**
202
+ * Clear batch timer on unmount
203
+ */
204
+ (0, react.useEffect)(() => {
205
+ return () => {
206
+ if (batchTimerRef.current) clearTimeout(batchTimerRef.current);
207
+ };
208
+ }, []);
209
+ /**
210
+ * Change locale and load translations dynamically
211
+ */
212
+ const setLocale = (0, react.useCallback)(async (newLocale) => {
213
+ (0, _lingo_dev_compiler_virtual_locale_client.persistLocale)(newLocale);
214
+ setLocaleState(newLocale);
215
+ if (router) router.refresh();
216
+ setIsLoading(true);
217
+ const startTime = performance.now();
218
+ try {
219
+ require_logger.logger.info(`Fetching translations for locale: ${newLocale}. Server url: ${_lingo_dev_compiler_virtual_config.serverUrl}`);
220
+ const translatedDict = await require_utils.fetchTranslations(newLocale, [], _lingo_dev_compiler_virtual_config.serverUrl);
221
+ const endTime = performance.now();
222
+ require_logger.logger.info(`Translation fetch complete for ${newLocale} in ${(endTime - startTime).toFixed(2)}ms`);
223
+ const allTranslations = translatedDict.entries || {};
224
+ require_logger.logger.debug(`Translations loaded for ${newLocale}:`, allTranslations);
225
+ setTranslations(allTranslations);
226
+ } catch (error) {
227
+ require_logger.logger.error(`Failed to load translations for ${newLocale}:`, error);
228
+ setTranslations({});
229
+ } finally {
230
+ setIsLoading(false);
231
+ }
232
+ }, [router]);
233
+ (0, react.useEffect)(() => {
234
+ if (devWidget?.enabled !== false) Promise.resolve().then(() => require("../../widget/lingo-dev-widget.cjs")).catch((err) => {
235
+ require_logger.logger.error("Failed to load dev widget:", err, err.message);
236
+ });
237
+ }, [devWidget?.enabled]);
238
+ (0, react.useEffect)(() => {
239
+ if (typeof window !== "undefined" && devWidget?.enabled !== false) {
240
+ window.__LINGO_DEV_STATE__ = {
241
+ isLoading,
242
+ locale,
243
+ sourceLocale: _lingo_dev_compiler_virtual_config.sourceLocale,
244
+ pendingCount: pendingHashesRef.current.size,
245
+ position: devWidget?.position || "bottom-left"
246
+ };
247
+ window.__LINGO_DEV_WS_URL__ = _lingo_dev_compiler_virtual_config.serverUrl;
248
+ window.__LINGO_DEV_UPDATE__?.();
249
+ }
250
+ }, [
251
+ isLoading,
252
+ locale,
253
+ _lingo_dev_compiler_virtual_config.sourceLocale,
254
+ devWidget
255
+ ]);
256
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_LingoContext.LingoContext.Provider, {
257
+ value: {
258
+ locale,
259
+ setLocale,
260
+ translations,
261
+ registerHashes,
262
+ isLoading,
263
+ sourceLocale: _lingo_dev_compiler_virtual_config.sourceLocale,
264
+ _devStats: {
265
+ pendingCount: pendingHashesRef.current.size,
266
+ totalRegisteredCount: registeredHashesRef.current.size
267
+ }
268
+ },
269
+ children
270
+ });
271
+ }
272
+
273
+ //#endregion
274
+ exports.LingoProvider = LingoProvider;
@@ -0,0 +1,76 @@
1
+ import { LocaleCode } from "lingo.dev/spec";
2
+ import * as react_jsx_runtime2 from "react/jsx-runtime";
3
+ import { PropsWithChildren } from "react";
4
+
5
+ //#region src/react/shared/LingoProvider.d.ts
6
+ /**
7
+ * Translation provider props
8
+ */
9
+ type LingoProviderProps = PropsWithChildren<{
10
+ /**
11
+ * Initial locale to use
12
+ */
13
+ initialLocale?: LocaleCode;
14
+ /**
15
+ * Initial translations (pre-loaded)
16
+ */
17
+ initialTranslations?: Record<string, string>;
18
+ /**
19
+ * Optional router instance for Next.js integration
20
+ * If provided, calls router.refresh() after locale change
21
+ * This ensures Server Components re-render with new locale
22
+ */
23
+ router?: {
24
+ refresh: () => void;
25
+ };
26
+ /**
27
+ * Development widget configuration
28
+ */
29
+ devWidget?: {
30
+ /**
31
+ * Enable/disable widget (default: true in dev mode)
32
+ * Set to false to opt-out
33
+ */
34
+ enabled?: boolean;
35
+ /**
36
+ * Widget position on screen
37
+ * @default 'bottom-left'
38
+ */
39
+ position?: "bottom-left" | "bottom-right" | "top-left" | "top-right";
40
+ };
41
+ }>;
42
+ /**
43
+ * Translation Provider Component
44
+ *
45
+ * Wraps your app to provide translation context to all components.
46
+ * Handles locale switching and on-demand translation loading.
47
+ *
48
+ * @example
49
+ * ```tsx
50
+ * // In your root layout
51
+ * import { LingoProvider } from '@lingo.dev/compiler-beta/react';
52
+ *
53
+ * export default function RootLayout({ children }) {
54
+ * return (
55
+ * <html>
56
+ * <body>
57
+ * <LingoProvider initialLocale="en">
58
+ * {children}
59
+ * </LingoProvider>
60
+ * </body>
61
+ * </html>
62
+ * );
63
+ * }
64
+ * ```
65
+ */
66
+ declare const LingoProvider: typeof LingoProvider__Dev;
67
+ declare function LingoProvider__Dev({
68
+ initialLocale,
69
+ initialTranslations,
70
+ router,
71
+ devWidget,
72
+ children
73
+ }: LingoProviderProps): react_jsx_runtime2.JSX.Element;
74
+ //#endregion
75
+ export { LingoProvider, LingoProviderProps };
76
+ //# sourceMappingURL=LingoProvider.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LingoProvider.d.cts","names":[],"sources":["../../../src/react/shared/LingoProvider.tsx"],"sourcesContent":[],"mappings":";;;;;;;;AAyBY,KAAA,kBAAA,GAAqB,iBAAH,CAAA;EAIZ;;;EAJgC,aAAA,CAAA,EAIhC,UAJgC;EAgErC;AAAkE;;EAoI7E,mBAAA,CAAA,EA3LsB,MA2LtB,CAAA,MAAA,EAAA,MAAA,CAAA;EACA;;;;;EAGmB,MAAA,CAAA,EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAxIR,sBAAa;iBAkIjB,kBAAA;;;;;;GAMN,qBAAkB,kBAAA,CAAA,GAAA,CAAA"}
@@ -0,0 +1,76 @@
1
+ import { PropsWithChildren } from "react";
2
+ import * as react_jsx_runtime2 from "react/jsx-runtime";
3
+ import { LocaleCode } from "lingo.dev/spec";
4
+
5
+ //#region src/react/shared/LingoProvider.d.ts
6
+ /**
7
+ * Translation provider props
8
+ */
9
+ type LingoProviderProps = PropsWithChildren<{
10
+ /**
11
+ * Initial locale to use
12
+ */
13
+ initialLocale?: LocaleCode;
14
+ /**
15
+ * Initial translations (pre-loaded)
16
+ */
17
+ initialTranslations?: Record<string, string>;
18
+ /**
19
+ * Optional router instance for Next.js integration
20
+ * If provided, calls router.refresh() after locale change
21
+ * This ensures Server Components re-render with new locale
22
+ */
23
+ router?: {
24
+ refresh: () => void;
25
+ };
26
+ /**
27
+ * Development widget configuration
28
+ */
29
+ devWidget?: {
30
+ /**
31
+ * Enable/disable widget (default: true in dev mode)
32
+ * Set to false to opt-out
33
+ */
34
+ enabled?: boolean;
35
+ /**
36
+ * Widget position on screen
37
+ * @default 'bottom-left'
38
+ */
39
+ position?: "bottom-left" | "bottom-right" | "top-left" | "top-right";
40
+ };
41
+ }>;
42
+ /**
43
+ * Translation Provider Component
44
+ *
45
+ * Wraps your app to provide translation context to all components.
46
+ * Handles locale switching and on-demand translation loading.
47
+ *
48
+ * @example
49
+ * ```tsx
50
+ * // In your root layout
51
+ * import { LingoProvider } from '@lingo.dev/compiler-beta/react';
52
+ *
53
+ * export default function RootLayout({ children }) {
54
+ * return (
55
+ * <html>
56
+ * <body>
57
+ * <LingoProvider initialLocale="en">
58
+ * {children}
59
+ * </LingoProvider>
60
+ * </body>
61
+ * </html>
62
+ * );
63
+ * }
64
+ * ```
65
+ */
66
+ declare const LingoProvider: typeof LingoProvider__Dev;
67
+ declare function LingoProvider__Dev({
68
+ initialLocale,
69
+ initialTranslations,
70
+ router,
71
+ devWidget,
72
+ children
73
+ }: LingoProviderProps): react_jsx_runtime2.JSX.Element;
74
+ //#endregion
75
+ export { LingoProvider, LingoProviderProps };
76
+ //# sourceMappingURL=LingoProvider.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LingoProvider.d.mts","names":[],"sources":["../../../src/react/shared/LingoProvider.tsx"],"sourcesContent":[],"mappings":";;;;;;;;AAyBY,KAAA,kBAAA,GAAqB,iBAAH,CAAA;EAIZ;;;EAJgC,aAAA,CAAA,EAIhC,UAJgC;EAgErC;AAAkE;;EAoI7E,mBAAA,CAAA,EA3LsB,MA2LtB,CAAA,MAAA,EAAA,MAAA,CAAA;EACA;;;;;EAGmB,MAAA,CAAA,EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAxIR,sBAAa;iBAkIjB,kBAAA;;;;;;GAMN,qBAAkB,kBAAA,CAAA,GAAA,CAAA"}