fdam
fdam

Reputation: 830

How to configure messages.properties in spring bean annotation?

I am starting a new project with spring 4 and I am confusing how I can map my i18n file: messages.properties..

In spring 3 I was using xml configuration like this:

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames" value="messages" />
</bean>
<bean id="i18n" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <list>
            <value>classpath:/..../messages.properties</value>
        </list>
    </property>
    <property name="ignoreResourceNotFound" value="true" />
</bean>

And in my jsps files I access it by:

<spring:message code="any key" />

In spring 4 I am avoiding to use xml configuration.. I tried the following:

@Bean
public ResourceBundleMessageSource messageSource() throws Exception {
    ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
    resourceBundleMessageSource.setBasename("message.properties");
    return resourceBundleMessageSource;
}

@Bean
public PropertiesFactoryBean propertiesFactoryBean() throws Exception {
    PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
    Resource resource = new ClassPathResource("messages.properties");
    propertiesFactoryBean.setLocation(resource);
    return propertiesFactoryBean;
}

That class is annotated with @Configuration, but apparently is missing anything..

When I try to access index.jsp, I receive the following exception:

org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspTagException: No message found under code 'application.title' for locale 'pt_BR'.

Any help is appreciated.

Thanks

Upvotes: 2

Views: 9506

Answers (2)

Jaganath Kamble
Jaganath Kamble

Reputation: 556

You need to put file name for each local language. In your case you need

messages_BR.properties or messages_pt_BR.properties

And all the messages in those files.

Upvotes: 0

Juan Rada
Juan Rada

Reputation: 3786

try

<spring:message code="myMessage"/> with <fmt:message key="myMessage"/>

and on web.xml

<context-param>
  <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
  <param-value>messages</param-value>
</context-param>

dont forget to add fmt taglib

Upvotes: 1

Related Questions