2323 THE SOFTWARE.
2424*/
2525import * as React from 'react' ;
26+ import * as _ from 'lodash' ;
2627import { ExampleDescription , nestedArray as NestedArrayExample } from '@jsonforms/examples' ;
2728import 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' ;
2937import { AnyAction , Dispatch } from 'redux' ;
3038
3139export 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+
41107export 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 } ) ;
0 commit comments