[AIR接入Android 平台]处理res资源之文字国际化

来源:互联网 发布:c语言电影院售票系统 编辑:程序博客网 时间:2024/05/22 05:06

原文:http://www.shadowkong.com/archives/1132

一点声明:我的水平有限 这里仅仅只能写出我认为可行的方案 和 代码

文字国际化 其实是android编码的规范之一。但是不同的运营商提供的SDK 这块处理的都不一样。

就目前我接触过的平台来说。当乐 UC 91 360 算做得很好。

但是其他小平台  特别是有一个运营商 他们客户端技术才一个人。

都很不注重android文字国际化的问题。打包进ane的资源 布局文件XML必须是规范的国际化处理的。

否则就会在打包APK的时候报错。 例如下面:

 

 

一般这个错误在打包ANE的时候是不会报的。而在打包APK的时候就会报出来。

如图所示 这个错误主要是由于  ANE中的资源文件夹RES下的layout文件夹下的布局XML中的text 被SDK客户端程序员直接用代码写的。所以会直接写在layout的布局XML里面。

而一般android官方开发标准推荐更标准化的写法。把layout中的text的内容写入 value文件夹下的string.xml中。

而我们的ADT打包就是遵从这种标准。

例如layout某XML的一段:

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”账号 : “
android:textColor=”#A67D3D”
android:textSize=”17sp” />
红色部分的写法是错误的 正确的写法是 把红色部分变为:
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/text001 “
android:textColor=”#A67D3D”
android:textSize=”17sp” />
在res下的value文件夹下的string.xml中加入:
<string name=”text001“>账号 :</string>
这样才是规范的res文件。
为了批量出来这种事情 我写了一个工具类
 
resHandle.as
package {     import flash.desktop.Clipboard;    import flash.desktop.ClipboardFormats;    import flash.desktop.NativeDragManager;    import flash.display.InteractiveObject;    import flash.display.Loader;    import flash.display.Sprite;    import flash.events.Event;    import flash.events.MouseEvent;    import flash.events.NativeDragEvent;    import flash.filesystem.File;    import flash.filesystem.FileMode;    import flash.filesystem.FileStream;    import flash.net.FileFilter;    import flash.net.FileReference;    import flash.net.URLLoader;    import flash.net.URLRequest;    import flash.text.TextField;    import flash.text.TextFormat;        import flashButton.backButton;        /**     *      * @author Rect  2013-4-16      *      */    [SWF(width="800" , height="600")]    public class FileXMLHandle extends Sprite     {         private var sp:Sprite;        private var mapAry:Array = [];        private var test:TextField;        private var beginBtn:backButton;        private var SelectBtn:backButton;        private var outBtn:backButton;        private var textSize:TextField;        private var textQuality:TextField;        private var fileObj:FileReference = new FileReference();        private var selectedSaveFile:File;        private var fileArr:Array = [];        private var key:String;        public function FileXMLHandle()        {            super();            init();        }         private function init():void{            sp = new Sprite();            test = new TextField();            test.defaultTextFormat = (new TextFormat("",18,0xFFFFF));            test.text = "拖入此处";            test.width = 400;            test.height = 400;            test.wordWrap = true;            test.multiline = true;                        sp.addChild(test);            sp.graphics.beginFill(0x3CB371);            sp.graphics.drawRect(0,0,800,600);            sp.graphics.endFill();            this.addChild(sp);            sp.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, onDragInHandler);            sp.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onDropHandler);                        selectedSaveFile = new File();            selectedSaveFile.addEventListener(flash.events.Event.SELECT, onSelectedSaveFile);            var fileFilt:FileFilter = new FileFilter("输出文件(*.xml)", "*.xml");            fileArr.push(fileFilt);                        beginBtn = new backButton("开始");            beginBtn.x = 600,beginBtn.y = 110;            beginBtn.mouseEnabled = false;            beginBtn.addEventListener(MouseEvent.CLICK,onChick);            this.addChild(beginBtn);                        SelectBtn = new backButton("选择string.xml");            SelectBtn.x = 600,SelectBtn.y = 10;            SelectBtn.addEventListener(MouseEvent.CLICK,onSelectFile);            this.addChild(SelectBtn);                        outBtn = new backButton("选择输出文件夹");            outBtn.x = 600,outBtn.y = 60;            outBtn.addEventListener(MouseEvent.CLICK,onSelectPath);            this.addChild(outBtn);                                }                private function onSelectedSaveFile(e:flash.events.Event):void        {            var str:String = selectedSaveFile.nativePath;            var __reg:RegExp = new RegExp('\\\', '');                        switch(key)            {                case "file":                    motherFileArys = str.replace(/\\/g,"/");                    break;                                case "path":                    outPath = str.replace(/\\/g,"/");                    trace(outPath)                    break;            }                        if(motherFileArys && outPath)HandleMaps();        }        private var motherFileArys:String = null;        private var outPath:String = null;        private var _loaderImages:URLLoader  = new URLLoader();        private var stringXML:XML;        private var miscXML:XML;        private function HandleMaps():void{                _loaderImages.addEventListener(Event.COMPLETE,loaderInitListeners);                _loaderImages.load(new URLRequest(motherFileArys));        }                private function loaderInitListeners(evt:Event):void {            _loaderImages.removeEventListener(Event.COMPLETE,loaderInitListeners);            stringXML = XML(_loaderImages.data);            _loaderImages.close();                    }                /**********************************/        private function onChick(ev:MouseEvent):void{                        HandleMap();        }                private function onSelectFile(ev:MouseEvent):void{            key = "file";            selectedSaveFile.browse(fileArr);        }                private function onSelectPath(ev:MouseEvent):void{            key = "path";            selectedSaveFile.browseForDirectory("请选择目录");        }                private function textDown(ev:MouseEvent):void{            var text:TextField = ev.currentTarget as TextField;            text.text = "";        }        protected function onDragInHandler(event : NativeDragEvent) : void        {            var transferable :Clipboard = event.clipboard;            if(transferable.hasFormat(ClipboardFormats.FILE_LIST_FORMAT))            {                var files : Array = transferable.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;                if(files)                {                    var file : File = File(files[0]);                                        if(file && file.name.indexOf(".xml") >= 0 || file && file.name.indexOf(".XML") >= 0)                    {                        NativeDragManager.acceptDragDrop(event.currentTarget as InteractiveObject);                    }                }                            }        }                private var motherFileAry:Array = [];        private var sonFileAry:Array = [];        private var xmlHead:String = '<?xml version="1.0" encoding="utf-8" ?>';        protected function onDropHandler(event : NativeDragEvent) : void        {            motherFileAry = [];            sonFileAry = [];            var transferable :Clipboard = event.clipboard;            var files : Array = transferable.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;            if(files)            {                var file :File;                for(var t:int = 0;t<files.length;t++){                    file  = File(files[t]);                    test.appendText("\n"+file.url);                    motherFileAry.push(file.url);                                        sonFileAry.push(outPath+"/"+ file.name);                }                if(sonFileAry.length != 0 ){                    if(motherFileArys && outPath)this.beginBtn.mouseEnabled = true;                }else{                }                                trace(motherFileAry,outPath);//(file.name.length - 4))));            }        }                private var _loaderImage:URLLoader  = new URLLoader();        private function HandleMap():void{            if(motherFileAry.length){                _loaderImage.addEventListener(Event.COMPLETE,loaderInitListener);                _loaderImage.load(new URLRequest(motherFileAry.splice(0,1)));            }else            {                this.beginBtn.mouseEnabled = false;                test.text = ("完成!");                makeValue();            }        }                private function makeValue():void        {                        for(var t:int = 0;t<stringArr.length;t++)            {                stringXML.appendChild(<string name = {stringName[t]}>{stringArr[t]}</string>);            }                        var filePath:String = motherFileArys ;            var f:FileStream = new FileStream();            var fl:File = new File(filePath);                        f.open(fl,FileMode.WRITE);            var xmlStr1:String = stringXML.toString();            var pattern1:RegExp = /\n/g;            xmlStr1=xmlStr1.replace(pattern1, "\r\n");            f.writeUTFBytes(String(xmlHead+"\r\n"+ xmlStr1));            f.close();                                }                private function loaderInitListener(evt:Event):void {                        _loaderImage.removeEventListener(Event.COMPLETE,loaderInitListener);                        var xml:XML = XML(_loaderImage.data);                        walk(xml);                        makeFile(xml);                        HandleMap();        }                        private function makeFile(str:XML):void{                        var filePath:String = (sonFileAry.splice(0,1)) ;            var f:FileStream = new FileStream();            var fl:File = new File(filePath);                        f.open(fl,FileMode.WRITE);                        var xmlStr:String = str.toString();            var pattern:RegExp = /\n/g;            xmlStr=xmlStr.replace(pattern, "\r\n");            f.writeUTFBytes(String(xmlHead+"\r\n"+ xmlStr));                        f.close();                    }                private var stringArr:Array = [];        private var stringName:Array = [];        private var stringLen:int = 0;                        private function walk( node:XML ):void {                        var androidNS:Namespace = new Namespace("android","http://schemas.android.com/apk/res/android");            node.addNamespace(androidNS);                                    // Loop over all of the child elements of the node            for each ( var element:XML in node.elements( ) ) {                // Output the label attribute                var attributes:XMLList = element.attributes( );                                for(var t:int = 0;t<100;t++)                {                    if(!attributes[t])                        break;                                        var str:String = attributes[t] ;                    var pattern4:RegExp=/[\u4e00-\u9fa5]/;                     if(pattern4.test(str))                    {                        stringArr.push(str);                        stringName.push("DW_string_" + stringLen);                                                attributes[t] = "@string/DW_string_" + stringLen.toString();                                                stringLen++;                    }                                            }                walk( element );            }        }    }}


Buton

package flashButton{    import flash.display.Sprite;    import flash.events.MouseEvent;    import flash.text.TextField;    import flash.text.TextFieldAutoSize;    import flash.text.TextFormat;    public class backButton extends Sprite    {        private var _buttonName:String;        private var _szWide:Number;        private var _szHeight:Number;        private var _Color:int;        public function backButton(            buttonName:String,            szWide:Number = 180,            szHeight:Number = 40,            Color:int = 0xFFFFFF)        {            _buttonName = buttonName;            _szWide = szWide;            _szHeight = szHeight;            _Color = Color;            init();        }        private var _text:TextField;        private var _textFormat:TextFormat;        private function init():void{            _text = new TextField();            _text.mouseEnabled = false;            _text.text = _buttonName;            _text.x = 2;            _text.y = 2;            _text.width = _szWide-5;            _text.height = _szHeight-5;            _text.autoSize = TextFieldAutoSize.CENTER;            _textFormat = new TextFormat();            _textFormat.size = 25;            _textFormat.color = 0;//0x228b22;            _text.setTextFormat(_textFormat);            addChild(_text);            this.graphics.beginFill(_Color,.9);            this.graphics.drawRoundRect(0,0,_szWide,_szHeight,5,5);            this.graphics.endFill();            this.addEventListener(MouseEvent.ROLL_OVER,onMouseOver);            this.addEventListener(MouseEvent.ROLL_OUT,onMouseOut);        }        private function onMouseOut(ev:MouseEvent):void{            this.graphics.clear();            this.graphics.beginFill(_Color,.9);            this.graphics.drawRoundRect(0,0,_szWide,_szHeight,5,5);            this.graphics.endFill();        }        private function onMouseOver(ev:MouseEvent):void{            this.graphics.clear();            this.graphics.beginFill(0xeedd82,.9);            this.graphics.drawRoundRect(1,1,_szWide-1,_szHeight-1,5,5);            this.graphics.endFill();        }    }}


 

使用方式:

A.选择 要处理的资源文件 res/value/string.xml

B.选择输出文件夹(用来输出处理过的布局xml)

C.拖动 res/layout下的所有XML到程序界面

D.点击开始 E.结束后把输出文件夹的XML复制到res/layout下完工。。

PS:这里只针对中文 一般英文的比较少 手动修改就是了

 

0 0
原创粉丝点击