@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,7 @@
1
+ const require_ServerWeglotProvider = require('./ServerWeglotProvider.cjs');
2
+ const require_LocaleSwitcher = require('../shared/LocaleSwitcher.cjs');
3
+ const require_useTranslation = require('./useTranslation.cjs');
4
+
5
+ exports.LocaleSwitcher = require_LocaleSwitcher.LocaleSwitcher;
6
+ exports.WeglotProvider = require_ServerWeglotProvider.WeglotProvider;
7
+ exports.useTranslation = require_useTranslation.useTranslation;
@@ -0,0 +1,4 @@
1
+ import { LocaleSwitcher } from "../shared/LocaleSwitcher.cjs";
2
+ import { WeglotProvider } from "./ServerWeglotProvider.cjs";
3
+ import { useTranslation } from "./useTranslation.cjs";
4
+ export { LocaleSwitcher, WeglotProvider, useTranslation };
@@ -0,0 +1,4 @@
1
+ import { LocaleSwitcher } from "../shared/LocaleSwitcher.mjs";
2
+ import { WeglotProvider } from "./ServerWeglotProvider.mjs";
3
+ import { useTranslation } from "./useTranslation.mjs";
4
+ export { LocaleSwitcher, WeglotProvider, useTranslation };
@@ -0,0 +1,5 @@
1
+ import { WeglotProvider } from "./ServerWeglotProvider.mjs";
2
+ import { LocaleSwitcher } from "../shared/LocaleSwitcher.mjs";
3
+ import { useTranslation } from "./useTranslation.mjs";
4
+
5
+ export { LocaleSwitcher, WeglotProvider, useTranslation };
@@ -0,0 +1,60 @@
1
+ const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
2
+ const require_logger = require('../../utils/logger.cjs');
3
+ const require_translations = require('../server-only/translations.cjs');
4
+ const require_render_rich_text = require('../shared/render-rich-text.cjs');
5
+ let react = require("react");
6
+ let _weglot_translate_react_virtual_locale_server = require("@weglot/translate-react/virtual/locale/server");
7
+
8
+ //#region src/react/server/useTranslation.ts
9
+ const getTranslations = async (hashes) => {
10
+ const locale = await (0, _weglot_translate_react_virtual_locale_server.getServerLocale)();
11
+ const translations = await require_translations.fetchTranslationsOnServer(locale, hashes);
12
+ require_logger.logger.debug(`Server. The translations for locale ${locale} are: ${JSON.stringify(translations)}`);
13
+ return {
14
+ locale,
15
+ translations
16
+ };
17
+ };
18
+ /**
19
+ * Server-side translation hook
20
+ *
21
+ * Works in Server Components WITHOUT async/await!
22
+ * Uses React's use() hook to unwrap the cached promise.
23
+ *
24
+ * This hook has the SAME signature as the client-side version,
25
+ * making components truly isomorphic.
26
+ *
27
+ * @param hashes - List of translation hashes used in component (injected by compiler)
28
+ * @returns Translation function
29
+ *
30
+ * @example
31
+ * ```tsx
32
+ * // Works in Server Components (no async needed!)
33
+ * export default function ServerPage() {
34
+ * const t = useTranslation(['hash_abc', 'hash_def']);
35
+ * return <h1>{t('hash_abc', 'Welcome')}</h1>;
36
+ * }
37
+ *
38
+ * // Also works in Client Components (via conditional exports)
39
+ * 'use client';
40
+ * export default function ClientPage() {
41
+ * const t = useTranslation(['hash_abc', 'hash_def']);
42
+ * return <h1>{t('hash_abc', 'Welcome')}</h1>;
43
+ * }
44
+ * ```
45
+ */
46
+ const useTranslation = (hashes) => {
47
+ const { locale, translations } = (0, react.use)(getTranslations(hashes));
48
+ require_logger.logger.debug(`Server. The translations for locale ${locale} are: ${JSON.stringify(translations)}`);
49
+ return {
50
+ t: (hash, source, params) => {
51
+ const text = translations[hash] || source;
52
+ if (!params) return text;
53
+ return require_render_rich_text.renderRichText(text, params, locale);
54
+ },
55
+ locale
56
+ };
57
+ };
58
+
59
+ //#endregion
60
+ exports.useTranslation = useTranslation;
@@ -0,0 +1,36 @@
1
+ import { TranslationHook } from "../types.cjs";
2
+
3
+ //#region src/react/server/useTranslation.d.ts
4
+
5
+ /**
6
+ * Server-side translation hook
7
+ *
8
+ * Works in Server Components WITHOUT async/await!
9
+ * Uses React's use() hook to unwrap the cached promise.
10
+ *
11
+ * This hook has the SAME signature as the client-side version,
12
+ * making components truly isomorphic.
13
+ *
14
+ * @param hashes - List of translation hashes used in component (injected by compiler)
15
+ * @returns Translation function
16
+ *
17
+ * @example
18
+ * ```tsx
19
+ * // Works in Server Components (no async needed!)
20
+ * export default function ServerPage() {
21
+ * const t = useTranslation(['hash_abc', 'hash_def']);
22
+ * return <h1>{t('hash_abc', 'Welcome')}</h1>;
23
+ * }
24
+ *
25
+ * // Also works in Client Components (via conditional exports)
26
+ * 'use client';
27
+ * export default function ClientPage() {
28
+ * const t = useTranslation(['hash_abc', 'hash_def']);
29
+ * return <h1>{t('hash_abc', 'Welcome')}</h1>;
30
+ * }
31
+ * ```
32
+ */
33
+ declare const useTranslation: TranslationHook;
34
+ //#endregion
35
+ export { useTranslation };
36
+ //# sourceMappingURL=useTranslation.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useTranslation.d.cts","names":[],"sources":["../../../src/react/server/useTranslation.ts"],"sourcesContent":[],"mappings":";;;;;;AAyDA;;;;;;;;;;;;;;;;;;;;;;;;;;cAAa,gBAAgB"}
@@ -0,0 +1,36 @@
1
+ import { TranslationHook } from "../types.mjs";
2
+
3
+ //#region src/react/server/useTranslation.d.ts
4
+
5
+ /**
6
+ * Server-side translation hook
7
+ *
8
+ * Works in Server Components WITHOUT async/await!
9
+ * Uses React's use() hook to unwrap the cached promise.
10
+ *
11
+ * This hook has the SAME signature as the client-side version,
12
+ * making components truly isomorphic.
13
+ *
14
+ * @param hashes - List of translation hashes used in component (injected by compiler)
15
+ * @returns Translation function
16
+ *
17
+ * @example
18
+ * ```tsx
19
+ * // Works in Server Components (no async needed!)
20
+ * export default function ServerPage() {
21
+ * const t = useTranslation(['hash_abc', 'hash_def']);
22
+ * return <h1>{t('hash_abc', 'Welcome')}</h1>;
23
+ * }
24
+ *
25
+ * // Also works in Client Components (via conditional exports)
26
+ * 'use client';
27
+ * export default function ClientPage() {
28
+ * const t = useTranslation(['hash_abc', 'hash_def']);
29
+ * return <h1>{t('hash_abc', 'Welcome')}</h1>;
30
+ * }
31
+ * ```
32
+ */
33
+ declare const useTranslation: TranslationHook;
34
+ //#endregion
35
+ export { useTranslation };
36
+ //# sourceMappingURL=useTranslation.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useTranslation.d.mts","names":[],"sources":["../../../src/react/server/useTranslation.ts"],"sourcesContent":[],"mappings":";;;;;;AAyDA;;;;;;;;;;;;;;;;;;;;;;;;;;cAAa,gBAAgB"}
@@ -0,0 +1,60 @@
1
+ import { logger } from "../../utils/logger.mjs";
2
+ import { fetchTranslationsOnServer } from "../server-only/translations.mjs";
3
+ import { renderRichText } from "../shared/render-rich-text.mjs";
4
+ import { use } from "react";
5
+ import { getServerLocale } from "@weglot/translate-react/virtual/locale/server";
6
+
7
+ //#region src/react/server/useTranslation.ts
8
+ const getTranslations = async (hashes) => {
9
+ const locale = await getServerLocale();
10
+ const translations = await fetchTranslationsOnServer(locale, hashes);
11
+ logger.debug(`Server. The translations for locale ${locale} are: ${JSON.stringify(translations)}`);
12
+ return {
13
+ locale,
14
+ translations
15
+ };
16
+ };
17
+ /**
18
+ * Server-side translation hook
19
+ *
20
+ * Works in Server Components WITHOUT async/await!
21
+ * Uses React's use() hook to unwrap the cached promise.
22
+ *
23
+ * This hook has the SAME signature as the client-side version,
24
+ * making components truly isomorphic.
25
+ *
26
+ * @param hashes - List of translation hashes used in component (injected by compiler)
27
+ * @returns Translation function
28
+ *
29
+ * @example
30
+ * ```tsx
31
+ * // Works in Server Components (no async needed!)
32
+ * export default function ServerPage() {
33
+ * const t = useTranslation(['hash_abc', 'hash_def']);
34
+ * return <h1>{t('hash_abc', 'Welcome')}</h1>;
35
+ * }
36
+ *
37
+ * // Also works in Client Components (via conditional exports)
38
+ * 'use client';
39
+ * export default function ClientPage() {
40
+ * const t = useTranslation(['hash_abc', 'hash_def']);
41
+ * return <h1>{t('hash_abc', 'Welcome')}</h1>;
42
+ * }
43
+ * ```
44
+ */
45
+ const useTranslation = (hashes) => {
46
+ const { locale, translations } = use(getTranslations(hashes));
47
+ logger.debug(`Server. The translations for locale ${locale} are: ${JSON.stringify(translations)}`);
48
+ return {
49
+ t: (hash, source, params) => {
50
+ const text = translations[hash] || source;
51
+ if (!params) return text;
52
+ return renderRichText(text, params, locale);
53
+ },
54
+ locale
55
+ };
56
+ };
57
+
58
+ //#endregion
59
+ export { useTranslation };
60
+ //# sourceMappingURL=useTranslation.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useTranslation.mjs","names":["useTranslation: TranslationHook"],"sources":["../../../src/react/server/useTranslation.ts"],"sourcesContent":["import type { ReactNode } from \"react\";\nimport { use } from \"react\";\nimport { getServerLocale } from \"@weglot/translate-react/virtual/locale/server\";\nimport { fetchTranslationsOnServer } from \"../server-only/translations\";\nimport {\n renderRichText,\n type RichTextParams,\n} from \"../shared/render-rich-text\";\nimport { logger } from \"../../utils/logger\";\nimport type { TranslationHook } from \"../types\";\n\n// TODO (AleksandrSl 01/12/2025): Should we add back the cache?\nconst getTranslations = async (hashes: string[]) => {\n // 1. Resolve locale (framework-specific)\n const locale = await getServerLocale();\n\n // 2. Fetch translations (universal)\n const translations = await fetchTranslationsOnServer(locale, hashes);\n\n logger.debug(\n `Server. The translations for locale ${locale} are: ${JSON.stringify(translations)}`,\n );\n\n return {\n locale,\n translations,\n };\n};\n\n/**\n * Server-side translation hook\n *\n * Works in Server Components WITHOUT async/await!\n * Uses React's use() hook to unwrap the cached promise.\n *\n * This hook has the SAME signature as the client-side version,\n * making components truly isomorphic.\n *\n * @param hashes - List of translation hashes used in component (injected by compiler)\n * @returns Translation function\n *\n * @example\n * ```tsx\n * // Works in Server Components (no async needed!)\n * export default function ServerPage() {\n * const t = useTranslation(['hash_abc', 'hash_def']);\n * return <h1>{t('hash_abc', 'Welcome')}</h1>;\n * }\n *\n * // Also works in Client Components (via conditional exports)\n * 'use client';\n * export default function ClientPage() {\n * const t = useTranslation(['hash_abc', 'hash_def']);\n * return <h1>{t('hash_abc', 'Welcome')}</h1>;\n * }\n * ```\n */\nexport const useTranslation: TranslationHook = (hashes: string[]) => {\n const { locale, translations } = use(getTranslations(hashes));\n logger.debug(\n `Server. The translations for locale ${locale} are: ${JSON.stringify(translations)}`,\n );\n\n return {\n t: (\n hash: string,\n source: string,\n params?: RichTextParams,\n ): string | ReactNode => {\n const text = translations[hash] || source;\n\n if (!params) {\n return text;\n }\n\n return renderRichText(text, params, locale);\n },\n locale,\n };\n};\n"],"mappings":";;;;;;;AAYA,MAAM,kBAAkB,OAAO,WAAqB;CAElD,MAAM,SAAS,MAAM,iBAAiB;CAGtC,MAAM,eAAe,MAAM,0BAA0B,QAAQ,OAAO;AAEpE,QAAO,MACL,uCAAuC,OAAO,QAAQ,KAAK,UAAU,aAAa,GACnF;AAED,QAAO;EACL;EACA;EACD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BH,MAAaA,kBAAmC,WAAqB;CACnE,MAAM,EAAE,QAAQ,iBAAiB,IAAI,gBAAgB,OAAO,CAAC;AAC7D,QAAO,MACL,uCAAuC,OAAO,QAAQ,KAAK,UAAU,aAAa,GACnF;AAED,QAAO;EACL,IACE,MACA,QACA,WACuB;GACvB,MAAM,OAAO,aAAa,SAAS;AAEnC,OAAI,CAAC,OACH,QAAO;AAGT,UAAO,eAAe,MAAM,QAAQ,OAAO;;EAE7C;EACD"}
@@ -0,0 +1,42 @@
1
+ const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
2
+ const require_logger = require('../../utils/logger.cjs');
3
+ const require_translations = require('./translations.cjs');
4
+ const require_render_rich_text = require('../shared/render-rich-text.cjs');
5
+ let _weglot_translate_react_virtual_locale_server = require("@weglot/translate-react/virtual/locale/server");
6
+
7
+ //#region src/react/server-only/index.ts
8
+ /**
9
+ * Server-side translation utilities for React Server Components
10
+ * Provides async API for explicit locale passing
11
+ *
12
+ * NOTE: For most cases, use the unified `useTranslation()` hook instead.
13
+ * This module is for advanced use cases where you need explicit control.
14
+ *
15
+ * @module @weglot/translate-react/react/server
16
+ */
17
+ /**
18
+ * Get server-side translations function
19
+ * This is the async alternative to useTranslation() for Server Components
20
+ *
21
+ * For the most common use case, use the unified `useTranslation()` hook instead.
22
+ *
23
+ * The function automatically fetches translations from static files in production
24
+ * and falls back to the development server in development mode.
25
+ */
26
+ async function getServerTranslations(options) {
27
+ const locale = options.locale || await (0, _weglot_translate_react_virtual_locale_server.getServerLocale)();
28
+ require_logger.logger.debug(`Async Server. Fetching translations for ${locale}`);
29
+ const translations = await require_translations.fetchTranslationsOnServer(locale, options.hashes ?? [], { basePath: options.basePath });
30
+ return {
31
+ t: (hash, sourceText, params) => {
32
+ const text = translations[hash] || sourceText;
33
+ if (!params) return text;
34
+ return require_render_rich_text.renderRichText(text, params, locale);
35
+ },
36
+ locale,
37
+ translations
38
+ };
39
+ }
40
+
41
+ //#endregion
42
+ exports.getServerTranslations = getServerTranslations;
@@ -0,0 +1,38 @@
1
+ import { LocaleCode } from "../../locale.cjs";
2
+ import { RichTextParams } from "../shared/render-rich-text.cjs";
3
+ import { ReactNode } from "react";
4
+
5
+ //#region src/react/server-only/index.d.ts
6
+
7
+ /**
8
+ * Get server-side translations function
9
+ * This is the async alternative to useTranslation() for Server Components
10
+ *
11
+ * For the most common use case, use the unified `useTranslation()` hook instead.
12
+ *
13
+ * The function automatically fetches translations from static files in production
14
+ * and falls back to the development server in development mode.
15
+ */
16
+ declare function getServerTranslations(options: {
17
+ /**
18
+ * Target locale for translations (required)
19
+ */
20
+ locale?: LocaleCode;
21
+ /**
22
+ * List of translation hashes needed for this component
23
+ * Typically injected at build time by the Babel plugin
24
+ */
25
+ hashes?: string[];
26
+ /**
27
+ * Base path for translation files
28
+ * @default process.cwd()
29
+ */
30
+ basePath?: string;
31
+ }): Promise<{
32
+ t: (hash: string, sourceText: string, params?: RichTextParams) => string | ReactNode;
33
+ locale: LocaleCode;
34
+ translations: Record<string, string>;
35
+ }>;
36
+ //#endregion
37
+ export { getServerTranslations };
38
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../../../src/react/server-only/index.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;;;;;;iBA8BsB,qBAAA;;;;WAIX;;;;;;;;;;;IAaP;iDAIS,4BACG;UACN;gBACM"}
@@ -0,0 +1,38 @@
1
+ import { LocaleCode } from "../../locale.mjs";
2
+ import { RichTextParams } from "../shared/render-rich-text.mjs";
3
+ import { ReactNode } from "react";
4
+
5
+ //#region src/react/server-only/index.d.ts
6
+
7
+ /**
8
+ * Get server-side translations function
9
+ * This is the async alternative to useTranslation() for Server Components
10
+ *
11
+ * For the most common use case, use the unified `useTranslation()` hook instead.
12
+ *
13
+ * The function automatically fetches translations from static files in production
14
+ * and falls back to the development server in development mode.
15
+ */
16
+ declare function getServerTranslations(options: {
17
+ /**
18
+ * Target locale for translations (required)
19
+ */
20
+ locale?: LocaleCode;
21
+ /**
22
+ * List of translation hashes needed for this component
23
+ * Typically injected at build time by the Babel plugin
24
+ */
25
+ hashes?: string[];
26
+ /**
27
+ * Base path for translation files
28
+ * @default process.cwd()
29
+ */
30
+ basePath?: string;
31
+ }): Promise<{
32
+ t: (hash: string, sourceText: string, params?: RichTextParams) => string | ReactNode;
33
+ locale: LocaleCode;
34
+ translations: Record<string, string>;
35
+ }>;
36
+ //#endregion
37
+ export { getServerTranslations };
38
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../../../src/react/server-only/index.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;;;;;;iBA8BsB,qBAAA;;;;WAIX;;;;;;;;;;;IAaP;iDAIS,4BACG;UACN;gBACM"}
@@ -0,0 +1,42 @@
1
+ import { logger } from "../../utils/logger.mjs";
2
+ import { fetchTranslationsOnServer } from "./translations.mjs";
3
+ import { renderRichText } from "../shared/render-rich-text.mjs";
4
+ import { getServerLocale } from "@weglot/translate-react/virtual/locale/server";
5
+
6
+ //#region src/react/server-only/index.ts
7
+ /**
8
+ * Server-side translation utilities for React Server Components
9
+ * Provides async API for explicit locale passing
10
+ *
11
+ * NOTE: For most cases, use the unified `useTranslation()` hook instead.
12
+ * This module is for advanced use cases where you need explicit control.
13
+ *
14
+ * @module @weglot/translate-react/react/server
15
+ */
16
+ /**
17
+ * Get server-side translations function
18
+ * This is the async alternative to useTranslation() for Server Components
19
+ *
20
+ * For the most common use case, use the unified `useTranslation()` hook instead.
21
+ *
22
+ * The function automatically fetches translations from static files in production
23
+ * and falls back to the development server in development mode.
24
+ */
25
+ async function getServerTranslations(options) {
26
+ const locale = options.locale || await getServerLocale();
27
+ logger.debug(`Async Server. Fetching translations for ${locale}`);
28
+ const translations = await fetchTranslationsOnServer(locale, options.hashes ?? [], { basePath: options.basePath });
29
+ return {
30
+ t: (hash, sourceText, params) => {
31
+ const text = translations[hash] || sourceText;
32
+ if (!params) return text;
33
+ return renderRichText(text, params, locale);
34
+ },
35
+ locale,
36
+ translations
37
+ };
38
+ }
39
+
40
+ //#endregion
41
+ export { getServerTranslations };
42
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../../../src/react/server-only/index.ts"],"sourcesContent":["/**\n * Server-side translation utilities for React Server Components\n * Provides async API for explicit locale passing\n *\n * NOTE: For most cases, use the unified `useTranslation()` hook instead.\n * This module is for advanced use cases where you need explicit control.\n *\n * @module @weglot/translate-react/react/server\n */\n\nimport { fetchTranslationsOnServer } from \"./translations\";\nimport {\n renderRichText,\n type RichTextParams,\n} from \"../shared/render-rich-text\";\nimport type { ReactNode } from \"react\";\nimport { logger } from \"../../utils/logger\";\n// Keep this import full for replacement during build.\nimport { getServerLocale } from \"@weglot/translate-react/virtual/locale/server\";\nimport type { LocaleCode } from \"../../locale\";\n\n/**\n * Get server-side translations function\n * This is the async alternative to useTranslation() for Server Components\n *\n * For the most common use case, use the unified `useTranslation()` hook instead.\n *\n * The function automatically fetches translations from static files in production\n * and falls back to the development server in development mode.\n */\nexport async function getServerTranslations(options: {\n /**\n * Target locale for translations (required)\n */\n locale?: LocaleCode;\n\n /**\n * List of translation hashes needed for this component\n * Typically injected at build time by the Babel plugin\n */\n hashes?: string[];\n\n /**\n * Base path for translation files\n * @default process.cwd()\n */\n basePath?: string;\n}): Promise<{\n t: (\n hash: string,\n sourceText: string,\n params?: RichTextParams,\n ) => string | ReactNode;\n locale: LocaleCode;\n translations: Record<string, string>;\n}> {\n const locale = options.locale || (await getServerLocale());\n\n logger.debug(`Async Server. Fetching translations for ${locale}`);\n\n const translations = await fetchTranslationsOnServer(\n locale,\n options.hashes ?? [],\n {\n basePath: options.basePath,\n },\n );\n\n return {\n t: (hash: string, sourceText: string, params?: RichTextParams) => {\n const text = translations[hash] || sourceText;\n\n if (!params) {\n return text;\n }\n\n return renderRichText(text, params, locale);\n },\n locale,\n translations,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA8BA,eAAsB,sBAAsB,SAyBzC;CACD,MAAM,SAAS,QAAQ,UAAW,MAAM,iBAAiB;AAEzD,QAAO,MAAM,2CAA2C,SAAS;CAEjE,MAAM,eAAe,MAAM,0BACzB,QACA,QAAQ,UAAU,EAAE,EACpB,EACE,UAAU,QAAQ,UACnB,CACF;AAED,QAAO;EACL,IAAI,MAAc,YAAoB,WAA4B;GAChE,MAAM,OAAO,aAAa,SAAS;AAEnC,OAAI,CAAC,OACH,QAAO;AAGT,UAAO,eAAe,MAAM,QAAQ,OAAO;;EAE7C;EACA;EACD"}
@@ -0,0 +1,86 @@
1
+ const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
2
+ const require_logger = require('../../utils/logger.cjs');
3
+ const require_utils = require('../shared/utils.cjs');
4
+ let _weglot_translate_react_virtual_config = require("@weglot/translate-react/virtual/config");
5
+ let fs_promises = require("fs/promises");
6
+ let path = require("path");
7
+
8
+ //#region src/react/server-only/translations.ts
9
+ /**
10
+ * Core translation service - framework-agnostic
11
+ * Handles fetching translations from dev server or filesystem
12
+ *
13
+ * This is the UNIVERSAL pattern that works across all frameworks:
14
+ * Given a locale, fetch the translation dictionary for that locale.
15
+ *
16
+ * @module @weglot/translate-react/react/server/translations
17
+ */
18
+ /**
19
+ * Read translations from filesystem
20
+ * Used in production builds or as fallback in development
21
+ *
22
+ * Tries multiple common locations for translation files:
23
+ * - weglot/cache/{locale}.json (dev cache)
24
+ * - .next/{locale}.json (Next.js)
25
+ *
26
+ * @param locale - Target locale
27
+ * @param basePath - Base directory to search from (default: cwd)
28
+ * @returns Translation dictionary (hash -> translated text)
29
+ */
30
+ async function readFromFilesystem(locale, basePath = process.cwd()) {
31
+ const possiblePaths = [(0, path.join)(basePath, ".next", `${locale}.json`)];
32
+ if (process.env.NODE_ENV === "development") possiblePaths.push((0, path.isAbsolute)(_weglot_translate_react_virtual_config.cacheDir) ? (0, path.join)(_weglot_translate_react_virtual_config.cacheDir, `${locale}.json`) : (0, path.join)(basePath, _weglot_translate_react_virtual_config.cacheDir, `${locale}.json`));
33
+ for (const filePath of possiblePaths) try {
34
+ const fileContent = await (0, fs_promises.readFile)(filePath, "utf-8");
35
+ const translations = JSON.parse(fileContent).entries || {};
36
+ require_logger.logger.debug(`Loaded ${Object.keys(translations).length} translations from ${filePath}`);
37
+ return translations;
38
+ } catch {
39
+ continue;
40
+ }
41
+ if (process.env.NODE_ENV === "development") require_logger.logger.warn(`Translation file not found for locale: ${locale}. Tried: ${possiblePaths.join(", ")}`);
42
+ return {};
43
+ }
44
+ /**
45
+ * Fetch translations for a given locale
46
+ *
47
+ * This is the CORE UNIVERSAL function that works across all frameworks.
48
+ * It handles the logic of fetching translations from either:
49
+ * - Development server (if running)
50
+ * - Filesystem (production or fallback)
51
+ *
52
+ * Framework-specific code should NOT duplicate this logic.
53
+ * Instead, they should:
54
+ * 1. Resolve the locale (framework-specific: cookies, URL, headers, etc.)
55
+ * 2. Call this function to get translations
56
+ *
57
+ * @example
58
+ * ```typescript
59
+ * // In Next.js locale resolver
60
+ * const locale = await getLocaleFromCookies();
61
+ * const translations = await fetchTranslations(locale);
62
+ * ```
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * // In React Router loader
67
+ * const locale = getLocaleFromUrl(request.url);
68
+ * const translations = await fetchTranslations(locale);
69
+ * ```
70
+ */
71
+ async function fetchTranslationsOnServer(locale, hashes, config = {}) {
72
+ if (process.env.NODE_ENV === "development" && _weglot_translate_react_virtual_config.serverUrl && hashes.length > 0) {
73
+ require_logger.logger.debug(`Server. Fetching translations for ${locale} and ${hashes.join(", ")} from dev server (${_weglot_translate_react_virtual_config.serverUrl})`);
74
+ try {
75
+ return await require_utils.fetchTranslations(locale, hashes, _weglot_translate_react_virtual_config.serverUrl);
76
+ } catch (error) {
77
+ require_logger.logger.warn(`Failed to fetch translations from translation server: ${error}.`);
78
+ return {};
79
+ }
80
+ }
81
+ require_logger.logger.debug(`Reading translations for ${locale} from filesystem`);
82
+ return await readFromFilesystem(locale, config.basePath);
83
+ }
84
+
85
+ //#endregion
86
+ exports.fetchTranslationsOnServer = fetchTranslationsOnServer;
@@ -0,0 +1,86 @@
1
+ import { logger } from "../../utils/logger.mjs";
2
+ import { fetchTranslations } from "../shared/utils.mjs";
3
+ import { cacheDir, serverUrl } from "@weglot/translate-react/virtual/config";
4
+ import { readFile } from "fs/promises";
5
+ import { isAbsolute, join } from "path";
6
+
7
+ //#region src/react/server-only/translations.ts
8
+ /**
9
+ * Core translation service - framework-agnostic
10
+ * Handles fetching translations from dev server or filesystem
11
+ *
12
+ * This is the UNIVERSAL pattern that works across all frameworks:
13
+ * Given a locale, fetch the translation dictionary for that locale.
14
+ *
15
+ * @module @weglot/translate-react/react/server/translations
16
+ */
17
+ /**
18
+ * Read translations from filesystem
19
+ * Used in production builds or as fallback in development
20
+ *
21
+ * Tries multiple common locations for translation files:
22
+ * - weglot/cache/{locale}.json (dev cache)
23
+ * - .next/{locale}.json (Next.js)
24
+ *
25
+ * @param locale - Target locale
26
+ * @param basePath - Base directory to search from (default: cwd)
27
+ * @returns Translation dictionary (hash -> translated text)
28
+ */
29
+ async function readFromFilesystem(locale, basePath = process.cwd()) {
30
+ const possiblePaths = [join(basePath, ".next", `${locale}.json`)];
31
+ if (process.env.NODE_ENV === "development") possiblePaths.push(isAbsolute(cacheDir) ? join(cacheDir, `${locale}.json`) : join(basePath, cacheDir, `${locale}.json`));
32
+ for (const filePath of possiblePaths) try {
33
+ const fileContent = await readFile(filePath, "utf-8");
34
+ const translations = JSON.parse(fileContent).entries || {};
35
+ logger.debug(`Loaded ${Object.keys(translations).length} translations from ${filePath}`);
36
+ return translations;
37
+ } catch {
38
+ continue;
39
+ }
40
+ if (process.env.NODE_ENV === "development") logger.warn(`Translation file not found for locale: ${locale}. Tried: ${possiblePaths.join(", ")}`);
41
+ return {};
42
+ }
43
+ /**
44
+ * Fetch translations for a given locale
45
+ *
46
+ * This is the CORE UNIVERSAL function that works across all frameworks.
47
+ * It handles the logic of fetching translations from either:
48
+ * - Development server (if running)
49
+ * - Filesystem (production or fallback)
50
+ *
51
+ * Framework-specific code should NOT duplicate this logic.
52
+ * Instead, they should:
53
+ * 1. Resolve the locale (framework-specific: cookies, URL, headers, etc.)
54
+ * 2. Call this function to get translations
55
+ *
56
+ * @example
57
+ * ```typescript
58
+ * // In Next.js locale resolver
59
+ * const locale = await getLocaleFromCookies();
60
+ * const translations = await fetchTranslations(locale);
61
+ * ```
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * // In React Router loader
66
+ * const locale = getLocaleFromUrl(request.url);
67
+ * const translations = await fetchTranslations(locale);
68
+ * ```
69
+ */
70
+ async function fetchTranslationsOnServer(locale, hashes, config = {}) {
71
+ if (process.env.NODE_ENV === "development" && serverUrl && hashes.length > 0) {
72
+ logger.debug(`Server. Fetching translations for ${locale} and ${hashes.join(", ")} from dev server (${serverUrl})`);
73
+ try {
74
+ return await fetchTranslations(locale, hashes, serverUrl);
75
+ } catch (error) {
76
+ logger.warn(`Failed to fetch translations from translation server: ${error}.`);
77
+ return {};
78
+ }
79
+ }
80
+ logger.debug(`Reading translations for ${locale} from filesystem`);
81
+ return await readFromFilesystem(locale, config.basePath);
82
+ }
83
+
84
+ //#endregion
85
+ export { fetchTranslationsOnServer };
86
+ //# sourceMappingURL=translations.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"translations.mjs","names":["fetchFromDevServer"],"sources":["../../../src/react/server-only/translations.ts"],"sourcesContent":["/**\n * Core translation service - framework-agnostic\n * Handles fetching translations from dev server or filesystem\n *\n * This is the UNIVERSAL pattern that works across all frameworks:\n * Given a locale, fetch the translation dictionary for that locale.\n *\n * @module @weglot/translate-react/react/server/translations\n */\n\nimport { readFile } from \"fs/promises\";\nimport { isAbsolute, join } from \"path\";\nimport { logger } from \"../../utils/logger\";\nimport { fetchTranslations as fetchFromDevServer } from \"../shared/utils\";\nimport { cacheDir, serverUrl } from \"@weglot/translate-react/virtual/config\";\nimport type { LocaleCode } from \"../../locale\";\n\n/**\n * Configuration for translation fetching\n */\nexport interface TranslationFetchConfig {\n /**\n * Base path for translation files (default: process.cwd())\n */\n basePath?: string;\n}\n\n/**\n * Read translations from filesystem\n * Used in production builds or as fallback in development\n *\n * Tries multiple common locations for translation files:\n * - weglot/cache/{locale}.json (dev cache)\n * - .next/{locale}.json (Next.js)\n *\n * @param locale - Target locale\n * @param basePath - Base directory to search from (default: cwd)\n * @returns Translation dictionary (hash -> translated text)\n */\nasync function readFromFilesystem(\n locale: LocaleCode,\n basePath: string = process.cwd(),\n): Promise<Record<string, string>> {\n // TODO (AleksandrSl 15/12/2025): What should be the correct way here to load translations on server for other frameworks?\n // Build output is authoritative: the dev cache must never stand in for it.\n const possiblePaths = [join(basePath, \".next\", `${locale}.json`)];\n\n if (process.env.NODE_ENV === \"development\") {\n // cacheDir is emitted absolute, and join() would append it to basePath.\n possiblePaths.push(\n isAbsolute(cacheDir)\n ? join(cacheDir, `${locale}.json`)\n : join(basePath, cacheDir, `${locale}.json`),\n );\n }\n\n // Try each path until we find the file\n for (const filePath of possiblePaths) {\n try {\n const fileContent = await readFile(filePath, \"utf-8\");\n const data = JSON.parse(fileContent);\n const translations = data.entries || {};\n\n logger.debug(\n `Loaded ${Object.keys(translations).length} translations from ${filePath}`,\n );\n return translations;\n } catch {\n // File doesn't exist at this path, try next one\n continue;\n }\n }\n\n // No translations found\n if (process.env.NODE_ENV === \"development\") {\n logger.warn(\n `Translation file not found for locale: ${locale}. Tried: ${possiblePaths.join(\", \")}`,\n );\n }\n\n return {};\n}\n\n/**\n * Fetch translations for a given locale\n *\n * This is the CORE UNIVERSAL function that works across all frameworks.\n * It handles the logic of fetching translations from either:\n * - Development server (if running)\n * - Filesystem (production or fallback)\n *\n * Framework-specific code should NOT duplicate this logic.\n * Instead, they should:\n * 1. Resolve the locale (framework-specific: cookies, URL, headers, etc.)\n * 2. Call this function to get translations\n *\n * @example\n * ```typescript\n * // In Next.js locale resolver\n * const locale = await getLocaleFromCookies();\n * const translations = await fetchTranslations(locale);\n * ```\n *\n * @example\n * ```typescript\n * // In React Router loader\n * const locale = getLocaleFromUrl(request.url);\n * const translations = await fetchTranslations(locale);\n * ```\n */\nexport async function fetchTranslationsOnServer(\n locale: LocaleCode,\n hashes: string[],\n config: TranslationFetchConfig = {},\n): Promise<Record<string, string>> {\n const isDev = process.env.NODE_ENV === \"development\";\n\n // A provider wrapping the whole tree has no hashes to ask for; without the\n // cache below, SSR would render source text and the client would swap it.\n if (isDev && serverUrl && hashes.length > 0) {\n logger.debug(\n `Server. Fetching translations for ${locale} and ${hashes.join(\", \")} from dev server (${serverUrl})`,\n );\n try {\n return await fetchFromDevServer(locale, hashes, serverUrl);\n } catch (error) {\n logger.warn(\n `Failed to fetch translations from translation server: ${error}.`,\n );\n return {};\n }\n }\n\n logger.debug(`Reading translations for ${locale} from filesystem`);\n return await readFromFilesystem(locale, config.basePath);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCA,eAAe,mBACb,QACA,WAAmB,QAAQ,KAAK,EACC;CAGjC,MAAM,gBAAgB,CAAC,KAAK,UAAU,SAAS,GAAG,OAAO,OAAO,CAAC;AAEjE,KAAI,QAAQ,IAAI,aAAa,cAE3B,eAAc,KACZ,WAAW,SAAS,GAChB,KAAK,UAAU,GAAG,OAAO,OAAO,GAChC,KAAK,UAAU,UAAU,GAAG,OAAO,OAAO,CAC/C;AAIH,MAAK,MAAM,YAAY,cACrB,KAAI;EACF,MAAM,cAAc,MAAM,SAAS,UAAU,QAAQ;EAErD,MAAM,eADO,KAAK,MAAM,YAAY,CACV,WAAW,EAAE;AAEvC,SAAO,MACL,UAAU,OAAO,KAAK,aAAa,CAAC,OAAO,qBAAqB,WACjE;AACD,SAAO;SACD;AAEN;;AAKJ,KAAI,QAAQ,IAAI,aAAa,cAC3B,QAAO,KACL,0CAA0C,OAAO,WAAW,cAAc,KAAK,KAAK,GACrF;AAGH,QAAO,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BX,eAAsB,0BACpB,QACA,QACA,SAAiC,EAAE,EACF;AAKjC,KAJc,QAAQ,IAAI,aAAa,iBAI1B,aAAa,OAAO,SAAS,GAAG;AAC3C,SAAO,MACL,qCAAqC,OAAO,OAAO,OAAO,KAAK,KAAK,CAAC,oBAAoB,UAAU,GACpG;AACD,MAAI;AACF,UAAO,MAAMA,kBAAmB,QAAQ,QAAQ,UAAU;WACnD,OAAO;AACd,UAAO,KACL,yDAAyD,MAAM,GAChE;AACD,UAAO,EAAE;;;AAIb,QAAO,MAAM,4BAA4B,OAAO,kBAAkB;AAClE,QAAO,MAAM,mBAAmB,QAAQ,OAAO,SAAS"}