solid-intlayer 9.8.0 → 9.8.2

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 (122) hide show
  1. package/dist/cjs/LoadingBoundary.cjs +1 -0
  2. package/dist/cjs/analytics/useAnalytics.cjs +1 -1
  3. package/dist/cjs/analytics/useExperiment.cjs +1 -1
  4. package/dist/cjs/client/IntlayerProvider.cjs +1 -1
  5. package/dist/cjs/client/getBrowserLocale.cjs +1 -1
  6. package/dist/cjs/client/t.cjs +1 -1
  7. package/dist/cjs/client/useDictionary.cjs +1 -1
  8. package/dist/cjs/client/useDictionaryAsync.cjs +1 -1
  9. package/dist/cjs/client/useDictionaryDynamic.cjs +1 -1
  10. package/dist/cjs/client/useLoadDynamic.cjs +1 -1
  11. package/dist/cjs/client/useLocale.cjs +1 -1
  12. package/dist/cjs/client/useLocaleStorage.cjs +1 -1
  13. package/dist/cjs/client/usePathname.cjs +1 -1
  14. package/dist/cjs/client/useRewriteURL.cjs +1 -1
  15. package/dist/cjs/client/useTraduction.cjs +1 -1
  16. package/dist/cjs/editor/useEditor.cjs +1 -1
  17. package/dist/cjs/getDictionary.cjs +1 -1
  18. package/dist/cjs/html/HTMLProvider.cjs +1 -1
  19. package/dist/cjs/html/HTMLRenderer.cjs +1 -1
  20. package/dist/cjs/markdown/MarkdownProvider.cjs +1 -1
  21. package/dist/cjs/markdown/MarkdownRenderer.cjs +1 -1
  22. package/dist/cjs/markdown/runtime.cjs +1 -1
  23. package/dist/cjs/plugins.cjs +1 -1
  24. package/dist/cjs/server/useDictionary.cjs +1 -1
  25. package/dist/cjs/solidCompat.cjs +1 -0
  26. package/dist/cjs/solidElement/renderSolidElement.cjs +1 -1
  27. package/dist/esm/LoadingBoundary.mjs +12 -0
  28. package/dist/esm/analytics/useAnalytics.mjs +14 -11
  29. package/dist/esm/analytics/useExperiment.mjs +5 -4
  30. package/dist/esm/client/IntlayerProvider.mjs +28 -28
  31. package/dist/esm/client/getBrowserLocale.mjs +21 -21
  32. package/dist/esm/client/t.mjs +4 -4
  33. package/dist/esm/client/useDictionary.mjs +22 -14
  34. package/dist/esm/client/useDictionaryAsync.mjs +3 -3
  35. package/dist/esm/client/useDictionaryDynamic.mjs +26 -23
  36. package/dist/esm/client/useLoadDynamic.mjs +4 -5
  37. package/dist/esm/client/useLocale.mjs +15 -14
  38. package/dist/esm/client/useLocaleStorage.mjs +7 -7
  39. package/dist/esm/client/usePathname.mjs +9 -8
  40. package/dist/esm/client/useRewriteURL.mjs +6 -6
  41. package/dist/esm/client/useTraduction.mjs +5 -5
  42. package/dist/esm/editor/useEditor.mjs +11 -11
  43. package/dist/esm/getDictionary.mjs +3 -3
  44. package/dist/esm/html/HTMLProvider.mjs +5 -4
  45. package/dist/esm/html/HTMLRenderer.mjs +6 -6
  46. package/dist/esm/markdown/MarkdownProvider.mjs +20 -19
  47. package/dist/esm/markdown/MarkdownRenderer.mjs +27 -25
  48. package/dist/esm/markdown/runtime.mjs +8 -13
  49. package/dist/esm/plugins.mjs +76 -75
  50. package/dist/esm/server/useDictionary.mjs +9 -6
  51. package/dist/esm/solidCompat.mjs +48 -0
  52. package/dist/esm/solidElement/renderSolidElement.mjs +14 -16
  53. package/dist/rolldown-runtime-BocRIvOZ.js +1 -0
  54. package/dist/solid/IntlayerNode.jsx +10 -0
  55. package/dist/solid/LoadingBoundary.jsx +10 -0
  56. package/dist/solid/analytics/AnalyticsProvider.jsx +11 -0
  57. package/dist/solid/analytics/exposureSink.js +21 -0
  58. package/dist/solid/analytics/index.js +5 -0
  59. package/dist/solid/analytics/useAnalytics.jsx +58 -0
  60. package/dist/solid/analytics/useConversion.js +24 -0
  61. package/dist/solid/analytics/useExperiment.js +58 -0
  62. package/dist/solid/client/IntlayerProvider.jsx +81 -0
  63. package/dist/solid/client/getBrowserLocale.jsx +113 -0
  64. package/dist/solid/client/index.js +14 -0
  65. package/dist/solid/client/t.js +15 -0
  66. package/dist/solid/client/useContent.js +15 -0
  67. package/dist/solid/client/useDictionary.js +56 -0
  68. package/dist/solid/client/useDictionaryAsync.js +17 -0
  69. package/dist/solid/client/useDictionaryDynamic.js +105 -0
  70. package/dist/solid/client/useIntlayer.js +55 -0
  71. package/dist/solid/client/useLoadDynamic.js +134 -0
  72. package/dist/solid/client/useLocale.js +51 -0
  73. package/dist/solid/client/useLocaleBase.js +18 -0
  74. package/dist/solid/client/useLocaleStorage.js +52 -0
  75. package/dist/solid/client/usePathname.js +40 -0
  76. package/dist/solid/client/useRewriteURL.js +20 -0
  77. package/dist/solid/client/useTraduction.js +31 -0
  78. package/dist/solid/editor/ContentSelector.jsx +9 -0
  79. package/dist/solid/editor/EditorProvider.jsx +7 -0
  80. package/dist/solid/editor/useEditor.jsx +37 -0
  81. package/dist/solid/esm-BS5TZIsL.js +644 -0
  82. package/dist/solid/format/index.js +10 -0
  83. package/dist/solid/format/useCompact.js +14 -0
  84. package/dist/solid/format/useCurrency.js +15 -0
  85. package/dist/solid/format/useDate.js +24 -0
  86. package/dist/solid/format/useIntl.js +30 -0
  87. package/dist/solid/format/useList.js +14 -0
  88. package/dist/solid/format/useNumber.js +18 -0
  89. package/dist/solid/format/usePercentage.js +15 -0
  90. package/dist/solid/format/useRelativeTime.js +15 -0
  91. package/dist/solid/format/useUnit.js +14 -0
  92. package/dist/solid/getDictionary.js +16 -0
  93. package/dist/solid/getIntlayer.js +13 -0
  94. package/dist/solid/html/HTMLProvider.jsx +16 -0
  95. package/dist/solid/html/HTMLRenderer.jsx +45 -0
  96. package/dist/solid/html/index.js +3 -0
  97. package/dist/solid/html/types.js +0 -0
  98. package/dist/solid/index.js +21 -0
  99. package/dist/solid/isEnabled-C5NUhdhZ.js +100 -0
  100. package/dist/solid/markdown/MarkdownProvider.jsx +38 -0
  101. package/dist/solid/markdown/MarkdownRenderer.jsx +47 -0
  102. package/dist/solid/markdown/compiler.jsx +62 -0
  103. package/dist/solid/markdown/index.js +4 -0
  104. package/dist/solid/markdown/runtime.jsx +42 -0
  105. package/dist/solid/plugins.jsx +230 -0
  106. package/dist/solid/proxyKeys.js +13 -0
  107. package/dist/solid/server/index.js +1 -0
  108. package/dist/solid/server/useDictionary.js +29 -0
  109. package/dist/solid/solidCompat.js +155 -0
  110. package/dist/solid/solidElement/renderSolidElement.jsx +32 -0
  111. package/dist/types/LoadingBoundary.d.ts +12 -0
  112. package/dist/types/client/useDictionaryDynamic.d.ts +1 -1
  113. package/dist/types/client/useLoadDynamic.d.ts +4 -4
  114. package/dist/types/client/useLocaleStorage.d.ts +2 -2
  115. package/dist/types/format/useIntl.d.ts +1 -1
  116. package/dist/types/getDictionary.d.ts +5 -1
  117. package/dist/types/html/HTMLProvider.d.ts +1 -1
  118. package/dist/types/markdown/MarkdownProvider.d.ts +5 -1
  119. package/dist/types/server/useDictionary.d.ts +6 -6
  120. package/dist/types/solidCompat.d.ts +92 -0
  121. package/dist/types/solidElement/renderSolidElement.d.ts +1 -1
  122. package/package.json +25 -18
@@ -0,0 +1,81 @@
1
+ import { LoadingBoundary } from "../LoadingBoundary.jsx";
2
+ import { createWritableMemo, getContextProvider, lazyComponent, onMounted, runInTransition } from "../solidCompat.js";
3
+ import { getLocaleInStorage, setLocaleInStorage } from "./useLocaleStorage.js";
4
+ import { internationalization } from "@intlayer/config/built";
5
+ import { createContext, createMemo, untrack, useContext } from "solid-js";
6
+ import { localeResolver } from "@intlayer/core/localization";
7
+ import { setIntlayerIdentifier } from "@intlayer/config/client";
8
+ // src/client/IntlayerProvider.tsx
9
+ var LazyEditorProvider = process.env.INTLAYER_EDITOR_ENABLED !== "false" ? lazyComponent(() => import("../editor/EditorProvider.jsx").then((m) => ({ default: m.EditorProvider }))) : null;
10
+ var LazyAnalyticsProvider = process.env.INTLAYER_ANALYTICS_ENABLED !== "false" ? lazyComponent(() => import("../analytics/AnalyticsProvider.jsx").then((m) => ({ default: m.AnalyticsProvider }))) : null;
11
+ /**
12
+ * Context that store the current locale on the client side
13
+ */
14
+ var IntlayerClientContext = createContext({
15
+ locale: () => getLocaleInStorage() ?? internationalization?.defaultLocale,
16
+ setLocale: () => null
17
+ });
18
+ var IntlayerClientContextProvider = getContextProvider(IntlayerClientContext);
19
+ /**
20
+ * Hook that provides the current locale
21
+ */
22
+ var useIntlayerContext = () => useContext(IntlayerClientContext);
23
+ /**
24
+ * Provider that store the current locale on the client side
25
+ */
26
+ var IntlayerProviderContent = (props) => {
27
+ const { defaultLocale: defaultLocaleConfig, locales: availableLocales } = internationalization ?? {};
28
+ const defaultLocale = untrack(() => props.locale ?? getLocaleInStorage() ?? props.defaultLocale ?? defaultLocaleConfig);
29
+ const [currentLocale, setCurrentLocale] = createWritableMemo((previousLocale) => props.locale || previousLocale || defaultLocale);
30
+ const setLocaleBase = (newLocale) => {
31
+ if (currentLocale().toString() === newLocale.toString()) return;
32
+ if (!availableLocales?.map(String).includes(newLocale)) {
33
+ console.error(`Locale ${newLocale} is not available`);
34
+ return;
35
+ }
36
+ runInTransition(() => setCurrentLocale(newLocale));
37
+ setLocaleInStorage(newLocale, props.isCookieEnabled);
38
+ };
39
+ const setLocale = props.setLocale ?? setLocaleBase;
40
+ const locale = createMemo(() => localeResolver(currentLocale()));
41
+ onMounted(() => {
42
+ setIntlayerIdentifier();
43
+ });
44
+ return <IntlayerClientContextProvider value={{
45
+ locale,
46
+ setLocale,
47
+ variant: () => props.variant
48
+ }}>
49
+ {props.children}
50
+ </IntlayerClientContextProvider>;
51
+ };
52
+ /**
53
+ * Main provider for Intlayer in Solid applications.
54
+ *
55
+ * It provides the Intlayer context to your application, allowing the use
56
+ * of hooks like `useIntlayer` and `useLocale`.
57
+ *
58
+ * @param props - The provider props.
59
+ * @returns The provider component.
60
+ *
61
+ * @example
62
+ * ```tsx
63
+ * import { IntlayerProvider } from 'solid-intlayer';
64
+ *
65
+ * const App = () => (
66
+ * <IntlayerProvider>
67
+ * <MyComponent />
68
+ * </IntlayerProvider>
69
+ * );
70
+ * ```
71
+ */
72
+ var IntlayerProvider = (props) => <IntlayerProviderContent {...props}>
73
+ {process.env.INTLAYER_EDITOR_ENABLED !== "false" && LazyEditorProvider && <LoadingBoundary>
74
+ <LazyEditorProvider />
75
+ </LoadingBoundary>}
76
+ {process.env.INTLAYER_ANALYTICS_ENABLED !== "false" && LazyAnalyticsProvider && <LoadingBoundary>
77
+ <LazyAnalyticsProvider />
78
+ </LoadingBoundary>}
79
+ {props.children}
80
+ </IntlayerProviderContent>;
81
+ export { IntlayerClientContext, IntlayerProvider, IntlayerProviderContent, useIntlayerContext };
@@ -0,0 +1,113 @@
1
+ import { getLocaleFromStorageClient } from "@intlayer/core/utils";
2
+ import { internationalization } from "@intlayer/config/built";
3
+ import { DEFAULT_LOCALE } from "@intlayer/config/defaultValues";
4
+ import { localeDetector } from "@intlayer/core/localization";
5
+ // src/client/getBrowserLocale.tsx
6
+ var LanguageDetector = /* @__PURE__ */ function(LanguageDetector) {
7
+ LanguageDetector["Querystring"] = "querystring";
8
+ LanguageDetector["Storage"] = "storage";
9
+ LanguageDetector["Navigator"] = "navigator";
10
+ LanguageDetector["HtmlTag"] = "htmlTag";
11
+ return LanguageDetector;
12
+ }({});
13
+ var getDefaultsOptions = () => {
14
+ return {
15
+ order: [
16
+ "querystring",
17
+ "storage",
18
+ "navigator",
19
+ "htmlTag"
20
+ ],
21
+ lookupQuerystring: "locale",
22
+ htmlTag: typeof document !== "undefined" ? document.documentElement : null
23
+ };
24
+ };
25
+ var detectLanguage = (order, options) => {
26
+ const detected = {};
27
+ const queryStringDetector = () => {
28
+ if (typeof window === "undefined") return;
29
+ const search = window.location.search || "";
30
+ const value = new URLSearchParams(search).get(options.lookupQuerystring ?? "");
31
+ if (value) detected["querystring"] = value;
32
+ };
33
+ const storageDetector = () => {
34
+ if (typeof window === "undefined") return;
35
+ const locale = getLocaleFromStorageClient({
36
+ getCookie: (name) => {
37
+ try {
38
+ const cookies = document.cookie.split(";");
39
+ const cookieName = `${name}=`;
40
+ const cookie = cookies.find((c) => c.trim().startsWith(cookieName));
41
+ if (cookie) return cookie.split("=")[1].trim();
42
+ } catch {}
43
+ },
44
+ getSessionStorage: (name) => {
45
+ try {
46
+ return window.sessionStorage.getItem(name) ?? void 0;
47
+ } catch {}
48
+ },
49
+ getLocaleStorage: (name) => {
50
+ try {
51
+ return window.localStorage.getItem(name) ?? void 0;
52
+ } catch {}
53
+ }
54
+ });
55
+ if (locale) detected["storage"] = locale;
56
+ };
57
+ const navigatorDetector = () => {
58
+ if (typeof navigator === "undefined") return;
59
+ const languages = navigator.languages ?? [navigator.language];
60
+ const locale = localeDetector({ "accept-language": languages.join(",") }, internationalization.locales, internationalization.defaultLocale);
61
+ if (locale) detected["navigator"] = locale;
62
+ };
63
+ const htmlTagDetector = () => {
64
+ const htmlTag = options.htmlTag;
65
+ if (htmlTag && typeof htmlTag.getAttribute === "function") {
66
+ const lang = htmlTag.getAttribute("lang");
67
+ if (lang) {
68
+ const locale = localeDetector({ "accept-language": lang }, internationalization.locales, internationalization.defaultLocale);
69
+ detected["htmlTag"] = locale;
70
+ }
71
+ }
72
+ };
73
+ const detectors = {
74
+ ["querystring"]: queryStringDetector,
75
+ ["storage"]: storageDetector,
76
+ ["navigator"]: navigatorDetector,
77
+ ["htmlTag"]: htmlTagDetector
78
+ };
79
+ order.forEach((detectorName) => {
80
+ detectors[detectorName]?.();
81
+ });
82
+ return detected;
83
+ };
84
+ var getFirstAvailableLocale = (locales, order) => {
85
+ for (const detector of order) {
86
+ const locale = locales[detector];
87
+ if (locale && internationalization.locales.includes(locale)) return locale;
88
+ }
89
+ return internationalization?.defaultLocale ?? DEFAULT_LOCALE;
90
+ };
91
+ /**
92
+ * Core language detector function for browser environments.
93
+ *
94
+ * Detects the user's preferred locale by checking multiple sources in order:
95
+ * 1. Query string parameter
96
+ * 2. Storage (cookies, localStorage, sessionStorage) - uses getLocaleFromStorage
97
+ * 3. Navigator languages - uses localeDetector
98
+ * 4. HTML lang attribute - uses localeDetector
99
+ *
100
+ * @param userOptions - Optional configuration for detection order and lookup keys
101
+ * @returns The detected locale or the default locale
102
+ *
103
+ * @example
104
+ * const locale = getBrowserLocale({ order: [LanguageDetector.Storage, LanguageDetector.Navigator] });
105
+ */
106
+ var getBrowserLocale = (userOptions = {}) => {
107
+ const options = {
108
+ ...getDefaultsOptions(),
109
+ ...userOptions
110
+ };
111
+ return getFirstAvailableLocale(detectLanguage(options.order ?? [], options), options.order ?? []);
112
+ };
113
+ export { LanguageDetector, getBrowserLocale };
@@ -0,0 +1,14 @@
1
+ import { getBrowserLocale } from "./getBrowserLocale.jsx";
2
+ import { IntlayerClientContext, IntlayerProvider, IntlayerProviderContent, useIntlayerContext } from "./IntlayerProvider.jsx";
3
+ import { t } from "./t.js";
4
+ import { useDictionary } from "./useDictionary.js";
5
+ import { useDictionaryAsync } from "./useDictionaryAsync.js";
6
+ import { useDictionaryDynamic } from "./useDictionaryDynamic.js";
7
+ import { useIntlayer } from "./useIntlayer.js";
8
+ import { useLoadDynamic } from "./useLoadDynamic.js";
9
+ import { useLocale } from "./useLocale.js";
10
+ import { useLocaleBase } from "./useLocaleBase.js";
11
+ import { localeCookie, localeInStorage, setLocaleCookie, setLocaleInStorage, useLocaleCookie, useLocaleStorage } from "./useLocaleStorage.js";
12
+ import { usePathname } from "./usePathname.js";
13
+ import { useRewriteURL } from "./useRewriteURL.js";
14
+ export { IntlayerClientContext, IntlayerProvider, IntlayerProviderContent, getBrowserLocale, localeCookie, localeInStorage, setLocaleCookie, setLocaleInStorage, t, useDictionary, useDictionaryAsync, useDictionaryDynamic, useIntlayer, useIntlayerContext, useLoadDynamic, useLocale, useLocaleBase, useLocaleCookie, useLocaleStorage, usePathname, useRewriteURL };
@@ -0,0 +1,15 @@
1
+ import { IntlayerClientContext } from "./IntlayerProvider.jsx";
2
+ import { getTranslation } from "@intlayer/core/interpreter";
3
+ import { useContext } from "solid-js";
4
+ // src/client/t.ts
5
+ /**
6
+ * On the client side, this function returns the translation of the provided multilang content.
7
+ *
8
+ * If the locale is not provided, it will use the locale from the client context
9
+ */
10
+ var t = (multilangContent, locale) => {
11
+ const { locale: currentLocale } = useContext(IntlayerClientContext) ?? {};
12
+ const localeTarget = locale ?? (typeof currentLocale === "function" ? currentLocale() : currentLocale);
13
+ return getTranslation(multilangContent, localeTarget);
14
+ };
15
+ export { t };
@@ -0,0 +1,15 @@
1
+ import { useLocaleBase } from "./useLocaleBase.js";
2
+ import { useTranslation } from "./useTraduction.js";
3
+ // src/client/useContent.ts
4
+ /**
5
+ * On the client side, hook to get the translation content based on the locale
6
+ */
7
+ var useContent = (languageContent) => {
8
+ const { locale } = useLocaleBase();
9
+ return {
10
+ locale,
11
+ content: useTranslation(languageContent),
12
+ t: useTranslation
13
+ };
14
+ };
15
+ export { useContent };
@@ -0,0 +1,56 @@
1
+ import { getDictionary as getDictionary$1 } from "../getDictionary.js";
2
+ import { getPlugins } from "../plugins.jsx";
3
+ import { IntlayerClientContext } from "./IntlayerProvider.jsx";
4
+ import { unwrapLoadable } from "./useLoadDynamic.js";
5
+ import { collectTransformCacheWrites, deleteTransformCacheWrites } from "@intlayer/core/interpreter";
6
+ import { createMemo, onCleanup, useContext } from "solid-js";
7
+ import { resolveDictionaryArgument } from "@intlayer/core/dictionaryManipulator";
8
+ // src/client/useDictionary.ts
9
+ /**
10
+ * On the client side, Hook that transforms a dictionary (or qualified
11
+ * dictionary group) and returns its reactive content.
12
+ *
13
+ * The second argument is either a locale or a selector object
14
+ * (`{ item }`, `{ variant }`, optionally with `locale`).
15
+ *
16
+ * If the locale is not provided (directly or through the selector), it will
17
+ * use the locale from the client context.
18
+ */
19
+ var useDictionary = (dictionary, localeOrSelector) => {
20
+ const context = useContext(IntlayerClientContext) ?? {};
21
+ const instancePlugins = /* @__PURE__ */ new Map();
22
+ const getInstancePlugins = (locale) => {
23
+ const sharedPlugins = getPlugins(locale);
24
+ let ownPlugins = instancePlugins.get(sharedPlugins);
25
+ if (!ownPlugins) {
26
+ ownPlugins = [...sharedPlugins];
27
+ instancePlugins.set(sharedPlugins, ownPlugins);
28
+ }
29
+ return ownPlugins;
30
+ };
31
+ const interpret = () => {
32
+ const currentLocale = context?.locale?.();
33
+ const source = unwrapLoadable(dictionary) ?? dictionary;
34
+ if (process.env.INTLAYER_DICTIONARY_SELECTOR !== "false") return getDictionary$1(source, resolveDictionaryArgument({
35
+ localeOrSelector,
36
+ contextLocale: currentLocale,
37
+ contextVariant: context?.variant?.(),
38
+ dictionaryKey: dictionary.key
39
+ }), getInstancePlugins);
40
+ return getDictionary$1(source, localeOrSelector ?? currentLocale, getInstancePlugins);
41
+ };
42
+ const accessor = createMemo(() => {
43
+ const { result, writes } = collectTransformCacheWrites(interpret);
44
+ if (writes.length > 0) onCleanup(() => deleteTransformCacheWrites(writes));
45
+ return result;
46
+ });
47
+ return new Proxy(accessor, {
48
+ get(target, prop) {
49
+ return target()?.[prop];
50
+ },
51
+ apply(target, thisArg, args) {
52
+ return Reflect.apply(target, thisArg, args);
53
+ }
54
+ });
55
+ };
56
+ export { useDictionary };
@@ -0,0 +1,17 @@
1
+ import { IntlayerClientContext } from "./IntlayerProvider.jsx";
2
+ import { useDictionary } from "./useDictionary.js";
3
+ import { resolveInterpreterLocale } from "@intlayer/core/interpreter";
4
+ import { useContext } from "solid-js";
5
+ // src/client/useDictionaryAsync.ts
6
+ /**
7
+ * On the server side, Hook that transform a dictionary and return the content
8
+ *
9
+ * If the locale is not provided, it will use the locale from the client context
10
+ */
11
+ var useDictionaryAsync = async (dictionaryPromise, locale) => {
12
+ const { locale: currentLocale } = useContext(IntlayerClientContext) ?? {};
13
+ const localeTarget = locale ?? currentLocale() ?? resolveInterpreterLocale();
14
+ const dictionary = await dictionaryPromise[localeTarget]?.();
15
+ return useDictionary(dictionary, localeTarget);
16
+ };
17
+ export { useDictionaryAsync };
@@ -0,0 +1,105 @@
1
+ import { getDictionary as getDictionary$1 } from "../getDictionary.js";
2
+ import { createAsyncValue, trackAsyncValue } from "../solidCompat.js";
3
+ import { IntlayerClientContext } from "./IntlayerProvider.jsx";
4
+ import { useDictionary } from "./useDictionary.js";
5
+ import { seedDynamicValue, useLoadDynamic } from "./useLoadDynamic.js";
6
+ import { resolveInterpreterLocale } from "@intlayer/core/interpreter";
7
+ import { createMemo, useContext } from "solid-js";
8
+ import { QUALIFIER_DYNAMIC_TYPES_KEY, getDictionarySelectorCacheKey, getPreloadedDictionary, isQualifiedDynamicLoaderMap, parseDictionarySelector, resolveDictionaryArgument, resolveQualifiedDynamicContentAsync } from "@intlayer/core/dictionaryManipulator";
9
+ // src/client/useDictionaryDynamic.ts
10
+ /**
11
+ * On the client side, Hook that lazily loads a dictionary and returns its
12
+ * reactive content.
13
+ *
14
+ * The dictionary entry is either a plain dynamic loader map
15
+ * (`locale → loader`) or a qualified one (collection / variant,
16
+ * possibly combined). For a qualified map, only the chunk(s) the selector
17
+ * targets are loaded; the resolution mirrors static mode.
18
+ *
19
+ * If the locale is not provided (directly or through the selector), it will
20
+ * use the locale from the client context.
21
+ */
22
+ var useDictionaryDynamic = (dictionaryPromise, key, localeOrSelector) => {
23
+ const context = useContext(IntlayerClientContext) ?? {};
24
+ const { locale: currentLocale } = context;
25
+ const dictionaryKey = String(key);
26
+ if (process.env.INTLAYER_DICTIONARY_SELECTOR !== "false" && isQualifiedDynamicLoaderMap(dictionaryPromise)) {
27
+ const { locale: selectorLocale, selector: callSelector } = parseDictionarySelector(localeOrSelector);
28
+ const selectorAccessor = () => parseDictionarySelector(resolveDictionaryArgument({
29
+ localeOrSelector,
30
+ contextLocale: currentLocale?.(),
31
+ contextVariant: context.variant?.(),
32
+ dictionaryKey
33
+ })).selector;
34
+ const isCollection = dictionaryPromise[QUALIFIER_DYNAMIC_TYPES_KEY].includes("item") && callSelector?.item === void 0;
35
+ const localeAccessor = () => selectorLocale ?? currentLocale?.() ?? resolveInterpreterLocale();
36
+ if (!isCollection) {
37
+ const dictionarySourceAccessor = () => {
38
+ const localeTarget = localeAccessor();
39
+ return {
40
+ cacheKey: `${dictionaryKey}.${localeTarget}.${getDictionarySelectorCacheKey(selectorAccessor())}`,
41
+ locale: localeTarget
42
+ };
43
+ };
44
+ const loadEntry = ({ locale: localeTarget }) => resolveQualifiedDynamicContentAsync({
45
+ loaderMap: dictionaryPromise,
46
+ key: dictionaryKey,
47
+ locale: localeTarget,
48
+ selector: selectorAccessor(),
49
+ transform: (dictionary) => dictionary
50
+ }).then((resolved) => Array.isArray(resolved) ? resolved[0] : resolved);
51
+ const dictionary = useLoadDynamic(dictionarySourceAccessor, loadEntry);
52
+ return useDictionary(dictionary, selectorLocale);
53
+ }
54
+ const resolvedEntries = createAsyncValue(() => ({
55
+ locale: localeAccessor(),
56
+ selector: selectorAccessor()
57
+ }), ({ locale: localeTarget, selector }) => resolveQualifiedDynamicContentAsync({
58
+ loaderMap: dictionaryPromise,
59
+ key: dictionaryKey,
60
+ locale: localeTarget,
61
+ selector,
62
+ transform: (dictionary) => dictionary
63
+ }));
64
+ const accessor = createMemo(() => {
65
+ const resolved = resolvedEntries();
66
+ const localeTarget = localeAccessor();
67
+ if (!Array.isArray(resolved)) return [];
68
+ return resolved.map((dictionary) => getDictionary$1(dictionary, localeTarget));
69
+ });
70
+ trackAsyncValue(resolvedEntries);
71
+ return new Proxy(accessor, {
72
+ get(target, prop) {
73
+ const content = target();
74
+ const value = content?.[prop];
75
+ return typeof value === "function" ? value.bind(content) : value;
76
+ },
77
+ has(target, prop) {
78
+ return prop in target();
79
+ },
80
+ apply(target) {
81
+ return target();
82
+ }
83
+ });
84
+ }
85
+ const dictionaryLoaders = dictionaryPromise;
86
+ const locale = localeOrSelector;
87
+ const localeAccessor = () => locale ?? currentLocale?.() ?? resolveInterpreterLocale();
88
+ const dictionarySourceAccessor = () => {
89
+ const localeTarget = localeAccessor();
90
+ return {
91
+ cacheKey: `${dictionaryKey}.${localeTarget}`,
92
+ locale: localeTarget
93
+ };
94
+ };
95
+ const loadDictionary = ({ locale: localeTarget }) => {
96
+ const dictionaryLoader = dictionaryLoaders[localeTarget];
97
+ if (!dictionaryLoader) return Promise.reject(/* @__PURE__ */ new Error(`No dynamic dictionary loader found for key "${dictionaryKey}" and locale "${localeTarget}".`));
98
+ return dictionaryLoader();
99
+ };
100
+ const preloadedDictionary = getPreloadedDictionary(dictionaryLoaders, localeAccessor());
101
+ if (preloadedDictionary) seedDynamicValue(dictionarySourceAccessor(), preloadedDictionary);
102
+ const dictionary = useLoadDynamic(dictionarySourceAccessor, loadDictionary);
103
+ return useDictionary(dictionary, locale);
104
+ };
105
+ export { useDictionaryDynamic };
@@ -0,0 +1,55 @@
1
+ import { getIntlayer } from "../getIntlayer.js";
2
+ import { IntlayerClientContext } from "./IntlayerProvider.jsx";
3
+ import { createMemo, useContext } from "solid-js";
4
+ import { resolveDictionaryArgument } from "@intlayer/core/dictionaryManipulator";
5
+ // src/client/useIntlayer.ts
6
+ /**
7
+ * Solid hook that picks one dictionary by its key and returns its reactive content.
8
+ *
9
+ * The second argument is either a locale or a selector object:
10
+ * - `{ item: 2 }` — collection item (omit `item` to get every item as array)
11
+ * - `{ variant: 'black-friday' }` — named variant (omit for the `default` one)
12
+ * - `locale` composes with any selector and overrides the context locale
13
+ *
14
+ * Supports both direct property access and the accessor call form:
15
+ * - `content.myField.value` — reactive when used inside JSX or other Solid reactive contexts
16
+ * - `content()` — backward-compatible accessor form
17
+ *
18
+ * @param key - The unique key of the dictionary to retrieve.
19
+ * @param localeOrSelector - Optional locale or selector.
20
+ * @returns Reactive content accessible via property access or function call.
21
+ *
22
+ * @example
23
+ * ```tsx
24
+ * import { useIntlayer } from 'solid-intlayer';
25
+ *
26
+ * const MyComponent = () => {
27
+ * const content = useIntlayer('my-dictionary-key');
28
+ * const faq2 = useIntlayer('faq', { item: 2 });
29
+ *
30
+ * return <div>{content.myField.value}</div>;
31
+ * };
32
+ * ```
33
+ */
34
+ var useIntlayer = (key, localeOrSelector) => {
35
+ const context = useContext(IntlayerClientContext) ?? {};
36
+ const accessor = createMemo(() => {
37
+ const currentLocale = context?.locale?.();
38
+ if (process.env.INTLAYER_DICTIONARY_SELECTOR !== "false") return getIntlayer(key, resolveDictionaryArgument({
39
+ localeOrSelector,
40
+ contextLocale: currentLocale,
41
+ contextVariant: context?.variant?.(),
42
+ dictionaryKey: key
43
+ }));
44
+ return getIntlayer(key, localeOrSelector ?? currentLocale);
45
+ });
46
+ return new Proxy(accessor, {
47
+ get(target, prop) {
48
+ return target()?.[prop];
49
+ },
50
+ apply(target, thisArg, args) {
51
+ return Reflect.apply(target, thisArg, args);
52
+ }
53
+ });
54
+ };
55
+ export { useIntlayer };
@@ -0,0 +1,134 @@
1
+ import { PROXY_RESERVED_KEYS } from "../proxyKeys.js";
2
+ import { createAsyncValue, trackAsyncValue } from "../solidCompat.js";
3
+ import { untrack } from "solid-js";
4
+ // src/client/useLoadDynamic.ts
5
+ var NO_PENDING_PRIMITIVE_FALLBACK = Symbol("NO_PENDING_PRIMITIVE_FALLBACK");
6
+ /**
7
+ * Property exposing the settled value behind a loadable proxy, or `undefined`
8
+ * while the chunk is still in flight. See {@link unwrapLoadable}.
9
+ */
10
+ var LOADABLE_SETTLED_VALUE = Symbol("LOADABLE_SETTLED_VALUE");
11
+ /**
12
+ * Module-level cache to dedupe dynamic imports by key. Solid async primitives
13
+ * still own suspense, SSR serialization, hydration, and refetching on source
14
+ * change.
15
+ */
16
+ var cache = /* @__PURE__ */ new Map();
17
+ var getDynamicSourceCacheKey = (source) => typeof source === "string" ? source : source.cacheKey;
18
+ var readValueAtPath = (value, path) => path.reduce((currentValue, property) => {
19
+ if (currentValue === null || currentValue === void 0) return void 0;
20
+ return Reflect.get(Object(currentValue), property);
21
+ }, value);
22
+ var resolveDynamicLoader = (loader, source) => typeof loader === "function" ? loader(source) : loader;
23
+ var loadDynamicValue = (source, loader) => {
24
+ const key = getDynamicSourceCacheKey(source);
25
+ const cachedEntry = cache.get(key);
26
+ if (cachedEntry?.status === "success") return cachedEntry.value;
27
+ if (cachedEntry?.status === "pending") return cachedEntry.promise;
28
+ const promise = resolveDynamicLoader(loader, source).then((value) => {
29
+ cache.set(key, {
30
+ status: "success",
31
+ value
32
+ });
33
+ return value;
34
+ }, (error) => {
35
+ cache.delete(key);
36
+ throw error;
37
+ });
38
+ cache.set(key, {
39
+ status: "pending",
40
+ promise
41
+ });
42
+ return promise;
43
+ };
44
+ /**
45
+ * Records a value that is already available, so the next
46
+ * {@link loadDynamicValue} for this key returns it synchronously.
47
+ *
48
+ * Used for dictionaries a build-tool plugin awaited while their entry point
49
+ * evaluated: the async value resolves without ever entering a pending state,
50
+ * so the loading boundary is not triggered and the first render already has the content.
51
+ *
52
+ * An existing entry is never replaced — an in-flight load owns its key.
53
+ */
54
+ var seedDynamicValue = (source, value) => {
55
+ const key = getDynamicSourceCacheKey(source);
56
+ if (cache.has(key)) return;
57
+ cache.set(key, {
58
+ status: "success",
59
+ value
60
+ });
61
+ };
62
+ var bindPropertyValue = (value, propertyValue) => typeof propertyValue === "function" ? propertyValue.bind(value) : propertyValue;
63
+ var createPendingPrimitiveFallback = (property) => {
64
+ if (property === Symbol.toPrimitive) return () => "";
65
+ if (property === Symbol.iterator) return () => ({ next: () => ({
66
+ done: true,
67
+ value: void 0
68
+ }) });
69
+ if (property === "length") return 0;
70
+ if (property === PROXY_RESERVED_KEYS.toString) return () => "";
71
+ if (property === PROXY_RESERVED_KEYS.valueOf) return () => void 0;
72
+ if (property === PROXY_RESERVED_KEYS.value) return "";
73
+ return NO_PENDING_PRIMITIVE_FALLBACK;
74
+ };
75
+ /**
76
+ * Creates a stable callable proxy that re-reads the Solid async value on each
77
+ * property access. This keeps synchronous Intlayer access (`content.x.value`)
78
+ * safe while the Solid loading boundary owns the actual pending state.
79
+ */
80
+ var createLoadableProxy = (read) => {
81
+ const createProxyAtPath = (path) => {
82
+ const target = () => void 0;
83
+ return new Proxy(target, {
84
+ get(_target, property) {
85
+ if (property === PROXY_RESERVED_KEYS.promiseThen) return void 0;
86
+ const currentValue = readValueAtPath(read(), path);
87
+ if (property === LOADABLE_SETTLED_VALUE) return currentValue;
88
+ if (currentValue !== null && currentValue !== void 0) {
89
+ if (property === Symbol.toPrimitive) return () => currentValue;
90
+ return bindPropertyValue(currentValue, Reflect.get(Object(currentValue), property));
91
+ }
92
+ const primitiveFallback = createPendingPrimitiveFallback(property);
93
+ if (primitiveFallback !== NO_PENDING_PRIMITIVE_FALLBACK) return primitiveFallback;
94
+ return createProxyAtPath([...path, property]);
95
+ },
96
+ apply(_target, thisArgument, argumentsList) {
97
+ const currentValue = readValueAtPath(read(), path);
98
+ if (typeof currentValue !== "function") {
99
+ if (argumentsList.length === 0) return currentValue ?? "";
100
+ return createLoadableProxy(() => {
101
+ const resolvedValue = readValueAtPath(read(), path);
102
+ if (typeof resolvedValue !== "function") return void 0;
103
+ return untrack(() => Reflect.apply(resolvedValue, thisArgument, argumentsList));
104
+ });
105
+ }
106
+ return Reflect.apply(currentValue, thisArgument, argumentsList);
107
+ }
108
+ });
109
+ };
110
+ return createProxyAtPath([]);
111
+ };
112
+ /**
113
+ * Returns the settled value a loadable proxy stands for, or `undefined` while
114
+ * its chunk is in flight — and for any value that is not a loadable proxy.
115
+ *
116
+ * The read stays tracked, so a caller inside a reactive scope re-runs once the
117
+ * chunk lands. Interpreting the settled dictionary rather than the stand-in is
118
+ * what lets `getDictionary` memoize the transform: the stand-in is created per
119
+ * component and its content changes over time, while the settled dictionary is
120
+ * one shared object every component resolves to.
121
+ *
122
+ * @param value - A loadable proxy, or any other value.
123
+ */
124
+ var unwrapLoadable = (value) => {
125
+ if (value === null || typeof value !== "object" && typeof value !== "function") return void 0;
126
+ return value[LOADABLE_SETTLED_VALUE];
127
+ };
128
+ var useLoadDynamic = (key, loader) => {
129
+ const keyAccessor = () => typeof key === "function" ? key() : key;
130
+ const asyncValue = createAsyncValue(keyAccessor, (resolvedSource) => loadDynamicValue(resolvedSource, loader));
131
+ trackAsyncValue(asyncValue);
132
+ return createLoadableProxy(asyncValue);
133
+ };
134
+ export { createLoadableProxy, getDynamicSourceCacheKey, loadDynamicValue, seedDynamicValue, unwrapLoadable, useLoadDynamic };
@@ -0,0 +1,51 @@
1
+ import { createChangeEffect } from "../solidCompat.js";
2
+ import { IntlayerClientContext } from "./IntlayerProvider.jsx";
3
+ import { setLocaleInStorage } from "./useLocaleStorage.js";
4
+ import { internationalization } from "@intlayer/config/built";
5
+ import { useContext } from "solid-js";
6
+ // src/client/useLocale.ts
7
+ var { defaultLocale, locales: availableLocales } = internationalization ?? {};
8
+ /**
9
+ * Solid hook to manage the current locale and related functions.
10
+ *
11
+ * @param props - Optional configuration for locale management.
12
+ * @returns An object containing the current locale (accessor), default locale, available locales, and a function to update the locale.
13
+ *
14
+ * @example
15
+ * ```tsx
16
+ * import { useLocale } from 'solid-intlayer';
17
+ *
18
+ * const LocaleSwitcher = () => {
19
+ * const { locale, setLocale, availableLocales } = useLocale();
20
+ *
21
+ * return (
22
+ * <select value={locale()} onChange={(e) => setLocale(e.target.value)}>
23
+ * <For each={availableLocales}>
24
+ * {(loc) => <option value={loc}>{loc}</option>}
25
+ * </For>
26
+ * </select>
27
+ * );
28
+ * };
29
+ * ```
30
+ */
31
+ var useLocale = ({ isCookieEnabled, onLocaleChange } = {}) => {
32
+ const context = useContext(IntlayerClientContext);
33
+ const setLocale = (locale) => {
34
+ if (!availableLocales?.map(String).includes(locale)) {
35
+ console.error(`Locale ${locale} is not available`);
36
+ return;
37
+ }
38
+ context?.setLocale(locale);
39
+ setLocaleInStorage(locale, isCookieEnabled ?? context?.isCookieEnabled ?? true);
40
+ };
41
+ createChangeEffect(() => context?.locale?.(), (currentLocale) => {
42
+ if (onLocaleChange && currentLocale !== void 0) onLocaleChange(currentLocale);
43
+ });
44
+ return {
45
+ locale: context?.locale,
46
+ defaultLocale,
47
+ availableLocales,
48
+ setLocale
49
+ };
50
+ };
51
+ export { useLocale };