The Source for Java Technology Collaboration
User: Password:



Bruno Ghisi's Blog

Bruno Ghisi Bruno Ghisi is a Java enthusiast interested in mobile, open source and innovation. He is a Mobile & Embedded Community Star and Java Champion. He helps a JUG called GUJavaSC and maintains some open source projects, such as Marge (Mobile Application Video Contest 2007) and mOOo (OpenOffice Innovation Program Awards 2008). He holds a SCJA, SCJP, SCMAD and SCSNI. He is bachelor in Information Systems from Federal University of Santa Catarina and currently works for CERTI Foundation. Also, he lives in an island in the south of Brazil called Florianópolis.



MessagePropertyGenerator: a java util class for translating message bundles

Posted by brunogh on May 19, 2009 at 06:28 PM | Permalink | Comments (2)

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

May 2009
Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            


Search this blog:
  

Categories
Community: Java Communications
Community: Java Enterprise
Community: Java Tools
Community: Java User Groups
Community: JavaDesktop
Community: Mobile & Embedded
Community: NetBeans
J2EE
J2SE
JavaOne
Mobility
Open Source
Tools
Archives

May 2009
February 2009
January 2009
December 2008
November 2008
October 2008
August 2008
July 2008
June 2008
May 2008
April 2008
March 2008
January 2008
December 2007
November 2007
October 2007
September 2007
August 2007
July 2007
June 2007
May 2007

Recent Entries

MessagePropertyGenerator: a java util class for translating message bundles

Sun has fixed SBDTV Forum's messy with the new Java DTV specification

Simple Book Borrowing using Grails

Articles

j1-2k8-mtW09: Marge: Java Bluetooth Framework
The idea of this Mini-Talk on Community Corner is to show a little about Bluetooth, JSR 82 (Java Apis for Bluetooth) and Project Marge. Tired of big texts, full descriptions, etc? Watch this, this and thisJul. 23, 2008

All articles by Bruno Ghisi »



Powered by
Movable Type 3.01D


 Feed java.net RSS Feeds