gettext-common示例不起作用

时间:2011-11-24 16:17:00

标签: java gettext

我需要翻译我的应用,所以我想使用http://code.google.com/p/gettext-commons

中的gettext-common

我检查了svn并尝试编译示例:

javac -classpath ../java I18nExample.java
java -classpath ../../target/gettext-commons-0.9.6.jar:. I18nExample

该程序没有给我目标输出;我完全不知道发生了什么!

de.properties似乎完全被忽略了。如果我在Factory的构造函数中将Properties文件设置为"de",那么我得到的部分是我想要看到的输出。

互联网上的任何地方都有java的gettext的工作示例吗?

这是示例脚本的输出:

First run
This text is marked for translation and is translated
This text is marked for translation but not translated
This text is marked for translation but not translated
Four: 4
chat
chat
1 file is open
2 files are open
Second run
This text is marked for translation and is translated
This text is marked for translation but not translated
This text is marked for translation but not translated
Four: 4
chat
chat
1 file is open
2 files are open

1 个答案:

答案 0 :(得分:1)

有一些问题,可能是由于构建过程。

首先,要使邮件查找生效,我需要将ende资源移动到Messages_en.propertiesMessages_de.properties,以便创建真正的资源包。

其次,示例代码尝试使用没有可用翻译的消息,例如“文件打开”的内容。这是我尝试过的更新版本;这一切似乎都适用于上述修改:

public static void main(String[] args) {
    I18n i18n = I18nFactory.getI18n(I18nExample.class, "Messages");
    for (int i = 0; i < 2; i++) {
        if (i == 0) {
            print("First run");
        } else {
            print("Second run");
            i18n.setLocale(Locale.GERMAN);
        }

        print("Current locale: " + i18n.getLocale());

        print(i18n.tr("This text is marked for translation and is translated"));

        String mark = i18n.marktr("This text is marked for translation but not translated");
        print(mark);
        print(i18n.tr(mark));

        mark = i18n.tr("This is the {0}. text to be translated", "chat (noun)");
        print(mark);

        mark = i18n.tr("This is the {0}. text to be translated", "chat (verb)");
        print(mark);

        print(i18n.tr("chat (noun)"));
        print(i18n.tr("chat (verb)"));

        print("");
    }
}

另请注意,要插入翻译的单词,您需要这样的内容:

print(i18n.tr("This is the {0}. text to be translated", i18n.tr("chat (noun)")));
print(i18n.tr("This is the {0}. text to be translated", i18n.tr("chat (verb)")));

但是,如果没有取消(删除!并在Messages_en.properties中提供英文翻译,则会显示为chat (noun),这对我来说几乎没用。

这方面缺乏文档。