piral-translate 0.15.0-alpha.3933 → 0.15.0-alpha.4027
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/package.json +3 -3
- package/src/actions.test.ts +129 -1
- package/src/create.test.ts +39 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "piral-translate",
|
|
3
|
-
"version": "0.15.0-alpha.
|
|
3
|
+
"version": "0.15.0-alpha.4027",
|
|
4
4
|
"description": "Plugin for providing translated messages in Piral.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"piral",
|
|
@@ -47,12 +47,12 @@
|
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@types/react": "^17.0.0",
|
|
50
|
-
"piral-core": "0.15.0-alpha.
|
|
50
|
+
"piral-core": "0.15.0-alpha.4027",
|
|
51
51
|
"react": "^17.0.1"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
54
|
"piral-core": "0.14.x",
|
|
55
55
|
"react": ">=16.8.0"
|
|
56
56
|
},
|
|
57
|
-
"gitHead": "
|
|
57
|
+
"gitHead": "324ff88850415188d0015efc509346705224a4dc"
|
|
58
58
|
}
|
package/src/actions.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Atom, deref } from '@dbeining/react-atom';
|
|
1
|
+
import { Atom, deref, swap } from '@dbeining/react-atom';
|
|
2
2
|
import { createListener } from 'piral-base';
|
|
3
3
|
import { createActions as ca } from 'piral-core';
|
|
4
4
|
import { createActions } from './actions';
|
|
@@ -36,4 +36,132 @@ describe('Translation Action Module', () => {
|
|
|
36
36
|
},
|
|
37
37
|
});
|
|
38
38
|
});
|
|
39
|
+
|
|
40
|
+
it('translate', () => {
|
|
41
|
+
const state = Atom.of({
|
|
42
|
+
foo: 5,
|
|
43
|
+
language: {
|
|
44
|
+
foo: 10,
|
|
45
|
+
loading: false,
|
|
46
|
+
selected: 'fr',
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
const localizer = {
|
|
50
|
+
language: 'fr',
|
|
51
|
+
languages: ['fr'],
|
|
52
|
+
messages: {
|
|
53
|
+
fr: {
|
|
54
|
+
bar: 'bár',
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
localizeGlobal(key, variables) {
|
|
58
|
+
const messages = {
|
|
59
|
+
fr: {
|
|
60
|
+
bar: 'bár',
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
return messages.fr[key];
|
|
64
|
+
},
|
|
65
|
+
localizeLocal() {
|
|
66
|
+
return '';
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
const actions = createActions(localizer);
|
|
70
|
+
const ctx = ca(state, createListener({}));
|
|
71
|
+
const result = actions.translate(ctx, 'bar');
|
|
72
|
+
expect(result).toEqual('bár');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('setTranslations sets translations to the global translations', () => {
|
|
76
|
+
const state = Atom.of({
|
|
77
|
+
foo: 5,
|
|
78
|
+
language: {
|
|
79
|
+
foo: 10,
|
|
80
|
+
loading: false,
|
|
81
|
+
selected: 'fr',
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
const localizer = {
|
|
85
|
+
language: 'de',
|
|
86
|
+
languages: ['de'],
|
|
87
|
+
messages: {
|
|
88
|
+
de: {},
|
|
89
|
+
},
|
|
90
|
+
localizeGlobal() {
|
|
91
|
+
return '';
|
|
92
|
+
},
|
|
93
|
+
localizeLocal() {
|
|
94
|
+
return '';
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
const ctx = {
|
|
98
|
+
emit: jest.fn(),
|
|
99
|
+
state,
|
|
100
|
+
dispatch(update) {
|
|
101
|
+
swap(state, update);
|
|
102
|
+
},
|
|
103
|
+
apis: {
|
|
104
|
+
firstApi: {
|
|
105
|
+
getTranslations: () => {
|
|
106
|
+
return localizer.messages;
|
|
107
|
+
},
|
|
108
|
+
setTranslations: async (translations) => {
|
|
109
|
+
localizer.messages = await translations;
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
const actions = createActions(localizer);
|
|
115
|
+
const data = {
|
|
116
|
+
global: {},
|
|
117
|
+
locals: [{ name: 'firstApi', value: { car: 'Auto', table: 'Tisch' } }],
|
|
118
|
+
};
|
|
119
|
+
actions.setTranslations(ctx, 'de', data);
|
|
120
|
+
expect(localizer.messages).toEqual({
|
|
121
|
+
de: { car: 'Auto', table: 'Tisch' },
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('getTranslations returns translations', () => {
|
|
126
|
+
const state = Atom.of({
|
|
127
|
+
foo: 5,
|
|
128
|
+
language: {
|
|
129
|
+
foo: 10,
|
|
130
|
+
loading: false,
|
|
131
|
+
selected: 'fr',
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
const localizer = {
|
|
135
|
+
language: 'fr',
|
|
136
|
+
languages: ['fr'],
|
|
137
|
+
messages: {
|
|
138
|
+
fr: {
|
|
139
|
+
foo: 'bár',
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
localizeGlobal() {
|
|
143
|
+
return '';
|
|
144
|
+
},
|
|
145
|
+
localizeLocal() {
|
|
146
|
+
return '';
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
const actions = createActions(localizer);
|
|
150
|
+
const ctx = {
|
|
151
|
+
emit: jest.fn(),
|
|
152
|
+
state,
|
|
153
|
+
dispatch(update) {
|
|
154
|
+
swap(state, update);
|
|
155
|
+
},
|
|
156
|
+
apis: {
|
|
157
|
+
firstApi: {
|
|
158
|
+
getTranslations: () => {
|
|
159
|
+
return localizer.messages;
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
const result = actions.getTranslations(ctx, 'fr');
|
|
165
|
+
expect(result).toEqual({ global: { foo: 'bár' }, locals: [{ name: 'firstApi', value: { foo: 'bár' } }] });
|
|
166
|
+
});
|
|
39
167
|
});
|
package/src/create.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Atom, swap } from '@dbeining/react-atom';
|
|
1
|
+
import { Atom, swap, deref } from '@dbeining/react-atom';
|
|
2
2
|
import { createLocaleApi, setupLocalizer } from './create';
|
|
3
3
|
|
|
4
4
|
describe('Create Localize API', () => {
|
|
@@ -6,6 +6,9 @@ describe('Create Localize API', () => {
|
|
|
6
6
|
const context: any = {
|
|
7
7
|
defineActions() {},
|
|
8
8
|
state,
|
|
9
|
+
readState(cb) {
|
|
10
|
+
return cb(deref(state));
|
|
11
|
+
},
|
|
9
12
|
dispatch(update) {
|
|
10
13
|
swap(state, update);
|
|
11
14
|
},
|
|
@@ -95,4 +98,39 @@ describe('Create Localize API', () => {
|
|
|
95
98
|
const result = api.translate('qxz');
|
|
96
99
|
expect(result).toEqual('__fr_qxz__');
|
|
97
100
|
});
|
|
101
|
+
|
|
102
|
+
it('getTranslations return the translations', () => {
|
|
103
|
+
const config = {
|
|
104
|
+
language: 'fr',
|
|
105
|
+
messages: {
|
|
106
|
+
fr: {
|
|
107
|
+
foo: 'bár',
|
|
108
|
+
bar: 'bár',
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
const api = (createLocaleApi(setupLocalizer(config))(context) as any)();
|
|
113
|
+
api.setTranslations({
|
|
114
|
+
fr: {
|
|
115
|
+
foo: 'boo',
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
const result = api.getTranslations();
|
|
119
|
+
expect(result).toEqual({ fr: { foo: 'boo' } });
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('getCurrentLanguage return the current language', () => {
|
|
123
|
+
const config = {
|
|
124
|
+
language: 'fr',
|
|
125
|
+
messages: {
|
|
126
|
+
fr: {
|
|
127
|
+
foo: 'bár',
|
|
128
|
+
bar: 'bár',
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
const api = (createLocaleApi(setupLocalizer(config))(context) as any)();
|
|
133
|
+
const result = api.getCurrentLanguage();
|
|
134
|
+
expect(result).toEqual('fr');
|
|
135
|
+
});
|
|
98
136
|
});
|