@tolgee/svelte 4.9.2 → 5.0.0-rc.9be0f0e.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.
- package/CHANGELOG.md +16 -0
- package/GlobalContextPlugin.d.ts +4 -0
- package/GlobalContextPlugin.js +10 -0
- package/README.md +0 -0
- package/SveltePlugin.d.ts +5 -0
- package/SveltePlugin.js +6 -0
- package/T.svelte +9 -40
- package/T.svelte.d.ts +1 -1
- package/TolgeeProvider.svelte +14 -21
- package/TolgeeProvider.svelte.d.ts +3 -2
- package/getTolgee.d.ts +98 -0
- package/getTolgee.js +14 -0
- package/getTolgeeContext.d.ts +2 -6
- package/getTolgeeContext.js +4 -3
- package/getTranslate.d.ts +6 -2
- package/getTranslate.js +10 -85
- package/getTranslateInternal.d.ts +10 -0
- package/getTranslateInternal.js +41 -0
- package/index.d.ts +5 -2
- package/index.js +5 -2
- package/package.json +9 -4
- package/types.d.ts +3 -14
- package/types.js +0 -0
- package/getLanguageStore.d.ts +0 -2
- package/getLanguageStore.js +0 -13
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,22 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# 5.0.0-rc.9be0f0e.0 (2022-09-29)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* prepare for rc release ([9be0f0e](https://github.com/tolgee/tolgee-js/commit/9be0f0e475edfacf948ecf0b93979936aae7ae79))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
### BREAKING CHANGES
|
|
15
|
+
|
|
16
|
+
* new tolgee API
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
6
22
|
## [4.9.2](https://github.com/tolgee/tolgee-js/compare/v4.9.1...v4.9.2) (2022-09-07)
|
|
7
23
|
|
|
8
24
|
**Note:** Version bump only for package @tolgee/svelte
|
package/README.md
CHANGED
|
File without changes
|
package/SveltePlugin.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DevTools, BrowserExtensionPlugin } from '@tolgee/devtools-web';
|
|
2
|
+
import { GlobalContextPlugin } from './GlobalContextPlugin';
|
|
3
|
+
export const SveltePlugin = (props) => (tolgee) => tolgee
|
|
4
|
+
.use(BrowserExtensionPlugin({ noReload: true }))
|
|
5
|
+
.use(DevTools({ observer: props }))
|
|
6
|
+
.use(GlobalContextPlugin());
|
package/T.svelte
CHANGED
|
@@ -1,48 +1,17 @@
|
|
|
1
|
-
<script>import
|
|
2
|
-
import { onDestroy } from 'svelte';
|
|
1
|
+
<script>import getTranslateInternal from './getTranslateInternal';
|
|
3
2
|
export let keyName;
|
|
4
|
-
export let
|
|
3
|
+
export let params = undefined;
|
|
5
4
|
export let noWrap = false;
|
|
6
5
|
export let defaultValue = undefined;
|
|
7
6
|
if (!keyName) {
|
|
8
7
|
console.error('Missing keyName prop!');
|
|
9
8
|
}
|
|
10
|
-
const
|
|
11
|
-
let translated = keyName
|
|
12
|
-
? tolgeeContext.tolgee.instant(keyName, parameters, noWrap, true)
|
|
13
|
-
: '';
|
|
14
|
-
const translate = () => {
|
|
15
|
-
if (keyName) {
|
|
16
|
-
tolgeeContext.tolgee
|
|
17
|
-
.translate({
|
|
18
|
-
key: keyName,
|
|
19
|
-
params: parameters,
|
|
20
|
-
noWrap,
|
|
21
|
-
defaultValue,
|
|
22
|
-
})
|
|
23
|
-
.then((result) => {
|
|
24
|
-
translated = result;
|
|
25
|
-
})
|
|
26
|
-
.catch(() => {
|
|
27
|
-
console.error('Failed to resolve translation for key: ', keyName);
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
const onTranslationChangeSubscription = tolgeeContext.tolgee.onTranslationChange.subscribe((changeData) => {
|
|
32
|
-
if (changeData.key === keyName) {
|
|
33
|
-
translate();
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
const onLangChangeSubscription = tolgeeContext.tolgee.onLangChange.subscribe(() => {
|
|
37
|
-
translate();
|
|
38
|
-
});
|
|
39
|
-
if (typeof window !== 'undefined') {
|
|
40
|
-
onDestroy(() => {
|
|
41
|
-
onTranslationChangeSubscription.unsubscribe();
|
|
42
|
-
onLangChangeSubscription.unsubscribe();
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
$: translate();
|
|
9
|
+
const { t } = getTranslateInternal();
|
|
46
10
|
</script>
|
|
47
11
|
|
|
48
|
-
{
|
|
12
|
+
{$t({
|
|
13
|
+
key: keyName,
|
|
14
|
+
params: params,
|
|
15
|
+
noWrap: noWrap,
|
|
16
|
+
defaultValue: defaultValue
|
|
17
|
+
})}
|
package/T.svelte.d.ts
CHANGED
package/TolgeeProvider.svelte
CHANGED
|
@@ -1,33 +1,26 @@
|
|
|
1
1
|
<script>import { onDestroy, onMount, setContext } from 'svelte';
|
|
2
|
-
|
|
3
|
-
export let
|
|
4
|
-
|
|
5
|
-
wrapperMode: 'invisible',
|
|
6
|
-
ui: process.env.NODE_ENV !== 'development'
|
|
7
|
-
? undefined
|
|
8
|
-
: typeof require !== 'undefined'
|
|
9
|
-
? require('@tolgee/ui')
|
|
10
|
-
: import('@tolgee/ui'),
|
|
11
|
-
...(config || new TolgeeConfig()),
|
|
12
|
-
});
|
|
13
|
-
let tolgeeRunPromise;
|
|
2
|
+
export let tolgee;
|
|
3
|
+
export let fallback = undefined;
|
|
4
|
+
let isLoading = !tolgee.isLoaded();
|
|
14
5
|
setContext('tolgeeContext', {
|
|
15
6
|
tolgee,
|
|
16
7
|
});
|
|
17
8
|
if (typeof window !== 'undefined') {
|
|
18
|
-
onMount(() =>
|
|
9
|
+
onMount(() => {
|
|
10
|
+
tolgee.run().then(() => {
|
|
11
|
+
isLoading = false;
|
|
12
|
+
});
|
|
13
|
+
});
|
|
19
14
|
onDestroy(tolgee.stop);
|
|
20
15
|
}
|
|
21
16
|
</script>
|
|
22
17
|
|
|
23
|
-
{#if !
|
|
18
|
+
{#if !isLoading }
|
|
24
19
|
<slot />
|
|
25
|
-
{:else
|
|
26
|
-
|
|
27
|
-
{
|
|
28
|
-
{
|
|
20
|
+
{:else }
|
|
21
|
+
{#if fallback}
|
|
22
|
+
{fallback}
|
|
23
|
+
{:else}
|
|
29
24
|
<slot name="loading-fallback" />
|
|
30
|
-
{
|
|
31
|
-
<slot />
|
|
32
|
-
{/await}
|
|
25
|
+
{/if}
|
|
33
26
|
{/if}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { SvelteComponentTyped } from "svelte";
|
|
2
|
-
import {
|
|
2
|
+
import type { TolgeeInstance } from '@tolgee/core';
|
|
3
3
|
declare const __propDef: {
|
|
4
4
|
props: {
|
|
5
|
-
|
|
5
|
+
tolgee: TolgeeInstance;
|
|
6
|
+
fallback?: any;
|
|
6
7
|
};
|
|
7
8
|
events: {
|
|
8
9
|
[evt: string]: CustomEvent<any>;
|
package/getTolgee.d.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { TolgeeEvent } from '@tolgee/core';
|
|
2
|
+
export declare const getTolgee: (events?: TolgeeEvent[]) => {
|
|
3
|
+
subscribe: (this: void, run: import("svelte/store").Subscriber<Readonly<{
|
|
4
|
+
on: import("@tolgee/core").TolgeeOn;
|
|
5
|
+
onKeyUpdate: (handler: import("@tolgee/core").ListenerHandler<void>) => import("@tolgee/core").ListenerSelective;
|
|
6
|
+
use: (plugin: import("@tolgee/core").TolgeePlugin) => Readonly<any>;
|
|
7
|
+
getLanguage: () => string;
|
|
8
|
+
getPendingLanguage: () => string;
|
|
9
|
+
changeLanguage: (language: string) => Promise<void>;
|
|
10
|
+
changeTranslation: import("@tolgee/core").ChangeTranslationInterface;
|
|
11
|
+
addActiveNs: (ns: import("@tolgee/core").FallbackNSTranslation, forget?: boolean) => Promise<void>;
|
|
12
|
+
removeActiveNs: (ns: import("@tolgee/core").FallbackNSTranslation) => void;
|
|
13
|
+
loadRecords: (descriptors: import("@tolgee/core").CacheDescriptor[]) => Promise<import("@tolgee/core").TranslationsFlat[]>;
|
|
14
|
+
loadRecord: (descriptors: import("@tolgee/core").CacheDescriptor) => Promise<import("@tolgee/core").TranslationsFlat>;
|
|
15
|
+
addStaticData: (data: {
|
|
16
|
+
[key: string]: import("@tolgee/core").TreeTranslationsData | (() => Promise<import("@tolgee/core").TreeTranslationsData>);
|
|
17
|
+
}) => void;
|
|
18
|
+
getRecord: (descriptor: import("@tolgee/core").CacheDescriptorInternal) => import("@tolgee/core").TranslationsFlat;
|
|
19
|
+
getAllRecords: () => import("@tolgee/core").CachePublicRecord[];
|
|
20
|
+
isInitialLoading: () => boolean;
|
|
21
|
+
isLoading: (ns?: import("@tolgee/core").FallbackNSTranslation) => boolean;
|
|
22
|
+
isLoaded: (ns?: import("@tolgee/core").FallbackNSTranslation) => boolean;
|
|
23
|
+
isFetching: (ns?: import("@tolgee/core").FallbackNSTranslation) => boolean;
|
|
24
|
+
isRunning: () => boolean;
|
|
25
|
+
highlight: import("@tolgee/core").HighlightInterface;
|
|
26
|
+
getInitialOptions: () => import("@tolgee/core").Options;
|
|
27
|
+
isDev: () => boolean;
|
|
28
|
+
init: (options: Partial<import("@tolgee/core").Options>) => Readonly<any>;
|
|
29
|
+
run: () => Promise<void>;
|
|
30
|
+
stop: () => void;
|
|
31
|
+
t: (props: import("@tolgee/core").TranslatePropsInternal) => string;
|
|
32
|
+
wrap: (params: import("@tolgee/core").TranslatePropsInternal) => string;
|
|
33
|
+
unwrap: (text: string) => import("@tolgee/core").Unwrapped;
|
|
34
|
+
}>>, invalidate?: (value?: Readonly<{
|
|
35
|
+
on: import("@tolgee/core").TolgeeOn;
|
|
36
|
+
onKeyUpdate: (handler: import("@tolgee/core").ListenerHandler<void>) => import("@tolgee/core").ListenerSelective;
|
|
37
|
+
use: (plugin: import("@tolgee/core").TolgeePlugin) => Readonly<any>;
|
|
38
|
+
getLanguage: () => string;
|
|
39
|
+
getPendingLanguage: () => string;
|
|
40
|
+
changeLanguage: (language: string) => Promise<void>;
|
|
41
|
+
changeTranslation: import("@tolgee/core").ChangeTranslationInterface;
|
|
42
|
+
addActiveNs: (ns: import("@tolgee/core").FallbackNSTranslation, forget?: boolean) => Promise<void>;
|
|
43
|
+
removeActiveNs: (ns: import("@tolgee/core").FallbackNSTranslation) => void;
|
|
44
|
+
loadRecords: (descriptors: import("@tolgee/core").CacheDescriptor[]) => Promise<import("@tolgee/core").TranslationsFlat[]>;
|
|
45
|
+
loadRecord: (descriptors: import("@tolgee/core").CacheDescriptor) => Promise<import("@tolgee/core").TranslationsFlat>;
|
|
46
|
+
addStaticData: (data: {
|
|
47
|
+
[key: string]: import("@tolgee/core").TreeTranslationsData | (() => Promise<import("@tolgee/core").TreeTranslationsData>);
|
|
48
|
+
}) => void;
|
|
49
|
+
getRecord: (descriptor: import("@tolgee/core").CacheDescriptorInternal) => import("@tolgee/core").TranslationsFlat;
|
|
50
|
+
getAllRecords: () => import("@tolgee/core").CachePublicRecord[];
|
|
51
|
+
isInitialLoading: () => boolean;
|
|
52
|
+
isLoading: (ns?: import("@tolgee/core").FallbackNSTranslation) => boolean;
|
|
53
|
+
isLoaded: (ns?: import("@tolgee/core").FallbackNSTranslation) => boolean;
|
|
54
|
+
isFetching: (ns?: import("@tolgee/core").FallbackNSTranslation) => boolean;
|
|
55
|
+
isRunning: () => boolean;
|
|
56
|
+
highlight: import("@tolgee/core").HighlightInterface;
|
|
57
|
+
getInitialOptions: () => import("@tolgee/core").Options;
|
|
58
|
+
isDev: () => boolean;
|
|
59
|
+
init: (options: Partial<import("@tolgee/core").Options>) => Readonly<any>;
|
|
60
|
+
run: () => Promise<void>;
|
|
61
|
+
stop: () => void;
|
|
62
|
+
t: (props: import("@tolgee/core").TranslatePropsInternal) => string;
|
|
63
|
+
wrap: (params: import("@tolgee/core").TranslatePropsInternal) => string;
|
|
64
|
+
unwrap: (text: string) => import("@tolgee/core").Unwrapped;
|
|
65
|
+
}>) => void) => import("svelte/store").Unsubscriber;
|
|
66
|
+
value: Readonly<{
|
|
67
|
+
on: import("@tolgee/core").TolgeeOn;
|
|
68
|
+
onKeyUpdate: (handler: import("@tolgee/core").ListenerHandler<void>) => import("@tolgee/core").ListenerSelective;
|
|
69
|
+
use: (plugin: import("@tolgee/core").TolgeePlugin) => Readonly<any>;
|
|
70
|
+
getLanguage: () => string;
|
|
71
|
+
getPendingLanguage: () => string;
|
|
72
|
+
changeLanguage: (language: string) => Promise<void>;
|
|
73
|
+
changeTranslation: import("@tolgee/core").ChangeTranslationInterface;
|
|
74
|
+
addActiveNs: (ns: import("@tolgee/core").FallbackNSTranslation, forget?: boolean) => Promise<void>;
|
|
75
|
+
removeActiveNs: (ns: import("@tolgee/core").FallbackNSTranslation) => void;
|
|
76
|
+
loadRecords: (descriptors: import("@tolgee/core").CacheDescriptor[]) => Promise<import("@tolgee/core").TranslationsFlat[]>;
|
|
77
|
+
loadRecord: (descriptors: import("@tolgee/core").CacheDescriptor) => Promise<import("@tolgee/core").TranslationsFlat>;
|
|
78
|
+
addStaticData: (data: {
|
|
79
|
+
[key: string]: import("@tolgee/core").TreeTranslationsData | (() => Promise<import("@tolgee/core").TreeTranslationsData>);
|
|
80
|
+
}) => void;
|
|
81
|
+
getRecord: (descriptor: import("@tolgee/core").CacheDescriptorInternal) => import("@tolgee/core").TranslationsFlat;
|
|
82
|
+
getAllRecords: () => import("@tolgee/core").CachePublicRecord[];
|
|
83
|
+
isInitialLoading: () => boolean;
|
|
84
|
+
isLoading: (ns?: import("@tolgee/core").FallbackNSTranslation) => boolean;
|
|
85
|
+
isLoaded: (ns?: import("@tolgee/core").FallbackNSTranslation) => boolean;
|
|
86
|
+
isFetching: (ns?: import("@tolgee/core").FallbackNSTranslation) => boolean;
|
|
87
|
+
isRunning: () => boolean;
|
|
88
|
+
highlight: import("@tolgee/core").HighlightInterface;
|
|
89
|
+
getInitialOptions: () => import("@tolgee/core").Options;
|
|
90
|
+
isDev: () => boolean;
|
|
91
|
+
init: (options: Partial<import("@tolgee/core").Options>) => Readonly<any>;
|
|
92
|
+
run: () => Promise<void>;
|
|
93
|
+
stop: () => void;
|
|
94
|
+
t: (props: import("@tolgee/core").TranslatePropsInternal) => string;
|
|
95
|
+
wrap: (params: import("@tolgee/core").TranslatePropsInternal) => string;
|
|
96
|
+
unwrap: (text: string) => import("@tolgee/core").Unwrapped;
|
|
97
|
+
}>;
|
|
98
|
+
};
|
package/getTolgee.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { readable } from 'svelte/store';
|
|
2
|
+
import { getTolgeeContext } from './getTolgeeContext';
|
|
3
|
+
const DEFAULT_EVENTS = ['language', 'pendingLanguage'];
|
|
4
|
+
export const getTolgee = (events = DEFAULT_EVENTS) => {
|
|
5
|
+
const tolgeeContext = getTolgeeContext();
|
|
6
|
+
const tolgee = tolgeeContext.tolgee;
|
|
7
|
+
const { subscribe } = readable(tolgee, (set) => {
|
|
8
|
+
const listeners = events.map((e) => tolgee.on(e, () => {
|
|
9
|
+
set({ ...tolgee });
|
|
10
|
+
}));
|
|
11
|
+
return () => listeners.forEach((listener) => listener.unsubscribe());
|
|
12
|
+
});
|
|
13
|
+
return { subscribe, value: tolgee };
|
|
14
|
+
};
|
package/getTolgeeContext.d.ts
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare type TolgeeContext = {
|
|
3
|
-
tolgee: Tolgee;
|
|
4
|
-
};
|
|
1
|
+
import type { TolgeeSvelteContext } from './types';
|
|
5
2
|
/**
|
|
6
3
|
* Returns Tolgee context.
|
|
7
4
|
* @throws Error when context is not defined.
|
|
8
5
|
*/
|
|
9
|
-
declare const getTolgeeContext: () =>
|
|
10
|
-
export default getTolgeeContext;
|
|
6
|
+
export declare const getTolgeeContext: () => TolgeeSvelteContext;
|
package/getTolgeeContext.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { getContext } from 'svelte';
|
|
2
|
+
import { getGlobalContext } from './GlobalContextPlugin';
|
|
2
3
|
/**
|
|
3
4
|
* Returns Tolgee context.
|
|
4
5
|
* @throws Error when context is not defined.
|
|
5
6
|
*/
|
|
6
|
-
const getTolgeeContext = () => {
|
|
7
|
-
const context = getContext('tolgeeContext')
|
|
7
|
+
export const getTolgeeContext = () => {
|
|
8
|
+
const context = (getContext('tolgeeContext') ||
|
|
9
|
+
getGlobalContext());
|
|
8
10
|
if (context === undefined) {
|
|
9
11
|
throw Error('Tolgee context is undefined. Trying to use getTranslate method or T component outside TolgeeProvider?');
|
|
10
12
|
}
|
|
11
13
|
return context;
|
|
12
14
|
};
|
|
13
|
-
export default getTolgeeContext;
|
package/getTranslate.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
import type
|
|
2
|
-
|
|
1
|
+
import { type Readable } from 'svelte/store';
|
|
2
|
+
import { type FallbackNSTranslation, type TFnType } from '@tolgee/core';
|
|
3
|
+
declare const getTranslate: (ns?: FallbackNSTranslation) => {
|
|
4
|
+
t: Readable<TFnType<import("@tolgee/core").DefaultParamType, string>>;
|
|
5
|
+
isLoading: Readable<boolean>;
|
|
6
|
+
};
|
|
3
7
|
export default getTranslate;
|
package/getTranslate.js
CHANGED
|
@@ -1,88 +1,13 @@
|
|
|
1
|
-
import { derived
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
const getTranslate = () => {
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
// dummy store which is updated to forces providing of new translate method
|
|
11
|
-
const updateStore = writable(0);
|
|
12
|
-
const update = () => updateStore.update((number) => number + 1);
|
|
13
|
-
const translate = (key, parameters, noWrap, defaultValue) => {
|
|
14
|
-
if (!keys.has(key)) {
|
|
15
|
-
keys.add(key);
|
|
16
|
-
}
|
|
17
|
-
// get result from sync method instant, which is not reliable, since
|
|
18
|
-
// languages may not be loaded yet
|
|
19
|
-
const instantTranslated = tolgee.instant({
|
|
20
|
-
key: key,
|
|
21
|
-
params: parameters,
|
|
22
|
-
noWrap: noWrap,
|
|
23
|
-
defaultValue: defaultValue,
|
|
24
|
-
});
|
|
25
|
-
// so we have to use also translate method, to get reliable result
|
|
26
|
-
tolgee
|
|
27
|
-
.translate({ key: key, params: parameters, noWrap, defaultValue })
|
|
28
|
-
.then((translated) => {
|
|
29
|
-
// when the result value is different, we have to update the store and
|
|
30
|
-
// so return new translate function
|
|
31
|
-
if (instantTranslated !== translated) {
|
|
32
|
-
update();
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
return instantTranslated;
|
|
36
|
-
};
|
|
37
|
-
// subscribe for translation change
|
|
38
|
-
const onTranslationChangeSubscription = tolgee.onTranslationChange.subscribe(async (changeData) => {
|
|
39
|
-
// update only when this getTranslate translated the changed key value
|
|
40
|
-
if (keys.has(changeData.key)) {
|
|
41
|
-
update();
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
// subscribe for language change
|
|
45
|
-
const onLangChangeSubscription = tolgee.onLangChange.subscribe(async () => {
|
|
46
|
-
// wait for translations to be loaded, so we are waiting for translation of any key
|
|
47
|
-
await tolgee.translate('key');
|
|
48
|
-
update();
|
|
49
|
-
});
|
|
50
|
-
if (typeof window !== 'undefined') {
|
|
51
|
-
//unsubscribe on destroy
|
|
52
|
-
onDestroy(() => {
|
|
53
|
-
onTranslationChangeSubscription.unsubscribe();
|
|
54
|
-
onLangChangeSubscription.unsubscribe();
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
// return new translate method when something is changed
|
|
58
|
-
return derived(
|
|
59
|
-
// when updateStore changes, translate function gets changed as well
|
|
60
|
-
updateStore, () => (keyOrProps, ...params) => {
|
|
61
|
-
let parameters = undefined;
|
|
62
|
-
let noWrap = undefined;
|
|
63
|
-
let defaultValue = undefined;
|
|
64
|
-
// allow user to pass object of params and make the code cleaner
|
|
65
|
-
const key = typeof keyOrProps === 'object' ? keyOrProps.key : keyOrProps;
|
|
66
|
-
if (typeof keyOrProps === 'object') {
|
|
67
|
-
parameters = keyOrProps.parameters;
|
|
68
|
-
noWrap = keyOrProps.noWrap;
|
|
69
|
-
defaultValue = keyOrProps.defaultValue;
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
params.forEach((param) => {
|
|
73
|
-
switch (typeof param) {
|
|
74
|
-
case 'object':
|
|
75
|
-
parameters = param;
|
|
76
|
-
break;
|
|
77
|
-
case 'boolean':
|
|
78
|
-
noWrap = param;
|
|
79
|
-
break;
|
|
80
|
-
case 'string':
|
|
81
|
-
defaultValue = param;
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
return translate(key, parameters, noWrap, defaultValue);
|
|
1
|
+
import { derived } from 'svelte/store';
|
|
2
|
+
import { getTranslateParams, } from '@tolgee/core';
|
|
3
|
+
import getTranslateInternal from './getTranslateInternal';
|
|
4
|
+
const getTranslate = (ns) => {
|
|
5
|
+
const { t: tInternal, isLoading } = getTranslateInternal(ns);
|
|
6
|
+
const t = derived(tInternal, (value) => (...params) => {
|
|
7
|
+
//@ts-ignore
|
|
8
|
+
const props = getTranslateParams(...params);
|
|
9
|
+
return value(props);
|
|
86
10
|
});
|
|
11
|
+
return { t, isLoading };
|
|
87
12
|
};
|
|
88
13
|
export default getTranslate;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Readable } from 'svelte/store';
|
|
2
|
+
import { type FallbackNSTranslation, type TranslateProps } from '@tolgee/core';
|
|
3
|
+
declare const getTranslateInternal: (ns?: FallbackNSTranslation) => {
|
|
4
|
+
t: {
|
|
5
|
+
subscribe: (this: void, run: import("svelte/store").Subscriber<(props: TranslateProps<any>) => string>, invalidate?: (value?: (props: TranslateProps<any>) => string) => void) => import("svelte/store").Unsubscriber;
|
|
6
|
+
value: import("svelte/store").Writable<(props: TranslateProps<any>) => string>;
|
|
7
|
+
};
|
|
8
|
+
isLoading: Readable<boolean>;
|
|
9
|
+
};
|
|
10
|
+
export default getTranslateInternal;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { writable } from 'svelte/store';
|
|
2
|
+
import { onDestroy } from 'svelte';
|
|
3
|
+
import { getFallback, } from '@tolgee/core';
|
|
4
|
+
import { getTolgeeContext } from './index';
|
|
5
|
+
const getTranslateInternal = (ns) => {
|
|
6
|
+
const namespaces = getFallback(ns);
|
|
7
|
+
const tolgeeContext = getTolgeeContext();
|
|
8
|
+
const tolgee = tolgeeContext?.tolgee;
|
|
9
|
+
if (!tolgee) {
|
|
10
|
+
throw new Error('Tolgee instance not provided');
|
|
11
|
+
}
|
|
12
|
+
const tFunction = createTFunction();
|
|
13
|
+
const t = writable(tFunction);
|
|
14
|
+
const subscription = tolgee.onKeyUpdate(() => {
|
|
15
|
+
t.set(createTFunction());
|
|
16
|
+
isLoading.set(!tolgee.isLoaded(namespaces));
|
|
17
|
+
});
|
|
18
|
+
subscription.subscribeNs(namespaces);
|
|
19
|
+
tolgee.addActiveNs(namespaces);
|
|
20
|
+
onDestroy(() => {
|
|
21
|
+
subscription?.unsubscribe();
|
|
22
|
+
tolgee.removeActiveNs(namespaces);
|
|
23
|
+
});
|
|
24
|
+
const isLoading = writable(!tolgee.isLoaded(namespaces));
|
|
25
|
+
const subscribeToKey = (key) => {
|
|
26
|
+
subscription.subscribeKey(key);
|
|
27
|
+
};
|
|
28
|
+
function createTFunction() {
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
30
|
+
return (props) => {
|
|
31
|
+
const fallbackNs = props.ns || namespaces?.[0];
|
|
32
|
+
subscribeToKey({ key: props.key, ns: fallbackNs });
|
|
33
|
+
return tolgee.t({ ...props, ns: fallbackNs });
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
t: { subscribe: t.subscribe, value: t },
|
|
38
|
+
isLoading: { subscribe: isLoading.subscribe },
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
export default getTranslateInternal;
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
export { default as T } from './T.svelte';
|
|
2
2
|
export { default as getTranslate } from './getTranslate';
|
|
3
3
|
export { default as TolgeeProvider } from './TolgeeProvider.svelte';
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
4
|
+
export { getTolgeeContext } from './getTolgeeContext';
|
|
5
|
+
export { SveltePlugin } from './SveltePlugin';
|
|
6
|
+
export { getTolgee } from './getTolgee';
|
|
7
|
+
export * from '@tolgee/core';
|
|
8
|
+
export * from '@tolgee/devtools-web';
|
package/index.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
export { default as T } from './T.svelte';
|
|
2
2
|
export { default as getTranslate } from './getTranslate';
|
|
3
3
|
export { default as TolgeeProvider } from './TolgeeProvider.svelte';
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
4
|
+
export { getTolgeeContext } from './getTolgeeContext';
|
|
5
|
+
export { SveltePlugin } from './SveltePlugin';
|
|
6
|
+
export { getTolgee } from './getTolgee';
|
|
7
|
+
export * from '@tolgee/core';
|
|
8
|
+
export * from '@tolgee/devtools-web';
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tolgee/svelte",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0-rc.9be0f0e.0",
|
|
4
4
|
"dependencies": {
|
|
5
|
-
"@tolgee/core": "^
|
|
6
|
-
"@tolgee/
|
|
5
|
+
"@tolgee/core": "^5.0.0-rc.9be0f0e.0",
|
|
6
|
+
"@tolgee/devtools-web": "^5.0.0-rc.9be0f0e.0",
|
|
7
|
+
"@tolgee/format-icu": "^5.0.0-rc.9be0f0e.0"
|
|
7
8
|
},
|
|
8
9
|
"devDependencies": {
|
|
9
10
|
"@sveltejs/adapter-auto": "1.0.0-next.65",
|
|
@@ -11,6 +12,7 @@
|
|
|
11
12
|
"@sveltejs/package": "1.0.0-next.1",
|
|
12
13
|
"@testing-library/jest-dom": "^5.16.5",
|
|
13
14
|
"@testing-library/svelte": "^3.2.1",
|
|
15
|
+
"@tsconfig/svelte": "^3.0.0",
|
|
14
16
|
"@types/jest": "^28.1.7",
|
|
15
17
|
"@typescript-eslint/eslint-plugin": "^5.34.0",
|
|
16
18
|
"@typescript-eslint/parser": "^5.34.0",
|
|
@@ -39,11 +41,14 @@
|
|
|
39
41
|
},
|
|
40
42
|
"exports": {
|
|
41
43
|
"./package.json": "./package.json",
|
|
44
|
+
"./GlobalContextPlugin": "./GlobalContextPlugin.js",
|
|
45
|
+
"./SveltePlugin": "./SveltePlugin.js",
|
|
42
46
|
"./T.svelte": "./T.svelte",
|
|
43
47
|
"./TolgeeProvider.svelte": "./TolgeeProvider.svelte",
|
|
44
|
-
"./
|
|
48
|
+
"./getTolgee": "./getTolgee.js",
|
|
45
49
|
"./getTolgeeContext": "./getTolgeeContext.js",
|
|
46
50
|
"./getTranslate": "./getTranslate.js",
|
|
51
|
+
"./getTranslateInternal": "./getTranslateInternal.js",
|
|
47
52
|
".": "./index.js",
|
|
48
53
|
"./types": "./types.js"
|
|
49
54
|
},
|
package/types.d.ts
CHANGED
|
@@ -1,15 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
key: string;
|
|
5
|
-
parameters?: TranslationParams;
|
|
6
|
-
noWrap?: boolean;
|
|
7
|
-
defaultValue?: string;
|
|
1
|
+
import type { TolgeeInstance } from '@tolgee/core';
|
|
2
|
+
export declare type TolgeeSvelteContext = {
|
|
3
|
+
tolgee: TolgeeInstance;
|
|
8
4
|
};
|
|
9
|
-
export declare type GetTranslateType = () => Readable<{
|
|
10
|
-
(props: GetTranslateResultFnProps): string;
|
|
11
|
-
(key: string, defaultValue?: string, noWrap?: boolean): string;
|
|
12
|
-
(key: string, defaultValue?: string, parameters?: TranslationParams): string;
|
|
13
|
-
(key: string, parameters?: TranslationParams, defaultValue?: string): string;
|
|
14
|
-
(key: string, parameters?: TranslationParams, noWrap?: boolean, defaultValue?: string): string;
|
|
15
|
-
}>;
|
package/types.js
CHANGED
|
File without changes
|
package/getLanguageStore.d.ts
DELETED
package/getLanguageStore.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { writable } from 'svelte/store';
|
|
2
|
-
import { getTolgeeContext } from './index';
|
|
3
|
-
export const getLanguageStore = () => {
|
|
4
|
-
const context = getTolgeeContext();
|
|
5
|
-
const store = writable(context.tolgee.lang);
|
|
6
|
-
context.tolgee.onLangChange.subscribe((lang) => {
|
|
7
|
-
store.set(lang);
|
|
8
|
-
});
|
|
9
|
-
store.subscribe((lang) => {
|
|
10
|
-
context.tolgee.changeLanguage(lang);
|
|
11
|
-
});
|
|
12
|
-
return store;
|
|
13
|
-
};
|