xref: /aosp_15_r20/external/json-schema-validator/doc/multiple-language.md (revision 78c4dd6aa35290980cdcd1623a7e337e8d021c7c)
1*78c4dd6aSAndroid Build Coastguard WorkerThe error messages have been translated to several languages by contributors, defined in the `jsv-messages.properties` resource
2*78c4dd6aSAndroid Build Coastguard Workerbundle under https://github.com/networknt/json-schema-validator/tree/master/src/main/resources. To use one of the
3*78c4dd6aSAndroid Build Coastguard Workeravailable translations the simplest approach is to set your default locale before running the validation:
4*78c4dd6aSAndroid Build Coastguard Worker
5*78c4dd6aSAndroid Build Coastguard Worker```java
6*78c4dd6aSAndroid Build Coastguard Worker// Set the default locale to German (needs only to be set once before using the validator)
7*78c4dd6aSAndroid Build Coastguard WorkerLocale.setDefault(Locale.GERMAN);
8*78c4dd6aSAndroid Build Coastguard WorkerJsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
9*78c4dd6aSAndroid Build Coastguard WorkerJsonSchema schema = factory.getSchema(source);
10*78c4dd6aSAndroid Build Coastguard Worker...
11*78c4dd6aSAndroid Build Coastguard Worker```
12*78c4dd6aSAndroid Build Coastguard Worker
13*78c4dd6aSAndroid Build Coastguard WorkerNote that the above approach changes the locale for the entire JVM which is probably not what you want to do if you are
14*78c4dd6aSAndroid Build Coastguard Workerusing this in an application expected to support multiple languages (for example a localised web application). In this
15*78c4dd6aSAndroid Build Coastguard Workercase you should use the `SchemaValidatorsConfig` class before loading your schema:
16*78c4dd6aSAndroid Build Coastguard Worker
17*78c4dd6aSAndroid Build Coastguard Worker```java
18*78c4dd6aSAndroid Build Coastguard Worker// Set the configuration with a specific locale (you can create this before each validation)
19*78c4dd6aSAndroid Build Coastguard WorkerSchemaValidatorsConfig config = new SchemaValidatorsConfig();
20*78c4dd6aSAndroid Build Coastguard Workerconfig.setLocale(myLocale);
21*78c4dd6aSAndroid Build Coastguard WorkerJsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
22*78c4dd6aSAndroid Build Coastguard WorkerJsonSchema schema = factory.getSchema(source, config);
23*78c4dd6aSAndroid Build Coastguard Worker...
24*78c4dd6aSAndroid Build Coastguard Worker```
25*78c4dd6aSAndroid Build Coastguard Worker
26*78c4dd6aSAndroid Build Coastguard WorkerBesides setting the locale and using the default resource bundle, you may also specify your own to cover any languages you
27*78c4dd6aSAndroid Build Coastguard Workerchoose 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*78c4dd6aSAndroid Build Coastguard Worker
29*78c4dd6aSAndroid Build Coastguard Worker```java
30*78c4dd6aSAndroid Build Coastguard Worker// Set the configuration with a custom message source
31*78c4dd6aSAndroid Build Coastguard WorkerMessageSource messageSource = new ResourceBundleMessageSource("my-messages");
32*78c4dd6aSAndroid Build Coastguard WorkerSchemaValidatorsConfig config = new SchemaValidatorsConfig();
33*78c4dd6aSAndroid Build Coastguard Workerconfig.setMessageSource(messageSource);
34*78c4dd6aSAndroid Build Coastguard WorkerJsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
35*78c4dd6aSAndroid Build Coastguard WorkerJsonSchema schema = factory.getSchema(source, config);
36*78c4dd6aSAndroid Build Coastguard Worker...
37*78c4dd6aSAndroid Build Coastguard Worker```
38*78c4dd6aSAndroid Build Coastguard Worker
39*78c4dd6aSAndroid Build Coastguard WorkerIt 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*78c4dd6aSAndroid Build Coastguard Worker
41*78c4dd6aSAndroid Build Coastguard Worker```java
42*78c4dd6aSAndroid Build Coastguard Worker// Set the configuration with a custom message source
43*78c4dd6aSAndroid Build Coastguard WorkerMessageSource messageSource = new ResourceBundleMessageSource("jsv-messages-override", DefaultMessageSource.BUNDLE_BASE_NAME);
44*78c4dd6aSAndroid Build Coastguard WorkerSchemaValidatorsConfig config = new SchemaValidatorsConfig();
45*78c4dd6aSAndroid Build Coastguard Workerconfig.setMessageSource(messageSource);
46*78c4dd6aSAndroid Build Coastguard WorkerJsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
47*78c4dd6aSAndroid Build Coastguard WorkerJsonSchema schema = factory.getSchema(source, config);
48*78c4dd6aSAndroid Build Coastguard Worker...
49*78c4dd6aSAndroid Build Coastguard Worker```
50*78c4dd6aSAndroid Build Coastguard Worker
51*78c4dd6aSAndroid Build Coastguard WorkerThe following approach can be used to determine the locale to use on a per user basis using a language tag priority list.
52*78c4dd6aSAndroid Build Coastguard Worker
53*78c4dd6aSAndroid Build Coastguard Worker```java
54*78c4dd6aSAndroid Build Coastguard WorkerSchemaValidatorsConfig config = new SchemaValidatorsConfig();
55*78c4dd6aSAndroid Build Coastguard WorkerJsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
56*78c4dd6aSAndroid Build Coastguard WorkerJsonSchema schema = factory.getSchema(source, config);
57*78c4dd6aSAndroid Build Coastguard Worker
58*78c4dd6aSAndroid Build Coastguard Worker// Uses the fr locale for this user
59*78c4dd6aSAndroid Build Coastguard WorkerLocale locale = Locales.findSupported("it;q=0.9,fr;q=1.0");
60*78c4dd6aSAndroid Build Coastguard WorkerExecutionContext executionContext = jsonSchema.createExecutionContext();
61*78c4dd6aSAndroid Build Coastguard WorkerexecutionContext.getExecutionConfig().setLocale(locale);
62*78c4dd6aSAndroid Build Coastguard WorkerSet<ValidationMessage> messages = jsonSchema.validate(executionContext, rootNode);
63*78c4dd6aSAndroid Build Coastguard Worker
64*78c4dd6aSAndroid Build Coastguard Worker// Uses the it locale for this user
65*78c4dd6aSAndroid Build Coastguard Workerlocale = Locales.findSupported("it;q=1.0,fr;q=0.9");
66*78c4dd6aSAndroid Build Coastguard WorkerexecutionContext = jsonSchema.createExecutionContext();
67*78c4dd6aSAndroid Build Coastguard WorkerexecutionContext.getExecutionConfig().setLocale(locale);
68*78c4dd6aSAndroid Build Coastguard Workermessages = jsonSchema.validate(executionContext, rootNode);
69*78c4dd6aSAndroid Build Coastguard Worker...
70*78c4dd6aSAndroid Build Coastguard Worker```
71