svelte-intlayer 7.3.5 → 7.3.7

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.
@@ -1,10 +1,15 @@
1
1
  import type { Dictionary, DictionaryKeys, LocalesValues, StrictModeLocaleMap } from '@intlayer/types';
2
2
  import { type Readable } from 'svelte/store';
3
+ import type { DeepTransformContent } from '../plugins';
3
4
  /**
4
- * Svelte hook for handling dynamic dictionary loading
5
- * @param dictionaryPromise Promise-based dictionary content
6
- * @param key Dictionary key for caching
7
- * @param locale Target locale (optional)
8
- * @returns Reactive store with loaded dictionary content
5
+ * Svelte hook for dynamic dictionary loading
6
+ * Loads dictionary content asynchronously and returns a reactive store.
7
+ *
8
+ * @param dictionaryPromise - Object mapping locales to import functions (e.g. { en: () => import(...) })
9
+ * @param locale - Optional fixed locale. If not provided, follows the global intlayerStore.
10
+ * @returns Readable store with the loaded dictionary content
9
11
  */
10
- export declare const useDictionaryDynamic: <T extends Dictionary, K extends DictionaryKeys>(dictionaryPromise: StrictModeLocaleMap<() => Promise<T>>, key: K, locale?: LocalesValues) => Readable<T | null>;
12
+ export declare function useDictionaryDynamic<T extends Dictionary>(dictionaryPromise: StrictModeLocaleMap<() => Promise<T>>, _key: DictionaryKeys, locale?: LocalesValues): Readable<DeepTransformContent<T['content']> & {
13
+ isLoading: boolean;
14
+ error: Error | null;
15
+ }>;
@@ -1,38 +1,89 @@
1
- import { derived, writable } from 'svelte/store';
1
+ import { derived } from 'svelte/store';
2
+ import { getDictionary } from '../getDictionary';
2
3
  import { getIntlayerContext } from './intlayerContext';
3
4
  import { intlayerStore } from './intlayerStore';
5
+ // Proxy that returns itself for any property access to handle nested paths during loading
6
+ const recursiveProxy = new Proxy(() => { }, {
7
+ get: (_target, prop) => {
8
+ if (prop === Symbol.toPrimitive) {
9
+ return () => undefined;
10
+ }
11
+ if (prop === 'toString') {
12
+ return () => '';
13
+ }
14
+ if (prop === 'then') {
15
+ return undefined;
16
+ }
17
+ return recursiveProxy;
18
+ },
19
+ apply: () => recursiveProxy,
20
+ });
4
21
  /**
5
- * Svelte hook for handling dynamic dictionary loading
6
- * @param dictionaryPromise Promise-based dictionary content
7
- * @param key Dictionary key for caching
8
- * @param locale Target locale (optional)
9
- * @returns Reactive store with loaded dictionary content
22
+ * Svelte hook for dynamic dictionary loading
23
+ * Loads dictionary content asynchronously and returns a reactive store.
24
+ *
25
+ * @param dictionaryPromise - Object mapping locales to import functions (e.g. { en: () => import(...) })
26
+ * @param locale - Optional fixed locale. If not provided, follows the global intlayerStore.
27
+ * @returns Readable store with the loaded dictionary content
10
28
  */
11
- export const useDictionaryDynamic = (dictionaryPromise, key, locale) => {
29
+ export function useDictionaryDynamic(dictionaryPromise, _key, locale) {
12
30
  const context = getIntlayerContext();
13
- const dictionaryStore = writable(null);
14
- // Create a derived store that loads the dictionary when locale changes
15
- return derived([intlayerStore], ([$store]) => {
16
- const targetLocale = locale ?? context?.locale ?? $store.locale;
17
- // Load dictionary for the target locale asynchronously
18
- const loadDictionary = async () => {
31
+ const localeStore = derived(intlayerStore, ($store) => locale ?? context?.locale ?? $store.locale);
32
+ return derived(localeStore, ($locale, set) => {
33
+ // Set loading state immediately with proxy
34
+ set(new Proxy({ isLoading: true, error: null }, {
35
+ get: (_target, prop) => {
36
+ if (prop === 'isLoading')
37
+ return true;
38
+ if (prop === 'error')
39
+ return null;
40
+ // For any other property, return the recursive proxy
41
+ // to allow nested access without errors
42
+ return recursiveProxy;
43
+ },
44
+ }));
45
+ let isCancelled = false;
46
+ const load = async () => {
19
47
  try {
20
- const dictionaryLoader = dictionaryPromise[targetLocale];
21
- if (dictionaryLoader) {
22
- const loadedDictionary = await dictionaryLoader();
23
- dictionaryStore.set(loadedDictionary);
24
- }
25
- else {
26
- dictionaryStore.set(null);
48
+ // Access the loader for the current locale
49
+ // dictionaryPromise is indexed by locale
50
+ const loader = dictionaryPromise[$locale];
51
+ if (!loader) {
52
+ return;
27
53
  }
54
+ const dict = await loader();
55
+ if (isCancelled)
56
+ return;
57
+ const content = getDictionary(dict, $locale);
58
+ set({
59
+ ...content,
60
+ isLoading: false,
61
+ error: null,
62
+ });
28
63
  }
29
64
  catch (error) {
30
- console.error(`Failed to load dictionary for key: ${String(key)}`, error);
31
- dictionaryStore.set(null);
65
+ if (isCancelled)
66
+ return;
67
+ console.error(error);
68
+ set({
69
+ isLoading: false,
70
+ error: error,
71
+ });
32
72
  }
33
73
  };
34
- loadDictionary();
35
- // Return the current state, actual loading happens asynchronously
36
- return null;
37
- });
38
- };
74
+ load();
75
+ return () => {
76
+ isCancelled = true;
77
+ };
78
+ },
79
+ // Initial value
80
+ new Proxy({ isLoading: true, error: null }, {
81
+ get: (_target, prop) => {
82
+ if (prop === 'isLoading')
83
+ return true;
84
+ if (prop === 'error')
85
+ return null;
86
+ return recursiveProxy;
87
+ },
88
+ }));
89
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-intlayer",
3
- "version": "7.3.5",
3
+ "version": "7.3.7",
4
4
  "description": "Easily internationalize i18n your Svelte applications with type-safe multilingual content management.",
5
5
  "keywords": [
6
6
  "intlayer",
@@ -68,12 +68,12 @@
68
68
  "typecheck": "tsc --noEmit --project tsconfig.types.json"
69
69
  },
70
70
  "dependencies": {
71
- "@intlayer/api": "7.3.5",
72
- "@intlayer/config": "7.3.5",
73
- "@intlayer/core": "7.3.5",
74
- "@intlayer/editor": "7.3.5",
75
- "@intlayer/types": "7.3.5",
76
- "@intlayer/unmerged-dictionaries-entry": "7.3.5"
71
+ "@intlayer/api": "7.3.7",
72
+ "@intlayer/config": "7.3.7",
73
+ "@intlayer/core": "7.3.7",
74
+ "@intlayer/editor": "7.3.7",
75
+ "@intlayer/types": "7.3.7",
76
+ "@intlayer/unmerged-dictionaries-entry": "7.3.7"
77
77
  },
78
78
  "devDependencies": {
79
79
  "@sveltejs/adapter-auto": "^7.0.0",