从messageSource读取unicode会给Java 5带来问题

Mic*_*hel 9 java unicode utf-8 internationalization character-encoding

我使用i18n支持属性文件(例如:messages_en_US.properties,messages_de_DE.properties)制作了一个Spring(2.5.6)Web应用程序.

这个.properties文件带有uni-codes.例如:

busy = Besch\u00E4ftigt
Run Code Online (Sandbox Code Playgroud)

busymessageSource给出这个结果中读取关键字时:

...
private static ReloadableResourceBundleMessageSource messageSource;

    /**
     * Gets a message from the resources (.properties) defined in the applicationContext.xml
     *
     * @param input string to hook up
     * @return the the message hooked up from the resources
     */
    public static String getMessage(String input){
        System.out.println(input); //busy
        System.out.println(messageSource.getDefaultEncoding()); //UTF-8
        System.out.println(messageSource.getMessage(input, null, null)); //Beschu00E4ftigt
        return messageSource.getMessage(input, null, null);
    }
...
Run Code Online (Sandbox Code Playgroud)

所以没有 \

服务器上的文件也是UTF-8:

在此输入图像描述

发生问题的环境:

  • Tomcat 5.5.28(运行jsp-api.jarservlet-api.jarcommon/lib)
  • JDK 1.5.0_22
  • JSTL 1.1.2(从申请中读取lib)

  • Tomcat 6.0.32(运行jsp-api.jarservlet-api.jarlib)

  • JDK 1.5.0_22
  • JSTL 1.1.2(从申请中读取lib)

在问题解决环境(完全相同的分布): -的Tomcat 6.0.32(运行jsp-api.jarservlet-api.jarlib) - JDK 1.6.0_13 - JSTL 1.1.2(从应用程序读取lib)

如果您需要更多信息,请与我们联系.并且不要说我需要更新我的JDK,因为这是不可能的.

在applicationContext.xml中更新绑定messageSource

<b:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <b:property name="defaultEncoding" value="UTF-8"/>
    <b:property name="fallbackToSystemLocale" value="false" />
    <b:property name="basenames">
        <b:list>
            <b:value>classpath:messages</b:value>
            <b:value>/public/custom/i18n/portalmessages</b:value>
        </b:list>
    </b:property>    
    <b:property name="cacheSeconds" value="1"/>
</b:bean>
Run Code Online (Sandbox Code Playgroud)

更新2:将资源属性文件放在classpath和classloader上:

URLClassLoader cl = (URLClassLoader) IOUtils.class.getClassLoader();
InputStream resourceAsStream = cl.getResourceAsStream("messages_de_DE.properties");
Properties prop = new Properties();
prop.load(resourceAsStream);
System.out.println("From classpath --> " + prop.get("busy")); //Beschäftigt
System.out.println("From i18n folder --> " + I18nFunctions.getMessage("busy")); //Beschu00E4ftigt
Run Code Online (Sandbox Code Playgroud)

Har*_*ime 10

我看了一下DefaultPropertiesPersister它的源代码(它在ReloadableResourceBundleMessageSource内部使用).

如果defaultEncoding指定了a ,则从a Reader而不是使用传统Properties.load()方法逐行手动加载属性.

在将键/值对添加到Properties对象之前,将unescape()Strings上调用该方法

protected String unescape(String str) {
    StringBuffer outBuffer = new StringBuffer(str.length());
    for (int index = 0; index < str.length();) {
        char c = str.charAt(index++);
        if (c == '\\') {
            c = str.charAt(index++);
            if (c == 't') {
                c = '\t';
            }
            else if (c == 'r') {
                c = '\r';
            }
            else if (c == 'n') {
                c = '\n';
            }
            else if (c == 'f') {
                c = '\f';
            }
        }
        outBuffer.append(c);
    }
    return outBuffer.toString();
}
Run Code Online (Sandbox Code Playgroud)

这是\角色被删除的地方.

如果您创建DefaultPropertiesPersister如下的子类

package com.something;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.util.DefaultPropertiesPersister;

public class MyPropertiesPersister extends DefaultPropertiesPersister {
    protected String unescape(String str)
    {
        return StringEscapeUtils.unescapeJava(str);
    }    
}
Run Code Online (Sandbox Code Playgroud)

将其设置在spring配置中,如下所示:

<b:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <b:property name="defaultEncoding" value="UTF-8"/>
    <b:property name="fallbackToSystemLocale" value="false" />
    <b:property name="basenames">
        <b:list>
            <b:value>classpath:messages</b:value>
            <b:value>/public/custom/i18n/portalmessages</b:value>
        </b:list>
    </b:property>    
    <b:property name="cacheSeconds" value="1"/>
    <b:property name="propertiesPersister">
        <b:bean class="com.something.MyPropertiesPersister"/>
    </b:property>
</b:bean>
Run Code Online (Sandbox Code Playgroud)

它会工作..可能需要进一步的jiggery-pokery来获得你想要的其他编码等:)