本文介绍了在JSP中使用资源包属性进行国际化,非拉丁文本变成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

推荐答案

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

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

...输入/输出流以 ISO 8859-1 字符编码进行编码.不能直接用这种编码表示的字符可以使用 Java™ 语言规范 的第 3.3 节中定义的 Unicode 转义来编写;转义序列中只允许有一个u"字符.native2ascii 工具可用于在属性文件与其他字符编码之间进行转换.

因此,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 = 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" %>

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

如果您使用的是 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.

这篇关于在JSP中使用资源包属性进行国际化,非拉丁文本变成Mojibake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 08:48