translation

by wh0
translate textfields in the display list...
it doesn't work as well as I want it to
♥0 | Line 58 | Modified 2011-05-08 15:29:09 | MIT License | (replaced)
play

ActionScript3 source code

/**
 * Copyright wh0 ( http://wonderfl.net/user/wh0 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/rTkS
 */

package {
    import flash.geom.Rectangle;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.system.*;
    import flash.text.*;
    
    import com.adobe.serialization.json.JSON;
    import com.actionscriptbible.Example;
    public class FlashTest extends Example {
        
        private static const V:String = '1.0';
        private static const LANGPAIR:String = '|en';
        private static const BASE:String = 'http://ajax.googleapis.com/ajax/services/language/translate?v=' + encodeURIComponent(V) + '&langpair=' + encodeURIComponent(LANGPAIR) + '&q=';
        private static const SENTINEL:String = '\uFEFF'; // zwnbsp
        
        private static const sample:String = 'http://swf.wonderfl.net/swf/usercode/7/7d/7d68/7d68db6fc691d168b2f326339afea87146d3d503.swf';
        private var content:DisplayObject;
        
        public function FlashTest() {
            loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, function (e:UncaughtErrorEvent):void { trace(e.error); });
            
            trace('loading sample');
            var l:Loader = new Loader();
            l.contentLoaderInfo.addEventListener(Event.COMPLETE, complete);
            // use separate ApplicationDomain in case it's also called FlashTest
            l.load(new URLRequest(sample), new LoaderContext(false, new ApplicationDomain()));
            addChild(l).y = 200;
        }
        
        private function complete(e:Event):void {
            content = e.target.content;
            trace('traversing display list');
            search(content);
        }
        
        private function search(o:DisplayObject):void {
            if (o is DisplayObjectContainer) {
                // container: recurse on children
                var c:DisplayObjectContainer = o as DisplayObjectContainer;
                for (var i:int = 0; i < c.numChildren; i++) {
                    search(c.getChildAt(i));
                }
            } else if (o is TextField) {
                // textfield: replace text
                var t:TextField = o as TextField;
                if (!t.text || t.text.charAt(0) == SENTINEL || /^\d+$/.test(t.text)) return;
                // highlight it until we have the translation
                var r:Rectangle = t.getRect(this);
                var sh:Shape = new Shape();
                sh.graphics.beginFill(0xffff00, 0.25);
                sh.graphics.drawRect(r.x, r.y, r.width, r.height);
                addChild(sh);
                trace('translating ' + t.text);
                translate(t.text, function (s:String):void {
                    t.text = SENTINEL + s;
                    removeChild(sh);
                });
            }
        }
        
        private function translate(q:String, callback:Function):void {
            // this is just a wrapper around the REST API
            var ul:URLLoader = new URLLoader(new URLRequest(BASE + encodeURIComponent(q)));
            ul.addEventListener(Event.COMPLETE, function (e:Event):void {
                callback(JSON.decode(ul.data).responseData.translatedText.replace(/&#39;/g, '\''));
            });
        }
        
    }
}