Skip to content

Commit 0d11318

Browse files
Apply suggestions from code review
Co-authored-by: Lucas Koehler <[email protected]>
1 parent d0f5c29 commit 0d11318

File tree

11 files changed

+145
-34
lines changed

11 files changed

+145
-34
lines changed
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export interface DefaultTranslation {
1+
export interface ArrayDefaultTranslation {
22
key: ArrayTranslationEnum,
33
default: (variable?: string) => string
44
}
@@ -12,8 +12,8 @@ export enum ArrayTranslationEnum{
1212
noSelection = 'noSelection',
1313
removeAriaLabel = 'removeAriaLabel',
1414
noDataMessage = 'noDataMessage',
15-
deleteDialogHeader = 'deleteDialogHeader',
16-
deleteDialogMsg = 'deleteDialogMsg',
15+
deleteDialogTitle = 'deleteDialogTitle',
16+
deleteDialogMessage = 'deleteDialogMessage',
1717
deleteDialogAccept = 'deleteDialogAccept',
1818
deleteDialogDecline = 'deleteDialogDecline'
1919
}
@@ -22,7 +22,7 @@ export type ArrayTranslations = {
2222
[key in ArrayTranslationEnum]?: string
2323
}
2424

25-
export const arrayDefaultTranslations: DefaultTranslation[] = [
25+
export const arrayDefaultTranslations: ArrayDefaultTranslation[] = [
2626
{key: ArrayTranslationEnum.addTooltip, default: (input) => input?`Add to ${input}`:'Add'},
2727
{key: ArrayTranslationEnum.addAriaLabel, default: (input) => input?`Add to ${input}`:'Add'},
2828
{key: ArrayTranslationEnum.removeTooltip, default: () => 'Delete'},
@@ -31,10 +31,8 @@ export const arrayDefaultTranslations: DefaultTranslation[] = [
3131
{key: ArrayTranslationEnum.removeAriaLabel, default: () => 'Delete button'},
3232
{key: ArrayTranslationEnum.noDataMessage, default: () => 'No Data'},
3333
{key: ArrayTranslationEnum.noSelection, default: () => 'No Selection'},
34-
{key: ArrayTranslationEnum.deleteDialogHeader, default: () => 'Confirm Deletion'},
35-
{key: ArrayTranslationEnum.deleteDialogMsg, default: () => 'Are you sure you want to delete the selected entry?'},
36-
{key: ArrayTranslationEnum.deleteDialogAccept, default: () => 'YES'},
37-
{key: ArrayTranslationEnum.deleteDialogDecline, default: () => 'NO'}
38-
]
39-
40-
export const arrayTranslations: DefaultTranslation[] = Object.values(arrayDefaultTranslations).map(value=>value);
34+
{key: ArrayTranslationEnum.deleteDialogTitle, default: () => 'Confirm Deletion'},
35+
{key: ArrayTranslationEnum.deleteDialogMessage, default: () => 'Are you sure you want to delete the selected entry?'},
36+
{key: ArrayTranslationEnum.deleteDialogAccept, default: () => 'Yes'},
37+
{key: ArrayTranslationEnum.deleteDialogDecline, default: () => 'No'}
38+
]

packages/core/src/i18n/i18nUtil.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { isInternationalized, Labelable, UISchemaElement } from '../models';
33
import { getControlPath } from '../reducers';
44
import { formatErrorMessage } from '../util';
55
import type { i18nJsonSchema, ErrorTranslator, Translator } from './i18nTypes';
6-
import { DefaultTranslation, ArrayTranslations } from './arrayTranslations'
6+
import { ArrayDefaultTranslation, ArrayTranslations } from './arrayTranslations'
77

88
export const getI18nKeyPrefixBySchema = (
99
schema: i18nJsonSchema | undefined,
@@ -49,10 +49,10 @@ export const getI18nKey = (
4949
};
5050

5151
export const addI18nKeyToPrefix = (
52-
I18nKeyPrefix: string,
52+
i18nKeyPrefix: string,
5353
key: string
5454
): string => {
55-
return `${I18nKeyPrefix}.${key}`;
55+
return `${i18nKeyPrefix}.${key}`;
5656
};
5757

5858
export const defaultTranslator: Translator = (_id: string, defaultMessage: string | undefined) => defaultMessage;
@@ -132,18 +132,16 @@ export const deriveLabelForUISchemaElement = (uischema: Labelable<boolean>, t: T
132132
return t(i18nKey, stringifiedLabel, { uischema: uischema });
133133
}
134134

135-
export const translateElements = (
135+
export const getArrayTranslations = (
136136
t: Translator,
137-
controlElements: DefaultTranslation [],
137+
defaultTranslations: ArrayDefaultTranslation [],
138138
i18nKeyPrefix: string,
139139
label: string
140140
): ArrayTranslations => {
141141
const translations:ArrayTranslations = {};
142-
console.log("translating...");
143-
controlElements.forEach((controlElement)=>{
142+
defaultTranslations.forEach((controlElement)=>{
144143
const key = addI18nKeyToPrefix(i18nKeyPrefix, controlElement.key);
145144
translations[controlElement.key]=t(key, controlElement.default(label));
146-
console.log("translations: ", translations);
147145
})
148146
return translations;
149147
}

packages/core/src/i18n/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './i18nTypes';
22
export * from './i18nUtil';
3+
export * from './arrayTranslations'

packages/core/src/util/renderer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ import { composePaths, composeWithUi } from './path';
5656
import { CoreActions, update } from '../actions';
5757
import type { ErrorObject } from 'ajv';
5858
import type { JsonFormsState } from '../store';
59-
import { deriveLabelForUISchemaElement, getCombinedErrorMessage, getI18nKey, getI18nKeyPrefix, getI18nKeyPrefixBySchema, translateElements, Translator } from '../i18n';
59+
import { deriveLabelForUISchemaElement, getCombinedErrorMessage, getI18nKey, getI18nKeyPrefix, getI18nKeyPrefixBySchema, getArrayTranslations, Translator } from '../i18n';
6060
import { arrayDefaultTranslations, ArrayTranslations } from '../i18n/arrayTranslations';
6161

6262
const isRequired = (
@@ -1017,8 +1017,8 @@ export const mapStateToOneOfProps = (
10171017

10181018
export interface StatePropsOfArrayLayout extends StatePropsOfControlWithDetail {
10191019
data: number;
1020+
translations: ArrayTranslations;
10201021
minItems?: number;
1021-
translations?: ArrayTranslations;
10221022
}
10231023
/**
10241024
* Map state to table props
@@ -1067,7 +1067,7 @@ export const mapStateToArrayLayoutProps = (
10671067
data: props.data ? props.data.length : 0,
10681068
errors: allErrors,
10691069
minItems: schema.minItems,
1070-
translations: translateElements(t,arrayDefaultTranslations,i18nKeyPrefix, label)
1070+
translations: getArrayTranslations(t,arrayDefaultTranslations,i18nKeyPrefix, label)
10711071
};
10721072
};
10731073

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
The MIT License
3+
4+
Copyright (c) 2017-2019 EclipseSource Munich
5+
https://github.com/eclipsesource/jsonforms
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
*/
25+
import { registerExamples } from '../register';
26+
import { ArrayTranslationEnum, Translator } from '@jsonforms/core';
27+
import { get } from 'lodash';
28+
29+
export const schema = {
30+
type: 'object',
31+
properties: {
32+
comments: {
33+
type: 'array',
34+
items: {
35+
type: 'object',
36+
properties: {
37+
date: {
38+
type: 'string',
39+
format: 'date'
40+
},
41+
message: {
42+
type: 'string',
43+
maxLength: 5
44+
},
45+
enum: {
46+
type: 'string',
47+
const: 'foo'
48+
}
49+
}
50+
}
51+
},
52+
foo:{type:'string'}
53+
}
54+
};
55+
56+
export const uischema = {
57+
type: 'VerticalLayout',
58+
elements: [
59+
{
60+
type: 'Control',
61+
scope: '#/properties/foo'
62+
},
63+
{
64+
type: 'Control',
65+
scope: '#/properties/comments',
66+
options: {
67+
showSortButtons: true,
68+
}
69+
}
70+
]
71+
};
72+
73+
export const data = {
74+
comments: [
75+
{
76+
date: new Date(2001, 8, 11).toISOString().substr(0, 10),
77+
message: 'This is an example message'
78+
},
79+
{
80+
date: new Date().toISOString().substr(0, 10),
81+
message: 'Get ready for booohay'
82+
}
83+
]
84+
};
85+
86+
export const translations = {
87+
comments: {
88+
[ArrayTranslationEnum.noDataMessage]: 'Be the first to write a comment',
89+
[ArrayTranslationEnum.addTooltip]: 'Add a Comment',
90+
[ArrayTranslationEnum.deleteDialogAccept]: 'Delete!',
91+
[ArrayTranslationEnum.deleteDialogDecline]: 'Cancel!',
92+
[ArrayTranslationEnum.deleteDialogMessage]: 'Are you sure you want to delete this comment?'
93+
}
94+
};
95+
export const translate: Translator = (key: string, defaultMessage: string) => {
96+
console.log(key);
97+
return get(translations, key) ?? defaultMessage;
98+
};
99+
100+
registerExamples([
101+
{
102+
name: 'array (i18n)',
103+
label: 'Array (i18n)',
104+
data,
105+
schema,
106+
uischema,
107+
i18n: {
108+
translate: translate,
109+
locale: 'en'
110+
}
111+
}
112+
]);

packages/examples/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import * as oneOf from './examples/oneOf';
2828
import * as oneOfArray from './examples/oneOfArray';
2929
import * as anyOfOneOfAllOfResolve from './examples/anyOf-oneOf-allOf-resolve';
3030
import * as array from './examples/arrays';
31+
import * as arrayI18n from './examples/arraysI18n';
3132
import * as nestedArray from './examples/nestedArrays';
3233
import * as arrayWithDetail from './examples/arrays-with-detail';
3334
import * as arrayWithDetailAndRule from './examples/arrays-with-detail-and-rule';
@@ -97,6 +98,7 @@ export {
9798
anyOfOneOfAllOfResolve,
9899
stringArray,
99100
array,
101+
arrayI18n,
100102
nestedArray,
101103
arrayWithDetail,
102104
arrayWithDetailAndRule,

packages/material-renderers/src/complex/DeleteDialog.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export interface DeleteDialogProps {
3737
onClose(): void;
3838
onConfirm(): void;
3939
onCancel(): void;
40-
headerText: string,
41-
msg: string,
40+
title: string,
41+
message: string,
4242
acceptText: string,
4343
declineText: string
4444
}
@@ -47,7 +47,7 @@ export interface WithDeleteDialogSupport {
4747
openDeleteDialog(path: string, data: number): void;
4848
}
4949

50-
export const DeleteDialog = React.memo(({ open, onClose, onConfirm, onCancel, headerText: header, msg, acceptText: accept, declineText: decline }: DeleteDialogProps) => {
50+
export const DeleteDialog = React.memo(({ open, onClose, onConfirm, onCancel, title, message, acceptText, declineText }: DeleteDialogProps) => {
5151
return (
5252
<Dialog
5353
open={open}
@@ -57,19 +57,19 @@ export const DeleteDialog = React.memo(({ open, onClose, onConfirm, onCancel, he
5757
aria-describedby='alert-dialog-confirmdelete-description'
5858
>
5959
<DialogTitle id='alert-dialog-confirmdelete-title'>
60-
{header}
60+
{title}
6161
</DialogTitle>
6262
<DialogContent>
6363
<DialogContentText id='alert-dialog-confirmdelete-description'>
64-
{msg}
64+
{message}
6565
</DialogContentText>
6666
</DialogContent>
6767
<DialogActions>
6868
<Button onClick={onCancel} color='primary'>
69-
{decline}
69+
{declineText}
7070
</Button>
7171
<Button onClick={onConfirm} color='primary'>
72-
{accept}
72+
{acceptText}
7373
</Button>
7474
</DialogActions>
7575
</Dialog>

packages/material-renderers/src/complex/MaterialArrayControlRenderer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ export const MaterialArrayControlRenderer = (props: ArrayLayoutProps) => {
6161
onClose={deleteClose}
6262
acceptText={props.translations.deleteDialogAccept}
6363
declineText={props.translations.deleteDialogDecline}
64-
headerText={props.translations.deleteDialogHeader}
65-
msg={props.translations.deleteDialogMsg}
64+
title={props.translations.deleteDialogTitle}
65+
message={props.translations.deleteDialogMessage}
6666
/>
6767
</Hidden>
6868
);

packages/material-renderers/src/complex/MaterialTableControl.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ import NoBorderTableCell from './NoBorderTableCell';
6565
import TableToolbar from './TableToolbar';
6666
import { ErrorObject } from 'ajv';
6767
import merge from 'lodash/merge';
68-
import { ArrayTranslations } from '@jsonforms/core/lib/i18n/arrayTranslations';
68+
import { ArrayTranslations } from '@jsonforms/core';
6969

7070
// we want a cell that doesn't automatically span
7171
const styles = {

packages/material-renderers/src/complex/TableToolbar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { Grid, Typography } from '@mui/material';
3737
import AddIcon from '@mui/icons-material/Add';
3838
import ValidationIcon from './ValidationIcon';
3939
import NoBorderTableCell from './NoBorderTableCell';
40-
import { ArrayTranslations } from '@jsonforms/core/lib/i18n/arrayTranslations';
40+
import { ArrayTranslations } from '@jsonforms/core';
4141

4242
export interface MaterialTableToolbarProps {
4343
numColumns: number;

0 commit comments

Comments
 (0)