i18n : Umlaut not being displayed correctly in JSP
I h开发者_JS百科ave a JSP that is supposed to display some German text from some .properties files by using fmt:message, e.g.
The corresponding entry in the .properties file is: service.test.hware.test = Hardware prüfen (umlaut between r and f in 2nd word).
On internet explorer this displays as:
Hardware prüfen
the umlaut being corrupted. Any ideas as to what is going on here? Note that we are using Spring MVC.
The ü
is typical for an UTF-8 originated ü
being incorrectly encoded as ISO-8859-1 instead of UTF-8. Here's a programmatic evidence:
System.out.println(new String("ü".getBytes("UTF-8"), "ISO-8859-1")); // ü
Since you mention that the very same character from the properties file works fine in some JSP's, but not in other JSP's, then it means that the browser is by those JSP's not correctly been instructed to use UTF-8 to display the characters returned by the server.
This instruction happens in the HTTP Content-Type
header. Using any HTTP header debugging tool, you must be able to figure the returned header. One of the popular tools is Firebug.
Note the presence of charset=utf-8
.
Usually, in JSP this is achieved by simply placing the following line in top of the JSP file:
<%@ page pageEncoding="UTF-8" %>
See also:
- Unicode - How to get the characters right?
If you've defined your Spring messageSource
via org.springframework.context.support.ResourceBundleMessageSource
the properties are loaded with iso-8859-1
encoding, even if the properties file is utf-8
encoded (Java loads properties per default with iso-8859-1
encoding).
Consider to use org.springframework.context.support.ReloadableResourceBundleMessageSource
. You can configure the default encoding with that MessageSource
implementation. See the Javadoc for further information/features of that class.
Example:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:message"/>
<property name="defaultEncoding" value="UTF-8" />
</bean>
Probably an encoding problem. It could be on the encoding you serve in which case you should
- Try converting ü instead. This will not fix other characters, but you can convert all non-ASCII codepoints to { form where 1234 is the decimal value of the character, and/or
- Mandate a page encoding as Fred Basset describes. You should use UTF-8 unless you have a specific reason not to.
Or it could be a problem with the encoding used to read the properties file. If you are using FileReader
, don't. Use new InputStreamReader(new FileInputStream(...), encoding)
instead where encoding
is the encoding of the properties file.
精彩评论