Use cyrillic .properties file in eclipse project

不羁的心 提交于 2019-12-04 03:59:50

问题


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.

Here is my code:

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

And in my .properties file:

quit = Изход

And I get rubbish. When i try

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

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

Any ideas?


回答1:


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)

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)

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")));



回答2:


Properties are ISO-8859-1 encoded by default. You must use native2ascii to convert your UTF-8 properties to a valid ISO-8859-1 properties file containing unicode escape sequences for all non-ISO-8859-1 characters.



来源:https://stackoverflow.com/questions/3596276/use-cyrillic-properties-file-in-eclipse-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!