Spring does not using sources in default locale - java

I've changed default locale in spring configuration, but spring always uses messages_en.properties instead of messages.properties.It seems that Spring is ignoring my choice of locale.
defined locases:
messages.properties
messages_en.properties
Spring configuration:
application-context.xml
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="cs"/>
</bean>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8" />
</bean>
servlet-context.xml
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<exclude-mapping path="/admin/**"/>
<exclude-mapping path="/image/**"/>
<exclude-mapping path="/ajax/**"/>
<beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
In JSP page
<spring:message code="csn.terminology.csnId" />
<p>Current Locale : ${pageContext.response.locale}</p>
<!-- output is 'cs', but messages are from messages_en.properties file -->
In project is used Spring Framework 3.2.4
Thank you in advance for your help.

if it's the complete configuration then you forgot to add the locale resolver
You can add a SessionLocaleResolver like this and set the default locale property
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en"></property>
</bean>

When locale is null, AbstractMessageSource calls Locale.getDefault()
So you should set set default locale as follow:
#Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource=new ResourceBundleMessageSource();
messageSource.setBasename("messages");
// SET DEFAULT LOCALE AS WELL
Locale.setDefault(Locale.US);
return messageSource;
}

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

Change DateFormat or Default Locale in Struts2 webapp

I have been having problem with my DateFormat. en_US, en_UK, en_IN are all same for my webapp. The only thing that matters is dateformat to be used throughout the webapp.
First i tried to override the dateformat of en locale, apparently i failed to find any good solution on how to do that.
Hence i planned to change the default locale to en_IN, because we use the date format as dd/mm/yy
No effect, then en_GB... but no luck.
Please note that i did restart tomcat after making changes but no luck.
my dispatcher-servlet.xml in WEB-INF sets it as follows.
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="request_locale" />
</bean>
<!-- <mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/locale*"/>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" >
<property name="paramName" value="request_locale" />
</bean>
</mvc:interceptor>
</mvc:interceptors>
-->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
<bean id="mappingHandler" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
</list>
</property>
</bean>
Adding
<constant name="struts.locale" value="en_GB" />
to struts.xml did the job.
If there are any possible side-effects, please let me know

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>

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

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