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: Ðогин
Help me, please, to change this to the normal cyrillic letters.
Property file:
messages_ru_RU.properties
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 = \u041B\u043E\u0433\u0438\u043D
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:
- Unicode - How to get the characters right?
- How to internationalize a Java web application?
Image showing to change to unicode
I had same problem with hindi language, so i changed my pageEncoding to UTF-8 and have saved file with Unicode encoding. Since i have given unicodes in .properties file. This worked for me.
Since Java SE properties files are loaded in UTF-8 encoding.
See https://docs.oracle.com/javase/9/intl/internationalization-enhancements-jdk-9.htm and https://stackoverflow.com/a/46926020/548473