Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add errors parameter to translation function #1939

Merged
merged 1 commit into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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