如何在Java中的应用程序属性文件中保存印地语字符?

发布于 2024-12-03 00:18:06 字数 888 浏览 5 评论 0原文

我们正在尝试用印地语国际化我们的 Spring MVC Web 应用程序。当我们尝试复制属性文件中的印地语文本时,属性文件在印地语字符的位置显示小框。

当我们运行应用程序并查看 JSP 时,它会显示问号 (?????????) 来代替印地语字符。

编辑:

我的属性文件具有以下内容。

login.message=\u0915\u0943\u092a\u092f\u093e \u0905\u092a\u0928\u0947 \u0916\u093e\u0924\u0947 \u092e\u0947\u0902 \u0932\u0949\u0917 \u0911\u0928 \u0915\u0930\u0928\u0947 \u0915\u0947 \u0932\u093f\u090f \u0928\u0940\u091a\u0947 \u0915\u0947 \u092b\u093e\u0930\u094d\u092e \u0915\u093e \u0909\u092a\u092f\u094b\u0917 \u0915\u0930\u0947\u0902

我使用以下命令来获取此编码字符串。

native2ascii -encoding utf-8 ApplicationResources_hi.properties gen\ApplicationResources_hi.properties

我的 JSP 页面的头部有以下行

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

我还需要执行其他操作吗?抱歉,我可能在这里遗漏了一些东西。

We are trying to internationalize our Spring MVC web application in Hindi language. When we try to copy the Hindi text in the properties file, the properties file shows small boxes in places of Hindi characters.

When we run the application and see the JSP, it shows questions marks (???????) in place of Hindi characters.

Edit:

My properties file has following contents.

login.message=\u0915\u0943\u092a\u092f\u093e \u0905\u092a\u0928\u0947 \u0916\u093e\u0924\u0947 \u092e\u0947\u0902 \u0932\u0949\u0917 \u0911\u0928 \u0915\u0930\u0928\u0947 \u0915\u0947 \u0932\u093f\u090f \u0928\u0940\u091a\u0947 \u0915\u0947 \u092b\u093e\u0930\u094d\u092e \u0915\u093e \u0909\u092a\u092f\u094b\u0917 \u0915\u0930\u0947\u0902

I have used following command to get this encoded string.

native2ascii -encoding utf-8 ApplicationResources_hi.properties gen\ApplicationResources_hi.properties

My JSP page has following line in head section

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

Do I need to do anything else? Sorry I may be missing something here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光瘦了 2024-12-10 00:18:06

实际上,您必须正确执行 2 个单独的步骤才能实现此目的:

  1. 将文本从 .properties 文件正确获取到 Java 代码,并
  2. 以符合要求的方式将文本从 Java 代码获取到浏览器。明白了。

第一个有点奇怪: .properties 文件是 定义为使用 ISO-8859-1 编码,该编码支持任意 Unicode 字符,但是幸运的是.properties 文件支持 Java 源代码也支持的相同 Unicode 转义,即 \uxxxx

现在,手动编写这些转义符可能会变得很麻烦,因此基本上有两种选择:

  • 使用您选择的编码(可能是 UTF-8)编写 .properties 文件并使用 native2ascii 将其转换为propper 编码
  • 使用专用的 .properties 编辑器,该编辑器已经在幕后执行此操作(并且通常更多)

一旦此工作正常(通过查看字符值在调试器中验证它(String.charAt() 一些本地化的字符串),您需要确保浏览器实际上以正确的方式接收数据,

这里最简单的方法是确保您使用 UTF-8 编码将数据获取到浏览器,因为 UTF-8 可以代表所有内容。 如果您使用JSP

生成输出,那么您可以使用类似的内容来指定您想要 UTF-8 输出:

<%@ page contentType="text/html; charset=utf-8" language="java" %>

请参阅 Java EE 教程了解详细信息。

You actually have to get 2 separate steps right for this to work:

  1. getting the text from your .properties file to your Java code correctly and
  2. getting the text from your Java code to the browser in a way that it understands.

The first one is somewhat strange: .properties files are defined to use the ISO-8859-1 encoding, which doesn't support arbitrary Unicode characters, but luckily .properties file support the same Unicode escapes that Java source code also supports, namely \uxxxx.

Now writing those escapes by hand can become nasty, so there are basically two alternatives:

  • write a .properties file with the encoding of your choice (UTF-8 probably) and use native2ascii to convert it to the propper encoding
  • use a dedicated .properties editor that already does this (and usually a bit more) behind the scenes

Once this works (verify it in the debugger by looking at the character values (String.charAt() of some localized strings), you need to make sure that the browser actually receives the data in the correct way.

The easiest way here is to make sure that you use UTF-8 encoding to get the data to the browser, since UTF-8 can represent all possible Unicode codepoints.

If you're using JSP to produce your output, then you can use something like this to specify that you want UTF-8 output:

<%@ page contentType="text/html; charset=utf-8" language="java" %>

See the Java EE Tutorial for details.

眼睛会笑 2024-12-10 00:18:06

确保.properties文件以UTF-8编码保存,并且JSP页面中的charset也设置为UTF-8

中您的 IDE(例如 Eclipse),转到 Window ->首选项->工作区,然后确保将文本文件编码设置为 UTF-8 以及

更新
您可能需要将文件转换为 Ascii,Java 已有该工具。
从命令行导航到 Java SDK 文件夹,然后尝试以下操作:

..\Java\bin>native2ascii -encoding utf-8 Labels.txt Labels_locale.properties

_locale 设置为您的印地语本地版本(仅限两个字母)

Make sure the .properties file is saved in UTF-8 encoding, and the charset in the JSP page is also set to UTF-8

In your IDE, (for example Eclipse), go to Window -> Preferences -> Workspace, then make sure Text File Encoding is set to UTF-8 as well

UPDATE
You might need to convert the file to Ascii, Java has that tool already.
From the command line navigate to Java SDK folder, then try this:

..\Java\bin>native2ascii -encoding utf-8 Labels.txt Labels_locale.properties

Set the _locale to your Hindi local (two letters only)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文