Search in sources :

Example 21 with ResourceBundleMessageSource

use of org.springframework.context.support.ResourceBundleMessageSource in project datagear by datageartech.

the class CoreConfig method messageSource.

@Bean
public MessageSource messageSource() {
    ResourceBundleMessageSource bean = new ResourceBundleMessageSource();
    bean.setBasename("org.datagear.web.i18n.message");
    bean.setDefaultEncoding(IOUtil.CHARSET_UTF_8);
    // i18n找不到指定语言的bundle时不使用操作系统默认语言重新查找,直接使用默认bundle。
    // 系统目前只有默认bundle(无后缀)、英语bundle("en"后缀),如果设置为true(默认值),
    // 当操作系统语言是英语时,会导致切换语言不起作用,i18n始终会被定位至英语bundle
    bean.setFallbackToSystemLocale(false);
    return bean;
}
Also used : ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) SqlSessionFactoryBean(org.mybatis.spring.SqlSessionFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 22 with ResourceBundleMessageSource

use of org.springframework.context.support.ResourceBundleMessageSource in project n2o-framework by i-novus-llc.

the class DataControllerTestBase method setUp.

@Before
public void setUp() {
    N2oEnvironment environment = new N2oEnvironment();
    environment.setNamespacePersisterFactory(new PersisterFactoryByMap());
    environment.setNamespaceReaderFactory(new ReaderFactoryByMap());
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasenames("n2o_messages", "messages");
    messageSource.setDefaultEncoding("UTF-8");
    environment.setMessageSource(new MessageSourceAccessor(messageSource));
    OverrideProperties properties = PropertiesReader.getPropertiesFromClasspath("META-INF/n2o.properties");
    properties.put("n2o.engine.mapper", "spel");
    SimplePropertyResolver propertyResolver = new SimplePropertyResolver(properties);
    setUpStaticProperties(propertyResolver);
    environment.setSystemProperties(propertyResolver);
    builder = new N2oApplicationBuilder(environment);
    configure(builder);
    CompileInfo.setSourceTypes(builder.getEnvironment().getSourceTypeRegister());
}
Also used : N2oEnvironment(net.n2oapp.framework.config.compile.pipeline.N2oEnvironment) PersisterFactoryByMap(net.n2oapp.framework.config.selective.persister.PersisterFactoryByMap) MessageSourceAccessor(org.springframework.context.support.MessageSourceAccessor) N2oApplicationBuilder(net.n2oapp.framework.config.N2oApplicationBuilder) ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) ReaderFactoryByMap(net.n2oapp.framework.config.selective.reader.ReaderFactoryByMap) OverrideProperties(net.n2oapp.properties.OverrideProperties) SimplePropertyResolver(net.n2oapp.framework.config.test.SimplePropertyResolver) Before(org.junit.Before)

Example 23 with ResourceBundleMessageSource

use of org.springframework.context.support.ResourceBundleMessageSource in project n2o-framework by i-novus-llc.

the class IOProcessorTest method testProps.

@Test
public void testProps() throws Exception {
    // test properties
    ReaderFactoryByMap readerFactory = new ReaderFactoryByMap();
    readerFactory.register(new BodyNamespaceEntityIO());
    IOProcessorImpl p = new IOProcessorImpl(readerFactory);
    Properties properties = new Properties();
    properties.setProperty("testProp1", "testProp1");
    PropertyResolver systemProperties = new SimplePropertyResolver(properties);
    p.setSystemProperties(systemProperties);
    testElementWithProperty(p, "testProp1");
    // test params
    HashMap<String, String> params = new HashMap<>();
    params.put("testProp1", "testProp2");
    p = new IOProcessorImpl(readerFactory);
    try {
        MetadataParamHolder.setParams(params);
        testElementWithProperty(p, "testProp2");
    } finally {
        MetadataParamHolder.setParams(null);
    }
    // test messages
    p = new IOProcessorImpl(readerFactory);
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasenames("test_messages");
    messageSource.setDefaultEncoding("UTF-8");
    p.setMessageSourceAccessor(new MessageSourceAccessor(messageSource));
    testElementWithProperty(p, "testProp3");
    // test override
    p = new IOProcessorImpl(readerFactory);
    p.setSystemProperties(systemProperties);
    p.setMessageSourceAccessor(new MessageSourceAccessor(messageSource));
    try {
        MetadataParamHolder.setParams(params);
        // самый приоритетный params
        testElementWithProperty(p, "testProp2");
    } finally {
        MetadataParamHolder.setParams(null);
    }
    // test fail fast
    p = new IOProcessorImpl(readerFactory);
    p.setFailFast(true);
    // p.setSystemProperties(systemProperties); not set to fail
    try {
        testElementWithProperty(p, "testProp1");
        fail();
    } catch (N2oException ignored) {
    }
    // test fail tolerance
    p = new IOProcessorImpl(readerFactory);
    p.setFailFast(false);
    // p.setSystemProperties(systemProperties); not set to fail
    try {
        Element in = dom("net/n2oapp/framework/config/io/ioprocessor22.xml");
        BaseEntity baseEntity = new BaseEntity();
        p.element(in, "attr", baseEntity::getAttr, baseEntity::setAttr);
        assertThat(baseEntity.getAttr(), equalTo("${testProp1}"));
    } catch (N2oException e) {
        fail();
    }
}
Also used : N2oException(net.n2oapp.framework.api.exception.N2oException) HashMap(java.util.HashMap) ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) ReaderFactoryByMap(net.n2oapp.framework.config.selective.reader.ReaderFactoryByMap) Properties(java.util.Properties) PropertyResolver(org.springframework.core.env.PropertyResolver) SimplePropertyResolver(net.n2oapp.framework.config.test.SimplePropertyResolver) MessageSourceAccessor(org.springframework.context.support.MessageSourceAccessor) SimplePropertyResolver(net.n2oapp.framework.config.test.SimplePropertyResolver) Test(org.junit.Test)

Example 24 with ResourceBundleMessageSource

use of org.springframework.context.support.ResourceBundleMessageSource in project n2o-framework by i-novus-llc.

the class N2oTestBase method setUp.

public void setUp() throws Exception {
    N2oEnvironment environment = new N2oEnvironment();
    environment.setNamespacePersisterFactory(new PersisterFactoryByMap());
    environment.setNamespaceReaderFactory(new ReaderFactoryByMap());
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.addBasenames("n2o_api_messages", "n2o_api_messages", "n2o_config_messages", "test_messages", "messages");
    messageSource.setDefaultEncoding("UTF-8");
    Locale locale = new Locale("ru");
    LocaleContextHolder.setLocale(locale);
    environment.setMessageSource(new MessageSourceAccessor(messageSource));
    OverrideProperties n2oProperties = PropertiesReader.getPropertiesFromClasspath("META-INF/n2o.properties");
    OverrideProperties appProperties = PropertiesReader.getPropertiesFromClasspath("application.properties");
    appProperties.setBaseProperties(n2oProperties);
    environment.setSystemProperties(new SimplePropertyResolver(appProperties));
    builder = new N2oApplicationBuilder(environment);
    configure(builder);
    CompileInfo.setSourceTypes(builder.getEnvironment().getSourceTypeRegister());
}
Also used : Locale(java.util.Locale) N2oEnvironment(net.n2oapp.framework.config.compile.pipeline.N2oEnvironment) PersisterFactoryByMap(net.n2oapp.framework.config.selective.persister.PersisterFactoryByMap) MessageSourceAccessor(org.springframework.context.support.MessageSourceAccessor) N2oApplicationBuilder(net.n2oapp.framework.config.N2oApplicationBuilder) ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) ReaderFactoryByMap(net.n2oapp.framework.config.selective.reader.ReaderFactoryByMap) OverrideProperties(net.n2oapp.properties.OverrideProperties)

Example 25 with ResourceBundleMessageSource

use of org.springframework.context.support.ResourceBundleMessageSource in project SpringMVCFinal by tonysodeep.

the class WebAppContextConfig method messageSource.

@Bean
public MessageSource messageSource() {
    ResourceBundleMessageSource msg = new ResourceBundleMessageSource();
    msg.setBasename("messages");
    return msg;
}
Also used : ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) 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