@tolgee/core 5.0.0-alpha.1 → 5.0.0-alpha.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.
- package/README.md +174 -0
- package/README.njk.md +61 -0
- package/dist/tolgee.cjs.js +185 -143
- package/dist/tolgee.cjs.js.map +1 -1
- package/dist/tolgee.cjs.min.js +1 -1
- package/dist/tolgee.cjs.min.js.map +1 -1
- package/dist/tolgee.esm.js +185 -143
- package/dist/tolgee.esm.js.map +1 -1
- package/dist/tolgee.esm.min.mjs +1 -1
- package/dist/tolgee.esm.min.mjs.map +1 -1
- package/dist/tolgee.umd.js +185 -143
- package/dist/tolgee.umd.js.map +1 -1
- package/dist/tolgee.umd.min.js +1 -1
- package/dist/tolgee.umd.min.js.map +1 -1
- package/lib/Controller/Controller.d.ts +8 -4
- package/lib/Controller/Events/EventEmitter.d.ts +1 -1
- package/lib/Controller/Events/EventEmitterSelective.d.ts +1 -1
- package/lib/Controller/Events/Events.d.ts +1 -0
- package/lib/Controller/Plugins/Plugins.d.ts +4 -4
- package/lib/Controller/State/State.d.ts +2 -1
- package/lib/Controller/State/initState.d.ts +13 -2
- package/lib/{Tolgee.d.ts → TolgeeCore.d.ts} +13 -4
- package/lib/helpers.d.ts +1 -0
- package/lib/index.d.ts +1 -1
- package/lib/types/events.d.ts +2 -2
- package/lib/types/index.d.ts +1 -1
- package/lib/types/plugin.d.ts +6 -9
- package/package.json +2 -2
- package/src/Controller/Controller.ts +5 -4
- package/src/Controller/Events/EventEmitter.ts +6 -2
- package/src/Controller/Events/EventEmitterSelective.test.ts +23 -0
- package/src/Controller/Events/EventEmitterSelective.ts +8 -5
- package/src/Controller/Events/Events.ts +19 -8
- package/src/Controller/Plugins/Plugins.ts +66 -63
- package/src/Controller/State/initState.ts +16 -1
- package/src/{Tolgee.ts → TolgeeCore.ts} +12 -3
- package/src/__test/backend.test.ts +2 -2
- package/src/__test/cache.test.ts +6 -3
- package/src/__test/client.test.ts +2 -2
- package/src/__test/events.test.ts +30 -5
- package/src/__test/format.simple.test.ts +2 -14
- package/src/__test/formatError.test.ts +61 -0
- package/src/__test/initialization.test.ts +4 -4
- package/src/__test/languageDetection.test.ts +8 -8
- package/src/__test/languageStorage.test.ts +10 -11
- package/src/__test/languages.test.ts +8 -8
- package/src/__test/loading.test.ts +2 -2
- package/src/__test/namespaces.fallback.test.ts +5 -5
- package/src/__test/namespaces.test.ts +4 -4
- package/src/__test/options.test.ts +3 -3
- package/src/__test/plugins.test.ts +4 -4
- package/src/helpers.ts +8 -0
- package/src/index.ts +1 -1
- package/src/types/events.ts +2 -2
- package/src/types/index.ts +1 -1
- package/src/types/plugin.ts +12 -12
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TolgeeCore, TolgeePlugin, TolgeeInstance } from '../index';
|
|
2
2
|
|
|
3
3
|
describe('language storage plugin', () => {
|
|
4
4
|
let tolgee: TolgeeInstance;
|
|
@@ -22,7 +22,7 @@ describe('language storage plugin', () => {
|
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
it('will restore language without loading', async () => {
|
|
25
|
-
tolgee =
|
|
25
|
+
tolgee = TolgeeCore()
|
|
26
26
|
.use(StoragePlugin('en'))
|
|
27
27
|
.init({
|
|
28
28
|
defaultLanguage: 'es',
|
|
@@ -46,7 +46,7 @@ describe('language storage plugin', () => {
|
|
|
46
46
|
});
|
|
47
47
|
|
|
48
48
|
it('will restore language async', async () => {
|
|
49
|
-
tolgee =
|
|
49
|
+
tolgee = TolgeeCore()
|
|
50
50
|
.use(StoragePlugin(Promise.resolve('en')))
|
|
51
51
|
.init({
|
|
52
52
|
defaultLanguage: 'es',
|
|
@@ -72,7 +72,7 @@ describe('language storage plugin', () => {
|
|
|
72
72
|
});
|
|
73
73
|
|
|
74
74
|
it('will fallback correctly', async () => {
|
|
75
|
-
tolgee =
|
|
75
|
+
tolgee = TolgeeCore()
|
|
76
76
|
.use(StoragePlugin(Promise.resolve(undefined)))
|
|
77
77
|
.init({
|
|
78
78
|
defaultLanguage: 'es',
|
|
@@ -88,7 +88,7 @@ describe('language storage plugin', () => {
|
|
|
88
88
|
});
|
|
89
89
|
|
|
90
90
|
it('will return key before language is loaded', async () => {
|
|
91
|
-
tolgee =
|
|
91
|
+
tolgee = TolgeeCore()
|
|
92
92
|
.use(StoragePlugin(Promise.resolve('en')))
|
|
93
93
|
.init({
|
|
94
94
|
defaultLanguage: 'es',
|
|
@@ -105,7 +105,7 @@ describe('language storage plugin', () => {
|
|
|
105
105
|
});
|
|
106
106
|
|
|
107
107
|
it('will store language value on language change', async () => {
|
|
108
|
-
tolgee =
|
|
108
|
+
tolgee = TolgeeCore()
|
|
109
109
|
.use(StoragePlugin(Promise.resolve('en')))
|
|
110
110
|
.init({
|
|
111
111
|
defaultLanguage: 'es',
|
|
@@ -122,7 +122,7 @@ describe('language storage plugin', () => {
|
|
|
122
122
|
});
|
|
123
123
|
|
|
124
124
|
it('will ignore invalid values', () => {
|
|
125
|
-
tolgee =
|
|
125
|
+
tolgee = TolgeeCore()
|
|
126
126
|
.use(StoragePlugin('eq'))
|
|
127
127
|
.init({
|
|
128
128
|
defaultLanguage: 'es',
|
|
@@ -135,11 +135,10 @@ describe('language storage plugin', () => {
|
|
|
135
135
|
expect(tolgee.getLanguage()).toEqual('es');
|
|
136
136
|
});
|
|
137
137
|
|
|
138
|
-
it('will
|
|
139
|
-
tolgee =
|
|
138
|
+
it('will throw an error when no available languages are specified', () => {
|
|
139
|
+
tolgee = TolgeeCore().use(StoragePlugin('eq')).init({
|
|
140
140
|
defaultLanguage: 'es',
|
|
141
141
|
});
|
|
142
|
-
tolgee.run();
|
|
143
|
-
expect(tolgee.getLanguage()).toEqual('eq');
|
|
142
|
+
expect(() => tolgee.run()).toThrowError('availableLanguages');
|
|
144
143
|
});
|
|
145
144
|
});
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TolgeeCore, TreeTranslationsData } from '../index';
|
|
2
2
|
import { resolvablePromise } from './testTools';
|
|
3
3
|
|
|
4
4
|
describe('language changes', () => {
|
|
5
5
|
it('changes language', async () => {
|
|
6
|
-
const tolgee =
|
|
6
|
+
const tolgee = TolgeeCore().init({ language: 'en' });
|
|
7
7
|
expect(tolgee.getLanguage()).toEqual('en');
|
|
8
8
|
await tolgee.changeLanguage('es');
|
|
9
9
|
expect(tolgee.getLanguage()).toEqual('es');
|
|
10
10
|
});
|
|
11
11
|
|
|
12
12
|
it('returns correct translation', async () => {
|
|
13
|
-
const tolgee =
|
|
13
|
+
const tolgee = TolgeeCore().init({
|
|
14
14
|
language: 'en',
|
|
15
15
|
staticData: { en: { hello: 'World' }, es: { hello: 'Mundo' } },
|
|
16
16
|
});
|
|
@@ -24,7 +24,7 @@ describe('language changes', () => {
|
|
|
24
24
|
it('fetches language data correctly', async () => {
|
|
25
25
|
const [promiseEn, resolveEn] = resolvablePromise<TreeTranslationsData>();
|
|
26
26
|
const [promiseEs, resolveEs] = resolvablePromise<TreeTranslationsData>();
|
|
27
|
-
const tolgee =
|
|
27
|
+
const tolgee = TolgeeCore().init({
|
|
28
28
|
language: 'en',
|
|
29
29
|
staticData: {
|
|
30
30
|
en: () => promiseEn,
|
|
@@ -53,7 +53,7 @@ describe('language changes', () => {
|
|
|
53
53
|
const [promiseEnIn, resolveEnIn] =
|
|
54
54
|
resolvablePromise<TreeTranslationsData>();
|
|
55
55
|
|
|
56
|
-
const tolgee =
|
|
56
|
+
const tolgee = TolgeeCore().init({
|
|
57
57
|
language: 'es',
|
|
58
58
|
staticData: {
|
|
59
59
|
en: () => promiseEn,
|
|
@@ -95,7 +95,7 @@ describe('language changes', () => {
|
|
|
95
95
|
});
|
|
96
96
|
|
|
97
97
|
it('will fallback to default value', () => {
|
|
98
|
-
const tolgee =
|
|
98
|
+
const tolgee = TolgeeCore().init({
|
|
99
99
|
defaultLanguage: 'en',
|
|
100
100
|
});
|
|
101
101
|
|
|
@@ -105,14 +105,14 @@ describe('language changes', () => {
|
|
|
105
105
|
});
|
|
106
106
|
|
|
107
107
|
it('will throw error when no language specified', () => {
|
|
108
|
-
const tolgee =
|
|
108
|
+
const tolgee = TolgeeCore().init({});
|
|
109
109
|
|
|
110
110
|
expect(() => tolgee.run()).toThrow(/'language'/);
|
|
111
111
|
});
|
|
112
112
|
|
|
113
113
|
it('loads fallback languages and namespaces', async () => {
|
|
114
114
|
const loadNs = jest.fn(() => Promise.resolve(undefined as any));
|
|
115
|
-
const tolgee =
|
|
115
|
+
const tolgee = TolgeeCore().init({
|
|
116
116
|
language: 'en',
|
|
117
117
|
fallbackNs: ['fallback'],
|
|
118
118
|
staticData: {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TolgeeCore, TreeTranslationsData } from '../index';
|
|
2
2
|
import { resolvablePromise } from './testTools';
|
|
3
3
|
|
|
4
4
|
describe('loading', () => {
|
|
@@ -7,7 +7,7 @@ describe('loading', () => {
|
|
|
7
7
|
const [promiseEs, resolveEs] = resolvablePromise<TreeTranslationsData>();
|
|
8
8
|
const onLoadHandler = jest.fn(() => {});
|
|
9
9
|
const onFetchingHandler = jest.fn(() => {});
|
|
10
|
-
const tolgee =
|
|
10
|
+
const tolgee = TolgeeCore().init({
|
|
11
11
|
language: 'en',
|
|
12
12
|
staticData: {
|
|
13
13
|
en: () => promiseEn,
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TolgeeCore } from '../TolgeeCore';
|
|
2
2
|
|
|
3
3
|
describe('namespaces fallback', () => {
|
|
4
4
|
test('works with multiple and default', () => {
|
|
5
|
-
const tolgee =
|
|
5
|
+
const tolgee = TolgeeCore().init({
|
|
6
6
|
language: 'en',
|
|
7
7
|
staticData: {
|
|
8
8
|
en: { '0': 'noNamespace' },
|
|
@@ -20,7 +20,7 @@ describe('namespaces fallback', () => {
|
|
|
20
20
|
});
|
|
21
21
|
|
|
22
22
|
test('works when no fallback specified', () => {
|
|
23
|
-
const tolgee =
|
|
23
|
+
const tolgee = TolgeeCore().init({
|
|
24
24
|
language: 'en',
|
|
25
25
|
staticData: {
|
|
26
26
|
en: { '0': 'noNamespace' },
|
|
@@ -37,7 +37,7 @@ describe('namespaces fallback', () => {
|
|
|
37
37
|
});
|
|
38
38
|
|
|
39
39
|
test('works when data present but no fallback', () => {
|
|
40
|
-
const tolgee =
|
|
40
|
+
const tolgee = TolgeeCore().init({
|
|
41
41
|
language: 'en',
|
|
42
42
|
staticData: {
|
|
43
43
|
en: { '0': 'noNamespace' },
|
|
@@ -55,7 +55,7 @@ describe('namespaces fallback', () => {
|
|
|
55
55
|
});
|
|
56
56
|
|
|
57
57
|
test('works with default', () => {
|
|
58
|
-
const tolgee =
|
|
58
|
+
const tolgee = TolgeeCore().init({
|
|
59
59
|
language: 'en',
|
|
60
60
|
staticData: {
|
|
61
61
|
en: { '0': 'empty' },
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TolgeeCore } from '../index';
|
|
2
2
|
|
|
3
3
|
const tolgeeWithNamespaces = () => {
|
|
4
4
|
const promiseEnTest = jest.fn(() => Promise.resolve({ test: 'Test' }));
|
|
@@ -8,7 +8,7 @@ const tolgeeWithNamespaces = () => {
|
|
|
8
8
|
Promise.resolve({ cancel: 'Cancellar' })
|
|
9
9
|
);
|
|
10
10
|
|
|
11
|
-
return
|
|
11
|
+
return TolgeeCore().init({
|
|
12
12
|
language: 'en',
|
|
13
13
|
ns: ['common'],
|
|
14
14
|
defaultNs: 'common',
|
|
@@ -23,7 +23,7 @@ const tolgeeWithNamespaces = () => {
|
|
|
23
23
|
|
|
24
24
|
describe('language changes', () => {
|
|
25
25
|
it('returns correct translation from namespace', () => {
|
|
26
|
-
const tolgee =
|
|
26
|
+
const tolgee = TolgeeCore().init({
|
|
27
27
|
language: 'en',
|
|
28
28
|
staticData: {
|
|
29
29
|
en: { hello: 'World' },
|
|
@@ -36,7 +36,7 @@ describe('language changes', () => {
|
|
|
36
36
|
});
|
|
37
37
|
|
|
38
38
|
it('uses defaultNs', async () => {
|
|
39
|
-
const tolgee =
|
|
39
|
+
const tolgee = TolgeeCore().init({
|
|
40
40
|
language: 'en',
|
|
41
41
|
defaultNs: 'common',
|
|
42
42
|
staticData: {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TolgeeCore } from '../TolgeeCore';
|
|
2
2
|
|
|
3
3
|
describe('initial options', () => {
|
|
4
4
|
it('combines options correctly', () => {
|
|
5
|
-
const tolgee =
|
|
5
|
+
const tolgee = TolgeeCore()
|
|
6
6
|
.updateDefaults({
|
|
7
7
|
observerType: 'text',
|
|
8
8
|
observerOptions: { highlightColor: 'red', restrictedElements: ['a'] },
|
|
@@ -45,7 +45,7 @@ describe('initial options', () => {
|
|
|
45
45
|
});
|
|
46
46
|
|
|
47
47
|
it('sanitizes url', () => {
|
|
48
|
-
const tolgee =
|
|
48
|
+
const tolgee = TolgeeCore().init({
|
|
49
49
|
language: 'cs',
|
|
50
50
|
apiUrl: 'http://localhost:8080/',
|
|
51
51
|
observerOptions: { highlightWidth: 90 },
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TolgeeCore } from '../TolgeeCore';
|
|
2
2
|
import {
|
|
3
3
|
FinalFormatterMiddleware,
|
|
4
4
|
FormatterMiddleware,
|
|
@@ -69,7 +69,7 @@ const formattersPlugin: TolgeePlugin = (tolgee, tools) => {
|
|
|
69
69
|
|
|
70
70
|
describe('plugins', () => {
|
|
71
71
|
it('wraps and formats translation', () => {
|
|
72
|
-
const tolgee =
|
|
72
|
+
const tolgee = TolgeeCore().init({
|
|
73
73
|
language: 'en',
|
|
74
74
|
staticData: { en: { hello: 'world' } },
|
|
75
75
|
});
|
|
@@ -83,7 +83,7 @@ describe('plugins', () => {
|
|
|
83
83
|
});
|
|
84
84
|
|
|
85
85
|
it("won't format when observer doesn't return formattable text", () => {
|
|
86
|
-
const tolgee =
|
|
86
|
+
const tolgee = TolgeeCore().init({
|
|
87
87
|
language: 'en',
|
|
88
88
|
staticData: { en: { hello: 'world' } },
|
|
89
89
|
});
|
|
@@ -97,7 +97,7 @@ describe('plugins', () => {
|
|
|
97
97
|
});
|
|
98
98
|
|
|
99
99
|
it("won't wrap before run", () => {
|
|
100
|
-
const tolgee =
|
|
100
|
+
const tolgee = TolgeeCore().init({
|
|
101
101
|
language: 'en',
|
|
102
102
|
staticData: { en: { hello: 'world' } },
|
|
103
103
|
});
|
package/src/helpers.ts
CHANGED
|
@@ -60,3 +60,11 @@ export function unique<T>(arr: T[]) {
|
|
|
60
60
|
export function sanitizeUrl(url: string | undefined) {
|
|
61
61
|
return url ? url.replace(/\/+$/, '') : url;
|
|
62
62
|
}
|
|
63
|
+
|
|
64
|
+
export function getErrorMessage(error: any): string | undefined {
|
|
65
|
+
if (typeof error === 'string') {
|
|
66
|
+
return error;
|
|
67
|
+
} else if (typeof error?.message === 'string') {
|
|
68
|
+
return error.message;
|
|
69
|
+
}
|
|
70
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { getFallback, getFallbackArray } from './helpers';
|
|
2
|
-
export {
|
|
2
|
+
export { TolgeeCore } from './TolgeeCore';
|
|
3
3
|
export * from './types';
|
|
4
4
|
export { getTranslateProps } from './TranslateParams';
|
|
5
5
|
export { FormatSimple } from './FormatSimple/FormatSimple';
|
package/src/types/events.ts
CHANGED
|
@@ -20,8 +20,8 @@ export type ListenerEvent<T> = { value: T };
|
|
|
20
20
|
export type Listener<T> = (e: ListenerEvent<T>) => void;
|
|
21
21
|
|
|
22
22
|
export type TolgeeEvent =
|
|
23
|
-
| 'pendingLanguage'
|
|
24
23
|
| 'language'
|
|
24
|
+
| 'pendingLanguage'
|
|
25
25
|
| 'loading'
|
|
26
26
|
| 'fetching'
|
|
27
27
|
| 'initialLoad'
|
|
@@ -30,8 +30,8 @@ export type TolgeeEvent =
|
|
|
30
30
|
| 'update';
|
|
31
31
|
|
|
32
32
|
export interface EventType {
|
|
33
|
-
pendingLanguage: string;
|
|
34
33
|
language: string;
|
|
34
|
+
pendingLanguage: string;
|
|
35
35
|
loading: boolean;
|
|
36
36
|
fetching: boolean;
|
|
37
37
|
initialLoad: void;
|
package/src/types/index.ts
CHANGED
package/src/types/plugin.ts
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
TranslatePropsInternal,
|
|
7
7
|
} from './general';
|
|
8
8
|
import type { ObserverOptionsInternal } from '../Controller/State/observerOptions';
|
|
9
|
-
import { TolgeeInstance } from '../
|
|
9
|
+
import { TolgeeInstance } from '../TolgeeCore';
|
|
10
10
|
|
|
11
11
|
export type BackendDevProps = {
|
|
12
12
|
apiUrl?: string;
|
|
@@ -145,24 +145,18 @@ export type UiKeyOption = {
|
|
|
145
145
|
translation: string | undefined;
|
|
146
146
|
};
|
|
147
147
|
|
|
148
|
-
export
|
|
148
|
+
export type UiMiddleware = (props: UiProps) => UiInterface;
|
|
149
|
+
|
|
150
|
+
export interface UiInterface {
|
|
149
151
|
handleElementClick(keysAndDefaults: UiKeyOption[], event: any): Promise<void>;
|
|
150
152
|
}
|
|
151
153
|
|
|
152
|
-
export type UiConstructor = new (props: UiProps) => UiMiddleware;
|
|
153
|
-
|
|
154
|
-
export type UiLibMiddleware = {
|
|
155
|
-
UI: UiConstructor;
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
export type UiType = UiConstructor | UiLibMiddleware;
|
|
159
|
-
|
|
160
154
|
export type PluginTools = Readonly<{
|
|
161
155
|
setFinalFormatter: (formatter: FinalFormatterMiddleware | undefined) => void;
|
|
162
156
|
addFormatter: (formatter: FormatterMiddleware | undefined) => void;
|
|
163
157
|
setObserver: (observer: ObserverMiddleware | undefined) => void;
|
|
164
158
|
hasObserver: () => boolean;
|
|
165
|
-
setUi: (ui:
|
|
159
|
+
setUi: (ui: UiMiddleware | undefined) => void;
|
|
166
160
|
hasUi: () => boolean;
|
|
167
161
|
addBackend: (backend: BackendMiddleware | undefined) => void;
|
|
168
162
|
setDevBackend: (backend: BackendDevMiddleware | undefined) => void;
|
|
@@ -172,10 +166,16 @@ export type PluginTools = Readonly<{
|
|
|
172
166
|
setLanguageStorage: (
|
|
173
167
|
languageStorage: LanguageStorageMiddleware | undefined
|
|
174
168
|
) => void;
|
|
175
|
-
onPrepare: (callback: () => void) => void;
|
|
176
169
|
}>;
|
|
177
170
|
|
|
178
171
|
export type TolgeePlugin = (
|
|
179
172
|
tolgee: TolgeeInstance,
|
|
180
173
|
tools: PluginTools
|
|
181
174
|
) => TolgeeInstance;
|
|
175
|
+
|
|
176
|
+
export type FormatErrorHandler = (
|
|
177
|
+
error: string,
|
|
178
|
+
info: TranslatePropsInternal
|
|
179
|
+
) => string;
|
|
180
|
+
|
|
181
|
+
export type OnFormatError = string | FormatErrorHandler;
|