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

Ajv i18n example #1457

Merged
merged 2 commits into from
Aug 16, 2019
Merged

Ajv i18n example #1457

merged 2 commits into from
Aug 16, 2019

Conversation

Lily418
Copy link
Contributor

@Lily418 Lily418 commented Jul 24, 2019

This PR is to add an example of using the onChange handler and error update action (PR #1450) to show how a user of the library can add localisation of the errors with i18n.

4c5a9fe is the new commit, others will be rebased away when #1450 is complete.

ajv-i18n in core

I had initally been adding ajv-i18n to the core package, however this became complex when trying to combine navigator languages, selected locale from i18n reducer, and languages supported by ajv-i18n.
There are a few cases to consider

  • Schemas are provided in languages which are not supported by ajv-i18n
  • Schemas are not provided for a language in the users navigator.language but which is supported by ajv-i18n

I think it is better for the user of JsonForms to implement this logic so they can implement i18n logic consistantly rather than enforcing a specific method on the user.

It may be possible for the user to provide their own language selection logic to JSONForms but I decided to try to keep it simple for now.

@Lily418 Lily418 mentioned this pull request Jul 24, 2019
@edgarmueller
Copy link
Contributor

edgarmueller commented Jul 24, 2019

Sounds like a new example! #1450 has been merged, you can rebase now.

@coveralls
Copy link

coveralls commented Jul 26, 2019

Coverage Status

Coverage remained the same at 83.217% when pulling 1254bb4 on HelixCentre:feature/ajv-i18n-example into 927e387 on eclipsesource:master.

Copy link
Contributor

@edgarmueller edgarmueller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@edgarmueller edgarmueller merged commit c5eeffc into eclipsesource:master Aug 16, 2019
edgarmueller pushed a commit to edgarmueller/jsonforms that referenced this pull request Aug 23, 2019
Extend i18n example to demonstrate usage of ajv-i18n
@LaszloDev
Copy link

LaszloDev commented Dec 6, 2019

Hey, I'm really appreciate your hard work on the package @Lily418 @edgarmueller . Would you mind sharing some more details regards how to implement the localization. Which packages are needed and how are they integrated into a JSONForms instance?

@Lily418
Copy link
Contributor Author

Lily418 commented Dec 6, 2019

Hi @LaszloDev ajv-i18n is the additional package.

The JsonForms component has an onChange prop which is called when either data or errors is changed.

This is the onChange I used in the example I here in this PR.

const localize = require('ajv-i18n');

export const onChange = (dispatch: Dispatch<AnyAction>) => (
  extensionState: any
) => ({ errors }: Pick<JsonFormsCore, 'data' | 'errors'>) => {
  if (!extensionState) {
    return;
  }
  const localiseFunc = localize[extensionState.locale.split('-')[0]];
  localiseFunc(errors);
  dispatch(updateErrors(errors));
};

JsonForms uses AJV to validate the forms data against the schema so this library ajv-i18n can be used because the errors you receive in the onChange method are directly from AJV.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants