ActionScript 3.0, first attempt

Finally I found the time (not so much) to play a little with Flex 2 alpha and ActionScript 3.0.
In particular I falled in love with the so long awaited ByteArray class, which let us to do bytes operations 🙂
So I’ve created a porting of the GNU gettext class for ActionScript. I’ve got this wish since a long long time, but it was always impossible without the new ByteArray class.
For the complete article read here, also for an explanation of gettext.

The project is hosted on google code here.

and this is the code I’ve used in flex:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="" xmlns="*" creationComplete="main()">
import flash.utils.trace;
import i18n.gettext;
import mx.controls.*;
import mx.controls.gridclasses.DataGridColumn
var ln:gettext;
var alert:Alert;
var added:Boolean;
* init gettext
public function init_gettext()
ln = new gettext();
ln.addEventListener("complete", this.handleEvent);
ln.addEventListener("ioError",  this.handleEvent);
ln.addEventListener("error",    this.handleError);
public function set_locale(lang:String)
alert ="Please wait while loading locale dictionary file", "Loading...", Alert.NONMODAL, this, null, null);
ln.translation("SEPY", "", lang);
log("innstall('" + lang + "')");
* items for the combobox component
public function get_avail_languages():Array
var data:Array = new Array();
data.push({label: _(gettext.FindLanguageInfo("it")), data:"it"});
data.push({label: _(gettext.FindLanguageInfo("en")), data:"en"});
data.push({label: _(gettext.FindLanguageInfo("de")), data:"de"});
data.push({label: _(gettext.FindLanguageInfo("zh_cn")), data:"zh_cn"});
data.push({label: _(gettext.FindLanguageInfo("zh_tw")), data:"zh_tw"});
data.push({label: _(gettext.FindLanguageInfo("nl")), data:"nl"});
data.push({label: _(gettext.FindLanguageInfo("fr")), data:"fr"});
data.push({label: _(gettext.FindLanguageInfo("pt")), data:"pt"});
return data;
* items for the datagrid component
public function get_dataprovider():Array
var data:Array = new Array();
data.push({label:"Add new folder", data:_("Add new folder")})
data.push({label:"An Error occurred, or the package need to be recompiled first", data:_("An Error occurred, or the package need to be recompiled first")})
data.push({label:"Are you sure?", data:_("Are you sure?")})
data.push({label:"Add @see if extended class (must be listed in the tags list)", data:_("Add @see if extended class (must be listed in the tags list)")})
data.push({label:"Browse new package", data:_("Browse new package")})
data.push({label:"Capture output", data:_("Capture output")})
data.push({label:"Cannot wrote to filesystem", data:_("Cannot wrote to filesystem")})
data.push({label:"Cannot read file, invalid zip file", data:_("Cannot read file, invalid zip file")})
data.push({label:"Cannot modify read-only document", data:_("Cannot modify read-only document")})
data.push({label:"Choose application", data:_("Choose application")})
return data;
* main HandleEvent Responder
* handle all the event dispatched
public function handleEvent(event:Event){
this.log("event: " + event.type);
case "click":
if( == "button_1")
var mc:ComboBox = ComboBox(this.h_box_1.getChildByName("combo_languages"));
} else {
case EventType.COMPLETE:
alert.visible = false;
var combo:ComboBox = new ComboBox();
var label:Label    = new Label();
var button:Button  = new Button();
var dp:DataGrid = new DataGrid();
var col_1:DataGridColumn = new DataGridColumn();
var col_2:DataGridColumn = new DataGridColumn();
var label_2:Label = new Label();
var hrule:HRule = new HRule() = "button_1"
button.addEventListener("click", this.handleEvent); = "combo_languages"; = "label_1";
this.h_box_1.addChild(button); = "datagrid_1";
dp.width = 560;
col_1.columnName = "label";
col_2.columnName = "data";
col_2.headerText = _("Translation")
dp.addColumn(col_2); = "label_2";
hrule.width = 560;
this.added = true;
Button(this.h_box_1.getChildByName("button_1")).label = _("Change to");
Label(this.h_box_1.getChildByName("label_1")).text    = _("Language") + ": ";
Label(this.v_box_1.getChildByName("label_2")).text    = _("Test project");
ComboBox(this.h_box_1.getChildByName("combo_languages")).dataProvider = this.get_avail_languages();
DataGrid(this.v_box_1.getChildByName("datagrid_1")).dataProvider = this.get_dataprovider();
DataGrid(this.v_box_1.getChildByName("datagrid_1")).getColumnAt(0).headerText = _("Select items");
* Application init()
public function main(){
var mc:Alert ="Demo application using gettext for internationalization (i18n) puropose. Press the 'OK' button to load the English default language, then use the combo for switch between languages", "Flash and gettext", Alert.OK, this, null, null, Alert.OK);
mc.addEventListener("click", this.handleEvent);
* shortcut usually used for
* gettext applications
public function _(name:String):String
return gettext.translate(name);
public function log(text:String)
logger.text += text + "\n"
logger.vPosition = logger.maxVPosition
public function handleError(event:ErrorEvent)
public function get_url()
navigateToURL(new URLRequest(''));
public function init_app():Void
{"Titolo", "testo", Alert.OK, this, null, null, Alert.OK);
<mx:Canvas width="100%" height="100%">
<mx:Label x="29" y="20" text="Gettext application demo" fontFamily="Georgia" fontWeight="bold" fontSize="18"/>
<mx:HRule x="33" y="39" width="560" height="20" themeColor="haloBlue"/>
<mx:HRule x="33" y="195" width="560" height="20" themeColor="haloBlue"/>
<mx:HRule x="33" y="89" width="560" height="20" themeColor="haloBlue"/>
<mx:Label x="32" y="111" text="log:" width="133"/>
<mx:Link x="369" y="26" label="" width="226" themeColor="haloBlue" textAlign="right" click="get_url()" toolTip="{_('Visit the GNU gettext project')}"/>
<mx:TextArea x="32" y="137" width="560" height="59" id="logger" editable="false" wordWrap="true"/>
<mx:HBox x="35" y="54" width="560" height="38" id="h_box_1" horizontalAlign="left" verticalAlign="middle" label="combo box languages">
<mx:VBox id="v_box_1" horizontalAlign="left" verticalAlign="top" height="362" y="217">
<mx:EdgeAnchor right="243" left="33"/>

  • The ByteArray itself was worth the addition of AS3 alone. With the use of the ByteArray, you can tap into its bytes and manipulate it providing there is an understanding of what is going on behind the scenes. This is a great example of how useful the ByteArray is in a world where flash text has grown to be archaic.
    I am surprised there were no new additions to the text class providing the ByteArray addition.
    I have utilized the ByteArray to extract the exact font size that is being used because their text metrics is far from being accurate on anything other than the TextField border Bounding area itself.

  • vinod

    can the same be implemented through Flash CS3, the reason being that I don’t know anything about flex. Wanted the same to be implemented in flash project.
    any help is appreciated.

  • Elisheva

    I am trying to implement the flex-“gettext” in our project which is multilangual.
    I am checking the Hebrew and Arabic translations. And I notice that in mixed sentences of English and Hebrew, the order of the words is revered.
    Do you have an example which demonstrates it in RTL languages ?

  • I figured out that the extraction of the gettext translation strings seems to be somewhat tricky for intermixed mxml files.
    The actionscript part usually works without any problems, but gettext using Python as extraction language ignores Bindable translation strings in the xml part of the files. Thus I created a small script named asgettext around gettext which makes life easier. Just use the script to extract the language strings and setup the gettext folder structure. The script can be found here:
    In the wiki part there is a small introduction.
    Hope this will help gettext to get a bit more into the Flex world.