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
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* @param
|
|
8
|
-
* @
|
|
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
|
|
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
|
|
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
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* @param
|
|
9
|
-
* @
|
|
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
|
|
29
|
+
export function useDictionaryDynamic(dictionaryPromise, _key, locale) {
|
|
12
30
|
const context = getIntlayerContext();
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
31
|
-
|
|
65
|
+
if (isCancelled)
|
|
66
|
+
return;
|
|
67
|
+
console.error(error);
|
|
68
|
+
set({
|
|
69
|
+
isLoading: false,
|
|
70
|
+
error: error,
|
|
71
|
+
});
|
|
32
72
|
}
|
|
33
73
|
};
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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.
|
|
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.
|
|
72
|
-
"@intlayer/config": "7.3.
|
|
73
|
-
"@intlayer/core": "7.3.
|
|
74
|
-
"@intlayer/editor": "7.3.
|
|
75
|
-
"@intlayer/types": "7.3.
|
|
76
|
-
"@intlayer/unmerged-dictionaries-entry": "7.3.
|
|
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",
|