Spring locale: can't get interceptors to work - java

I have a jsp file with 2 links which set a property lang to either en or de.
I also have a message that needs to be changed in the corresponding language either english or german.
I have the 2 property files with the codes. I made the configuration file for the locale.
I've tried different combination of classes and properties but I can never get the message changed.
This is my code:
jsp file:
Language : English|German
<h3>
<spring:message code="test" text="default text" />
</h3>
Current Locale : ${pageContext.response.locale} ---- ${pageContext.request.locale}
applicationContext.locale.xml file: (this is imported in applicationContext.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="WEB-INF/locale/welcome" />
<property name="cacheSeconds" value="-1" />
<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="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor"/>
</list>
</property>
</bean>
I have 2 property files:
welcome.properties and welcome_de.properties. Both have a code test and different values to it.
The problem is if I set the default Locale it will always take that. If I don't set it the locale resolver will take the locale of the request.
I can't set the locale of the response to be taken from the url parameter.
Do you have any suggestions?
Thanks :)

Check your code against the project you can download from the MVC Simplifications in Spring 3.0
https://src.springframework.org/svn/spring-samples/mvc-basic/trunk/

Related

How to read properties fragment from Spring configuration [duplicate]

This question already has answers here:
Reading a List from properties file and load with spring annotation #Value
(17 answers)
Closed last year.
Is it possible to read fragment of an array by #Value (Spring 5)?
Something like this:
Input
data.properties :
list.numbers=1,2,3
Read
#Value("${list.numbers[0]}")
int firstNumber;
Output
firstNumber=1
You can split and get the first element.
#Value("#{'${list.numbers}'.split(',')[0]}")
int firstNumber;
A long time ago, I have used bean injection with XML configuration.
You can read that blog article and using spring-core, spring-beans and spring-context-support dependencies.
Below you'll find the XML config of that bean injection with the <bean ref="beanListItemId"> (here : song1, song2, etc.) inside your bean list (here the list container is album):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="song1" class="org.kodejava.spring.core.Song">
<property name="title" value="I Saw Her Standing There" />
<property name="writer" value="Beatles" />
</bean>
<bean id="song2" class="org.kodejava.spring.core.Song">
<property name="title" value="Misery" />
<property name="writer" value="Beatles" />
</bean>
<bean id="song3" class="org.kodejava.spring.core.Song">
<property name="title" value="Anna (Go to Him)" />
<property name="writer" value="Beatles" />
</bean>
<bean id="album" class="org.kodejava.spring.core.Album">
<property name="title" value="Please Please Me" />
<property name="year" value="1963" />
<property name="songs">
<list>
<ref bean="song1" />
<ref bean="song2" />
<ref bean="song3" />
</list>
</property>
</bean>
</beans>

Configuring locale switching with Spring MVC

First of all I have to say, that I am an absolute beginner in developing Spring Application. What I try to do is to switch the locale from 'en' to 'de'. For this I found the configuration below witch I put in my mvc-dispatcher-servlet.xml
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="Messages" />
</bean>
<!-- Localization Start -->
<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>
After that I expect that I can change the locale by adding '?language=de' behind a existing URL. So the request 'http://localhost:8080/?language=de' should switch the locale. This didn’t work. The website is shown in the defined default language
My property files are located in /src/main/resources. The names are “Messages_en.propperties” and “Messages_de.propperties”. If I switch the default language to “de”, the correct language file is loaded and the website is shown in german.
Has someone an idea what’s wrong in my configuration?
I believe you have to register the LocaleChangeInterceptor with an interceptor in Spring
<!-- Declare the Interceptor -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
p:paramName="locale" />
</mvc:interceptors>
The LocaleChangeInterceptor is configured to look for the parameter name 'locale' to indicate a change of the user's locale, and is registered as an interceptor using the Spring MVC Namespace. For example, adding 'locale=es' to a URL would change the locale to Spanish.

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

Categories