Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26647

How to enable foreign characters for a Spring message localization?

The messages are loaded alright but the foreign chars do not render as expected:

enter image description here

My message properties (messages_sv.properties) are

login.title=Logga in
login.username=Användarnamn
login.password=Lösenord

And the JSP is

<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ page import="com.ses.admin.controller.ObjectName" %>
<!DOCTYPE html>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib prefix="pu" tagdir="/WEB-INF/tags/node" %>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
    <title><fmt:message key="login.title"/></title>
    <fmt:message var="jqueryUrl" key="jquery.js.url"/>
    <script src="<c:url value='${jqueryUrl}'/>" type="text/javascript"></script>
    <fmt:message var="applicationJsUrl" key="application.js.url"/>
    <script src="<c:url value='${applicationJsUrl}'/>" type="text/javascript"></script>
    <link href="/css/style.css" rel="stylesheet" type="text/css">
    <link href="http://fonts.googleapis.com/css?family=Raleway:400,200" rel="stylesheet" type="text/css">
</head>
<body>

<div id="header">
    <div id="header-title"><img src="/images/logga.png">Account Administration</div>
</div>

           <div class="login">

               <hr />


                   <spring:url var="action" value="/admin/execute"/>
                   <form name='f' class="marg-left" id="inputForm" method="post" action="<c:url value='j_spring_security_check'/>" >

                   <h4 class="title"><spring:message code="login.title" /></h4>
                   <label>
                       <span><spring:message code="login.username" /></span>
                       <input type="text" name="j_username" />
                   </label>

                   <label>
                       <span><spring:message code="login.password" /></span>
                       <input type="password" name="j_password" />
                   </label>

                   <div class="buttons">
                       <button type="submit" ><spring:message code="login.title" /></button>
                       <button type="reset" ><spring:message code="login.reset" /></button>
                   </div>
                   </form>
           </div>
</body>
</html>

My XML config 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:context="http://www.springframework.org/schema/context"
       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/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <mvc:resources mapping="/images/**" location="/images/"/>
    <mvc:resources mapping="/css/**" location="/css/"/>

    <mvc:annotation-driven/>
    <context:annotation-config/>
<!--
    <bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">
        <property name="basenames">
            <list>
                <value>messages</value>
                <value>errors</value>
                <value>urls</value>
            </list>
        </property>
    </bean>
-->
    <bean id="messageSource"
          class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>

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

    <bean id="localeResolver"
          class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="en"/>
    </bean>

    <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>

    <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>
    <bean id="jsonHttpMessageConverter"
          class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
    <bean id="methodHandlerExceptionResolver"
          class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">
        <property name="messageConverters">
            <list>
                <ref bean="stringHttpMessageConverter"/>
                <ref bean="jsonHttpMessageConverter"/>
            </list>
        </property>
    </bean>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <bean id="assetFactory" class="com.ses.service.asset.PdfAssetFactoryImpl">
        <constructor-arg ref="partyRepository"/>
        <constructor-arg ref="customerAccountRepository"/>
        <constructor-arg name="registrationTemplatePath" value="${SES_SERVICE_ASSET_FACTORY_REGISTRATION_TEMPLATE}"/>
    </bean>
</beans>

And in my web.xml I got

<filter>
    <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

I know I should use UTF-8 everywhere so what did I miss? In the HTML that is rendered the chars ä and ö do not render correctly:

enter image description here

Update 131218 15:53 CET

However, using this properties file gives the right result. Is it really necessary to encode åäö this way?

login.title=Logga in
login.username=Anv\u00e4ndarnamn
login.password=L\u00f6senord

Update 131218 20:40 CET

The answer says that I should set the encoding which I did and I even created new files that I can verify is in the encoding and if I then change to native text (åäö) it won't give the correct result. The oinly way I can get the correct result is with escape syntax which is not preferrable.

enter image description here

Upvotes: 2

Views: 4208

Answers (1)

Harald Wellmann
Harald Wellmann

Reputation: 12865

Properties files have to be encoded in ISO-8859-1. You can't simply encode a properties file in UTF-8 and expect things to work. Did you check your encoding?

There is an alternative XML-based format for properties files which is more verbose, but allows you to use UTF-8 without ugly escape syntax.

Upvotes: 4

Related Questions