Question

I have a Spring MVC J2EE application that leverages Spring's localeChangeInterceptor and CookieLocaleResolver to render locale-driven support. This is working, however when I try to encode letters with accent marks, the front-end fails to render them as expected.

Here's a snippet from my webmvc-config.xml file:

    <!-- Internalization and localization support -->
    <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>

Along with this I have some message_xx.properties files that contian my tags to render things out. Included are tags like this with accent marks embedded: district.manager.approval=Aprobaci&#243;n del Gerente de Distrito. My beef is that this displays exactly like this on the front-end instead of showing me Aprobación del Gerente de Distrito.

Any idea where I could have gone wrong?

Was it helpful?

Solution 2

After poking around a bit, it seems I left out a key detail: this only seems to be happening when I use JSTL <c:set> along with my Spring tags where the encoding does not work properly. As it turns out, when using <c:out>, you need to accompany it with the escapeXml="false" attribute. Here is what I did and it seems to be working appropriately now:

This is set in one page

<c:set var="headerScreenTitle">
    <spring:message code='district.manager.review.and.approval' />
</c:set>   

This is consumed in an imported page

<c:out value="${headerScreenTitle}" escapeXml="false" />

And it handsomely gives me this:

REVISIÓN Y APROBACIÓN DEL GERENTE DE DISTRITO

Thanks everyone for your responses!

OTHER TIPS

The encoding of .properties files is usually (with few exceptions) expected to be Latin-1. Therefore for presenting Unicode content you need to escape the characters beyond Latin-1 repertoire:

district.manager.approval=Aprobaci\u00F3n del Gerente de Distrito

Or use XML properties files which can be encoded as UTF8.

If you use html entities (e.g. &) and use <spring:message code="property.name" /> tag to print values set "htmlEscape" attribute to "false":

<spring:message code="property.name" htmlEscape="false" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top