Search in sources :

Example 46 with ResourceBundleMessageSource

use of org.springframework.context.support.ResourceBundleMessageSource in project spring-framework by spring-projects.

the class DataBinderTests method bindingErrors.

@Test
void bindingErrors() {
    TestBean rod = new TestBean();
    DataBinder binder = new DataBinder(rod, "person");
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("age", "32x");
    binder.bind(pvs);
    Errors errors = binder.getBindingResult();
    FieldError ageError = errors.getFieldError("age");
    assertThat(ageError.getCode()).isEqualTo("typeMismatch");
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename("org.springframework.validation.messages1");
    String msg = messageSource.getMessage(ageError, Locale.getDefault());
    assertThat(msg).isEqualTo("Field age did not have correct type");
    messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename("org.springframework.validation.messages2");
    msg = messageSource.getMessage(ageError, Locale.getDefault());
    assertThat(msg).isEqualTo("Field Age did not have correct type");
    messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename("org.springframework.validation.messages3");
    msg = messageSource.getMessage(ageError, Locale.getDefault());
    assertThat(msg).isEqualTo("Field Person Age did not have correct type");
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) Test(org.junit.jupiter.api.Test)

Example 47 with ResourceBundleMessageSource

use of org.springframework.context.support.ResourceBundleMessageSource in project spring-framework by spring-projects.

the class JstlUtils method getJstlAwareMessageSource.

/**
 * Checks JSTL's "jakarta.servlet.jsp.jstl.fmt.localizationContext"
 * context-param and creates a corresponding child message source,
 * with the provided Spring-defined MessageSource as parent.
 * @param servletContext the ServletContext we're running in
 * (to check JSTL-related context-params in {@code web.xml})
 * @param messageSource the MessageSource to expose, typically
 * the ApplicationContext of the current DispatcherServlet
 * @return the MessageSource to expose to JSTL; first checking the
 * JSTL-defined bundle, then the Spring-defined MessageSource
 * @see org.springframework.context.ApplicationContext
 */
public static MessageSource getJstlAwareMessageSource(@Nullable ServletContext servletContext, MessageSource messageSource) {
    if (servletContext != null) {
        String jstlInitParam = servletContext.getInitParameter(Config.FMT_LOCALIZATION_CONTEXT);
        if (jstlInitParam != null) {
            // Create a ResourceBundleMessageSource for the specified resource bundle
            // basename in the JSTL context-param in web.xml, wiring it with the given
            // Spring-defined MessageSource as parent.
            ResourceBundleMessageSource jstlBundleWrapper = new ResourceBundleMessageSource();
            jstlBundleWrapper.setBasename(jstlInitParam);
            jstlBundleWrapper.setParentMessageSource(messageSource);
            return jstlBundleWrapper;
        }
    }
    return messageSource;
}
Also used : ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource)

Example 48 with ResourceBundleMessageSource

use of org.springframework.context.support.ResourceBundleMessageSource in project smart-cloud by smart-cloud.

the class LocaleAutoConfiguration method messageSource.

@Bean
public MessageSource messageSource(final SmartProperties smartProperties) {
    LocaleProperties localeProperties = smartProperties.getLocale();
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    Resource[] resources = null;
    try {
        resources = resolver.getResources(LocaleConstant.LOCALE_PATTERN);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    if (resources != null) {
        String[] strResources = new String[resources.length];
        for (int i = 0; i < resources.length; i++) {
            Resource resource = resources[i];
            strResources[i] = LocaleConstant.LOCALE_DIR + resource.getFilename().replace(LocaleConstant.LOCALE_PROPERTIES_SUFFIX, "");
        }
        messageSource.setBasenames(strResources);
    }
    messageSource.setDefaultEncoding(localeProperties.getEncoding());
    messageSource.setCacheSeconds(localeProperties.getCacheSeconds());
    return messageSource;
}
Also used : PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) Resource(org.springframework.core.io.Resource) ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) LocaleProperties(org.smartframework.cloud.starter.configure.properties.LocaleProperties) IOException(java.io.IOException) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) Bean(org.springframework.context.annotation.Bean)

Example 49 with ResourceBundleMessageSource

use of org.springframework.context.support.ResourceBundleMessageSource in project cloud-pipeline by epam.

the class TestApplicationWithAclSecurity method messageSource.

@Bean
public ResourceBundleMessageSource messageSource() {
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename("messages");
    messageSource.setDefaultEncoding("UTF-8");
    return messageSource;
}
Also used : ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) Bean(org.springframework.context.annotation.Bean)

Example 50 with ResourceBundleMessageSource

use of org.springframework.context.support.ResourceBundleMessageSource in project libresonic by Libresonic.

the class LibresonicThemeSource method createMessageSource.

@Override
protected MessageSource createMessageSource(String basename) {
    ResourceBundleMessageSource messageSource = (ResourceBundleMessageSource) super.createMessageSource(basename);
    // Create parent theme recursively.
    for (Theme theme : settingsService.getAvailableThemes()) {
        if (basename.equals(basenamePrefix + theme.getId()) && theme.getParent() != null) {
            String parent = basenamePrefix + theme.getParent();
            messageSource.setParentMessageSource(createMessageSource(parent));
            break;
        }
    }
    return messageSource;
}
Also used : ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) Theme(org.libresonic.player.domain.Theme)

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