I'm working to translate my desktop java application ( on windows 10 OS ), so I'm using gettext-common from http://code.google.com/p/gettext-commons
I have following the tutorial providing by gettext-common : https://code.google.com/archive/p/gettext-commons/wikis/Tutorial.wiki
by following all steps I get create Messages_fr.class
,Messages_en.class
the problem is in gettext-common they have Messages_en.properties not .class, when I create Messages_en.properties ( manually ) it works! and I can not do it manually it will take forever (I have more than 700 sentences )
JAVA code :
static I18n i18n = I18nFactory.getI18n(ParserTest.class,
"resources.messages");
public static void main(String args[]) {
for (int i = 0; i < 2; i++) {
if (i == 0) {
print("First run");
} else {
print("Second run");
i18n.setLocale(Locale.FRANCE);
}
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("");
}
}
private static void print(String text) {
System.out.println(text);
}
I have a problem with this:
Problem 1: My message files are located under resources/Messages
directory. It contains only 2 files: messages_en.class and messages_fr.class not messages_fr.properties and messages_en.properties.
If I try to run the above code I'm getting a warning: "ResourceBundle [messages] ,this because there is no .properties files
I decide to work with PropertyResourceBundle so I followed all steps and I jump msgfmt ( I'm not going to create .class
) so I use msgcat with --properties-output
option to create .properties instead of .class
commend:
msgcat --properties-output msgs/fr.po -o src/com/i18n/Messages_fr.properties
or create shell script po2pr.sh (./po2pr.sh fr )
if [ -z "$1" ]; then
echo "Please specify which language_country suffix(es) you want"
exit 1
fi
for lang in $*
do
echo "Creating .properties template file for $lang"
msgcat --properties-output msgs/${lang}.po -o src/com/i18n/Messages_${lang}.properties
msgs/messages.pot
done
java code :
public class I {
private static I18n getI18n() {
I18n getI18n = I18nFactory.getI18n(I.class, "i18n.Messages");
Locale Locale = new Locale("fr");
getI18n.setLocale(Locale);
return getI18n;
}
public static String tr(String str) {
return getI18n().tr(str);
}
public static String tr(String text, Object o1) {
return getI18n().tr(text, o1);
}
public static String tr(String text, Object o1, Object o2) {
return getI18n().tr(text, o1, o2);
}
public static String tr(String text, Object o1, Object o2, Object o3) {
return getI18n().tr(text, o1, o2, o3);
}
public static String tr(String text, Object o1, Object o2, Object o3,
Object o4) {
return getI18n().tr(text, o1, o2, o3, o4);
}
public static String tr(String text, Object[] objects) {
return getI18n().tr(text, objects);
}
}
Note:
1-you have to create Messages_fr.properties
2- make sure that you create Resource bundle in hue level src.com.i18n.Messages