Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

java - Internationalization using resource bundle properties in JSP, non-Latin text becomes Mojibake

I have the following index.jsp:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<fmt:setLocale value="ru_RU"/>
<fmt:setBundle basename="messages"/>
<html>
  <head>
    <title></title>
  </head>
  <body>
  <h1><fmt:message key="login"/></h1>
  </body>
</html>

And property file messages_ru_RU.properties:

login = Логин

The problem is that I get the junk unicode characters in the output:

???èí

Update

Changed the .properies file encoding to UTF-8. The latest output: DD?D3D?D?

Help me, please, to change this to the normal cyrillic letters.

Property file: messages_ru_RU.properties

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Properties files are as per specification read using ISO-8859-1.

... the input/output stream is encoded in ISO 8859-1 character encoding. Characters that cannot be directly represented in this encoding can be written using Unicode escapes as defined in section 3.3 of The Java? Language Specification; only a single 'u' character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.

So, any character which is not covered by the ISO-8859-1 range needs to be escaped in the Unicode escape sequences uXXXX. You can use the JDK-supplied native2ascii tool to convert them. You can find it in JDK's /bin folder.

Here's an example assuming that foo_utf8.properties is the one which you saved using UTF-8 and that foo.properties is the one which you'd like to use in your application:

native2ascii –encoding UTF-8 foo_utf8.properties foo.properties

In your particular case, the property in question would then be converted to:

login = u041Bu043Eu0433u0438u043D

This can then be successfully read and displayed in a JSP page with the below minimum @page configuration:

<%@ page pageEncoding="UTF-8" %>

(the remainder you had is irrelevant as those are the defaults already when above is set)

If you're using a Java-aware IDE such as Eclipse, then you can just use its builtin properties file editor which should automatically be associated with .properties files in a Java-faceted project. If you use this editor instead of the plain text editor / source editor, then it'll automatically escape the characters which are not covered by the ISO-8859-1 range.

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...