Skip to content

Commit

Permalink
add errors parameter to translation function
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Boll lukas-bool@web.de
  • Loading branch information
LukasBoll committed May 23, 2022
1 parent 8e9c14d commit 3d47d04
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions packages/core/src/i18n/i18nUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,26 @@ export const defaultErrorTranslator: ErrorTranslator = (error, t, uischema) => {
getControlPath(error),
`error.${error.keyword}`
);
const specializedKeywordMessage = t(i18nKey, undefined);
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);
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);
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');
return t('is a required property', 'is a required property', { error });
}

return error.message;
Expand All @@ -94,7 +94,7 @@ export const getCombinedErrorMessage = (
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);
const specializedErrorMessage = t(customErrorKey, undefined, {schema, uischema, path, errors});
if (specializedErrorMessage !== undefined) {
return specializedErrorMessage;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/util/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,8 @@ export const mapStateToControlProps = (
const schema = resolvedSchema ?? rootSchema;
const t = getTranslator()(state);
const te = getErrorTranslator()(state);
const i18nLabel = t(getI18nKey(schema, uischema, path, 'label'), label);
const i18nDescription = t(getI18nKey(schema, uischema, path, 'description'), description);
const i18nLabel = t(getI18nKey(schema, uischema, path, 'label'), label, {schema, uischema, path, errors} );
const i18nDescription = t(getI18nKey(schema, uischema, path, 'description'), description, {schema, uischema, path, errors});
const i18nErrorMessage = getCombinedErrorMessage(errors, te, t, schema, uischema, path);

return {
Expand Down

0 comments on commit 3d47d04

Please sign in to comment.