简体   繁体   English

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

[英]Internationalization using resource bundle properties in JSP, non-Latin text becomes Mojibake

I have the following index.jsp: 我有以下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: 和属性文件messages_ru_RU.properties:

login = Логин

The problem is that I get the junk unicode characters in the output: 问题是我在输出中得到了垃圾unicode字符:

Ëîãèí

Update 更新

Changed the .properies file encoding to UTF-8. 将.properies文件编码更改为UTF-8。 The latest output: Ðогин 最新产品:Ðогин

Help me, please, to change this to the normal cyrillic letters. 请帮助我,将其更改为正常的西里尔字母。

Property file: messages_ru_RU.properties 属性文件: messages_ru_RU.properties

Properties files are as per specification read using ISO-8859-1. 属性文件根据ISO-8859-1读取的规范

... the input/output stream is encoded in ISO 8859-1 character encoding. ...输入/输出流以ISO 8859-1字符编码进行编码。 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 ; 无法在此编码中直接表示的字符可以使用“Java™语言规范”第3.3节中定义的Unicode转义编写; only a single 'u' character is allowed in an escape sequence. 在转义序列中只允许一个'u'字符。 The native2ascii tool can be used to convert property files to and from other character encodings. native2ascii工具可用于将属性文件转换为其他字符编码或从其他字符编码转换。

So, any character which is not covered by the ISO-8859-1 range needs to be escaped in the Unicode escape sequences \\uXXXX . 因此,任何未被ISO-8859-1范围覆盖的字符都需要在Unicode转义序列 \\uXXXX进行转义。 You can use the JDK-supplied native2ascii tool to convert them. 您可以使用JDK提供的native2ascii工具进行转换。 You can find it in JDK's /bin folder. 您可以在JDK的/bin文件夹中找到它。

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: 这是一个例子,假设foo_utf8.properties是你使用UTF-8保存的那个,而foo.properties是你想在你的应用程序中使用的那个:

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

This can then be successfully read and displayed in a JSP page with the below minimum @page configuration: 然后可以成功读取并显示在具有以下最小@page配置的JSP页面中:

<%@ 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. 如果您使用的是Java感知IDE(如Eclipse),那么您可以使用其内置属性文件编辑器,该编辑器应自动与Java面向项目中的.properties文件相关联。 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. 如果您使用此编辑器而不是纯文本编辑器/源编辑器,那么它将自动转义ISO-8859-1范围未涵盖的字符。

See also: 也可以看看:

Image showing to change to unicode 图像显示更改为unicode

I had same problem with hindi language, so i changed my pageEncoding to UTF-8 and have saved file with Unicode encoding. 我对印地语有同样的问题,所以我将pageEncoding更改为UTF-8并使用Unicode编码保存了文件。 Since i have given unicodes in .properties file. 因为我在.properties文件中给出了unicodes。 This worked for me. 这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM