/
i18nUtil.ts
193 lines (178 loc) · 5.58 KB
/
i18nUtil.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import type { ErrorObject } from 'ajv';
import { isInternationalized, Labelable, UISchemaElement } from '../models';
import { getControlPath } from '../reducers';
import { formatErrorMessage } from '../util';
import type { i18nJsonSchema, ErrorTranslator, Translator } from './i18nTypes';
import {
ArrayDefaultTranslation,
ArrayTranslations,
} from './arrayTranslations';
import {
CombinatorDefaultTranslation,
CombinatorTranslations,
} from './combinatorTranslations';
export const getI18nKeyPrefixBySchema = (
schema: i18nJsonSchema | undefined,
uischema: unknown | undefined
): string | undefined => {
if (isInternationalized(uischema)) {
return uischema.i18n;
}
return schema?.i18n ?? undefined;
};
/**
* Transforms a given path to a prefix which can be used for i18n keys.
* Returns 'root' for empty paths and removes array indices
*/
export const transformPathToI18nPrefix = (path: string): string => {
return (
path
?.split('.')
.filter((segment) => !/^\d+$/.test(segment))
.join('.') || 'root'
);
};
export const getI18nKeyPrefix = (
schema: i18nJsonSchema | undefined,
uischema: unknown | undefined,
path: string | undefined
): string => {
return (
getI18nKeyPrefixBySchema(schema, uischema) ??
transformPathToI18nPrefix(path)
);
};
export const getI18nKey = (
schema: i18nJsonSchema | undefined,
uischema: unknown | undefined,
path: string | undefined,
key: string
): string => {
return `${getI18nKeyPrefix(schema, uischema, path)}.${key}`;
};
export const addI18nKeyToPrefix = (
i18nKeyPrefix: string,
key: string
): string => {
return `${i18nKeyPrefix}.${key}`;
};
export const defaultTranslator: Translator = (
_id: string,
defaultMessage: string | undefined
) => defaultMessage;
export const defaultErrorTranslator: ErrorTranslator = (error, t, uischema) => {
// check whether there is a special keyword message
const i18nKey = getI18nKey(
error.parentSchema,
uischema,
getControlPath(error),
`error.${error.keyword}`
);
const specializedKeywordMessage = t(i18nKey, undefined, { error });
if (specializedKeywordMessage !== undefined) {
return specializedKeywordMessage;
}
// check whether there is a generic keyword message
const genericKeywordMessage = t(`error.${error.keyword}`, undefined, {
error,
});
if (genericKeywordMessage !== undefined) {
return genericKeywordMessage;
}
// check whether there is a customization for the default message
const messageCustomization = t(error.message, undefined, { error });
if (messageCustomization !== undefined) {
return messageCustomization;
}
// rewrite required property messages (if they were not customized) as we place them next to the respective input
if (
error.keyword === 'required' &&
error.message?.startsWith('must have required property')
) {
return t('is a required property', 'is a required property', { error });
}
return error.message;
};
/**
* Returns the determined error message for the given errors.
* All errors must correspond to the given schema, uischema or path.
*/
export const getCombinedErrorMessage = (
errors: ErrorObject[],
et: ErrorTranslator,
t: Translator,
schema?: i18nJsonSchema,
uischema?: UISchemaElement,
path?: string
) => {
if (errors.length > 0 && t) {
// check whether there is a special message which overwrites all others
const customErrorKey = getI18nKey(schema, uischema, path, 'error.custom');
const specializedErrorMessage = t(customErrorKey, undefined, {
schema,
uischema,
path,
errors,
});
if (specializedErrorMessage !== undefined) {
return specializedErrorMessage;
}
}
return formatErrorMessage(errors.map((error) => et(error, t, uischema)));
};
/**
* This can be used to internationalize the label of the given Labelable (e.g. UI Schema elements).
* This should not be used for controls as there we have additional context in the form of the JSON Schema available.
*/
export const deriveLabelForUISchemaElement = (
uischema: Labelable<boolean>,
t: Translator
): string | undefined => {
if (uischema.label === false) {
return undefined;
}
if (
(uischema.label === undefined ||
uischema.label === null ||
uischema.label === true) &&
!isInternationalized(uischema)
) {
return undefined;
}
const stringifiedLabel =
typeof uischema.label === 'string'
? uischema.label
: JSON.stringify(uischema.label);
const i18nKeyPrefix = getI18nKeyPrefixBySchema(undefined, uischema);
const i18nKey =
typeof i18nKeyPrefix === 'string'
? `${i18nKeyPrefix}.label`
: stringifiedLabel;
return t(i18nKey, stringifiedLabel, { uischema: uischema });
};
export const getArrayTranslations = (
t: Translator,
defaultTranslations: ArrayDefaultTranslation[],
i18nKeyPrefix: string,
label: string
): ArrayTranslations => {
const translations: ArrayTranslations = {};
defaultTranslations.forEach((controlElement) => {
const key = addI18nKeyToPrefix(i18nKeyPrefix, controlElement.key);
translations[controlElement.key] = t(key, controlElement.default(label));
});
return translations;
};
export const getCombinatorTranslations = (
t: Translator,
defaultTranslations: CombinatorDefaultTranslation[],
i18nKeyPrefix: string,
label: string
): CombinatorTranslations => {
const translations: CombinatorTranslations = {};
defaultTranslations.forEach((controlElement) => {
const key = addI18nKeyToPrefix(i18nKeyPrefix, controlElement.key);
translations[controlElement.key] = t(key, controlElement.default(label));
});
return translations;
};