svelte-intlayer 9.8.5 → 9.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.
|
@@ -3,9 +3,8 @@ import type { LocalesValues, StrictModeLocaleMap } from '@intlayer/types/module_
|
|
|
3
3
|
import { type Readable } from 'svelte/store';
|
|
4
4
|
/**
|
|
5
5
|
* Svelte hook for handling dynamic dictionary loading
|
|
6
|
-
* @param dictionaryPromise
|
|
7
|
-
* @param
|
|
8
|
-
* @
|
|
9
|
-
* @returns Reactive store with loaded dictionary content
|
|
6
|
+
* @param dictionaryPromise Locale-keyed dictionary loaders
|
|
7
|
+
* @param locale Target locale (optional, defaults to the context locale)
|
|
8
|
+
* @returns Reactive store with the loaded dictionary, `null` while loading
|
|
10
9
|
*/
|
|
11
10
|
export declare const useDictionaryAsync: <const T extends Dictionary>(dictionaryPromise: StrictModeLocaleMap<() => Promise<T>>, locale?: LocalesValues) => Promise<Readable<T | null>>;
|
|
@@ -1,38 +1,29 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { loadDynamicDictionary } from '@intlayer/core/dictionaryManipulator';
|
|
2
|
+
import { derived } from 'svelte/store';
|
|
2
3
|
import { getIntlayerContext } from './intlayerContext';
|
|
3
4
|
import { intlayerStore } from './intlayerStore';
|
|
4
5
|
/**
|
|
5
6
|
* Svelte hook for handling dynamic dictionary loading
|
|
6
|
-
* @param dictionaryPromise
|
|
7
|
-
* @param
|
|
8
|
-
* @
|
|
9
|
-
* @returns Reactive store with loaded dictionary content
|
|
7
|
+
* @param dictionaryPromise Locale-keyed dictionary loaders
|
|
8
|
+
* @param locale Target locale (optional, defaults to the context locale)
|
|
9
|
+
* @returns Reactive store with the loaded dictionary, `null` while loading
|
|
10
10
|
*/
|
|
11
11
|
export const useDictionaryAsync = async (dictionaryPromise, locale) => {
|
|
12
12
|
const context = getIntlayerContext();
|
|
13
|
-
|
|
14
|
-
// Create a derived store that loads the dictionary when locale changes
|
|
15
|
-
return derived([intlayerStore], ([$store]) => {
|
|
13
|
+
return derived(intlayerStore, ($store, set) => {
|
|
16
14
|
const targetLocale = locale ?? context?.locale ?? $store.locale;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
catch (error) {
|
|
30
|
-
console.error(`Failed to load dictionary for key: ${String(targetLocale)}`, error);
|
|
31
|
-
dictionaryStore.set(null);
|
|
32
|
-
}
|
|
15
|
+
let isCancelled = false;
|
|
16
|
+
loadDynamicDictionary(dictionaryPromise, targetLocale).then((dictionary) => {
|
|
17
|
+
if (!isCancelled)
|
|
18
|
+
set(dictionary ?? null);
|
|
19
|
+
}, (error) => {
|
|
20
|
+
if (isCancelled)
|
|
21
|
+
return;
|
|
22
|
+
console.error(`Failed to load dictionary for locale: ${String(targetLocale)}`, error);
|
|
23
|
+
set(null);
|
|
24
|
+
});
|
|
25
|
+
return () => {
|
|
26
|
+
isCancelled = true;
|
|
33
27
|
};
|
|
34
|
-
|
|
35
|
-
// Return the current state, actual loading happens asynchronously
|
|
36
|
-
return null;
|
|
37
|
-
});
|
|
28
|
+
}, null);
|
|
38
29
|
};
|
|
@@ -1,33 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createDynamicContentStore, loadingContentPlaceholder, } from '@intlayer/core/dictionaryManipulator';
|
|
2
2
|
import { derived } from 'svelte/store';
|
|
3
3
|
import { getDictionary } from '../getDictionary';
|
|
4
4
|
import { getIntlayerContext } from './intlayerContext';
|
|
5
5
|
import { intlayerStore } from './intlayerStore';
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
get: (_target, prop) => {
|
|
9
|
-
if (prop === Symbol.toPrimitive) {
|
|
10
|
-
return () => undefined;
|
|
11
|
-
}
|
|
12
|
-
if (prop === 'toString') {
|
|
13
|
-
return () => '';
|
|
14
|
-
}
|
|
15
|
-
if (prop === 'then') {
|
|
16
|
-
return undefined;
|
|
17
|
-
}
|
|
18
|
-
return recursiveProxy;
|
|
19
|
-
},
|
|
20
|
-
apply: () => recursiveProxy,
|
|
21
|
-
});
|
|
6
|
+
/** Content cache shared by every dynamic read of the page. */
|
|
7
|
+
const dynamicContentStore = createDynamicContentStore((dictionary, argument) => getDictionary(dictionary, argument));
|
|
22
8
|
const loadingProxy = () => new Proxy({ isLoading: true, error: null }, {
|
|
23
9
|
get: (_target, prop) => {
|
|
24
10
|
if (prop === 'isLoading')
|
|
25
11
|
return true;
|
|
26
12
|
if (prop === 'error')
|
|
27
13
|
return null;
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
return recursiveProxy;
|
|
14
|
+
// Any other access is safe while loading and renders ''
|
|
15
|
+
return loadingContentPlaceholder;
|
|
31
16
|
},
|
|
32
17
|
});
|
|
33
18
|
/** Merges the resolved content with the loading/error state. */
|
|
@@ -54,84 +39,31 @@ const withState = (value, isLoading = false) => {
|
|
|
54
39
|
*/
|
|
55
40
|
export function useDictionaryDynamic(dictionaryPromise, key, localeOrSelector) {
|
|
56
41
|
const context = getIntlayerContext();
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
localeOrSelector !== null;
|
|
60
|
-
const callSelector = isSelector
|
|
61
|
-
? localeOrSelector
|
|
62
|
-
: undefined;
|
|
63
|
-
// Layers the provider's ambient variant under the call-site selector, so only
|
|
64
|
-
// the targeted chunk is loaded. A call-site variant always wins.
|
|
65
|
-
const variant = callSelector?.variant ?? resolveProviderVariant(context?.variant, key);
|
|
66
|
-
const selector = variant !== undefined
|
|
67
|
-
? { ...callSelector, variant }
|
|
68
|
-
: callSelector;
|
|
69
|
-
const explicitLocale = isSelector
|
|
70
|
-
? localeOrSelector.locale
|
|
71
|
-
: localeOrSelector;
|
|
72
|
-
const localeStore = derived(intlayerStore, ($store) => explicitLocale ?? context?.locale ?? $store.locale);
|
|
73
|
-
// Once content has resolved, a locale switch keeps it on screen (flagged as
|
|
74
|
-
// loading) until the new chunk lands. Falling back to `loadingProxy()` would
|
|
75
|
-
// blank every text node for a frame before rendering the target locale.
|
|
42
|
+
const readContent = dynamicContentStore.createReader(dictionaryPromise, String(key));
|
|
43
|
+
const contextLocaleStore = derived(intlayerStore, ($store) => context?.locale ?? $store.locale);
|
|
76
44
|
let resolvedContent;
|
|
77
|
-
return derived(
|
|
45
|
+
return derived(contextLocaleStore, ($contextLocale, set) => {
|
|
78
46
|
const publish = (content) => {
|
|
79
47
|
resolvedContent = content;
|
|
80
48
|
set(withState(content));
|
|
81
49
|
};
|
|
82
|
-
//
|
|
83
|
-
//
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
50
|
+
// Resolved per locale, so a locale pinned at the call site stays pinned
|
|
51
|
+
// while an unpinned one follows the store.
|
|
52
|
+
const settled = readContent({
|
|
53
|
+
localeOrSelector,
|
|
54
|
+
contextLocale: $contextLocale,
|
|
55
|
+
contextVariant: context?.variant,
|
|
56
|
+
}, publish, (error) => {
|
|
57
|
+
console.error(error);
|
|
58
|
+
set({ isLoading: false, error: error });
|
|
59
|
+
});
|
|
60
|
+
if (settled) {
|
|
61
|
+
publish(settled.content);
|
|
90
62
|
return;
|
|
91
63
|
}
|
|
64
|
+
// Keeps the previous locale's content on screen while the next loads
|
|
92
65
|
set(resolvedContent === undefined
|
|
93
66
|
? loadingProxy()
|
|
94
67
|
: withState(resolvedContent, true));
|
|
95
|
-
|
|
96
|
-
const load = async () => {
|
|
97
|
-
try {
|
|
98
|
-
let resolved;
|
|
99
|
-
if (process.env.INTLAYER_DICTIONARY_SELECTOR !== 'false' &&
|
|
100
|
-
isQualifiedDynamicLoaderMap(dictionaryPromise)) {
|
|
101
|
-
resolved = await resolveQualifiedDynamicContentAsync({
|
|
102
|
-
loaderMap: dictionaryPromise,
|
|
103
|
-
key: String(key),
|
|
104
|
-
locale: $locale,
|
|
105
|
-
selector,
|
|
106
|
-
transform: (dictionary) => getDictionary(dictionary, $locale),
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
const loader = dictionaryPromise[$locale];
|
|
111
|
-
if (!loader)
|
|
112
|
-
return;
|
|
113
|
-
const dict = await loader();
|
|
114
|
-
resolved = getDictionary(dict, $locale);
|
|
115
|
-
}
|
|
116
|
-
if (isCancelled)
|
|
117
|
-
return;
|
|
118
|
-
publish(resolved);
|
|
119
|
-
}
|
|
120
|
-
catch (error) {
|
|
121
|
-
if (isCancelled)
|
|
122
|
-
return;
|
|
123
|
-
console.error(error);
|
|
124
|
-
set({
|
|
125
|
-
isLoading: false,
|
|
126
|
-
error: error,
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
load();
|
|
131
|
-
return () => {
|
|
132
|
-
isCancelled = true;
|
|
133
|
-
};
|
|
134
|
-
},
|
|
135
|
-
// Initial value
|
|
136
|
-
loadingProxy());
|
|
68
|
+
}, loadingProxy());
|
|
137
69
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "svelte-intlayer",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.9.0",
|
|
4
4
|
"description": "Easily internationalize i18n your Svelte applications with type-safe multilingual content management.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"data",
|
|
@@ -83,11 +83,11 @@
|
|
|
83
83
|
"typecheck": "tsc --noEmit --project tsconfig.types.json"
|
|
84
84
|
},
|
|
85
85
|
"dependencies": {
|
|
86
|
-
"@intlayer/api": "9.
|
|
87
|
-
"@intlayer/config": "9.
|
|
88
|
-
"@intlayer/core": "9.
|
|
89
|
-
"@intlayer/editor": "9.
|
|
90
|
-
"@intlayer/types": "9.
|
|
86
|
+
"@intlayer/api": "9.9.0",
|
|
87
|
+
"@intlayer/config": "9.9.0",
|
|
88
|
+
"@intlayer/core": "9.9.0",
|
|
89
|
+
"@intlayer/editor": "9.9.0",
|
|
90
|
+
"@intlayer/types": "9.9.0"
|
|
91
91
|
},
|
|
92
92
|
"devDependencies": {
|
|
93
93
|
"@sveltejs/adapter-auto": "9.0.0",
|
|
@@ -109,7 +109,7 @@
|
|
|
109
109
|
"svelte": ">=5.0.0"
|
|
110
110
|
},
|
|
111
111
|
"optionalDependencies": {
|
|
112
|
-
"@intlayer/analytics": "9.
|
|
112
|
+
"@intlayer/analytics": "9.9.0"
|
|
113
113
|
},
|
|
114
114
|
"engines": {
|
|
115
115
|
"node": ">=14.18"
|