• Ext本地化(Localizing Ext )


    Tutorial:Localizing Ext (Chinese)

    From Learn About the Ext JavaScript Library

    Jump to: navigation, search
    Summary: 如何本地化ext的教程
    Author: Jozef Sakalos(译者:Frank Cheung)
    Published: March 9, 2008
    Ext Version: 2.0+
    Languages: en.png Englishkr.png Koreancn.png Chinese

    Contents

    [hide]

    引言

    如果你是英语的用户就不必做任何本地化的工作了,这篇教程是为非英语用户所准备的,好像一般的用户,开发主管,业务员等,他们的外语可能稍逊,这样就需要你对如何本地化ext的整个流程了解一番了。

    慢慢开始

    如果你曾浏览Ext 2.x目录的树状结构,你就会发现source/locale的目录(或SVN目录的src/locale)。此目录包含了Ext本地化类。先不长篇大论地讲太多概念的东西,我们应了解如何先使用。


    下面的一个例子就是使用了本地化的ext,但是不是在ext同一个目录下的。因此通常的,你需要调整head标签内的路径,以正确指向Ext的安装目录。尤其注意本地化的那个目录路径。

    把你服务器的路径Copy and paste到相应文件路径中:

    注意: 下里的代码我是动态加载到head头部的,只能在FireFox通过。不过实际情况你不必如此,你只要在服务端指定script的路径而不必动态加载。

    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link rel="stylesheet" type="text/css" href="../ext/resources/css/ext-all.css">
        <script type="text/javascript" src="../ext/adapter/ext/ext-base.js"></script>
        <script type="text/javascript" src="../ext/ext-all-debug.js"></script>
        <script type="text/javascript">
     
        //根据url上指定的语言进行解码
        var locale = window.location.search 
                     ? Ext.urlDecode(window.location.search.substring(1)).locale 
                     : '';
     
        // 将本地化的JS文件加入head元素内
        var head = Ext.fly(document.getElementsByTagName('head')[0]);
        if(locale) {
            Ext.DomHelper.append(head, {
                 tag:'script'
                ,type:'text/javascript'
                ,src:'../ext/src/locale/ext-lang-' + locale + '.js'
            });
        }
     
        //预先定义的window继承类
        Ext.ns('Tutorial');
        Tutorial.LocalizationWin = Ext.extend(Ext.Window, {
            initComponent:function() {
                Ext.apply(this, {
                     500
                    ,id:'winid'
                    ,height:300
                    ,layout:'fit'
                    ,border:false
                    ,closable:false
                    ,title:Ext.get('title').dom.innerHTML
                    ,items:[{
                         xtype:'form'
                        ,frame:true
                        ,defaultType:'textfield'
                        ,items:[{
                              xtype:'combo'
                             ,fieldLabel:'Select Language'
                             ,name:'locale'
                             ,store:new Ext.data.SimpleStore({
                                 id:0
                                ,fields:['file', 'locale']
                                ,data:[
                                     ['cs', 'Czech']
                                    ,['', 'English']
                                    ,['fr', 'French']
                                    ,['de', 'German']
                                    ,['gr', 'Greece']
                                    ,['hu', 'Hungarian']
                                    ,['it', 'Italian']
                                    ,['ja', 'Japaneese']
                                    ,['pl', 'Polish']
                                    ,['pt', 'Portugal']
                                    ,['ru', 'Russian']
                                    ,['sk', 'Slovak']
                                    ,['es', 'Spanish']
                                    ,['sv_SE', 'Swedish']
                                    ,['tr', 'Turkish']
                                ]
                             })
                            ,listeners:{
                                select:{fn:function(combo){
                                    window.location.search = '?' 
                                        + Ext.urlEncode({locale:combo.getValue()})
                                    ;
                                }}
                            }
                            ,mode:'local'
                            ,editable:false
                            ,forceSelection:true
                            ,valueField:'file'
                            ,displayField:'locale'
                            ,triggerAction:'all'
                            ,value:locale
                         },{
                             fieldLabel:'Text Field'
                            ,allowBlank:false
                        },{
                             xtype:'datefield'
                            ,fieldLabel:'Date Field'
                            ,allowBlank:false
                        }]
                    }]
                }); // eo apply
                Tutorial.LocalizationWin.superclass.initComponent.apply(this, arguments);
            } // eo function initComponent
        }); // eo Tutorial.LocalizationWin
     
    Ext.BLANK_IMAGE_URL = '../ext/resources/images/default/s.gif';
    Ext.onReady(function() {
        Ext.QuickTips.init();
        Ext.form.Field.prototype.msgTarget = 'side';
     
        // create example window
        var win = new Tutorial.LocalizationWin();
        win.show();
    });
        </script>
        <title id="title">Localization Example</title>
    </head>
    <body>
    </body>
    </html>

    现在完整的本地化工作是限于ComboBox、TextField和DateField这些Ext基础组件。试试在已翻译好的DataPicker组件中输入一些文字。

    原理是??

    原理是一改变cobmo box里面的"Select Language"就会包含(include)相应的Ext本地化文件(这里我使用了一些技巧性的做法,改变本地化文件的过程是用过客户端脚本完成的)

    本地化文件的葫芦里卖的什么药?

    首先要打开本地化文件来看看,读懂里面的源码后不但对Ext的理解有帮助而且对整个程序的本地化,也是有帮助的。我们以法语版的DatePicker为例子:

    if(Ext.DatePicker){
       Ext.override(Ext.DatePicker, {
          todayText         : "Aujourd'hui",
          minText           : "Cette date est antérieure à la date minimum",
          maxText           : "Cette date est postérieure à la date maximum",
          disabledDaysText  : "",
          disabledDatesText : "",
          monthNames        : Date.monthNames,
          dayNames          : Date.dayNames,
          nextText          : 'Mois suivant (CTRL+Flèche droite)',
          prevText          : "Mois précédent (CTRL+Flèche gauche)",
          monthYearText     : "Choisissez un mois (CTRL+Flèche haut ou bas pour changer d'année.)",
          todayTip          : "{0} (Barre d'espace)",
          okText            : "&#160;OK&#160;",
          cancelText        : "Annuler",
          format            : "d/m/y",
          startDay          : 1
       });
    }

    通过观察,我们得知如果DataPicker对象存在(这是需要检测的,事因有些ext是你自己可制定的)就重写DataPicker的部分属性;更严格说,我们是把法语的文字替换掉英语的文字而已。Ext.override的作用是在DatePicker建立实例之前改变class的原型。

    应用程序的本地化

    我们对范例文件略加修改,把所有的静态文本变为类成员,修改过后看起来是这样的:

    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link rel="stylesheet" type="text/css" href="../ext/resources/css/ext-all.css">
        <script type="text/javascript" src="../ext/adapter/ext/ext-base.js"></script>
        <script type="text/javascript" src="../ext/ext-all-debug.js"></script>
     
        <!-- Ext localization javascript -->
        <script type="text/javascript" id="extlocale"></script>
     
        <!-- Locale and example extension javascript -->
        <script type="text/javascript">
     
        // decode language passed in url
        var locale = window.location.search 
                     ? Ext.urlDecode(window.location.search.substring(1)).locale 
                     : ''
        ;
     
        // append locale script to the head
        var head = Ext.fly(document.getElementsByTagName('head')[0]);
        if(locale) {
            Ext.fly('extlocale').set({src:'../ext/src/locale/ext-lang-' + locale + '.js'});
        }
     
        // create pre-configured example window extension class
        Ext.ns('Tutorial');
        Tutorial.LocalizationWin = Ext.extend(Ext.Window, {
             titleText:'Localization Example'
            ,selectLangText:'Select Language'
            ,textFieldText:'Text Field'
            ,dateFieldText:'Date Field'
            ,initComponent:function() {
                Ext.apply(this, {
                     500
                    ,id:'winid'
                    ,height:300
                    ,layout:'fit'
                    ,border:false
                    ,closable:false
                    ,title:this.titleText
                    ,items:[{
                         xtype:'form'
                        ,frame:true
                        ,defaultType:'textfield'
                        ,items:[{
                              xtype:'combo'
                             ,fieldLabel:this.selectLangText
                             ,name:'locale'
                             ,store:new Ext.data.SimpleStore({
                                 id:0
                                ,fields:['file', 'locale']
                                ,data:[
                                     ['cs', 'Czech']
                                    ,['', 'English']
                                    ,['fr', 'French']
                                    ,['de', 'German']
                                    ,['gr', 'Greece']
                                    ,['hu', 'Hungarian']
                                    ,['it', 'Italian']
                                    ,['ja', 'Japaneese']
                                    ,['pl', 'Polish']
                                    ,['pt', 'Portugal']
                                    ,['ru', 'Russian']
                                    ,['sk', 'Slovak']
                                    ,['es', 'Spanish']
                                    ,['sv_SE', 'Swedish']
                                    ,['tr', 'Turkish']
                                ]
                             })
                            ,listeners:{
                                select:{fn:function(combo){
                                    window.location.search = '?' 
                                        + Ext.urlEncode({locale:combo.getValue()})
                                    ;
                                }}
                            }
                            ,mode:'local'
                            ,editable:false
                            ,forceSelection:true
                            ,valueField:'file'
                            ,displayField:'locale'
                            ,triggerAction:'all'
                            ,value:locale
                         },{
                             fieldLabel:this.textFieldText
                            ,allowBlank:false
                        },{
                             xtype:'datefield'
                            ,fieldLabel:this.dateFieldText
                            ,allowBlank:false
                        }]
                    }]
                }); // eo apply
                Tutorial.LocalizationWin.superclass.initComponent.apply(this, arguments);
            } // eo function initComponent
        }); // eo Tutorial.LocalizationWin
        </script>
     
        <!-- Application localization javascript -->
        <script type="text/javascript" id="applocale"></script>
     
        <!-- Set src for application localization javascript -->
        <script>
        if(locale) {
            Ext.fly('applocale').set({src:'app-lang-' + locale + '.js'});
        }
        </script>
     
        <!-- Main application -->
        <script type="text/javascript">
        Ext.BLANK_IMAGE_URL = '../ext/resources/images/default/s.gif';
        Ext.onReady(function() {
            Ext.QuickTips.init();
            Ext.form.Field.prototype.msgTarget = 'side';
     
            // create example window
            var win = new Tutorial.LocalizationWin();
            win.show();
        });
        </script>
        <title id="title">Localization Example</title>
    </head>
    <body>
    </body>
    </html>

    最后一步,我们需要为特定的语种创建程序本地化的文件(翻译,语种)。这里是app-lang-sk.js文件因为我操的是斯洛伐克语的缘故。你可因应你需求的语种去设置(保存html的格式放到同一目录下或修改上面代码的目录):

    /**
     * 斯洛伐克语版的教程之本地化文件
     */
    if(Tutorial.LocalizationWin) {
        Ext.override(Tutorial.LocalizationWin, {
             titleText:'Príklad lokalizácie'
            ,selectLangText:'Zvoľ jazyk'
            ,textFieldText:'Textové pole'
            ,dateFieldText:'Dátumové pole'
        });
    }

    高级提示

    请明确,要做Ext程序的本地化需要遵循哪些的原则。关键点在于将所有翻译文本作为公共类的成员出现,重写原先类的prototype的属性

    Advanced developers will immediately feel some drawback of having, maintaining and deploying that many localization files especially if they already have a server with some translating infrastructure such as GNU gettext. For these users I would recommend to generate localization files on the fly by the server using gettext as backend.

    这种做法最大的好处是可讲翻译文件集中在一块(*.po),客户端和服务端都可以使用。

    如果本地化这主题反映还不错的话而读者又非常想进一步了解我会写进阶的Ext本地化。

    Enjoy!

    延伸阅读:

  • 相关阅读:
    python 解释器交互模块 -- sys
    python 操作系统模块 -- OS
    python 随机数模块 -- random
    python 时间模块 -- time
    Python 面向对象
    python 模块
    python -- 面向对象进阶
    github连接提示
    linux day4
    git基本使用
  • 原文地址:https://www.cnblogs.com/meetrice/p/1206184.html
Copyright © 2020-2023  润新知