4

首先,我的问题与这两个类似:

JAVA gettext 公用

在java中读取.po文件时出错

  • 我想使用 GNU Gettext 实用程序来国际化我的应用程序。translations.properties我设法通过使用正确的键值对创建一个文件来让它工作,但translations_fr_CA.properties即使我明确地将语言环境设置为 fr_CA 或 Locale.CANADA_FRENCH,它也不会得到我的。这是我的第一个问题。

  • 第二个也是最重要的问题:我无法摆脱 .properties 文件,因为我无法使用不错的 gettext complete msgid 。我设法将我的 .po 文件编译为 .class ,msgfmt --java2 [...]但如果ResourceBundle没有 .properties 文件就不会加载。

这是我当前测试项目的 zip:java_gettext_testing.zip

我在 zip 中包含了 gnu.gettext.GettextResource 以使其更简单。您可以编译javac test/*.java并运行java test.test

谢谢你的帮助!

4

1 回答 1

5

I figured it out finally. I don't know why but I cant use .class files without putting them in a package. I reworked my structure and finally I can compile my .po file with

msgfmt --java2 -d . -r test.translations translations.po

which will compile the ./translations.po file to test/translations.class giving it the appropriate package information.

Now I can do this

ResourceBundle myRes = ResourceBundle.getBundle("test.translations"); // load my test/translations.class file

GettextResource.gettext(myRes, "message id to translate");

Still got to get the locale working but the essential is there for me right now.

Edit This question helped me to understand that msgfmt was compiling .po files into "real" java objects How to use ResourceBundle

于 2014-01-17T15:24:09.693 回答