translating a flash website text and audio

Step 1: Text Class

The Text Loader Class,
This is a class used for all the text in the site. It takes a few parameters, most notably your model, or whatever class you are storing the locale in, which I call _main, which is a MovieClip in this example. So it pulls a variable called 'loc' from the model which will be your locale ("en" for English, "de" for German, etc..). The defaultText parameter is a safety net and a fallback in case there are any problems finding the text from the server. It also makes it quicker to load the english version of the text, since I use English as the default text. The id references the table in the database table to pull the translated text which is organized with each column as a different language so an id can reference the same text and then matches it with the correct language. The params parameter was just to allow more full control over the text layout so I could loop through the params variable and apply those attributes to the text being displayed, things like color, text size, alignment, etc..


package com.chuggington.utils.text
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.text.AntiAliasType;
import flash.text.Font;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.getDefinitionByName;
public class LoadText extends Sprite
{
public var _main:MovieClip;
public var text:String;
public var loc:String;
public var _width_Number=50;
public var _height_Number=10;
public var embedFonts:Boolean = true;
public var mouseWheelEnabled:Boolean = false;
public var autoSize:String = "left";
public var multiline:Boolean = true;
public var wordWrap:Boolean = true;
public var font:String = "Gotham";
public var size:Number = 14;
public var color:Number = 0x000000;
public var align:String = "left";
public var translation:String = "";
public var id:int;
private var theFontClass:Class;
private var theFont:Font;
private var textFormat:TextFormat;
public var tf:TextField;
public function LoadText(main:MovieClip, defaultText:String ="", translationid_int=0, params_Object=null)
{
this._main = main;
this.loc = _main.loc;
this.id = translationid;
this.text = defaultText;
this.mouseEnabled = false;
for(var i in params){
Sprite(this)[i] = params[i];
}
if(loc!="en"){
//load translation
var textLoadVars:URLVariables = new URLVariables();
textLoadVars.id = this.id;
textLoadVars.loc = this.loc;
var textLoadReq:URLRequest = new URLRequest(dir+"translateText.php");
textLoadReq.data = textLoadVars;
textLoadReq.method = URLRequestMethod.POST;
var textLoader:URLLoader = new URLLoader();
textLoader.dataFormat = URLLoaderDataFormat.TEXT;
textLoader.load(textLoadReq);
textLoader.addEventListener( Event.COMPLETE, vHandle_textLoader_COMPLETE );
textLoader.addEventListener( IOErrorEvent.IO_ERROR, vHandle_textLoader_IO_ERROR );
}else{
//display default text
addTextField(text)
}
}
private function vHandle_textLoader_COMPLETE(e:Event):void
{
var foundVars:URLVariables = new URLVariables(e.target.data);
translation = foundVars.translation;
if(translation!="NULL"&&translation!=""){
addTextField(translation);
}else{
addTextField(text);
}
}
private function vHandle_textLoader_IO_ERROR(e:Event):void
{
trace("IOError:"+e);
}
private function addTextField(t:String)
{
theFontClass = getDefinitionByName(font) as Class;
theFont = new theFontClass() as Font;
textFormat = new TextFormat(theFont.fontName, size, color);
textFormat.align = align;
tf = new TextField();
tf.antiAliasType = AntiAliasType.NORMAL;
tf.selectable = false;
tf.mouseEnabled = this.mouseEnabled;
tf.mouseWheelEnabled = mouseWheelEnabled;
tf.embedFonts = embedFonts;
tf.width = _width;
tf.height = _height;
tf.autoSize = autoSize;
tf.multiline = multiline;
tf.wordWrap = wordWrap;
//tf.background = true;
tf.htmlText = t;
tf.setTextFormat(textFormat);
addChild(tf)
}
}
}

the rest to be continued…