xref: /aosp_15_r20/external/json-schema-validator/doc/multiple-language.md (revision 78c4dd6aa35290980cdcd1623a7e337e8d021c7c)
1The error messages have been translated to several languages by contributors, defined in the `jsv-messages.properties` resource
2bundle under https://github.com/networknt/json-schema-validator/tree/master/src/main/resources. To use one of the
3available translations the simplest approach is to set your default locale before running the validation:
4
5```java
6// Set the default locale to German (needs only to be set once before using the validator)
7Locale.setDefault(Locale.GERMAN);
8JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
9JsonSchema schema = factory.getSchema(source);
10...
11```
12
13Note that the above approach changes the locale for the entire JVM which is probably not what you want to do if you are
14using this in an application expected to support multiple languages (for example a localised web application). In this
15case you should use the `SchemaValidatorsConfig` class before loading your schema:
16
17```java
18// Set the configuration with a specific locale (you can create this before each validation)
19SchemaValidatorsConfig config = new SchemaValidatorsConfig();
20config.setLocale(myLocale);
21JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
22JsonSchema schema = factory.getSchema(source, config);
23...
24```
25
26Besides setting the locale and using the default resource bundle, you may also specify your own to cover any languages you
27choose without adapting the library's source, or to override default messages. In doing so you however you should ensure that your resource bundle covers all the keys defined by the default bundle.
28
29```java
30// Set the configuration with a custom message source
31MessageSource messageSource = new ResourceBundleMessageSource("my-messages");
32SchemaValidatorsConfig config = new SchemaValidatorsConfig();
33config.setMessageSource(messageSource);
34JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
35JsonSchema schema = factory.getSchema(source, config);
36...
37```
38
39It is possible to override specific keys from the default resource bundle. Note however that you will need to supply all the languages for that specific key as it will not fallback on the default resource bundle. For instance the jsv-messages-override resource bundle will take precedence when resolving the message key.
40
41```java
42// Set the configuration with a custom message source
43MessageSource messageSource = new ResourceBundleMessageSource("jsv-messages-override", DefaultMessageSource.BUNDLE_BASE_NAME);
44SchemaValidatorsConfig config = new SchemaValidatorsConfig();
45config.setMessageSource(messageSource);
46JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
47JsonSchema schema = factory.getSchema(source, config);
48...
49```
50
51The following approach can be used to determine the locale to use on a per user basis using a language tag priority list.
52
53```java
54SchemaValidatorsConfig config = new SchemaValidatorsConfig();
55JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
56JsonSchema schema = factory.getSchema(source, config);
57
58// Uses the fr locale for this user
59Locale locale = Locales.findSupported("it;q=0.9,fr;q=1.0");
60ExecutionContext executionContext = jsonSchema.createExecutionContext();
61executionContext.getExecutionConfig().setLocale(locale);
62Set<ValidationMessage> messages = jsonSchema.validate(executionContext, rootNode);
63
64// Uses the it locale for this user
65locale = Locales.findSupported("it;q=1.0,fr;q=0.9");
66executionContext = jsonSchema.createExecutionContext();
67executionContext.getExecutionConfig().setLocale(locale);
68messages = jsonSchema.validate(executionContext, rootNode);
69...
70```
71