Skip to main content

MessagePropertyGenerator: a java util class for translating message bundles

Posted by brunogh on May 19, 2009 at 6:28 PM PDT

If you want to translate your message files for a couple of different languages, but you do not have money, do not have time, do not have language specialists friends or do not have anything else to help you, we have a solution... actually we got a "use at your own risk" solution!

Here are the steps:

  • Download google-api-translate-java-0.53.jar and use it in the classpath.
  • Configure the default input file and output files (languages) in MessagePropertyGenerator.java util class, which is detailed above. In my example, I have setted a portuguese property file - which is detailed above as well- and generated an english, german and spanish file with Google translations.
  • Compile and run it!

MessagePropertyGenerator.java

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;
import java.io.FileInputStream;

public class MessagePropertyGenerator {

private static String baseLanguage = Language.PORTUGESE;
private static String basePropertyFileName = "/home/brunogh/NetBeansProjects/I18NGenerator/src/messages_pt_BR.properties";
private static String enconding = "ISO-8859-1";

private static String fullPathToWriteTargetFiles = "/home/brunogh/NetBeansProjects/I18NGenerator/src/";
private static String[][] targetLanguages = {
        {Language.ENGLISH,"messages_en_US.properties"},
        {Language.SPANISH,"messages_es.properties"},
        {Language.GERMAN, "messages_de.properties"}};

public static void main(String[] args) {
try {
            String currentLanguage = null;
            String currentFileName = null;
            String translatedText = null;
            BufferedReader d = null;
for (int i = 0; i < targetLanguages.length; i++) {
d = new BufferedReader(new InputStreamReader(new FileInputStream(basePropertyFileName),enconding));
currentLanguage = targetLanguages[i][0];
currentFileName = targetLanguages[i][1];
String s = "";
StringBuffer sb = new StringBuffer();
Vector notTranslated = new Vector();
while ((s = d.readLine()) != null) {
if ((!s.trim().equals("")) && (!s.startsWith("#"))&& (s.contains("="))) {
String[] keyValue = s.split("=");
try {
translatedText = Translate.translate(keyValue[1], baseLanguage, currentLanguage);
translatedText = replaceParenteses(translatedText);
sb.append(keyValue[0] + "=" + translatedText+ "\n");
System.out.println(keyValue[0] + "="+ translatedText);
} catch (Exception ex) {
sb.append(s);
notTranslated.add(s);
ex.printStackTrace();
}
} else {
sb.append(s + "\n");
}
}
createPropertyFile(sb, currentFileName);
printNotTranslated(notTranslated);
}
} catch (Exception e) {
e.printStackTrace();
}
}

private static String replaceParenteses(String translatedText) {
//TODO rewrite to be a generic solution
return translatedText.replace("(0)", "{0}").replace("(1)", "{1}").replace("(2)", "{2}");
}

private static void createPropertyFile(StringBuffer sb,
String currentFileName) throws FileNotFoundException, IOException {
FileOutputStream fos = new FileOutputStream(fullPathToWriteTargetFiles + currentFileName);
fos.write(sb.toString().getBytes(enconding));
fos.flush();
fos.close();
}

private static void printNotTranslated(Vector notTranslated) {
System.out.println("Not translated:");
for (String string : notTranslated) {
System.out.println(string);
}
System.out.println("-=-=-=-=");
}
}

messages_pt_BR.properties

test.helloworld=Olá Mundo!
test.couldihaveabeerplease=Gostaria de uma cerveja, por favor.
test.javarocks=Java é legal!
test.owyes=Sim, {0}, veja isto {1}

Note that you may have problems with large message property files. If is that your case, you can try another solution that does not make a request per line. Anyway, feel free to modify and improve.

Have a nice week!
Bruno Ghisi

Related Topics >>

Comments

Yep, that's right. Thanks!

Very nice tip Bruno. Just as correction, when you say "above" you meant "below", right? :)