在 JSP 中使用资源包属性进行国际化,非拉丁文本变成 Mojibake

2022-01-18 00:00:00 utf-8 internationalization java jsp jstl

我有以下 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

推荐答案

属性文件按照 规范 使用 ISO-8859-1 读取.

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<未涵盖的任何字符/a> 范围需要在 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 = u041Bu043Eu0433u0438u043D

然后可以通过以下最低 @page 配置成功读取并显示在 JSP 页面中:

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

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

(您所拥有的其余部分无关紧要,因为设置上述内容时已经是默认值)

如果您使用的是 Eclipse 等支持 Java 的 IDE,那么您只需使用其内置的属性文件编辑器,该编辑器应该会自动与 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 - 如何正确获取字符?
  • 如何国际化 Java Web 应用程序?

相关文章