views:

114

answers:

3

I am developing a spring mvc application which should support English & Arabic. I've configured the application as mentioned in the spring reference documents and the switching of locale is working perfectly. However the arabic messages in the resource bundle is been shown as junk characters. The encoding is set as UTF-8 and it is working properly. Also I tried running the native2ascii tool for converting the messages_ar.properties file to unicode.

Nothing works. Any help would be much appreciated.

web.xml (partial)

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<web-app version="2.4"...>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>

<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Handles all requests into the application -->
<servlet>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/mvc-config.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

mvc-config.xml (partial)

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<context:component-scan base-package="net.omnsoq.classified.controller" use-default-filters="false">
    <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" />
</context:component-scan>

<!-- Configures support for @Controllers -->
<mvc:annotation-driven />

<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource"
    p:basenames="WEB-INF/i18n/messages,WEB-INF/i18n/application" p:fallbackToSystemLocale="false" />

<!-- store preferred language configuration in a cookie -->
<bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver" id="localeResolver" p:cookieName="locale" />


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

jsp code

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
...
<%@page contentType="text/html;charset=UTF-8" %>
...
<spring:message code="nav.label.myaccount" />
A: 

Did you check the contents of the resource file? It should not contain any UTF-8 characters, only ASCII.

For that use:

<native2ascii encoding="UTF-8" src="${src.file}" dest="${conf.deploy.dir}" includes="**/messages.properties"/>

Eugene Retunsky
I tried this initially and to my surprise it didn't work. Thats when the real head ache started.
Jerrish Varghese
Yes, your solution also is working with <fmt:message>
Jerrish Varghese
A: 

I found the solution. So I just want to share it so that it might be helpful to someone else.

I set the fileEncodings and defaultEncoding properties to UTF-8 for the messageSource.

<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource"
    p:basenames="WEB-INF/i18n/messages,WEB-INF/i18n/application" p:fallbackToSystemLocale="false" p:fileEncodings="UTF-8"
    p:defaultEncoding="UTF-8" />
Jerrish Varghese
A: 

Hi Jerrish.. I have to design the same kind of application as you did, it should support both English and Arabic and must be in Spring. Can you help me about, how I should go with designing the same.

Also, if i try to save some arabic text, in the resource bundle properties its displaying them as junk characters. Your reply shows how to access these characters, but how did you go about resolving the display of arabic characters in your resource bundle file.

Abdul
The display is not maintained in the resource bundle. It should be within the jsp. <html dir="rtl"> should be used instead of <html> if the locale is arabic.
Jerrish Varghese