vanilla-intlayer 8.8.0 → 8.9.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 (209) hide show
  1. package/dist/cjs/_virtual/_rolldown/runtime.cjs +29 -0
  2. package/dist/cjs/client/index.cjs +24 -1
  3. package/dist/cjs/client/installIntlayer.cjs +98 -1
  4. package/dist/cjs/client/installIntlayer.cjs.map +1 -0
  5. package/dist/cjs/client/useDictionary.cjs +59 -1
  6. package/dist/cjs/client/useDictionary.cjs.map +1 -0
  7. package/dist/cjs/client/useDictionaryDynamic.cjs +93 -1
  8. package/dist/cjs/client/useDictionaryDynamic.cjs.map +1 -0
  9. package/dist/cjs/client/useIntlayer.cjs +65 -1
  10. package/dist/cjs/client/useIntlayer.cjs.map +1 -0
  11. package/dist/cjs/client/useLocale.cjs +58 -1
  12. package/dist/cjs/client/useLocale.cjs.map +1 -0
  13. package/dist/cjs/client/useLocaleStorage.cjs +51 -1
  14. package/dist/cjs/client/useLocaleStorage.cjs.map +1 -0
  15. package/dist/cjs/client/useRewriteURL.cjs +46 -1
  16. package/dist/cjs/client/useRewriteURL.cjs.map +1 -0
  17. package/dist/cjs/editor/useEditor.cjs +49 -1
  18. package/dist/cjs/editor/useEditor.cjs.map +1 -0
  19. package/dist/cjs/format/index.cjs +20 -1
  20. package/dist/cjs/format/useCompact.cjs +17 -1
  21. package/dist/cjs/format/useCompact.cjs.map +1 -0
  22. package/dist/cjs/format/useCurrency.cjs +17 -1
  23. package/dist/cjs/format/useCurrency.cjs.map +1 -0
  24. package/dist/cjs/format/useDate.cjs +26 -1
  25. package/dist/cjs/format/useDate.cjs.map +1 -0
  26. package/dist/cjs/format/useIntl.cjs +44 -1
  27. package/dist/cjs/format/useIntl.cjs.map +1 -0
  28. package/dist/cjs/format/useList.cjs +17 -1
  29. package/dist/cjs/format/useList.cjs.map +1 -0
  30. package/dist/cjs/format/useNumber.cjs +20 -1
  31. package/dist/cjs/format/useNumber.cjs.map +1 -0
  32. package/dist/cjs/format/usePercentage.cjs +17 -1
  33. package/dist/cjs/format/usePercentage.cjs.map +1 -0
  34. package/dist/cjs/format/useRelativeTime.cjs +17 -1
  35. package/dist/cjs/format/useRelativeTime.cjs.map +1 -0
  36. package/dist/cjs/format/useUnit.cjs +17 -1
  37. package/dist/cjs/format/useUnit.cjs.map +1 -0
  38. package/dist/cjs/getDictionary.cjs +11 -1
  39. package/dist/cjs/getDictionary.cjs.map +1 -0
  40. package/dist/cjs/getIntlayer.cjs +11 -1
  41. package/dist/cjs/getIntlayer.cjs.map +1 -0
  42. package/dist/cjs/html/HTMLRenderer.cjs +22 -1
  43. package/dist/cjs/html/HTMLRenderer.cjs.map +1 -0
  44. package/dist/cjs/html/index.cjs +9 -1
  45. package/dist/cjs/html/installIntlayerHTML.cjs +91 -1
  46. package/dist/cjs/html/installIntlayerHTML.cjs.map +1 -0
  47. package/dist/cjs/html/types.cjs +0 -1
  48. package/dist/cjs/index.cjs +37 -1
  49. package/dist/cjs/markdown/MarkdownRenderer.cjs +41 -1
  50. package/dist/cjs/markdown/MarkdownRenderer.cjs.map +1 -0
  51. package/dist/cjs/markdown/compiler.cjs +37 -1
  52. package/dist/cjs/markdown/compiler.cjs.map +1 -0
  53. package/dist/cjs/markdown/index.cjs +11 -1
  54. package/dist/cjs/markdown/installIntlayerMarkdown.cjs +98 -1
  55. package/dist/cjs/markdown/installIntlayerMarkdown.cjs.map +1 -0
  56. package/dist/cjs/markdown/runtime.cjs +87 -1
  57. package/dist/cjs/markdown/runtime.cjs.map +1 -0
  58. package/dist/cjs/plugins.cjs +240 -1
  59. package/dist/cjs/plugins.cjs.map +1 -0
  60. package/dist/cjs/renderIntlayerNode.cjs +44 -1
  61. package/dist/cjs/renderIntlayerNode.cjs.map +1 -0
  62. package/dist/esm/client/index.mjs +9 -8
  63. package/dist/esm/client/installIntlayer.mjs +93 -2
  64. package/dist/esm/client/installIntlayer.mjs.map +1 -0
  65. package/dist/esm/client/useDictionary.mjs +55 -8
  66. package/dist/esm/client/useDictionary.mjs.map +1 -0
  67. package/dist/esm/client/useDictionaryDynamic.mjs +82 -28
  68. package/dist/esm/client/useDictionaryDynamic.mjs.map +1 -0
  69. package/dist/esm/client/useIntlayer.mjs +61 -8
  70. package/dist/esm/client/useIntlayer.mjs.map +1 -0
  71. package/dist/esm/client/useLocale.mjs +51 -17
  72. package/dist/esm/client/useLocale.mjs.map +1 -0
  73. package/dist/esm/client/useLocaleStorage.mjs +39 -13
  74. package/dist/esm/client/useLocaleStorage.mjs.map +1 -0
  75. package/dist/esm/client/useRewriteURL.mjs +40 -12
  76. package/dist/esm/client/useRewriteURL.mjs.map +1 -0
  77. package/dist/esm/editor/useEditor.mjs +47 -2
  78. package/dist/esm/editor/useEditor.mjs.map +1 -0
  79. package/dist/esm/format/index.mjs +11 -10
  80. package/dist/esm/format/useCompact.mjs +11 -8
  81. package/dist/esm/format/useCompact.mjs.map +1 -0
  82. package/dist/esm/format/useCurrency.mjs +11 -8
  83. package/dist/esm/format/useCurrency.mjs.map +1 -0
  84. package/dist/esm/format/useDate.mjs +18 -12
  85. package/dist/esm/format/useDate.mjs.map +1 -0
  86. package/dist/esm/format/useIntl.mjs +36 -8
  87. package/dist/esm/format/useIntl.mjs.map +1 -0
  88. package/dist/esm/format/useList.mjs +11 -8
  89. package/dist/esm/format/useList.mjs.map +1 -0
  90. package/dist/esm/format/useNumber.mjs +14 -8
  91. package/dist/esm/format/useNumber.mjs.map +1 -0
  92. package/dist/esm/format/usePercentage.mjs +11 -8
  93. package/dist/esm/format/usePercentage.mjs.map +1 -0
  94. package/dist/esm/format/useRelativeTime.mjs +11 -8
  95. package/dist/esm/format/useRelativeTime.mjs.map +1 -0
  96. package/dist/esm/format/useUnit.mjs +11 -8
  97. package/dist/esm/format/useUnit.mjs.map +1 -0
  98. package/dist/esm/getDictionary.mjs +7 -4
  99. package/dist/esm/getDictionary.mjs.map +1 -0
  100. package/dist/esm/getIntlayer.mjs +7 -4
  101. package/dist/esm/getIntlayer.mjs.map +1 -0
  102. package/dist/esm/html/HTMLRenderer.mjs +17 -5
  103. package/dist/esm/html/HTMLRenderer.mjs.map +1 -0
  104. package/dist/esm/html/index.mjs +4 -3
  105. package/dist/esm/html/installIntlayerHTML.mjs +81 -13
  106. package/dist/esm/html/installIntlayerHTML.mjs.map +1 -0
  107. package/dist/esm/index.mjs +13 -12
  108. package/dist/esm/markdown/MarkdownRenderer.mjs +36 -16
  109. package/dist/esm/markdown/MarkdownRenderer.mjs.map +1 -0
  110. package/dist/esm/markdown/compiler.mjs +28 -4
  111. package/dist/esm/markdown/compiler.mjs.map +1 -0
  112. package/dist/esm/markdown/index.mjs +5 -4
  113. package/dist/esm/markdown/installIntlayerMarkdown.mjs +86 -23
  114. package/dist/esm/markdown/installIntlayerMarkdown.mjs.map +1 -0
  115. package/dist/esm/markdown/runtime.mjs +60 -28
  116. package/dist/esm/markdown/runtime.mjs.map +1 -0
  117. package/dist/esm/plugins.mjs +187 -134
  118. package/dist/esm/plugins.mjs.map +1 -0
  119. package/dist/esm/renderIntlayerNode.mjs +32 -15
  120. package/dist/esm/renderIntlayerNode.mjs.map +1 -0
  121. package/dist/types/client/index.d.ts +8 -0
  122. package/dist/{client → types/client}/installIntlayer.d.ts +27 -20
  123. package/dist/types/client/installIntlayer.d.ts.map +1 -0
  124. package/dist/{client → types/client}/useDictionary.d.ts +22 -17
  125. package/dist/types/client/useDictionary.d.ts.map +1 -0
  126. package/dist/{client → types/client}/useDictionaryDynamic.d.ts +10 -5
  127. package/dist/types/client/useDictionaryDynamic.d.ts.map +1 -0
  128. package/dist/{client → types/client}/useIntlayer.d.ts +9 -4
  129. package/dist/types/client/useIntlayer.d.ts.map +1 -0
  130. package/dist/{client → types/client}/useLocale.d.ts +14 -10
  131. package/dist/types/client/useLocale.d.ts.map +1 -0
  132. package/dist/types/client/useLocaleStorage.d.ts +37 -0
  133. package/dist/types/client/useLocaleStorage.d.ts.map +1 -0
  134. package/dist/{client → types/client}/useRewriteURL.d.ts +5 -1
  135. package/dist/types/client/useRewriteURL.d.ts.map +1 -0
  136. package/dist/{editor → types/editor}/useEditor.d.ts +5 -1
  137. package/dist/types/editor/useEditor.d.ts.map +1 -0
  138. package/dist/types/format/index.d.ts +10 -0
  139. package/dist/types/format/useCompact.d.ts +7 -0
  140. package/dist/types/format/useCompact.d.ts.map +1 -0
  141. package/dist/types/format/useCurrency.d.ts +7 -0
  142. package/dist/types/format/useCurrency.d.ts.map +1 -0
  143. package/dist/types/format/useDate.d.ts +10 -0
  144. package/dist/types/format/useDate.d.ts.map +1 -0
  145. package/dist/{format → types/format}/useIntl.d.ts +10 -5
  146. package/dist/types/format/useIntl.d.ts.map +1 -0
  147. package/dist/types/format/useList.d.ts +7 -0
  148. package/dist/types/format/useList.d.ts.map +1 -0
  149. package/dist/types/format/useNumber.d.ts +10 -0
  150. package/dist/types/format/useNumber.d.ts.map +1 -0
  151. package/dist/types/format/usePercentage.d.ts +7 -0
  152. package/dist/types/format/usePercentage.d.ts.map +1 -0
  153. package/dist/types/format/useRelativeTime.d.ts +7 -0
  154. package/dist/types/format/useRelativeTime.d.ts.map +1 -0
  155. package/dist/types/format/useUnit.d.ts +7 -0
  156. package/dist/types/format/useUnit.d.ts.map +1 -0
  157. package/dist/types/getDictionary.d.ts +9 -0
  158. package/dist/types/getDictionary.d.ts.map +1 -0
  159. package/dist/types/getIntlayer.d.ts +8 -0
  160. package/dist/types/getIntlayer.d.ts.map +1 -0
  161. package/dist/types/html/HTMLRenderer.d.ts +21 -0
  162. package/dist/types/html/HTMLRenderer.d.ts.map +1 -0
  163. package/dist/types/html/index.d.ts +4 -0
  164. package/dist/{html → types/html}/installIntlayerHTML.d.ts +18 -13
  165. package/dist/types/html/installIntlayerHTML.d.ts.map +1 -0
  166. package/dist/{html → types/html}/types.d.ts +12 -14
  167. package/dist/types/html/types.d.ts.map +1 -0
  168. package/dist/types/index.d.ts +20 -0
  169. package/dist/types/index.d.ts.map +1 -0
  170. package/dist/types/markdown/MarkdownRenderer.d.ts +32 -0
  171. package/dist/types/markdown/MarkdownRenderer.d.ts.map +1 -0
  172. package/dist/types/markdown/compiler.d.ts +23 -0
  173. package/dist/types/markdown/compiler.d.ts.map +1 -0
  174. package/dist/types/markdown/index.d.ts +4 -0
  175. package/dist/types/markdown/installIntlayerMarkdown.d.ts +71 -0
  176. package/dist/types/markdown/installIntlayerMarkdown.d.ts.map +1 -0
  177. package/dist/types/markdown/runtime.d.ts +16 -0
  178. package/dist/types/markdown/runtime.d.ts.map +1 -0
  179. package/dist/types/plugins.d.ts +80 -0
  180. package/dist/types/plugins.d.ts.map +1 -0
  181. package/dist/types/renderIntlayerNode.d.ts +31 -0
  182. package/dist/types/renderIntlayerNode.d.ts.map +1 -0
  183. package/package.json +10 -13
  184. package/dist/client/index.d.ts +0 -7
  185. package/dist/client/useLocaleStorage.d.ts +0 -31
  186. package/dist/format/index.d.ts +0 -9
  187. package/dist/format/useCompact.d.ts +0 -2
  188. package/dist/format/useCurrency.d.ts +0 -2
  189. package/dist/format/useDate.d.ts +0 -5
  190. package/dist/format/useList.d.ts +0 -2
  191. package/dist/format/useNumber.d.ts +0 -5
  192. package/dist/format/usePercentage.d.ts +0 -2
  193. package/dist/format/useRelativeTime.d.ts +0 -2
  194. package/dist/format/useUnit.d.ts +0 -2
  195. package/dist/getDictionary.d.ts +0 -4
  196. package/dist/getIntlayer.d.ts +0 -3
  197. package/dist/html/HTMLRenderer.d.ts +0 -14
  198. package/dist/html/index.d.ts +0 -3
  199. package/dist/index.d.ts +0 -11
  200. package/dist/installIntlayer-C99qMzFH.mjs +0 -47
  201. package/dist/installIntlayer-_1cC62gP.js +0 -1
  202. package/dist/markdown/MarkdownRenderer.d.ts +0 -22
  203. package/dist/markdown/compiler.d.ts +0 -19
  204. package/dist/markdown/index.d.ts +0 -3
  205. package/dist/markdown/installIntlayerMarkdown.d.ts +0 -70
  206. package/dist/markdown/runtime.d.ts +0 -12
  207. package/dist/plugins-Cv8sSUPw.js +0 -1
  208. package/dist/plugins.d.ts +0 -78
  209. package/dist/renderIntlayerNode.d.ts +0 -22
@@ -1,8 +1,9 @@
1
- import { i as e, n as t, r as n, t as r } from "../../installIntlayer-C99qMzFH.mjs";
2
- import { useDictionary as i } from "./useDictionary.mjs";
3
- import { useDictionaryDynamic as a } from "./useDictionaryDynamic.mjs";
4
- import { useIntlayer as o } from "./useIntlayer.mjs";
5
- import { localeCookie as s, localeInStorage as c, setLocaleCookie as l, setLocaleInStorage as u, useLocaleCookie as d, useLocaleStorage as f } from "./useLocaleStorage.mjs";
6
- import { useLocale as p } from "./useLocale.mjs";
7
- import { useRewriteURL as m } from "./useRewriteURL.mjs";
8
- export { r as IntlayerClient, t as createIntlayerClient, n as getIntlayerClient, e as installIntlayer, s as localeCookie, c as localeInStorage, l as setLocaleCookie, u as setLocaleInStorage, i as useDictionary, a as useDictionaryDynamic, o as useIntlayer, p as useLocale, d as useLocaleCookie, f as useLocaleStorage, m as useRewriteURL };
1
+ import { IntlayerClient, createIntlayerClient, getIntlayerClient, installIntlayer } from "./installIntlayer.mjs";
2
+ import { useDictionary } from "./useDictionary.mjs";
3
+ import { useDictionaryDynamic } from "./useDictionaryDynamic.mjs";
4
+ import { useIntlayer } from "./useIntlayer.mjs";
5
+ import { localeCookie, localeInStorage, setLocaleCookie, setLocaleInStorage, useLocaleCookie, useLocaleStorage } from "./useLocaleStorage.mjs";
6
+ import { useLocale } from "./useLocale.mjs";
7
+ import { useRewriteURL } from "./useRewriteURL.mjs";
8
+
9
+ export { IntlayerClient, createIntlayerClient, getIntlayerClient, installIntlayer, localeCookie, localeInStorage, setLocaleCookie, setLocaleInStorage, useDictionary, useDictionaryDynamic, useIntlayer, useLocale, useLocaleCookie, useLocaleStorage, useRewriteURL };
@@ -1,2 +1,93 @@
1
- import { i as e, n as t, r as n, t as r } from "../../installIntlayer-C99qMzFH.mjs";
2
- export { r as IntlayerClient, t as createIntlayerClient, n as getIntlayerClient, e as installIntlayer };
1
+ import { useEditor } from "../editor/useEditor.mjs";
2
+ import { internationalization } from "@intlayer/config/built";
3
+ import { setIntlayerIdentifier } from "@intlayer/config/client";
4
+
5
+ //#region src/client/installIntlayer.ts
6
+ var IntlayerClient = class {
7
+ _locale;
8
+ _listeners = /* @__PURE__ */ new Set();
9
+ isCookieEnabled;
10
+ constructor(locale, isCookieEnabled = true) {
11
+ const { defaultLocale } = internationalization ?? {};
12
+ setIntlayerIdentifier();
13
+ this._locale = locale ?? defaultLocale;
14
+ this.isCookieEnabled = isCookieEnabled;
15
+ }
16
+ get locale() {
17
+ return this._locale;
18
+ }
19
+ setLocale(newLocale) {
20
+ this._locale = newLocale;
21
+ for (const listener of this._listeners) listener(newLocale);
22
+ }
23
+ subscribe(listener) {
24
+ this._listeners.add(listener);
25
+ return () => {
26
+ this._listeners.delete(listener);
27
+ };
28
+ }
29
+ /**
30
+ * Fire all listeners with the current locale without changing it.
31
+ * Used to trigger re-renders after async data loads.
32
+ */
33
+ notify() {
34
+ for (const listener of this._listeners) listener(this._locale);
35
+ }
36
+ };
37
+ /**
38
+ * Singleton instance
39
+ */
40
+ let instance = null;
41
+ /**
42
+ * Create and return a single IntlayerClient instance.
43
+ */
44
+ const createIntlayerClient = (locale, isCookieEnabled = true) => {
45
+ if (instance) return instance;
46
+ instance = new IntlayerClient(locale, isCookieEnabled);
47
+ return instance;
48
+ };
49
+ /**
50
+ * Get the current IntlayerClient instance, creating one with defaults if needed.
51
+ */
52
+ const getIntlayerClient = () => {
53
+ if (!instance) instance = new IntlayerClient();
54
+ return instance;
55
+ };
56
+ /**
57
+ * Install Intlayer into your vanilla JS application.
58
+ *
59
+ * Call this once at application startup, before any Intlayer APIs are used.
60
+ *
61
+ * When called with a `config` argument, `window.INTLAYER_CONFIG` is set so
62
+ * that `@intlayer/config/built` resolves the correct values in the browser
63
+ * without requiring module aliasing in the bundler configuration.
64
+ *
65
+ * @param locale - Initial locale (defaults to config defaultLocale).
66
+ * @param isCookieEnabled - Whether to persist locale in cookies/localStorage.
67
+ * @param config - Optional Intlayer configuration. When provided, sets
68
+ * `window.INTLAYER_CONFIG` with the browser-safe subset of the config so
69
+ * the application works without a Vite/webpack plugin alias.
70
+ * @returns The IntlayerClient singleton.
71
+ *
72
+ * @example
73
+ * ```ts
74
+ * import { installIntlayer } from 'vanilla-intlayer';
75
+ *
76
+ * // With explicit config (no build plugin alias needed):
77
+ * installIntlayer('en', true, {
78
+ * internationalization: { locales: ['en', 'fr'], defaultLocale: 'en' },
79
+ * });
80
+ *
81
+ * // With build plugin (alias handles config automatically):
82
+ * installIntlayer('en');
83
+ * ```
84
+ */
85
+ const installIntlayer = ({ locale, isCookieEnabled } = {}) => {
86
+ const client = createIntlayerClient(locale, isCookieEnabled);
87
+ useEditor();
88
+ return client;
89
+ };
90
+
91
+ //#endregion
92
+ export { IntlayerClient, createIntlayerClient, getIntlayerClient, installIntlayer };
93
+ //# sourceMappingURL=installIntlayer.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"installIntlayer.mjs","names":[],"sources":["../../../src/client/installIntlayer.ts"],"sourcesContent":["import { internationalization } from '@intlayer/config/built';\nimport { setIntlayerIdentifier } from '@intlayer/config/client';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport { useEditor } from '../editor/useEditor';\n\ntype LocaleListener = (locale: LocalesValues) => void;\n\nexport class IntlayerClient {\n private _locale: LocalesValues;\n private _listeners: Set<LocaleListener> = new Set();\n isCookieEnabled: boolean;\n\n constructor(locale?: LocalesValues, isCookieEnabled = true) {\n const { defaultLocale } = internationalization ?? {};\n\n setIntlayerIdentifier();\n\n this._locale =\n (locale as LocalesValues) ?? (defaultLocale as LocalesValues);\n this.isCookieEnabled = isCookieEnabled;\n }\n\n get locale(): LocalesValues {\n return this._locale;\n }\n\n setLocale(newLocale: LocalesValues): void {\n this._locale = newLocale;\n for (const listener of this._listeners) {\n listener(newLocale);\n }\n }\n\n subscribe(listener: LocaleListener): () => void {\n this._listeners.add(listener);\n return () => {\n this._listeners.delete(listener);\n };\n }\n\n /**\n * Fire all listeners with the current locale without changing it.\n * Used to trigger re-renders after async data loads.\n */\n notify(): void {\n for (const listener of this._listeners) {\n listener(this._locale);\n }\n }\n}\n\n/**\n * Singleton instance\n */\nlet instance: IntlayerClient | null = null;\n\n/**\n * Create and return a single IntlayerClient instance.\n */\nexport const createIntlayerClient = (\n locale?: LocalesValues,\n isCookieEnabled = true\n): IntlayerClient => {\n if (instance) return instance;\n\n instance = new IntlayerClient(locale, isCookieEnabled);\n return instance;\n};\n\n/**\n * Get the current IntlayerClient instance, creating one with defaults if needed.\n */\nexport const getIntlayerClient = (): IntlayerClient => {\n if (!instance) {\n instance = new IntlayerClient();\n }\n return instance;\n};\n\n/**\n * Install Intlayer into your vanilla JS application.\n *\n * Call this once at application startup, before any Intlayer APIs are used.\n *\n * When called with a `config` argument, `window.INTLAYER_CONFIG` is set so\n * that `@intlayer/config/built` resolves the correct values in the browser\n * without requiring module aliasing in the bundler configuration.\n *\n * @param locale - Initial locale (defaults to config defaultLocale).\n * @param isCookieEnabled - Whether to persist locale in cookies/localStorage.\n * @param config - Optional Intlayer configuration. When provided, sets\n * `window.INTLAYER_CONFIG` with the browser-safe subset of the config so\n * the application works without a Vite/webpack plugin alias.\n * @returns The IntlayerClient singleton.\n *\n * @example\n * ```ts\n * import { installIntlayer } from 'vanilla-intlayer';\n *\n * // With explicit config (no build plugin alias needed):\n * installIntlayer('en', true, {\n * internationalization: { locales: ['en', 'fr'], defaultLocale: 'en' },\n * });\n *\n * // With build plugin (alias handles config automatically):\n * installIntlayer('en');\n * ```\n */\nexport const installIntlayer = ({\n locale,\n isCookieEnabled,\n}: {\n locale?: LocalesValues;\n isCookieEnabled?: boolean;\n} = {}): IntlayerClient => {\n const client = createIntlayerClient(locale, isCookieEnabled);\n\n useEditor();\n\n return client;\n};\n"],"mappings":";;;;;AAOA,IAAa,iBAAb,MAA4B;CAC1B,AAAQ;CACR,AAAQ,6BAAkC,IAAI,KAAK;CACnD;CAEA,YAAY,QAAwB,kBAAkB,MAAM;EAC1D,MAAM,EAAE,kBAAkB,wBAAwB,EAAE;AAEpD,yBAAuB;AAEvB,OAAK,UACF,UAA6B;AAChC,OAAK,kBAAkB;;CAGzB,IAAI,SAAwB;AAC1B,SAAO,KAAK;;CAGd,UAAU,WAAgC;AACxC,OAAK,UAAU;AACf,OAAK,MAAM,YAAY,KAAK,WAC1B,UAAS,UAAU;;CAIvB,UAAU,UAAsC;AAC9C,OAAK,WAAW,IAAI,SAAS;AAC7B,eAAa;AACX,QAAK,WAAW,OAAO,SAAS;;;;;;;CAQpC,SAAe;AACb,OAAK,MAAM,YAAY,KAAK,WAC1B,UAAS,KAAK,QAAQ;;;;;;AAQ5B,IAAI,WAAkC;;;;AAKtC,MAAa,wBACX,QACA,kBAAkB,SACC;AACnB,KAAI,SAAU,QAAO;AAErB,YAAW,IAAI,eAAe,QAAQ,gBAAgB;AACtD,QAAO;;;;;AAMT,MAAa,0BAA0C;AACrD,KAAI,CAAC,SACH,YAAW,IAAI,gBAAgB;AAEjC,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCT,MAAa,mBAAmB,EAC9B,QACA,oBAIE,EAAE,KAAqB;CACzB,MAAM,SAAS,qBAAqB,QAAQ,gBAAgB;AAE5D,YAAW;AAEX,QAAO"}
@@ -1,11 +1,58 @@
1
- import { getDictionary as e } from "../getDictionary.mjs";
2
- import { r as t } from "../../installIntlayer-C99qMzFH.mjs";
1
+ import { getDictionary } from "../getDictionary.mjs";
2
+ import { getIntlayerClient } from "./installIntlayer.mjs";
3
+
3
4
  //#region src/client/useDictionary.ts
4
- var n = (n, r) => {
5
- let i = t(), a = e(n, r ?? i.locale);
6
- return a.onChange = (t) => (i.subscribe((i) => {
7
- t(e(n, r ?? i));
8
- }), a), a;
5
+ /**
6
+ * Get the translated content for a raw dictionary object and optionally
7
+ * subscribe to locale changes via the chainable `.onChange()` method
8
+ * mirroring the API of `react-intlayer`.
9
+ *
10
+ * Unlike `useIntlayer` (which takes a registered key), this function accepts a
11
+ * dictionary object directly — useful for dictionaries loaded asynchronously
12
+ * or defined inline.
13
+ *
14
+ * The function returns the current content **directly** (same shape as
15
+ * `getDictionary(dict)`), plus the `.onChange()` helper:
16
+ *
17
+ * ```ts
18
+ * // React
19
+ * const content = useDictionary(myDict);
20
+ *
21
+ * // Vanilla — identical surface API, opt-in reactivity via .onChange()
22
+ * const content = useDictionary(myDict);
23
+ * const content = useDictionary(myDict).onChange((c) => render(c));
24
+ * ```
25
+ *
26
+ * @param dictionary - The raw dictionary object.
27
+ * @param locale - Optional locale override.
28
+ * @returns The current translated content with an `.onChange()` method.
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * import myDict from './myDictionary.content';
33
+ * import { installIntlayer, useDictionary } from 'vanilla-intlayer';
34
+ *
35
+ * installIntlayer();
36
+ *
37
+ * const content = useDictionary(myDict).onChange((c) => {
38
+ * document.querySelector('p').textContent = String(c.greeting);
39
+ * });
40
+ *
41
+ * document.querySelector('p').textContent = String(content.greeting);
42
+ * ```
43
+ */
44
+ const useDictionary = (dictionary, locale) => {
45
+ const client = getIntlayerClient();
46
+ const content = getDictionary(dictionary, locale ?? client.locale);
47
+ content.onChange = (callback) => {
48
+ client.subscribe((newLocale) => {
49
+ callback(getDictionary(dictionary, locale ?? newLocale));
50
+ });
51
+ return content;
52
+ };
53
+ return content;
9
54
  };
55
+
10
56
  //#endregion
11
- export { n as useDictionary };
57
+ export { useDictionary };
58
+ //# sourceMappingURL=useDictionary.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useDictionary.mjs","names":[],"sources":["../../../src/client/useDictionary.ts"],"sourcesContent":["import type { Dictionary } from '@intlayer/types/dictionary';\nimport type {\n DeclaredLocales,\n LocalesValues,\n} from '@intlayer/types/module_augmentation';\nimport { getDictionary } from '../getDictionary';\nimport type { DeepTransformContent } from '../plugins';\nimport { getIntlayerClient } from './installIntlayer';\n\nexport type WithOnChange<T> = T & {\n /**\n * Subscribe to locale changes. The callback receives the fresh content\n * whenever the active locale changes. Returns the content object itself\n * for convenient one-liner patterns:\n *\n * ```ts\n * const content = useDictionary(myDict).onChange((c) => {\n * document.querySelector('p')!.textContent = String(c.greeting);\n * });\n * ```\n */\n onChange: (\n callback: <T extends Dictionary, L extends LocalesValues>(\n content: DeepTransformContent<T['content'], L>\n ) => void\n ) => WithOnChange<T>;\n};\n\n/**\n * Get the translated content for a raw dictionary object and optionally\n * subscribe to locale changes via the chainable `.onChange()` method —\n * mirroring the API of `react-intlayer`.\n *\n * Unlike `useIntlayer` (which takes a registered key), this function accepts a\n * dictionary object directly — useful for dictionaries loaded asynchronously\n * or defined inline.\n *\n * The function returns the current content **directly** (same shape as\n * `getDictionary(dict)`), plus the `.onChange()` helper:\n *\n * ```ts\n * // React\n * const content = useDictionary(myDict);\n *\n * // Vanilla — identical surface API, opt-in reactivity via .onChange()\n * const content = useDictionary(myDict);\n * const content = useDictionary(myDict).onChange((c) => render(c));\n * ```\n *\n * @param dictionary - The raw dictionary object.\n * @param locale - Optional locale override.\n * @returns The current translated content with an `.onChange()` method.\n *\n * @example\n * ```ts\n * import myDict from './myDictionary.content';\n * import { installIntlayer, useDictionary } from 'vanilla-intlayer';\n *\n * installIntlayer();\n *\n * const content = useDictionary(myDict).onChange((c) => {\n * document.querySelector('p').textContent = String(c.greeting);\n * });\n *\n * document.querySelector('p').textContent = String(content.greeting);\n * ```\n */\nexport const useDictionary = <\n T extends Dictionary,\n L extends LocalesValues = DeclaredLocales,\n>(\n dictionary: T,\n locale?: L\n): WithOnChange<DeepTransformContent<T['content'], L>> => {\n const client = getIntlayerClient();\n const content = getDictionary(\n dictionary,\n (locale ?? client.locale) as L\n ) as WithOnChange<DeepTransformContent<T['content'], L>>;\n\n content.onChange = (\n callback: (content: DeepTransformContent<T['content'], L>) => void\n ) => {\n client.subscribe((newLocale) => {\n callback(getDictionary(dictionary, (locale ?? newLocale) as L));\n });\n return content;\n };\n\n return content;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmEA,MAAa,iBAIX,YACA,WACwD;CACxD,MAAM,SAAS,mBAAmB;CAClC,MAAM,UAAU,cACd,YACC,UAAU,OAAO,OACnB;AAED,SAAQ,YACN,aACG;AACH,SAAO,WAAW,cAAc;AAC9B,YAAS,cAAc,YAAa,UAAU,UAAgB,CAAC;IAC/D;AACF,SAAO;;AAGT,QAAO"}
@@ -1,37 +1,91 @@
1
- import { getDictionary as e } from "../getDictionary.mjs";
2
- import { r as t } from "../../installIntlayer-C99qMzFH.mjs";
3
- import { internationalization as n } from "@intlayer/config/built";
1
+ import { getDictionary } from "../getDictionary.mjs";
2
+ import { getIntlayerClient } from "./installIntlayer.mjs";
3
+ import { internationalization } from "@intlayer/config/built";
4
+
4
5
  //#region src/client/useDictionaryDynamic.ts
5
- var r = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), a = (e, t) => {
6
- if (r.has(e)) return Promise.resolve();
7
- if (i.has(e)) return i.get(e);
8
- let n = t().then((t) => {
9
- r.set(e, t), i.delete(e);
6
+ /** Simple in-memory cache shared across all calls in the same page. */
7
+ const cache = /* @__PURE__ */ new Map();
8
+ /** Tracks in-flight loads to avoid duplicate fetches. */
9
+ const inflight = /* @__PURE__ */ new Map();
10
+ const loadDictionary = (cacheKey, loader) => {
11
+ if (cache.has(cacheKey)) return Promise.resolve();
12
+ if (inflight.has(cacheKey)) return inflight.get(cacheKey);
13
+ const promise = loader().then((dictionary) => {
14
+ cache.set(cacheKey, dictionary);
15
+ inflight.delete(cacheKey);
10
16
  });
11
- return i.set(e, n), n;
12
- }, o = (i, o, s) => {
13
- let c = t(), l = s ?? c.locale ?? n.defaultLocale, u = `${String(o)}.${l}`, d = i[l], f = r.get(u);
14
- if (f) {
15
- let t = e(f, l);
16
- return t.onChange = (n) => (c.subscribe((t) => {
17
- let s = `${String(o)}.${t}`, c = i[t];
18
- c && a(s, c).then(() => {
19
- let i = r.get(s);
20
- i && n(e(i, t));
17
+ inflight.set(cacheKey, promise);
18
+ return promise;
19
+ };
20
+ /**
21
+ * Dynamically load and transform a locale-keyed dictionary.
22
+ *
23
+ * Works like `useIntlayer` / `useDictionary` but accepts a locale-keyed map
24
+ * of lazy loaders instead of a pre-imported dictionary. Call it inside your
25
+ * render function — the first call triggers a background fetch and returns
26
+ * placeholder values; when the load completes the client notifies all
27
+ * subscribers (including your render loop) so the render runs again with real
28
+ * content. Subsequent calls for the same locale return immediately from cache.
29
+ *
30
+ * Locale switches follow the same two-phase pattern: placeholder on first
31
+ * render, real content after the locale bundle loads.
32
+ *
33
+ * @param dictionaryLoaders - Locale-keyed map of `() => Promise<Dictionary>`.
34
+ * @param key - Dictionary key (used for cache namespacing).
35
+ * @param locale - Optional locale override.
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * import dynDic from '../.intlayer/dynamic_dictionary/app.mjs';
40
+ *
41
+ * const render = () => {
42
+ * const content = useDictionaryDynamic(dynDic, 'app');
43
+ * document.querySelector('h1')!.textContent = String(content.title);
44
+ * };
45
+ *
46
+ * render();
47
+ * getIntlayerClient().subscribe(() => render());
48
+ * ```
49
+ */
50
+ const useDictionaryDynamic = (dictionaryLoaders, key, locale) => {
51
+ const client = getIntlayerClient();
52
+ const currentLocale = locale ?? client.locale ?? internationalization.defaultLocale;
53
+ const cacheKey = `${String(key)}.${currentLocale}`;
54
+ const loader = dictionaryLoaders[currentLocale];
55
+ const cached = cache.get(cacheKey);
56
+ if (cached) {
57
+ const content = getDictionary(cached, currentLocale);
58
+ content.onChange = (callback) => {
59
+ client.subscribe((newLocale) => {
60
+ const newKey = `${String(key)}.${newLocale}`;
61
+ const newLoader = dictionaryLoaders[newLocale];
62
+ if (!newLoader) return;
63
+ loadDictionary(newKey, newLoader).then(() => {
64
+ const dict = cache.get(newKey);
65
+ if (dict) callback(getDictionary(dict, newLocale));
66
+ });
21
67
  });
22
- }), t), t;
68
+ return content;
69
+ };
70
+ return content;
23
71
  }
24
- d && a(u, d).then(() => {
25
- c.notify();
72
+ if (loader) loadDictionary(cacheKey, loader).then(() => {
73
+ client.notify();
26
74
  });
27
- let p = new Proxy(() => {}, {
28
- get: (e, t) => {
29
- if (t === Symbol.toPrimitive || t === "toString" || t === "valueOf") return () => "";
30
- if (t !== "then") return t === "onChange" ? (e) => p : p;
75
+ const recursiveProxy = new Proxy(() => {}, {
76
+ get: (_t, prop) => {
77
+ if (prop === Symbol.toPrimitive) return () => "";
78
+ if (prop === "toString") return () => "";
79
+ if (prop === "valueOf") return () => "";
80
+ if (prop === "then") return void 0;
81
+ if (prop === "onChange") return (_cb) => recursiveProxy;
82
+ return recursiveProxy;
31
83
  },
32
- apply: () => p
84
+ apply: () => recursiveProxy
33
85
  });
34
- return p;
86
+ return recursiveProxy;
35
87
  };
88
+
36
89
  //#endregion
37
- export { o as useDictionaryDynamic };
90
+ export { useDictionaryDynamic };
91
+ //# sourceMappingURL=useDictionaryDynamic.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useDictionaryDynamic.mjs","names":[],"sources":["../../../src/client/useDictionaryDynamic.ts"],"sourcesContent":["import { internationalization } from '@intlayer/config/built';\nimport type { Dictionary } from '@intlayer/types/dictionary';\nimport type {\n DictionaryKeys,\n LocalesValues,\n StrictModeLocaleMap,\n} from '@intlayer/types/module_augmentation';\nimport { getDictionary } from '../getDictionary';\nimport type { DeepTransformContent } from '../plugins';\nimport { getIntlayerClient } from './installIntlayer';\nimport type { WithOnChange } from './useDictionary';\n\n/** Simple in-memory cache shared across all calls in the same page. */\nconst cache = new Map<string, Dictionary>();\n\n/** Tracks in-flight loads to avoid duplicate fetches. */\nconst inflight = new Map<string, Promise<void>>();\n\nconst loadDictionary = <T extends Dictionary>(\n cacheKey: string,\n loader: () => Promise<T>\n): Promise<void> => {\n if (cache.has(cacheKey)) return Promise.resolve();\n if (inflight.has(cacheKey)) return inflight.get(cacheKey)!;\n\n const promise = loader().then((dictionary) => {\n cache.set(cacheKey, dictionary);\n inflight.delete(cacheKey);\n });\n\n inflight.set(cacheKey, promise);\n return promise;\n};\n\n/**\n * Dynamically load and transform a locale-keyed dictionary.\n *\n * Works like `useIntlayer` / `useDictionary` but accepts a locale-keyed map\n * of lazy loaders instead of a pre-imported dictionary. Call it inside your\n * render function — the first call triggers a background fetch and returns\n * placeholder values; when the load completes the client notifies all\n * subscribers (including your render loop) so the render runs again with real\n * content. Subsequent calls for the same locale return immediately from cache.\n *\n * Locale switches follow the same two-phase pattern: placeholder on first\n * render, real content after the locale bundle loads.\n *\n * @param dictionaryLoaders - Locale-keyed map of `() => Promise<Dictionary>`.\n * @param key - Dictionary key (used for cache namespacing).\n * @param locale - Optional locale override.\n *\n * @example\n * ```ts\n * import dynDic from '../.intlayer/dynamic_dictionary/app.mjs';\n *\n * const render = () => {\n * const content = useDictionaryDynamic(dynDic, 'app');\n * document.querySelector('h1')!.textContent = String(content.title);\n * };\n *\n * render();\n * getIntlayerClient().subscribe(() => render());\n * ```\n */\nexport const useDictionaryDynamic = <\n T extends Dictionary,\n K extends DictionaryKeys,\n L extends LocalesValues = LocalesValues,\n>(\n dictionaryLoaders: StrictModeLocaleMap<() => Promise<T>>,\n key: K,\n locale?: L\n): WithOnChange<DeepTransformContent<T['content'], L>> => {\n const client = getIntlayerClient();\n const currentLocale = (locale ??\n client.locale ??\n internationalization.defaultLocale) as L;\n\n const cacheKey = `${String(key)}.${currentLocale}`;\n const loader = (dictionaryLoaders as Record<string, () => Promise<T>>)[\n currentLocale\n ];\n\n // --- Cache hit: return real content synchronously ---\n const cached = cache.get(cacheKey);\n if (cached) {\n const content = getDictionary(cached as T, currentLocale) as WithOnChange<\n DeepTransformContent<T['content'], L>\n >;\n\n content.onChange = (\n callback: (content: DeepTransformContent<T['content'], L>) => void\n ) => {\n // Re-fire whenever content reloads (locale change → new cache entry).\n client.subscribe((newLocale) => {\n const newKey = `${String(key)}.${newLocale}`;\n const newLoader = (\n dictionaryLoaders as Record<string, () => Promise<T>>\n )[newLocale];\n\n if (!newLoader) return;\n\n loadDictionary(newKey, newLoader).then(() => {\n const dict = cache.get(newKey);\n if (dict) {\n callback(\n getDictionary(dict as T, newLocale as L) as DeepTransformContent<\n T['content'],\n L\n >\n );\n }\n });\n });\n\n return content;\n };\n\n return content;\n }\n\n // --- Cache miss: kick off background load, then notify to re-render ---\n if (loader) {\n loadDictionary(cacheKey, loader).then(() => {\n client.notify();\n });\n }\n\n // Placeholder proxy — safe for any property access while loading.\n const recursiveProxy: any = new Proxy(() => {}, {\n get: (_t, prop) => {\n if (prop === Symbol.toPrimitive) return () => '';\n if (prop === 'toString') return () => '';\n if (prop === 'valueOf') return () => '';\n if (prop === 'then') return undefined; // not a Promise\n if (prop === 'onChange') return (_cb: any) => recursiveProxy;\n return recursiveProxy;\n },\n apply: () => recursiveProxy,\n });\n\n return recursiveProxy as WithOnChange<DeepTransformContent<T['content'], L>>;\n};\n"],"mappings":";;;;;;AAaA,MAAM,wBAAQ,IAAI,KAAyB;;AAG3C,MAAM,2BAAW,IAAI,KAA4B;AAEjD,MAAM,kBACJ,UACA,WACkB;AAClB,KAAI,MAAM,IAAI,SAAS,CAAE,QAAO,QAAQ,SAAS;AACjD,KAAI,SAAS,IAAI,SAAS,CAAE,QAAO,SAAS,IAAI,SAAS;CAEzD,MAAM,UAAU,QAAQ,CAAC,MAAM,eAAe;AAC5C,QAAM,IAAI,UAAU,WAAW;AAC/B,WAAS,OAAO,SAAS;GACzB;AAEF,UAAS,IAAI,UAAU,QAAQ;AAC/B,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCT,MAAa,wBAKX,mBACA,KACA,WACwD;CACxD,MAAM,SAAS,mBAAmB;CAClC,MAAM,gBAAiB,UACrB,OAAO,UACP,qBAAqB;CAEvB,MAAM,WAAW,GAAG,OAAO,IAAI,CAAC,GAAG;CACnC,MAAM,SAAU,kBACd;CAIF,MAAM,SAAS,MAAM,IAAI,SAAS;AAClC,KAAI,QAAQ;EACV,MAAM,UAAU,cAAc,QAAa,cAAc;AAIzD,UAAQ,YACN,aACG;AAEH,UAAO,WAAW,cAAc;IAC9B,MAAM,SAAS,GAAG,OAAO,IAAI,CAAC,GAAG;IACjC,MAAM,YACJ,kBACA;AAEF,QAAI,CAAC,UAAW;AAEhB,mBAAe,QAAQ,UAAU,CAAC,WAAW;KAC3C,MAAM,OAAO,MAAM,IAAI,OAAO;AAC9B,SAAI,KACF,UACE,cAAc,MAAW,UAAe,CAIzC;MAEH;KACF;AAEF,UAAO;;AAGT,SAAO;;AAIT,KAAI,OACF,gBAAe,UAAU,OAAO,CAAC,WAAW;AAC1C,SAAO,QAAQ;GACf;CAIJ,MAAM,iBAAsB,IAAI,YAAY,IAAI;EAC9C,MAAM,IAAI,SAAS;AACjB,OAAI,SAAS,OAAO,YAAa,cAAa;AAC9C,OAAI,SAAS,WAAY,cAAa;AACtC,OAAI,SAAS,UAAW,cAAa;AACrC,OAAI,SAAS,OAAQ,QAAO;AAC5B,OAAI,SAAS,WAAY,SAAQ,QAAa;AAC9C,UAAO;;EAET,aAAa;EACd,CAAC;AAEF,QAAO"}
@@ -1,11 +1,64 @@
1
- import { getIntlayer as e } from "../getIntlayer.mjs";
2
- import { r as t } from "../../installIntlayer-C99qMzFH.mjs";
1
+ import { getIntlayer } from "../getIntlayer.mjs";
2
+ import { getIntlayerClient } from "./installIntlayer.mjs";
3
+
3
4
  //#region src/client/useIntlayer.ts
4
- var n = (n, r) => {
5
- let i = t(), a = e(n, r ?? i.locale);
6
- return a.onChange = (t) => (i.subscribe((i) => {
7
- t(e(n, r ?? i));
8
- }), a), a;
5
+ /**
6
+ * Get the translated content for the given key and optionally subscribe to
7
+ * locale changes via the chainable `.onChange()` method — mirroring the API
8
+ * of `react-intlayer`'s `useIntlayer`.
9
+ *
10
+ * Unlike React (where the hook system automatically re-runs on re-render),
11
+ * vanilla JS has no component lifecycle. The `.onChange()` callback is the
12
+ * explicit equivalent: it is called with fresh content whenever the active
13
+ * locale changes. Use it to patch your DOM or trigger a full re-render.
14
+ *
15
+ * The function returns the current content **directly** (same shape as
16
+ * `getIntlayer(key)`), plus the `.onChange()` helper:
17
+ *
18
+ * ```ts
19
+ * // React
20
+ * const content = useIntlayer('app');
21
+ *
22
+ * // Vanilla — identical surface API, opt-in reactivity via .onChange()
23
+ * const content = useIntlayer('app');
24
+ * const content = useIntlayer('app').onChange((c) => render(c));
25
+ * ```
26
+ *
27
+ * For cleanup (e.g. Vite HMR), subscribe via `getIntlayerClient().subscribe()`
28
+ * and hold the returned unsubscribe function.
29
+ *
30
+ * @param key - Dictionary key registered in your intlayer content files.
31
+ * @param locale - Optional locale override (defaults to the current app locale).
32
+ * @returns The current translated content with an `.onChange()` method.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * import { installIntlayer, useIntlayer } from 'vanilla-intlayer';
37
+ *
38
+ * installIntlayer();
39
+ *
40
+ * // Static read (no subscription)
41
+ * const content = useIntlayer('homepage');
42
+ * document.querySelector('h1').textContent = String(content.title);
43
+ *
44
+ * // Reactive read — onChange is called on every locale change
45
+ * useIntlayer('homepage').onChange((c) => {
46
+ * document.querySelector('h1').textContent = String(c.title);
47
+ * });
48
+ * ```
49
+ */
50
+ const useIntlayer = (key, locale) => {
51
+ const client = getIntlayerClient();
52
+ const content = getIntlayer(key, locale ?? client.locale);
53
+ content.onChange = (callback) => {
54
+ client.subscribe((newLocale) => {
55
+ callback(getIntlayer(key, locale ?? newLocale));
56
+ });
57
+ return content;
58
+ };
59
+ return content;
9
60
  };
61
+
10
62
  //#endregion
11
- export { n as useIntlayer };
63
+ export { useIntlayer };
64
+ //# sourceMappingURL=useIntlayer.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useIntlayer.mjs","names":[],"sources":["../../../src/client/useIntlayer.ts"],"sourcesContent":["import type {\n DictionaryKeys,\n DictionaryRegistryContent,\n LocalesValues,\n} from '@intlayer/types/module_augmentation';\nimport { getIntlayer } from '../getIntlayer';\nimport type { DeepTransformContent } from '../plugins';\nimport { getIntlayerClient } from './installIntlayer';\nimport type { WithOnChange } from './useDictionary';\n\n/**\n * Get the translated content for the given key and optionally subscribe to\n * locale changes via the chainable `.onChange()` method — mirroring the API\n * of `react-intlayer`'s `useIntlayer`.\n *\n * Unlike React (where the hook system automatically re-runs on re-render),\n * vanilla JS has no component lifecycle. The `.onChange()` callback is the\n * explicit equivalent: it is called with fresh content whenever the active\n * locale changes. Use it to patch your DOM or trigger a full re-render.\n *\n * The function returns the current content **directly** (same shape as\n * `getIntlayer(key)`), plus the `.onChange()` helper:\n *\n * ```ts\n * // React\n * const content = useIntlayer('app');\n *\n * // Vanilla — identical surface API, opt-in reactivity via .onChange()\n * const content = useIntlayer('app');\n * const content = useIntlayer('app').onChange((c) => render(c));\n * ```\n *\n * For cleanup (e.g. Vite HMR), subscribe via `getIntlayerClient().subscribe()`\n * and hold the returned unsubscribe function.\n *\n * @param key - Dictionary key registered in your intlayer content files.\n * @param locale - Optional locale override (defaults to the current app locale).\n * @returns The current translated content with an `.onChange()` method.\n *\n * @example\n * ```ts\n * import { installIntlayer, useIntlayer } from 'vanilla-intlayer';\n *\n * installIntlayer();\n *\n * // Static read (no subscription)\n * const content = useIntlayer('homepage');\n * document.querySelector('h1').textContent = String(content.title);\n *\n * // Reactive read — onChange is called on every locale change\n * useIntlayer('homepage').onChange((c) => {\n * document.querySelector('h1').textContent = String(c.title);\n * });\n * ```\n */\nexport const useIntlayer = <T extends DictionaryKeys>(\n key: T,\n locale?: LocalesValues\n): WithOnChange<DeepTransformContent<DictionaryRegistryContent<T>>> => {\n const client = getIntlayerClient();\n const content = getIntlayer(key, locale ?? client.locale) as WithOnChange<\n DeepTransformContent<DictionaryRegistryContent<T>>\n >;\n\n content.onChange = (callback) => {\n client.subscribe((newLocale) => {\n callback(getIntlayer(key, (locale ?? newLocale) as typeof locale));\n });\n return content;\n };\n\n return content;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuDA,MAAa,eACX,KACA,WACqE;CACrE,MAAM,SAAS,mBAAmB;CAClC,MAAM,UAAU,YAAY,KAAK,UAAU,OAAO,OAAO;AAIzD,SAAQ,YAAY,aAAa;AAC/B,SAAO,WAAW,cAAc;AAC9B,YAAS,YAAY,KAAM,UAAU,UAA4B,CAAC;IAClE;AACF,SAAO;;AAGT,QAAO"}
@@ -1,22 +1,56 @@
1
- import { r as e } from "../../installIntlayer-C99qMzFH.mjs";
2
- import { setLocaleInStorage as t } from "./useLocaleStorage.mjs";
3
- import { internationalization as n } from "@intlayer/config/built";
1
+ import { getIntlayerClient } from "./installIntlayer.mjs";
2
+ import { setLocaleInStorage } from "./useLocaleStorage.mjs";
3
+ import { internationalization } from "@intlayer/config/built";
4
+
4
5
  //#region src/client/useLocale.ts
5
- var r = (r = {}) => {
6
- let i = e(), { defaultLocale: a, locales: o } = n ?? {};
6
+ /**
7
+ * Get the current locale state and locale management utilities.
8
+ *
9
+ * Returns a plain object with the current `locale`, `defaultLocale`,
10
+ * `availableLocales`, a `setLocale` function, and a `subscribe` function
11
+ * to react to locale changes.
12
+ *
13
+ * @param props - Optional configuration.
14
+ * @returns Locale state and management utilities.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { installIntlayer, useLocale } from 'vanilla-intlayer';
19
+ *
20
+ * installIntlayer('en');
21
+ *
22
+ * const { locale, availableLocales, setLocale, subscribe } = useLocale();
23
+ *
24
+ * const unsubscribe = subscribe((newLocale) => {
25
+ * console.log('Locale changed to', newLocale);
26
+ * });
27
+ *
28
+ * document.querySelector('#lang-switcher').addEventListener('change', (e) => {
29
+ * setLocale(e.target.value);
30
+ * });
31
+ * ```
32
+ */
33
+ const useLocale = (props = {}) => {
34
+ const client = getIntlayerClient();
35
+ const { defaultLocale, locales: availableLocales } = internationalization ?? {};
36
+ const setLocale = (newLocale) => {
37
+ if (!(availableLocales ?? []).map(String).includes(newLocale)) {
38
+ console.error(`Locale "${newLocale}" is not available.`);
39
+ return;
40
+ }
41
+ client.setLocale(newLocale);
42
+ setLocaleInStorage(newLocale, props.isCookieEnabled ?? client.isCookieEnabled ?? true);
43
+ props.onLocaleChange?.(newLocale);
44
+ };
7
45
  return {
8
- locale: i.locale,
9
- defaultLocale: a,
10
- availableLocales: o ?? [],
11
- setLocale: (e) => {
12
- if (!(o ?? []).map(String).includes(e)) {
13
- console.error(`Locale "${e}" is not available.`);
14
- return;
15
- }
16
- i.setLocale(e), t(e, r.isCookieEnabled ?? i.isCookieEnabled ?? !0), r.onLocaleChange?.(e);
17
- },
18
- subscribe: (e) => i.subscribe(e)
46
+ locale: client.locale,
47
+ defaultLocale,
48
+ availableLocales: availableLocales ?? [],
49
+ setLocale,
50
+ subscribe: (callback) => client.subscribe(callback)
19
51
  };
20
52
  };
53
+
21
54
  //#endregion
22
- export { r as useLocale };
55
+ export { useLocale };
56
+ //# sourceMappingURL=useLocale.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useLocale.mjs","names":[],"sources":["../../../src/client/useLocale.ts"],"sourcesContent":["import { internationalization } from '@intlayer/config/built';\nimport type {\n DeclaredLocales,\n LocalesValues,\n} from '@intlayer/types/module_augmentation';\nimport { getIntlayerClient } from './installIntlayer';\nimport { setLocaleInStorage } from './useLocaleStorage';\n\ntype UseLocaleProps = {\n isCookieEnabled?: boolean;\n onLocaleChange?: (locale: LocalesValues) => void;\n};\n\n/**\n * Get the current locale state and locale management utilities.\n *\n * Returns a plain object with the current `locale`, `defaultLocale`,\n * `availableLocales`, a `setLocale` function, and a `subscribe` function\n * to react to locale changes.\n *\n * @param props - Optional configuration.\n * @returns Locale state and management utilities.\n *\n * @example\n * ```ts\n * import { installIntlayer, useLocale } from 'vanilla-intlayer';\n *\n * installIntlayer('en');\n *\n * const { locale, availableLocales, setLocale, subscribe } = useLocale();\n *\n * const unsubscribe = subscribe((newLocale) => {\n * console.log('Locale changed to', newLocale);\n * });\n *\n * document.querySelector('#lang-switcher').addEventListener('change', (e) => {\n * setLocale(e.target.value);\n * });\n * ```\n */\nexport const useLocale = (\n props: UseLocaleProps = {}\n): {\n locale: DeclaredLocales;\n defaultLocale: DeclaredLocales;\n availableLocales: DeclaredLocales[];\n setLocale: (newLocale: LocalesValues) => void;\n subscribe: (callback: (locale: LocalesValues) => void) => () => void;\n} => {\n const client = getIntlayerClient();\n const { defaultLocale, locales: availableLocales } =\n internationalization ?? {};\n\n const setLocale = (newLocale: LocalesValues): void => {\n if (!(availableLocales ?? []).map(String).includes(newLocale)) {\n console.error(`Locale \"${newLocale}\" is not available.`);\n return;\n }\n\n client.setLocale(newLocale);\n\n setLocaleInStorage(\n newLocale,\n props.isCookieEnabled ?? client.isCookieEnabled ?? true\n );\n\n props.onLocaleChange?.(newLocale);\n };\n\n return {\n locale: client.locale as DeclaredLocales,\n defaultLocale: defaultLocale as DeclaredLocales,\n availableLocales: (availableLocales ?? []) as DeclaredLocales[],\n setLocale,\n subscribe: (callback) => client.subscribe(callback),\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwCA,MAAa,aACX,QAAwB,EAAE,KAOvB;CACH,MAAM,SAAS,mBAAmB;CAClC,MAAM,EAAE,eAAe,SAAS,qBAC9B,wBAAwB,EAAE;CAE5B,MAAM,aAAa,cAAmC;AACpD,MAAI,EAAE,oBAAoB,EAAE,EAAE,IAAI,OAAO,CAAC,SAAS,UAAU,EAAE;AAC7D,WAAQ,MAAM,WAAW,UAAU,qBAAqB;AACxD;;AAGF,SAAO,UAAU,UAAU;AAE3B,qBACE,WACA,MAAM,mBAAmB,OAAO,mBAAmB,KACpD;AAED,QAAM,iBAAiB,UAAU;;AAGnC,QAAO;EACL,QAAQ,OAAO;EACA;EACf,kBAAmB,oBAAoB,EAAE;EACzC;EACA,YAAY,aAAa,OAAO,UAAU,SAAS;EACpD"}
@@ -1,18 +1,44 @@
1
- import { localeStorageOptions as e } from "@intlayer/core/localization";
2
- import { LocaleStorageClient as t, getLocaleFromStorageClient as n, setLocaleInStorageClient as r } from "@intlayer/core/utils";
1
+ import { localeStorageOptions } from "@intlayer/core/localization";
2
+ import { LocaleStorageClient, getLocaleFromStorageClient, setLocaleInStorageClient } from "@intlayer/core/utils";
3
+
3
4
  //#region src/client/useLocaleStorage.ts
4
- var i = n(e), a = i, o = (t, n) => r(t, {
5
- ...e,
6
- isCookieEnabled: n
7
- }), s = o, c = (n) => {
8
- let r = t({
9
- ...e,
10
- isCookieEnabled: n
5
+ /**
6
+ * Get the current locale from storage (cookie or localStorage).
7
+ */
8
+ const localeInStorage = getLocaleFromStorageClient(localeStorageOptions);
9
+ /**
10
+ * @deprecated Use localeInStorage instead.
11
+ */
12
+ const localeCookie = localeInStorage;
13
+ /**
14
+ * Persist the locale to storage (cookie and/or localStorage).
15
+ */
16
+ const setLocaleInStorage = (locale, isCookieEnabled) => setLocaleInStorageClient(locale, {
17
+ ...localeStorageOptions,
18
+ isCookieEnabled
19
+ });
20
+ /**
21
+ * @deprecated Use setLocaleInStorage instead.
22
+ */
23
+ const setLocaleCookie = setLocaleInStorage;
24
+ /**
25
+ * Returns the current locale from storage and a setter.
26
+ */
27
+ const useLocaleStorage = (isCookieEnabled) => {
28
+ const storage = LocaleStorageClient({
29
+ ...localeStorageOptions,
30
+ isCookieEnabled
11
31
  });
12
32
  return {
13
- localeStorage: r.getLocale(),
14
- setLocaleStorage: r.setLocale
33
+ localeStorage: storage.getLocale(),
34
+ setLocaleStorage: storage.setLocale
15
35
  };
16
- }, l = c;
36
+ };
37
+ /**
38
+ * @deprecated Use useLocaleStorage instead.
39
+ */
40
+ const useLocaleCookie = useLocaleStorage;
41
+
17
42
  //#endregion
18
- export { a as localeCookie, i as localeInStorage, s as setLocaleCookie, o as setLocaleInStorage, l as useLocaleCookie, c as useLocaleStorage };
43
+ export { localeCookie, localeInStorage, setLocaleCookie, setLocaleInStorage, useLocaleCookie, useLocaleStorage };
44
+ //# sourceMappingURL=useLocaleStorage.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useLocaleStorage.mjs","names":["setLocaleInStorageCore"],"sources":["../../../src/client/useLocaleStorage.ts"],"sourcesContent":["import { localeStorageOptions } from '@intlayer/core/localization';\nimport {\n getLocaleFromStorageClient,\n LocaleStorageClient,\n setLocaleInStorageClient as setLocaleInStorageCore,\n} from '@intlayer/core/utils';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\n\n/**\n * Get the current locale from storage (cookie or localStorage).\n */\nexport const localeInStorage = getLocaleFromStorageClient(localeStorageOptions);\n\n/**\n * @deprecated Use localeInStorage instead.\n */\nexport const localeCookie = localeInStorage;\n\n/**\n * Persist the locale to storage (cookie and/or localStorage).\n */\nexport const setLocaleInStorage = (\n locale: LocalesValues,\n isCookieEnabled: boolean\n) =>\n setLocaleInStorageCore(locale, {\n ...localeStorageOptions,\n isCookieEnabled,\n });\n\n/**\n * @deprecated Use setLocaleInStorage instead.\n */\nexport const setLocaleCookie = setLocaleInStorage;\n\n/**\n * Returns the current locale from storage and a setter.\n */\nexport const useLocaleStorage = (isCookieEnabled?: boolean) => {\n const storage = LocaleStorageClient({\n ...localeStorageOptions,\n isCookieEnabled,\n });\n\n return {\n localeStorage: storage.getLocale(),\n setLocaleStorage: storage.setLocale,\n };\n};\n\n/**\n * @deprecated Use useLocaleStorage instead.\n */\nexport const useLocaleCookie = useLocaleStorage;\n"],"mappings":";;;;;;;AAWA,MAAa,kBAAkB,2BAA2B,qBAAqB;;;;AAK/E,MAAa,eAAe;;;;AAK5B,MAAa,sBACX,QACA,oBAEAA,yBAAuB,QAAQ;CAC7B,GAAG;CACH;CACD,CAAC;;;;AAKJ,MAAa,kBAAkB;;;;AAK/B,MAAa,oBAAoB,oBAA8B;CAC7D,MAAM,UAAU,oBAAoB;EAClC,GAAG;EACH;EACD,CAAC;AAEF,QAAO;EACL,eAAe,QAAQ,WAAW;EAClC,kBAAkB,QAAQ;EAC3B;;;;;AAMH,MAAa,kBAAkB"}
@@ -1,16 +1,44 @@
1
- import { r as e } from "../../installIntlayer-C99qMzFH.mjs";
2
- import { routing as t } from "@intlayer/config/built";
3
- import { getRewritePath as n } from "@intlayer/core/localization";
1
+ import { getIntlayerClient } from "./installIntlayer.mjs";
2
+ import { routing } from "@intlayer/config/built";
3
+ import { getRewritePath } from "@intlayer/core/localization";
4
+
4
5
  //#region src/client/useRewriteURL.ts
5
- var r = (e) => {
6
- if (typeof window > "u" || !t?.rewrite) return;
7
- let r = window.location.pathname, i = n(r, e, t?.rewrite);
8
- i && i !== r && window.history.replaceState(window.history.state, "", i + window.location.search + window.location.hash);
9
- }, i = () => {
10
- let t = e();
11
- return r(t.locale), t.subscribe((e) => {
12
- r(e);
6
+ const rewrite = (locale) => {
7
+ if (typeof window === "undefined" || !routing?.rewrite) return;
8
+ const pathname = window.location.pathname;
9
+ const targetPath = getRewritePath(pathname, locale, routing?.rewrite);
10
+ if (targetPath && targetPath !== pathname) window.history.replaceState(window.history.state, "", targetPath + window.location.search + window.location.hash);
11
+ };
12
+ /**
13
+ * Keep the browser URL in sync with the current locale.
14
+ *
15
+ * Rewrites the current URL immediately and subscribes to future locale changes.
16
+ * Returns an `unsubscribe` function to stop listening.
17
+ *
18
+ * Uses `window.history.replaceState` — no full-page navigation.
19
+ *
20
+ * @returns A cleanup function that removes the locale change listener.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * import { installIntlayer, useRewriteURL } from 'vanilla-intlayer';
25
+ *
26
+ * installIntlayer('en');
27
+ *
28
+ * const unsubscribe = useRewriteURL();
29
+ *
30
+ * // Later, if you want to stop URL rewriting:
31
+ * unsubscribe();
32
+ * ```
33
+ */
34
+ const useRewriteURL = () => {
35
+ const client = getIntlayerClient();
36
+ rewrite(client.locale);
37
+ return client.subscribe((newLocale) => {
38
+ rewrite(newLocale);
13
39
  });
14
40
  };
41
+
15
42
  //#endregion
16
- export { i as useRewriteURL };
43
+ export { useRewriteURL };
44
+ //# sourceMappingURL=useRewriteURL.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useRewriteURL.mjs","names":[],"sources":["../../../src/client/useRewriteURL.ts"],"sourcesContent":["import { routing } from '@intlayer/config/built';\nimport { getRewritePath } from '@intlayer/core/localization';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport { getIntlayerClient } from './installIntlayer';\n\nconst rewrite = (locale: string): void => {\n if (typeof window === 'undefined' || !routing?.rewrite) return;\n\n const pathname = window.location.pathname;\n const targetPath = getRewritePath(\n pathname,\n locale as Locale,\n routing?.rewrite\n );\n\n if (targetPath && targetPath !== pathname) {\n window.history.replaceState(\n window.history.state,\n '',\n targetPath + window.location.search + window.location.hash\n );\n }\n};\n\n/**\n * Keep the browser URL in sync with the current locale.\n *\n * Rewrites the current URL immediately and subscribes to future locale changes.\n * Returns an `unsubscribe` function to stop listening.\n *\n * Uses `window.history.replaceState` — no full-page navigation.\n *\n * @returns A cleanup function that removes the locale change listener.\n *\n * @example\n * ```ts\n * import { installIntlayer, useRewriteURL } from 'vanilla-intlayer';\n *\n * installIntlayer('en');\n *\n * const unsubscribe = useRewriteURL();\n *\n * // Later, if you want to stop URL rewriting:\n * unsubscribe();\n * ```\n */\nexport const useRewriteURL = (): (() => void) => {\n const client = getIntlayerClient();\n\n // Rewrite immediately for the current locale\n rewrite(client.locale);\n\n return client.subscribe((newLocale) => {\n rewrite(newLocale);\n });\n};\n"],"mappings":";;;;;AAKA,MAAM,WAAW,WAAyB;AACxC,KAAI,OAAO,WAAW,eAAe,CAAC,SAAS,QAAS;CAExD,MAAM,WAAW,OAAO,SAAS;CACjC,MAAM,aAAa,eACjB,UACA,QACA,SAAS,QACV;AAED,KAAI,cAAc,eAAe,SAC/B,QAAO,QAAQ,aACb,OAAO,QAAQ,OACf,IACA,aAAa,OAAO,SAAS,SAAS,OAAO,SAAS,KACvD;;;;;;;;;;;;;;;;;;;;;;;;AA0BL,MAAa,sBAAoC;CAC/C,MAAM,SAAS,mBAAmB;AAGlC,SAAQ,OAAO,OAAO;AAEtB,QAAO,OAAO,WAAW,cAAc;AACrC,UAAQ,UAAU;GAClB"}