Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring's localization doesn't switch languages

The subject is in the topic - I can't figure out what is the problem with locales switching in my Spring MVC application. As a tutorial I was using that link + I've tried different variations I've found in google. When I click on my web page links to change the language the string ?lang=XX is being appended to the address, but nothing happens.

Here is my servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="ua.dod.picload.web" />

<!-- Internalization and localization support -->
<beans:bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <beans:property name="basename" value="classpath:message" />
    <beans:property name="defaultEncoding" value="UTF-8"/>
</beans:bean>
<beans:bean id="localeChangeInterceptor"
    class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <beans:property name="paramName" value="lang" />
</beans:bean>
<beans:bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <beans:property name="interceptors">
        <beans:ref bean="localeChangeInterceptor" />
    </beans:property>
</beans:bean>
<beans:bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    <beans:property name="defaultLocale" value="en"/>
</beans:bean>


</beans:beans>

My controller:

package ua.dod.picload.web;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {

@RequestMapping("/index")
public String listIndex(Map<String, Object> map) {
    return "index";
}

@RequestMapping("/")
public String home() {
    return "redirect:/index";
}

}

index.jsp

<%@ page language="java" contentType="text/html; charset=utf8" pageEncoding="utf8"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf8">
    <title><spring:message code="label.title" /></title>
</head>
<body>

    <span style="float: right">
    <a href="?lang=en">en</a>
    <a href="?lang=ru">ru</a>
    </span>

    <spring:message code="label.body" />

</body>
</html>

And I have messages_en.properties and messages_ru.properties in my src/main/resources directory. Apparently, I've missed some details, but I definitely can't catch the problem. BTW, when I am changing the value in <beans:property name="defaultLocale" value="en"/> the languages change properly. I would really appreciate your help.

like image 488
Vadim Chekry Avatar asked Feb 22 '12 18:02

Vadim Chekry


People also ask

How do I use localization in spring boot?

By default, a Spring Boot application will look for message files containing internationalization keys and values in the src/main/resources folder. The file for the default locale will have the name messages. properties, and files for each locale will be named messages_XX. properties, where XX is the locale code.

How do you apply localization in spring application?

The first step towards a localized application is to extract all static text from your templates into a localization file. Later, this is used to look up the required text bits according to the selected language.

How are I18n and localization supported in Spring MVC?

Most of the web application frameworks provide easy ways to localize the application based on user locale settings. Spring also follows the pattern and provides extensive support for internationalization (i18n) through the use of Spring interceptors, Locale Resolvers and Resource Bundles for different locales.


2 Answers

<mvc:annotaion-driven /> overrides LocaleChangeInterceptor defined in your XML config. Try to add this (according to spring reference) to XML config:

<mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>
</mvc:interceptors>

or try to get rid of <mvc:annotaion-driven />, which is discussed here.

like image 195
kurochenko Avatar answered Sep 29 '22 10:09

kurochenko


This worked for me as well. Note to others: I may be wrong, but it seems that mvc:interceptors is required when using Spring MVC 3.1. Also note, when using mvc:interceptors make sure you DO NOT have the handlermapping bean:

<bean id="handlerMapping"
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
        <ref bean="localeChangeInterceptor" />
    </property>
</bean>

Having this bean causes the error:

"Cannot resolve reference to bean 'localeChangeInterceptor' while setting bean property 'interceptors'" This was the source of my 8 hour frustration.

like image 41
user1250852 Avatar answered Sep 29 '22 10:09

user1250852