Я создал веб-приложение Spring (2.5.6) с поддержкой i18n с файлами свойств (например: messages_en_US.properties, messages_de_DE.properties).
Это файлы .properties с uni-кодами . например:
busy = Besch\u00E4ftigt
При чтении ключевого слова busy
из messageSource
дает следующий результат:
...
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);
}
...
, поэтому без \
файлы на сервере также являются UTF -8:
Среды, в которых возникла проблема:
jsp-api.jar
и servlet-api.jar
из общий / lib
) JSTL 1.1.2 (чтение из приложения lib
)
Tomcat 6.0.32 (запуск jsp-api.jar
и servlet-api.
- Tomcat 6.0.32 (запустите
jsp-api.jar
и servlet-api.jar
из lib
)
- JDK 1.6.0_13
- JSTL 1.1.2 (чтение из приложения lib
)
Пожалуйста, дайте мне знать, если вам нужна дополнительная информация. И не говорите, что мне нужно обновить JDK, потому что это невозможно.
Обновить привязку messageSource в applicationContext.xml
<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>
Обновление 2: Поместите файл свойств ресурса в путь к классам и с помощью загрузчика классов:
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