Groovy, XLIFF and a translation generator
We recently had to translate a site to 13 different languages using Actionscript 2 ( arghhh ).The translation provided us was in Excel worksheets, and was just a nightmare to get right. We needed a way to quickly generate the translations as they changed.
Since ActionScript uses a format named XLIFF, we needed a way to generate these quickly.
The way XLIFF works in Actionscript is with the Locale class. After loading the proper XML file, you can simply call Locale.loadString( “keyname” ) to retrieve this value.
I looked at my trusty friend Groovy, and we came up with a very simple and elegant solution.
We basically had all our translations in a folder named translations, each row looking like the following
DYNAMIC DYNAMICKÁ DYNAMISCH DYNAMIC DINÁMICA DYNAMIQUE DINAMIKUS DINAMICA DYNAMICZNA DINÂMICA DINÁMICA DINÁMICA DYNAMIQUE ダイナミック 动感劲爆 活潑 ДИНАМИЧНОЙ 역동적인
Then we had a key file that had this key
DYNAMIC_TEXT
After running the script, it would create a line like this in the XLIFF file:
<trans-unit resname=”DYNAMIC_TEXT”>
<source>ДИНАМИЧНОЙ</source>
</trans-unit>
The script basically just creates a es_ES.xml file for each of the languages, splitting all the translations into separate files.
here is the code ( I should have used a XML builder… next time! ):
def languages = [ "en","cs","de","en_GB","es_ES","fr","hu","it","pl","pt","es","es_CL","fr_CA","ja","zh_CN","zh_HK","ru","ko" ]
def writers = []
languages.each{
def writer = new OutputStreamWriter(new FileOutputStream(”//Users//tlin//client//flash//locales//${it}.xml” ), ‘utf-8′) ;
writers.add( writer );
writer << “”"<?xml version=”1.0″ encoding=”UTF-8″?>\r\n<xliff version=”1.0″ xml:lang=”${it}”>\r\n<file datatype=”plaintext” original=file.fla” source-language=”EN”>”"”
writer <<”"”\r\n<header></header>\r\n<body>\r\n”"”
}
new File( “//Users//tlin//client//flash//translations//files//keys//” ).eachFile{
f ->
if( !f.name.startsWith( “.” ))
{
print f.name
def lookup = []
f.eachLine{ l ->
lookup.add( l.trim() );
}
def infile = new File ( ‘//Users//tlin//client//flash//translations//files//translations//’ + f.name );
def reader = infile.newReader( “UTF-16″ )
def count = 0;
reader.eachLine(){ l ->
def vals = l.split(”\t”)
for( int i = 0 ; i < vals.length; i++){
writers[ i ] << “”"<trans-unit resname=”${lookup[count]}”><source>${vals[i].replaceAll(’”‘,”)}</source></trans-unit>\r\n”"”
}
count++;
}
}
}
writers.each{
it << “”" </body></file></xliff> “”"
it.close();
}