Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HV-1748 HV-1749 Add some documentation about the localization support
- Loading branch information
Showing
2 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
.../java/org/hibernate/validator/referenceguide/chapter12/localization/LocalizationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.hibernate.validator.referenceguide.chapter12.localization; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Locale.LanguageRange; | ||
import java.util.Set; | ||
|
||
import javax.validation.ConstraintViolation; | ||
import javax.validation.Validation; | ||
import javax.validation.Validator; | ||
import javax.validation.constraints.AssertTrue; | ||
|
||
import org.hibernate.validator.HibernateValidator; | ||
import org.hibernate.validator.spi.messageinterpolation.LocaleResolver; | ||
import org.hibernate.validator.spi.messageinterpolation.LocaleResolverContext; | ||
import org.junit.Test; | ||
|
||
public class LocalizationTest { | ||
|
||
@Test | ||
public void changeDefaultLocale() { | ||
// tag::default-locale[] | ||
Validator validator = Validation.byProvider( HibernateValidator.class ) | ||
.configure() | ||
.defaultLocale( Locale.FRANCE ) | ||
.buildValidatorFactory() | ||
.getValidator(); | ||
|
||
Set<ConstraintViolation<Bean>> violations = validator.validate( new Bean() ); | ||
assertEquals( "doit avoir la valeur vrai", violations.iterator().next().getMessage() ); | ||
// end::default-locale[] | ||
} | ||
|
||
@Test | ||
public void localeResolver() { | ||
// tag::locale-resolver[] | ||
LocaleResolver localeResolver = new LocaleResolver() { | ||
|
||
@Override | ||
public Locale resolve(LocaleResolverContext context) { | ||
// get the locales supported by the client from the Accept-Language header | ||
String acceptLanguageHeader = "it-IT;q=0.9,en-US;q=0.7"; | ||
|
||
List<LanguageRange> acceptedLanguages = LanguageRange.parse( acceptLanguageHeader ); | ||
List<Locale> resolvedLocales = Locale.filter( acceptedLanguages, context.getSupportedLocales() ); | ||
|
||
if ( resolvedLocales.size() > 0 ) { | ||
return resolvedLocales.get( 0 ); | ||
} | ||
|
||
return context.getDefaultLocale(); | ||
} | ||
}; | ||
|
||
Validator validator = Validation.byProvider( HibernateValidator.class ) | ||
.configure() | ||
.defaultLocale( Locale.FRANCE ) | ||
.locales( Locale.FRANCE, Locale.ITALY, Locale.US ) | ||
.localeResolver( localeResolver ) | ||
.buildValidatorFactory() | ||
.getValidator(); | ||
|
||
Set<ConstraintViolation<Bean>> violations = validator.validate( new Bean() ); | ||
assertEquals( "deve essere true", violations.iterator().next().getMessage() ); | ||
// end::locale-resolver[] | ||
} | ||
|
||
private static class Bean { | ||
|
||
@AssertTrue | ||
private boolean invalid = false; | ||
} | ||
} |