为什么 JSF 属性文件在 Eclipse 中不接受非 ASCII 字符?

发布于 2024-08-28 07:55:35 字数 255 浏览 6 评论 0原文

我想知道,为什么 JSF 属性文件在 Eclipse 中不接受非 ASCII 字符?

我有一些名为“messages.properties”的属性文件,我必须用 unicode 转义字符编写非 ASCII 字符,例如:

title=\u01af\u0020\u0a3f0
header=\u0ff0\u0020\u0ab1

这意味着客户无法使用常规文本编辑器编辑这些属性文件。

有什么解决办法吗?

I wonder, why do JSF properties files not accept non-ASCII characters in Eclipse?

I have some properties file named "messages.properties", I've to write in unicode escapped characters for non-ASCII characters, example:

title=\u01af\u0020\u0a3f0
header=\u0ff0\u0020\u0ab1

This means that customers cannot edit these properties files using regular text editors.

Is there any solution?

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

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

发布评论

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

评论(3

送舟行 2024-09-04 07:55:35

这是因为 java.util .Properties.load(InputStream) 使用 ISO-8859-1。

load(InputStream) / store(OutputStream, String) 方法的工作方式与 load(Reader)/store(Writer, String) 对相同,只是输入/输出流采用 ISO 8859-1 字符编码进行编码。不能直接用这种编码表示的字符可以使用 Unicode 转义来编写;转义序列中只允许使用单个“u”字符。 native2ascii 工具可用于将属性文件与其他字符编码相互转换。

多年来这一直是一个问题。我已经用 struts 通过实现进行编码转换的自定义标签来解决这个问题,但这通常很痛苦。

Java 6 引入了 Properties.load(Writer),它可以很好地处理 UTF-8,但似乎尚未得到广泛采用。

我建议使用 AnyEdit 工具 与 unicode 表示法相互转换。

至于最终用户 - 如果他们要编辑属性文件(这听起来很奇怪),那么您可以让他们写入他们喜欢的任何字符,然后使用 native2acii (或其包装器)转换文件

This stems in the fact that java.util.Properties.load(InputStream) uses ISO-8859-1.

The load(InputStream) / store(OutputStream, String) methods work the same way as the load(Reader)/store(Writer, String) pair, except 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 ; 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.

It has been a problem for ages. I've solved it with struts by implementing custom tags that make encoding transformations, but it is generally a pain.

Java 6 introduces the Properties.load(Writer), which works fine with UTF-8, but it seems it isn't widely adopted yet.

I'd suggest using AnyEdit tools to convert to and from the unicode notation.

As for the end-users - if they are to edit properties files (which sounds strange), then you can let them write in whatever characters they like and later convert the files using native2acii (or a wrapper of it)

恰似旧人归 2024-09-04 07:55:35

自 Eclipse 4.2(Juno,2012)以来,不再需要这样做。当您使用内置属性文件编辑器时,Eclipse 将透明地处理此问题。它将呈现并接受 UTF-8 格式的值,但它会在幕后默默地转换为 \uXXXX 格式。应该注意的是,这与 Git 插件结合会产生一些奇怪的副作用(例如,在合并期间删除旧行),如果在拉/推之前关闭所有属性文件,效果最好。

如果您还没有使用 Eclipse 4.x,请考虑使用 JDK 安装目录的 /bin 文件夹中的 native2ascii 工具将 UTF-8 属性文件转换为 ASCII 属性文件,如 java 的 javadoc 中所述。 util.Properties

您可以保留“原始”属性文件(例如为它们提供 .utf8 扩展名)并使用批处理/shell 文件来转换它们,如下所示:

cd c:\path\to\properties\files
c:\path\to\jdk\bin\native2ascii.exe -encoding UTF-8 text_cs.properties.utf8 text_cs.properties
c:\path\to\jdk\bin\native2ascii.exe -encoding UTF-8 text_ja.properties.utf8 text_ja.properties
c:\path\to\jdk\bin\native2ascii.exe -encoding UTF-8 text_zh.properties.utf8 text_zh.properties
# You can add more properties files here.

这样您就可以编辑 。 utf8 文件并运行一次批处理/shell 脚本以将本机字符转换为 \uXXXX。另请参阅此博客条目

另请参阅:

This is not necessary anymore since Eclipse 4.2 (Juno, 2012). Eclipse will take care of this transparently when you use the builtin properties file editor. It will present and accept the values in UTF-8, but it will under the covers silently convert to \uXXXX format. Noted should be that this has some strange side effects in combination with Git plugin (e.g. old lines deleted during merge), it works best if you close all properties files before pulling/pushing.

If you're not on Eclipse 4.x yet, consider using native2ascii tool found in /bin folder of the JDK installation directory to convert UTF-8 properties files to ASCII properties files, as described in javadoc of java.util.Properties class.

You can keep the "original" properties files (give them for example an .utf8 extension) and use a batch/shell file to convert them like this:

cd c:\path\to\properties\files
c:\path\to\jdk\bin\native2ascii.exe -encoding UTF-8 text_cs.properties.utf8 text_cs.properties
c:\path\to\jdk\bin\native2ascii.exe -encoding UTF-8 text_ja.properties.utf8 text_ja.properties
c:\path\to\jdk\bin\native2ascii.exe -encoding UTF-8 text_zh.properties.utf8 text_zh.properties
# You can add more properties files here.

This way you can just edit the .utf8 files and run the batch/shell script once to do the conversion of native characters to \uXXXX. Also see this blog entry.

See also:

乖乖兔^ω^ 2024-09-04 07:55:35

只需将 .properties 文件保存为 UTF-8 格式即可,它对我有用。

just save the .properties file as UTF-8 format and it works for me.

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