使用JSP中的资源包属性进行国际化,非拉丁文字变为Mojibake [英] Internationalization using resource bundle properties in JSP, non-Latin text becomes Mojibake
问题描述
我有以下index.jsp:
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>
和属性文件messages_ru_RU.properties:
And property file messages_ru_RU.properties:
login = Логин
问题是我在输出中得到了垃圾unicode字符:
The problem is that I get the junk unicode characters in the output:
Ëîãèí
更新
将.properies文件的编码更改为UTF-8. 最新输出:Ðоги½½
Changed the .properies file encoding to UTF-8. The latest output: Ðогин
请帮助我,将其更改为普通的西里尔字母.
Help me, please, to change this to the normal cyrillic letters.
属性文件: messages_ru_RU.properties
推荐答案
Properties files are as per specification read using ISO-8859-1.
...输入/输出流以ISO 8859-1字符编码进行编码.可以使用 Java™语言规范的第3.3节中定义的Unicode转义来编写无法直接用此编码表示的字符;转义序列中仅允许使用一个'u'字符. native2ascii工具可用于在其他字符编码之间来回转换属性文件.
... 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.
因此, ISO-8859-1未涵盖的任何字符范围需要在 Unicode转义序列 \uXXXX
中进行转义.您可以使用JDK提供的native2ascii
工具进行转换.您可以在JDK的/bin
文件夹中找到它.
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.
在此示例中,假设foo_utf8.properties
是您使用UTF-8保存的那个,而foo.properties
是您要在应用程序中使用的那个:
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 = \u041B\u043E\u0433\u0438\u043D
然后可以使用以下最低配置@page
成功读取并显示在JSP页面中:
This can then be successfully read and displayed in a JSP page with the below minimum @page
configuration:
<%@ page pageEncoding="UTF-8" %>
(剩下的与您无关,因为这些是设置了上述设置时的默认值)
如果使用的是Java感知的IDE(例如Eclipse),则可以使用其内置的属性文件编辑器,该编辑器应自动与Java方面的项目中的.properties
文件关联.如果使用此编辑器而不是纯文本编辑器/源代码编辑器,则它将自动转义ISO-8859-1范围未涵盖的字符.
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.
- Unicode - How to get the characters right?
- How to internationalize a Java web application?
这篇关于使用JSP中的资源包属性进行国际化,非拉丁文字变为Mojibake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!