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
Related
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>
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>
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;
}
I'm trying to get my application work according to the OS locale of the client machine. For now it works with the locale of server machine. I'm using string frmae work. Apache Tomcat 7 is used as the server. Here is the configuration I used. Any help would be appriciated.
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="language" />
</bean>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
</list>
</property>
</bean>
<!-- Register the welcome.properties -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>i18n.api/api</value>
<value>i18n.exceptions/exceptions</value>
<value>i18n.common/common</value>
<value>i18n.login/login</value>
<value>i18n.plan/plan</value>
<value>i18n.customer/customer</value>
<value>org.springframework.security.messages</value>
<value>org.hibernate.validator.ValidationMessages</value>
</list>
</property>
</bean>
Please see supported handler method argument types on Spring doc. You can inject the user's Locale on your handler method like this:
#RequestMapping("/home")
public String home(Locale userLocale) {
// do something with userLocale
return "home";
}
Locale is of type java.util.Locale.
Also have a look at getLocale() method of ServletRequest. The client has to provide Accept-Language header on their request, otherwise server's locale is used. I'm assuming Spring behave in the same fashion as this
I am using session locale resolver for my application. I am showing the languages in dropdown. if the user selects on any of the language then repopulates all values from that language.
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
</list>
</property>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="languageCode" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
But its is not reading it from the session. Always it considers the defaultlanguage from browser setting.
Please help on this.
You need to obtain the locale in this way:
Locale loc=RequestContextUtils.getLocale(request);
In Spring 4.0 we can Also use LocaleContextResolver.getLocale() method as well.