Spring ReloadableResourceBundleMessageSource VS ResourceBundleMessageSource - java

Hi I am trying to configure message sources in my Spring MVC web application .
I have currently got it running with ReloadableResourceBundleMessageSource but I am not able to get it running with ResourceBundleMessageSource . I would have preferred to use ResourceBundleMessageSource because I dont need the Reload capability and ResourceBundleMessageSource is slightly more efficient.
in my rootApplicationContext , I have defined the beans as follows.
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/resources/locale/messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
This works FINE ..
But as soon as I change to
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="/resources/locale/messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
The application breaks with the exception :
12:35:57,433 ERROR
[org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/SpringJAXWS].[jsp]]
(http-localhost-127.0.0.1-8080-1) Servlet.service() for servlet jsp
threw exception: org.apache.tiles.util.TilesIOException: JSPException
including path '/jsp/views/layout/top.jsp'. at
org.apache.tiles.servlet.context.ServletUtil.wrapServletException(ServletUtil.java:241)
[tiles-servlet-2.2.2.jar:2.2.2] at
org.apache.tiles.jsp.context.JspTilesRequestContext.include(JspTilesRequestContext.java:105)
[tiles-jsp-2.2.2.jar:2.2.2]
HELP !
The Entire project code is available at GITHUB
https://github.com/localghost8080/JaxWS
Here is the entire stack trace for all who are interested.
https://github.com/localghost8080/JaxWS/blob/master/ResourceBundleException.txt

ResourceBundleMessageSource uses a different format for passing basename values
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="resources.locale.messages" />
<property name="defaultEncoding" value="UTF-8"/>

Related

spring i18n not working

Hello I'm trying to do a i18n in: Spring-MVC-Maven configured-by-context.xml project
I have files (path:\src\main\resources):
messages.properties
messages_pl.properties
The app used to work properly when there is only one file -> reads
< spring:messages ...> fome the one.
But when there are 2 files app reads only messages_pl.properties no mather about ?lang=en.
DispatcherServlet-context.xml
/* [..] */
<mvc:annotation-driven enable-matrix-variables="true"/>
<mvc:resources location="/resources/" mapping="/resource/**"/>
<context:component-scan base-package="com.ehr" />
<mvc:interceptors>
<bean class="com.ehr.webstore.interceptor.PerformanceMonitorInterceptor"/>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang"/>
</bean>
</mvc:interceptors>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
/* [...] */
webpage.jsp
< div class="pull-right" style="padding-right:50px">
< a href="?lang=en" >en</a>|<a href="?lang=pl" >pl< /a>
< /div>
Ok, I've found the root of the problem. All in all it's a FallbackToSystemLocale property which by default is setted on "true". For more info check ResourceBundleMessageSource doc.
Solution:
added property <property name="fallbackToSystemLocale" value="false"/> to <bean id="messageSource" [...] ResourceBundleMessageSource">

Spring: How to implement multiple messgeSource?

I have 2 message sources, from database and many properties files
application_en_US.properties
application_vi_VN.properties
application_ja_JP.properties
application.properties
I try
<bean id="messageSource" class="com.example.InitializableMessageSource">
<property name="messageProvider">
<list>
<bean id="jdbcMessageProvider" class="com.example.common.ultils.JdbcMessageProvider">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="messageSource2"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
</list>
</property>
</bean>
I have
package com.example;
public class InitializableMessageSource extends AbstractMessageSource implements InitializingBean {
}
but error (error convention). Please help me fix it.

How to switch between 2 message bundles?

i'm having some issues handling the i18n of my webapp.
Is it possible through Spring to found a message in a MessageSource if it's not found in the other?
This is my Spring configuration
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="xxMessage" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
Example:
if the key is "login.user" and the locale is "en" the app shows the value in my xx_en.properties if exists, BUT i want to search in the other file (xx_es.properties) if the key doesn't exists.
Is that possible?
P.S: sorry for my english :D
Thanks in advance!
I think you need to have interceptor configuration in your spring configuration
<interceptors>
<beans:bean
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<beans:property name="paramName" value="locale" />
</beans:bean>
</interceptors>
Thanks, for your answer #DDK i've already managed to resolve the problem :D
Solution below.
Define chained message sources, provided by the interface HierarchicalMessageSource.
For example, if you have your i18n files
baseMessages.properties
and
messages.properties
you could chain them as
<bean id="baseMessageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="baseMessages" />
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
<property name="parentMessageSource" ref="baseMessageSource" />
</bean>

Multiple Resource Bundle in JSP message code

I am using Spring MVC 3. I have message bundles in following format :
message_en_US.properties
message_en.properties
message_USA.properties
In my JSP I am using :
Now, the order of finding the values should be en_US, then en then USA property files.
how can i customize this ?
You should use configuration below. So, if you want to set default language you can use the second one.
In my src/main/resources I have mymessages_en_US.properties and mymessages_es_ES.properties
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<array>
<value>classpath:mymessages</value>
</array>
</property>
<property name="defaultEncoding" value="UTF-8" />
<property name="cacheSeconds" value="0" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="es_ES" />
</bean>

JSP Spring internationalization using OSGi as service changing locale not working properly

First of all! Don't judge me the reason that I'm using MessageSource as a service. Since I'm in a phase learning OSGi and Spring.
I have a project that has many modules, in their pages, since I'm making internationalization for it. I saw that they use the same messages, so I put the codes in a common module that every module uses it. And I shared the message as a service osgi-context.xml:
<osgi:service ref="messageSource" interface="org.springframework.context.support.ReloadableResourceBundleMessageSource"/>
<osgi:service ref="localeResolver" interface="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>
<osgi:service ref="localeChangeInterceptor" interface="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
and in module-context.xml the beans:
<bean id="messageSource" scope="bundle" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeResolver" scope="bundle"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="et" />
</bean>
<bean id="localeChangeInterceptor" scope="bundle"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
in the module that uses the service:
<osgi:reference id="messageSource" interface="org.springframework.context.support.ReloadableResourceBundleMessageSource"/>
<osgi:reference id="localeResolver" interface="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>
<osgi:reference id="localeChangeInterceptor" interface="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
So internationalization works! But not completely... the problem comes when I try to change the locale, It partially works. The jsp pages where I use tag message like:
<spring:message code="general.welcome"/>
It does not change! But in the same time I pass some translations using a Controller to a JavaScript var like:
//Some page.jsp
<script>
translations = ${translations == null? '{}' : translations};
</script>
Since the controllers are wired to the messageSource:
#Autowired
MessageSource messageSource;
...
//the way that the request is returned by a method
//A map in JSON using messageSource is return
model.addAttribute("translations", someJSONmap);
It's working!
So in the controller the locale change is working, but in the JSP pages it isn't.
Do anyone know what I am missing? Or how to fix it?
Thanks for reading until here and sorry for the long question.
The problem was solved by removing the service:
module-context.xml:
<bean id="localeChangeInterceptor" scope="bundle"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
osgi-context.xml:
<osgi:service ref="localeChangeInterceptor" interface="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
and put it into the module, which is using the service, applicationContext.xml:
<mvc:interceptors>
...
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>

Categories

Resources