Multiple Resource Bundle in JSP message code - java

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>

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">

How to escape arguments when using hibernate validator with a custom messagesource

I've something like:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>...</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8"/>
<property name="fallbackToSystemLocale" value="false"/>
<property name="useCodeAsDefaultMessage" value="true"/>
</bean>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource" ref="messageSource"/>
</bean>
And some custom validator (has a values attribute) using this message:
validation.constraints.PossibleValues.message=The provided value "${validatedValue}" is invalid. Possible values: {values}
The issue is that the { and } are not there anymore in AbstractMessageInterpolator#interpolateMessage, so I don't get parameter interpolation.
I tried various escaping like '{'validatedValue'}', \\{ without success.
Does someone know how to escape the message arguments in such case?
(I saw this question, but it doesn't help)
I'm using almost the same configuration you have:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="defaultEncoding" value="ISO-8859-1" />
<property name="fallbackToSystemLocale" value="false" />
<property name="useCodeAsDefaultMessage" value="false" />
<property name="basenames" value="classpath:applicationMessages,classpath:ValidationMessages"/>
</bean>
And in my ValidationMessages.properties i overwrite some messages to Galician language using simple { and } for interpolated variables. For example:
javax.validation.constraints.Min.message=debe ser maior o igual a {value}
...
org.hibernate.validator.constraints.Length.message=debe ter unha lonxitude entre {min} e {max}
The only strange behaviour is that if you want to put single quote you need to escape it with a single quote:
org.hibernate.validator.constraints.ScriptAssert.message=o script ''{script}'' non devolve true
Hope this helps.

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>

MethodInvokingFactoryBean didn't work

I create a properties file contain system properties in classpath, the name is system.properties
the file like :
system.project_name=springsilkworm
I also use EHcache, the ehcache.xml like:
<diskStore path="${java.io.tmpdir}/${system.project_name}/cache" />
I want use the system.project_name define in system.properties。
I developed my project based on Spring, so I created applicationContext.xml like:
<context:property-placeholder location="classpath*:/system.properties" ignore-resource-not-found="true" ignore-unresolvable="true" />
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="java.lang.System" />
<property name="targetMethod" value="setProperty" />
<property name="arguments">
<list>
<value>system.project_name</value>
<value>${system.project_name}</value>
</list>
</property>
</bean>
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:/ehcache.xml" />
<property name="shared" value="true" />
</bean>
but when I start my app, I find the folder which EHcahce use like :
that was say, the MethodInvokingFactoryBean didn't work, I also get the property in my code with System.getProperty("system.project_name") code,I got the result of “null”, why? I couldn't find out the wrong.

Spring ReloadableResourceBundleMessageSource VS ResourceBundleMessageSource

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"/>

Categories

Resources