Search in sources :

Example 56 with ResourceBundleMessageSource

use of org.springframework.context.support.ResourceBundleMessageSource in project tutorials by eugenp.

the class ClientWebConfig method messageSource.

@Bean
@Description("Spring message resolver")
public MessageSource messageSource() {
    final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename("messages");
    return messageSource;
}
Also used : ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) Description(org.springframework.context.annotation.Description) Bean(org.springframework.context.annotation.Bean)

Example 57 with ResourceBundleMessageSource

use of org.springframework.context.support.ResourceBundleMessageSource in project irida by phac-nml.

the class ControllerExceptionHandlerTest method testHandleConstraintViolations.

@Test
public void testHandleConstraintViolations() {
    final String MESSAGES_BASENAME = "ValidationMessages";
    Configuration<?> configuration = Validation.byDefaultProvider().configure();
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename(MESSAGES_BASENAME);
    configuration.messageInterpolator(new ResourceBundleMessageInterpolator(new PlatformResourceBundleLocator(MESSAGES_BASENAME)));
    ValidatorFactory factory = configuration.buildValidatorFactory();
    Validator validator = factory.getValidator();
    Set<ConstraintViolation<?>> constraintViolations = new HashSet<>();
    Set<ConstraintViolation<IdentifiableTestEntity>> violations = validator.validate(new IdentifiableTestEntity());
    for (ConstraintViolation<IdentifiableTestEntity> v : violations) {
        constraintViolations.add(v);
    }
    ResponseEntity<Map<String, List<String>>> response = controller.handleConstraintViolations(new ConstraintViolationException(constraintViolations));
    assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
    // assertEquals("{\"label\":[\"You must provide a label.\"]}", response.getBody());
    Map<String, List<String>> body = response.getBody();
    assertTrue("The response must contain an error about a missing label.", body.containsKey("label"));
    List<String> labels = body.get("label");
    assertEquals("There must only be one error with the label.", 1, labels.size());
    String error = labels.get(0);
    assertEquals("The error must be 'You must provide a label.'", "You must provide a label.", error);
}
Also used : IdentifiableTestEntity(ca.corefacility.bioinformatics.irida.web.controller.test.unit.support.IdentifiableTestEntity) ValidatorFactory(javax.validation.ValidatorFactory) ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) PlatformResourceBundleLocator(org.hibernate.validator.resourceloading.PlatformResourceBundleLocator) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) List(java.util.List) Map(java.util.Map) Validator(javax.validation.Validator) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 58 with ResourceBundleMessageSource

use of org.springframework.context.support.ResourceBundleMessageSource in project irida by phac-nml.

the class IridaApiServicesConfig method validator.

@Bean
public Validator validator() {
    ResourceBundleMessageSource validatorMessageSource = new ResourceBundleMessageSource();
    validatorMessageSource.setBasename("ValidationMessages");
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.setValidationMessageSource(validatorMessageSource);
    return validator;
}
Also used : LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) ReloadableResourceBundleMessageSource(org.springframework.context.support.ReloadableResourceBundleMessageSource) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean)

Example 59 with ResourceBundleMessageSource

use of org.springframework.context.support.ResourceBundleMessageSource in project irida by phac-nml.

the class GalaxyProjectNameTest method setUp.

@Before
public void setUp() {
    b = ResourceBundle.getBundle(MESSAGES_BASENAME);
    Configuration<?> configuration = Validation.byDefaultProvider().configure();
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename(MESSAGES_BASENAME);
    configuration.messageInterpolator(new ResourceBundleMessageInterpolator(new PlatformResourceBundleLocator(MESSAGES_BASENAME)));
    ValidatorFactory factory = configuration.buildValidatorFactory();
    validator = factory.getValidator();
}
Also used : ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) ValidatorFactory(javax.validation.ValidatorFactory) PlatformResourceBundleLocator(org.hibernate.validator.resourceloading.PlatformResourceBundleLocator) ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) Before(org.junit.Before)

Example 60 with ResourceBundleMessageSource

use of org.springframework.context.support.ResourceBundleMessageSource in project PublicCMS-preview by sanluan.

the class ApplicationConfig method messageSource.

/**
 * 国际化处理
 *
 * @return message source
 */
@Bean
public MessageSource messageSource() {
    ResourceBundleMessageSource bean = new ResourceBundleMessageSource();
    bean.setBasenames(StringUtils.split(env.getProperty("cms.language"), ","));
    bean.setCacheSeconds(300);
    bean.setUseCodeAsDefaultMessage(true);
    return bean;
}
Also used : ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) FactoryBean(org.springframework.beans.factory.FactoryBean) SchedulerFactoryBean(org.springframework.scheduling.quartz.SchedulerFactoryBean) LocalSessionFactoryBean(org.springframework.orm.hibernate5.LocalSessionFactoryBean) SqlSessionFactoryBean(org.mybatis.spring.SqlSessionFactoryBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

ResourceBundleMessageSource (org.springframework.context.support.ResourceBundleMessageSource)179 Bean (org.springframework.context.annotation.Bean)99 Before (org.junit.Before)16 MessageSourceAccessor (org.springframework.context.support.MessageSourceAccessor)13 LocalValidatorFactoryBean (org.springframework.validation.beanvalidation.LocalValidatorFactoryBean)9 ValidatorFactory (javax.validation.ValidatorFactory)6 ResourceBundleMessageInterpolator (org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator)6 PlatformResourceBundleLocator (org.hibernate.validator.resourceloading.PlatformResourceBundleLocator)6 Test (org.junit.Test)6 Test (org.junit.jupiter.api.Test)6 IOException (java.io.IOException)5 List (java.util.List)5 Locale (java.util.Locale)5 ReaderFactoryByMap (net.n2oapp.framework.config.selective.reader.ReaderFactoryByMap)5 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)5 MessageSource (org.springframework.context.MessageSource)5 ArrayList (java.util.ArrayList)4 N2oApplicationBuilder (net.n2oapp.framework.config.N2oApplicationBuilder)4 N2oEnvironment (net.n2oapp.framework.config.compile.pipeline.N2oEnvironment)4 PersisterFactoryByMap (net.n2oapp.framework.config.selective.persister.PersisterFactoryByMap)4