-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Localize default messages #846
Changes from 4 commits
7c33356
1a02979
6563ec6
02571a1
f250154
c646dbb
c24173d
64f617d
5bce6c6
050bb27
b9d1be5
410c09b
b8f681c
800c388
3c54cbb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,7 @@ A [live playground](https://mozilla-services.github.io/react-jsonschema-form/) i | |
- [Custom error messages](#custom-error-messages) | ||
- [Error List Display](#error-list-display) | ||
- [The case of empty strings](#the-case-of-empty-strings) | ||
- [Localize default messages](#localize-default-messages) | ||
- [Styling your forms](#styling-your-forms) | ||
- [Schema definitions and references](#schema-definitions-and-references) | ||
- [Property dependencies](#property-dependencies) | ||
|
@@ -1589,6 +1590,21 @@ One consequence of this is that if you have an empty string in your `enum` array | |
|
||
If you want to have the field set to a default value when empty you can provide a `ui:emptyValue` field in the `uiSchema` object. | ||
|
||
## Localize default messages | ||
|
||
You can change localize to default messages errors. | ||
|
||
Here are some examples from the [playground](http://mozilla-services.github.io/react-jsonschema-form/). | ||
|
||
Here list of languages support [ajv-i18n](https://github.com/epoberezkin/ajv-i18n). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is the list of supported languages ajv-i18n. |
||
|
||
```jsx | ||
render(( | ||
<Form schema={schema} | ||
localize="es" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. localization="es" |
||
), document.getElementById("app")); | ||
``` | ||
|
||
## Styling your forms | ||
|
||
This library renders form fields and widgets leveraging the [Bootstrap](http://getbootstrap.com/) semantics. That means your forms will be beautiful by default if you're loading its stylesheet in your page. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module.exports = { | ||
schema: { | ||
title: "Contextualized localize", | ||
type: "object", | ||
properties: { | ||
active: { | ||
type: "boolean", | ||
title: "Active", | ||
}, | ||
}, | ||
}, | ||
formData: { | ||
active: "wrong", | ||
}, | ||
uiSchema: {}, | ||
localize: "es", | ||
}; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,5 +1,6 @@ | ||||||
import toPath from "lodash.topath"; | ||||||
import Ajv from "ajv"; | ||||||
import localize from "ajv-i18n"; | ||||||
const ajv = new Ajv({ | ||||||
errorDataPath: "property", | ||||||
allErrors: true, | ||||||
|
@@ -152,10 +153,12 @@ export default function validateFormData( | |||||
formData, | ||||||
schema, | ||||||
customValidate, | ||||||
transformErrors | ||||||
transformErrors, | ||||||
local | ||||||
) { | ||||||
try { | ||||||
ajv.validate(schema, formData); | ||||||
localize[local] && localize[local](ajv.errors); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All avji18n will be imported. Why, insted of take a "string" in props, taking a function, that can perform the localization ?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @miguelcast do you see how it would work here? Then to use the form, we would use the following:
|
||||||
} catch (e) { | ||||||
// swallow errors thrown in ajv due to invalid schemas, these | ||||||
// still get displayed | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can change localize to default messages errors.
=>
You can change the localization of the error messages.