обычный пример gettext не работает - PullRequest
0 голосов
/ 24 ноября 2011

Мне нужно перевести мое приложение, поэтому я хочу использовать gettext-common с http://code.google.com/p/gettext-commons

Я проверил svn и попытался скомпилировать пример:

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

Программа не дает мне целевой вывод; Я понятия не имею, что происходит!

Кажется, что de.properties полностью игнорируется. Если я установлю для файла свойств значение "de" в конструкторе Factory, то получу частично вывод, который хочу увидеть.

Есть ли где-нибудь в Интернете работающий пример gettext для Java?

это вывод из примера сценария:

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 Ответ

1 голос
/ 24 ноября 2011

Есть несколько проблем, возможно, из-за процесса сборки.

Во-первых, чтобы поиск сообщений работал, мне нужно было переместить ресурсы en и de в Messages_en.properties и Messages_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), что ... кажется мне почти бесполезным.

Документация по этому аспекту отсутствует.

...