Kian
Kian

Reputation: 735

Spring MVC i18n not working

I'am not able to get the internationalization in my project to run.
I've tried several stackoverflow solutions and online tutorials without success. The message-key, which I want to get, cannot be found and the locale/lang cannot be switched.

Here is the i18n part of my servlet-context.xml:

<beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
  <beans:property name="basename" value="classpath:messages" />
  <beans:property name="defaultEncoding" value="UTF-8"/>
</beans:bean>

<beans:bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
  <beans:property name="paramName" value="lang" />
</beans:bean>
<beans:bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <beans:property name="defaultLocale" value="en"/>
</beans:bean>

 <beans:bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" >  
    <beans:property name="interceptors">  
        <beans:ref bean="localeChangeInterceptor"/>  
    </beans:property>  
</beans:bean>

The handlerMapping is marked as deprecated.

And this is the part of my jsp where I get the message (spring namspace: http://www.springframework.org/tags):

<spring:message code="btn.ok"/>

My message-properties files are located under src/main/resources

It's a Spring Maven Project created with the Spring Tool Suite.

Does anyone know where the problem is and how can i add other properties with a different name than messages_en, like labels_en.properties and so on...?

It looks like I can't define in the spring:message tag which one (labels or messages) to select.

EDIT:
This is the exception i get:

SEVERE: Servlet.service() for servlet [appServlet] in context with path [/myapp] threw exception [javax.servlet.ServletException: javax.servlet.jsp.JspTagException: No message found under code 'btn.ok' for locale 'en'.] with root cause
at org.springframework.web.servlet.tags.MessageTag.doStartTagInternal(MessageTag.java:184)...

EDIT2:
What if I want to split my property-keys into multiple files, one for messages one for labels..?
Something like (bundle attr doesn't exist)

<spring:message bundle="messages" code="btn.ok"> and <spring:message bundle="labels" code="lbl.ok">

in the same view/jsp.

EDIT3:
I've finally got it working:

<bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="messages" />
    <property name="defaultEncoding" value="ISO-8859-2"/>
</bean>

<mvc:interceptors>
    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
      <property name="paramName" value="lang" />
    </bean>
</mvc:interceptors>

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <property name="defaultLocale" value="en"/>
</bean>

Upvotes: 1

Views: 1845

Answers (3)

Thiago Nobre
Thiago Nobre

Reputation: 1

You have to set jvm init arguments

-Duser.language=pt_BR -Duser.region=BR

https://docs.oracle.com/cd/E23095_01/Platform.93/ATGProgGuide/html/s1809settingthejavavirtualmachineloca01.html

Upvotes: 0

Shinichi Kai
Shinichi Kai

Reputation: 4513

For your second question you can use basenames property.

It looks like this:

<beans:bean id="messageSource"
  class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
  <beans:property name="basenames">
    <beans:list>
      <beans:value>classpath:messages</beans:value>
      <beans:value>classpath:labels</beans:value>
    </beans:list>
  </beans:property>
</beans:bean>

Upvotes: 0

terrafant
terrafant

Reputation: 561

Are you sure that you have "btn.ok" message in your message properties?

Does anyone know where the problem is and how can i add other properties with a different name than messages_en, like labels_en.properties and so on...?

You should change your basename property. E.g.:

<beans:property name="basename" value="classpath:labels" />

Upvotes: 1

Related Questions