简体   繁体   中英

gettext-common example doesn't work

I need to translate my app, so i want to use gettext-common from http://code.google.com/p/gettext-commons

I checked out the svn and tried to compile the example:

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

The program does not give me the targeted output; I have absolutely no idea whats going on!

It seems that the de.properties is completly ignored. If I set the Properties file to "de" in the Factory's constructor, I get partly the output I want to see.

Is there anywhere in the internet a working example of gettext for java?

this is the output from the example script:

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

There are a couple of issues, perhaps due to the build process.

First, for the message lookup to work, I needed to move the en and de resources into Messages_en.properties and Messages_de.properties in order to make a real resource bundle.

Second, the example code tries to use messages with no translations available, like the "file is open" stuff. Here's an updated version of what I tried; this all appears to work with the above modification:

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

Note also that to insert translated words, you need something like this:

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

However, without un-banging (removing the ! and providing an English translation in Messages_en.properties , it shows up as chat (noun) , which... strikes me as being almost useless.

The documentation is lacking on this aspect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM