views:

268

answers:

3

Hi all,

I am working on Spring web application and my application is multilingual. I have created the ResourceBundle property files as messages.properties and messages_ar.properties.

In my start page, I have set by default the locale to English. through:

<fmt:setLocale value="en" scope="session"/>

On the same page, I have provided users with access to other language (Arabic) through a link as :

<a href="index.htm?locale=ar">Arabic Version</a>

And I load the form texts, page title and other common elements from the properties file through the spring message tag:

<spring:message code="title"/>

Application works fine for English, but when I select the arabic version, the values from meaages_ar.properties is not loaded. What is my mistake or how can it be loaded and the application be made multilingual.

Thanks in advance for the help. Regards, Abdel Olakara

A: 

When setting <fmt:setLocale value="en" scope="session"/> you tell the application to use only English. the value of setLocale must be an expression that evaluates to the current locale. Something <fmt:setLocale value="${localeController.currentLocale}" />

Bozho
set I agree, but this is only on the index page. On this page, user can continue in English or select Arabic or French.
Abdel Olakara
I didn't get your comment.
Bozho
A: 

ou dono change any culture. for change between cultures you have to change the Culture and UICulture like

    public event CultureChanged OnCultureChanged;
    public string LastCultureName
    {
        get
        {
            string lastCultureName = (string)Session["lastCulture"];

            if (lastCultureName == null)
            {
                Session["lastCulture"] = Thread.CurrentThread.CurrentCulture.Name;
                //lastCultureName = "ar-EU";
            }

            return lastCultureName;
        }
        set
        {
            Session["lastCulture"] = value;
        }
    }    //  Session["lastCulture"] = "en-US"; // Session["lastCulture"] = "ar-EU";


protected override void InitializeCulture()
    {
        string lang = (string)Session["lastCulture"];

        if (lang == null || lang == String.Empty)
            lang = LastCultureName;
        if (lang != string.Empty)
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);
            Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(lang);
        }
    }
Nasser Hadjloo
this seems to be a .Net solution?
Abdel Olakara
Yes it is a .net solution and I mentioned it cause convrting this to spring is so easy and you just need to use equevalent. this solution willwork great
Nasser Hadjloo
I think this example won't solve the problem - only will confuse the OP.
Bozho
I Use this solution in many applications and all of them working perfect
Nasser Hadjloo
+1  A: 

Spring MVC does a very good job in supporting internationalization. You can register a LocaleChangeInterceptor in your application context to get this stuff done. Here is an example how this would look like in Spring 3 and the new mvc namespace.

<?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.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"&gt;

    <!-- use the annotation driven programming model -->
    <mvc:annotation-driven />

    <!-- register interceptors -->
    <mvc:interceptors>
        <!-- change the locale when a request parameter locale is received e.g. /?locale=de -->
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
    </mvc:interceptors>

    <!-- save the locale using a cookie -->
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />

    <!-- associate view names with jsp files in the directory /WEB-INF/views/ -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

You can find more details in the section of the official documentation:
http://static.springsource.org/spring/docs/1.2.9/reference/mvc.html#mvc-localeresolver

There is also a very helpful sample application in the speing examples repository:
https://src.springframework.org/svn/spring-samples/mvc-basic/

codescape