Skip to content

Commit 91a681c

Browse files
committed
[examples] Add i18n example
1 parent 8b84efc commit 91a681c

File tree

3 files changed

+133
-2
lines changed

3 files changed

+133
-2
lines changed

packages/example/src/util.tsx

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,17 @@
2323
THE SOFTWARE.
2424
*/
2525
import * as React from 'react';
26+
import * as _ from 'lodash';
2627
import { ExampleDescription, nestedArray as NestedArrayExample } from '@jsonforms/examples';
2728
import ConnectedRatingControl, { ratingControlTester } from './RatingControl';
28-
import { Actions } from '@jsonforms/core';
29+
import {
30+
Actions,
31+
JsonSchema,
32+
setLocale,
33+
setSchema,
34+
setUISchema,
35+
UISchemaElement
36+
} from '@jsonforms/core';
2937
import { AnyAction, Dispatch } from 'redux';
3038

3139
export interface ReactExampleDescription extends ExampleDescription {
@@ -38,6 +46,64 @@ const unregisterRatingControl = (dispatch: Dispatch<AnyAction>) => {
3846
dispatch(Actions.unregisterField(ratingControlTester, ConnectedRatingControl));
3947
};
4048

49+
export interface I18nExampleProps {
50+
schema: JsonSchema;
51+
uischema: UISchemaElement;
52+
dispatch: Dispatch<AnyAction>;
53+
}
54+
55+
class I18nExample extends React.Component<I18nExampleProps, {
56+
localizedSchemas: Map<string, JsonSchema>,
57+
localizedUISchemas: Map<string, UISchemaElement>
58+
}> {
59+
60+
constructor(props: I18nExampleProps) {
61+
super(props);
62+
const { schema, uischema } = props;
63+
const localizedSchemas = new Map<string, JsonSchema>();
64+
const deSchema = _.cloneDeep(schema);
65+
_.set(deSchema, 'properties.name.description', 'Name der Person');
66+
_.set(deSchema, 'properties.name.birthDate', 'Geburtstag der Person');
67+
localizedSchemas.set('de-DE', deSchema);
68+
localizedSchemas.set('en-US', schema);
69+
70+
const localizedUISchemas = new Map<string, UISchemaElement>();
71+
const deUISchema = _.cloneDeep(uischema);
72+
_.set(deUISchema, 'elements.0.elements.1.label', 'Geburtstag');
73+
_.set(deUISchema, 'elements.2.elements.0.label', 'Nationalität');
74+
_.set(deUISchema, 'elements.2.elements.1.label', 'Vegetarier');
75+
localizedUISchemas.set('de-DE', deUISchema);
76+
localizedUISchemas.set('en-US', uischema);
77+
78+
this.state = {
79+
localizedSchemas,
80+
localizedUISchemas
81+
};
82+
}
83+
84+
85+
changeLocale = (locale: string) => {
86+
const { dispatch } = this.props;
87+
const { localizedSchemas, localizedUISchemas } = this.state;
88+
dispatch(setLocale(locale));
89+
dispatch(setSchema(localizedSchemas.get(locale)));
90+
dispatch(setUISchema(localizedUISchemas.get(locale)));
91+
}
92+
93+
render() {
94+
return (
95+
<div>
96+
<button onClick={() => this.changeLocale('en-US')}>
97+
en-US
98+
</button>
99+
<button onClick={() => this.changeLocale('de-DE')}>
100+
de-DE
101+
</button>
102+
</div>
103+
);
104+
}
105+
}
106+
41107
export const enhanceExample: (examples: ExampleDescription[]) => ReactExampleDescription[] =
42108
examples => examples.map(e => {
43109
switch (e.name) {
@@ -90,6 +156,12 @@ export const enhanceExample: (examples: ExampleDescription[]) => ReactExampleDes
90156
)
91157
});
92158
return dynamic;
159+
case 'i18n':
160+
return Object.assign({}, e, {
161+
customReactExtension: (dispatch: Dispatch<AnyAction>) => (
162+
<I18nExample schema={e.schema} uischema={e.uischema} dispatch={dispatch}/>
163+
)
164+
});
93165
default: return e;
94166
}
95167
});

packages/examples/src/i18n.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { registerExamples } from './register';
2+
import { personCoreSchema } from './person';
3+
4+
export const uischema = {
5+
type: 'VerticalLayout',
6+
elements: [
7+
{
8+
type: 'HorizontalLayout',
9+
elements: [
10+
{
11+
type: 'Control',
12+
scope: '#/properties/name'
13+
},
14+
{
15+
type: 'Control',
16+
scope: '#/properties/birthDate'
17+
},
18+
]
19+
},
20+
{
21+
type: 'Label',
22+
text: 'Additional Information'
23+
},
24+
{
25+
type: 'HorizontalLayout',
26+
elements: [
27+
{
28+
type: 'Control',
29+
scope: '#/properties/nationality'
30+
},
31+
{
32+
type: 'Control',
33+
scope: '#/properties/vegetarian'
34+
}
35+
]
36+
}
37+
]
38+
};
39+
40+
export const data = {
41+
vegetarian: false,
42+
birthDate: '1985-06-02',
43+
personalData: {
44+
age: 34
45+
},
46+
postalCode: '12345',
47+
};
48+
49+
registerExamples([
50+
{
51+
name: 'i18n',
52+
label: 'Person (i18n)',
53+
data,
54+
schema: personCoreSchema,
55+
uischema
56+
}
57+
]);

packages/examples/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import * as config from './config';
4747
import * as text from './text';
4848
import * as numbers from './numbers';
4949
import * as listWithDetail from './list-with-detail';
50+
import * as i18n from './i18n';
5051
export * from './register';
5152
export * from './example';
5253
export {
@@ -74,5 +75,6 @@ export {
7475
config,
7576
text,
7677
numbers,
77-
listWithDetail
78+
listWithDetail,
79+
i18n
7880
};

0 commit comments

Comments
 (0)