在 Eclipse 项目中使用西里尔文 .properties 文件

2022-01-18 00:00:00 internationalization java eclipse

我正在开发一个小项目,我想为它使用国际化.问题是当我尝试使用带有西里尔符号的 .properties 文件时,文本显示为垃圾.当我对字符串进行硬编码时,它显示得很好.

I'm developing a small project and I'd like to use internationalization for it. The problem is that when I try to use .properties file with cyrillic symbols inside, the text is displayed as rubbish. When I hard-code the strings it's displayed just fine.

这是我的代码:

ResourceBundle labels = ResourceBundle.getBundle("Labels");
btnQuit = new JButton(labels.getString("quit"));

在我的 .properties 文件中:

And in my .properties file:

退出 = Изход

我得到了垃圾.当我尝试

And I get rubbish. When i try

btnQuit = new JButton("Изход);

显示正确.据我所知,UTF-8 是用于文件的编码.

It is displayed correctly. As far as I am aware, UTF-8 is the encoding used for the files.

有什么想法吗?

推荐答案

AnyEdit 是一个eclipse-插件,可让您轻松地将属性文件从 unicode 符号转换为 unicode 符号.(避免使用像 native2ascii 这样的命令行工具)

AnyEdit is an eclipse-plugin that allows you to easily convert your your properties files from and to unicode notation. (avoiding the use of command-line tools like native2ascii)

如果您使用的是 Properties 单独的类(没有资源包),从 Java 1.6 开始,您可以选择 用自定义编码加载文件,使用 Reader(而不是 InputStream)

If you were using the Properties class alone (without resource bundle), since Java 1.6 you have the option to load the file with a custom encoding, using a Reader (rather than an InputStream)

我猜你也可以使用 new PropertyResourceBundle(reader),而不是 ResourceBundle.getBundle(..),其中 reader 是:

I'd guess you can also use new PropertyResourceBundle(reader), rather than ResourceBundle.getBundle(..), where reader is:

Reader reader = new BufferedReader(new InputStreamReader(
     getClass().getResourceAsStream("messages.properties"), "utf-8")));

相关文章