Resolving locale in Spring MVC - java

I am trying to retrieve locale information in a Java application using this following line of code.
processor = new JQMenusTagProcessor(super.pageContext.getRequest().getLocale());
But the above piece of code always retrieve the default locale i.e en_GB.
How can I retireve the newly changed locale in my application which I am passing along with the url as shown below (as a request)
url?lang=fr
My application-context.xml file looks like as shown below:
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieName" value="myAppLocaleCookie"/>
<property name="defaultLocale" value="fr" />
<property name="cookieMaxAge" value="604800"/>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang"/>
</bean>
</mvc:interceptors>

Related

spring not working with multiple properties files

This question may sound old but I am not able to use multiple properties files as after looking for solution I came to know that for validator class I added hibernate validator 4,5,Jboss Logging final jars and now Its saying
org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1197783': Cannot resolve reference to bean 'validator' while setting bean property 'validator'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'validator' defined in ServletContext resource [/WEB-INF/springDispatcherServlet-servlet.xml]: Invocation of init method failed; nested exception is java.lang.AbstractMethodError: org.hibernate.validator.internal.engine.ConfigurationImpl.getDefaultParameterNameProvider()Ljavax/validation/ParameterNameProvider;
and this is my configuration file
<mvc:annotation-driven enable-matrix-variables="true"
validator="validator" />
<context:component-scan base-package="com.*" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="language" />
</bean>
</mvc:interceptor>
</mvc:interceptors>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
value="/com/resources/messages,/com/resources/messages_validation.properties"
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
</bean>
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource" ref="messageSource" />
</bean>
name of properties file (they are in same folder)
messages_en.properties
messages_nl.properties
messages_validations.properties
I am not able to understand why It is giving me error if everything is in right place
Please help
Try,
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:com/resources/messages</value>
<value>classpath:com/resources</value>
</list>
</property>
</bean>
I guess this should work assuming your messageproperties is under /com/resources
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:/com/resources/messages/" />
<property name="defaultEncoding" value="UTF-8" />
<property name="fallbackToSystemLocale" value="false"/>
</bean>

How to read properties file according to the language just set by the user (in a spring application)

I'm creating a little web application in Spring. I created 2 properties files, italian and english.
With links like IT the user can select the language.
In my file-servlet.xml I have this
......
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath: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.CookieLocaleResolver" >
<property name="defaultLocale" value="en"/>
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
........
I have no problem getting the right value when using <spring:message code="XXX"/>, but i dont understand how to get the right value, according to the language set by the user, when I'm "inside" a class.
I was trying to do something like this :
ApplicationContext context = new ClassPathXmlApplicationContext("messages.xml");
String s = context.getMessage("intestazione",null, Locale.getDefault()));
The problem is that with Locale.getDefault() iI get the Locale of the JVM and not the language set by the user.
You can have the current Locale injected into a controller method by simply declaring it as a parameter. Alternatively you could use LocaleContextHolder.getLocale().
If you can access HttpServletRequest form class, try like this
String s = context.getMessage("intestazione",null, request.getLocale()));

Retrieving locale information using a java program

Is it possible to store locale information in a spring bean and retrieve it in a java application?
Scenario...
I am passing the locale parameter along with the url as follows:
url?lang=fr
Spring context.xml configuration looks like below:
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieName" value="myAppLocaleCookie"/>
<property name="defaultLocale" value="fr" />
<property name="cookieMaxAge" value="604800"/>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang"/>
</bean>
</mvc:interceptors>
Need to fetch locale data from the applicationcontext.xml in my java application.
Is there a way to store current locale to a bean in .xml file and retrieve it in java application.
I am not able to retrieve the changed locale into my java application.
How can I get the locale information in my java application whenever it is changed?

How to use Spring's i18n mechanism?

I added localization to my Spring project and it appears to be working but I wonder how I change the language, if the language choice is done based on the browser setting, the HTTP header, a cookie or something else. Is there a way to be explicit as well e.g. taking the locale as a parameter in a way like e.g. hl=de on the HTTP query string? I also want to allow the user to set the language on a settings page, how can I do that? My implementation looks like this and writes messages in English:
<h4 class="title"><fmt:message key="login.title"/></h4>
servlet.xml:
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath: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.CookieLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
So how can I
a) Make the locale choice explicit by enabling overriding the locale with a HTTP GET parameter such as hl=de for German and hl=fr for French?
b) Let a user choose locale?
Update
The interceptor is not working. The XML is:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="sv" />
</bean>
<mvc:interceptors>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="language" />
</bean>
</mvc:interceptors>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>
</beans>
a) you defined a bean with id localeChangeInterceptor:
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
this interceptor enable you to change your locale using the param you choose (in this case: "lang") in your query string (ie: http://mydomain.com/mypage?lang=fr for french)
b) you can provide users link for changing locale using point a)
c) you selected a default locale: "en". otherwhise locale is choosen using browser language
NOTE: you should use <spring:message code="${msg.value}" arguments="${msg.args}"/>for your localized string, not fmt, for more integration with spring...
You already have configured the LocaleChangeInterceptor. Its parameter paramName (you set it to lang) is the request parameter that changed the locale.
change the configuration to hl, then you can use this parameter to change it:
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="hl" />
</bean>
to let the user change the local, you only need to add some link to the page
German
#See JavaDoc: LocalChangeInterceptor#setParamName

ReloadingPropertyPlaceholderConfigurer

For some reason the ReloadablePropertiesFactoryBean doens't seem to do what I expect. When I change a property in the test.properties file I get a log message saying:
9979 [timer] INFO com.krest.core.properties.ReloadablePropertiesFactoryBean - Reloading Properties...
But for some reason nothing happens. The old property value is still set on my test bean. Does anybody know why this is? I use Spring 3 and my configuration looks like this:
<bean id="configproperties"
class="com.krest.core.properties.ReloadablePropertiesFactoryBean">
<property name="location"
value="classpath:test.properties" />
</bean>
<bean id="propertyConfigurer"
class="com.krest.core.properties.ReloadingPropertyPlaceholderConfigurer">
<property name="properties" ref="configproperties" />
<property name="reloadingPlaceholderPrefix" value="rel{" />
<property name="reloadingPlaceholderSuffix" value="}" />
</bean>
<bean id="mybean" class="nl.mycompany.TestBean">
<property name="message" value="rel{timer-delay}" />
</bean>
<!-- regularly reload property files. -->
<bean id="timer" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<bean id="reloadProperties"
class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="period" value="1000" />
<property name="runnable">
<bean class="com.krest.core.properties.ReloadConfiguration">
<property name="reconfigurableBeans">
<list>
<ref bean="configproperties" />
</list>
</property>
</bean>
</property>
</bean>
</property>
</bean>

Categories