【问题标题】:ExtJS 4.0: Localization issuesExtJS 4.0:本地化问题
【发布时间】:2011-10-27 13:16:43
【问题描述】:

我密切关注本地化指南:http://docs.sencha.com/ext-js/4-0/#!/guide/localization,但是我无法使其在 MVC 模式下工作。 我不需要像前面的例子那样动态本地化,我只想在应用程序加载时设置它。

我试过这样:

Ext.application({
name: 'KS',

appFolder: 'app',

controllers: ['Menu', 'DailyReport', 'DP'],

launch: function() {
    Ext.Ajax.request({
        url: 'lib/ext-4.0/locale/ext-lang-es.js',
        success: function(response, opts) {
            eval(response.responseText);
        },
        failure: function() {
            Ext.Msg.alert('Error', 'Error al cargar archivos de idioma.');
        }
    });
    Ext.create('Ext.container.Viewport', {
        items: [{
            xtype: 'menu'
        },
        {
            xtype: 'dpedit'
        }]
    });

}
});

在萤火虫中,我得到:“Ext.view 未定义”错误,并且没有渲染。如果我在创建视口后尝试 Ajax 调用,我不会收到任何错误,但不会应用翻译。

【问题讨论】:

    标签: extjs localization extjs4


    【解决方案1】:

    更优雅的解决方案是让自动加载器在启动方法运行之前加载类。 您可以通过根据需要定义 Ext.view.View 来做到这一点:

    Ext.application({
        name: 'KS',
    
        appFolder: 'app',
    
        controllers: ['Menu', 'DailyReport', 'DP'],
    
        // This will make shure the class is loaded before your application runs:
        requires : ['Ext.view.View'],
    
        launch: function() {
            Ext.Ajax.request({
                url: 'lib/ext-4.0/locale/ext-lang-es.js',
                success: function(response, opts) {
                    eval(response.responseText);
                },
                failure: function() {
                    Ext.Msg.alert('Error', 'Error al cargar archivos de idioma.');
                }
            });
            Ext.create('Ext.container.Viewport', {
                items: [{
                    xtype: 'menu'
                },
                {
                    xtype: 'dpedit'
                }]
            });
        }
    });
    

    更多详情请参考extjsapi

    【讨论】:

    • 这不起作用,因为本地化会一一请求所有其他组件:网格、表单字段、选择器等。
    【解决方案2】:

    它将在生产模式下工作,即在您的应用程序启动之前加载所有 ext javascript 文件。我也有这个问题。尝试这样做来测试:导入“ext-all.js”文件,然后导入您的语言文件。这将起作用。不是最好的解决方案,但我发现唯一可行的解​​决方案。

    你的问题的原因:

    如果您打开翻译文件,您会注意到如下指令:

    Ext.view.View.prototype.emptyText = "";
    

    如果在您加载翻译文件的那一刻没有加载文件“Ext.view.View.js”,您将收到错误消息,因为“Ext.view.View”类不存在.

    我希望任何人都可以帮助您提供更好的解决方案。

    【讨论】:

    • 有效,但正如你所说,这不是最优雅的解决方案。
    【解决方案3】:

    我很容易解决了这个问题。这很简单,而且效果会更好。把它放在你的文档头部,在 ext.js/ext-all.js 之后,在你的 app.js 之前。 (我根据本地化指南将其放在 language.js 的底部)

    var params = Ext.urlDecode(window.location.search.substring(1));
    if (params.lang) {
      var url = Ext.util.Format.format('/assets/extjs/locale/ext-lang-{0}.js', params.lang);
      document.write("<script src='" + url + "'><\/script>");
    }
    

    我正在使用 /assets 来处理 rails 3.1。

    这有助于查询参数中的 ?lang=fr,应用程序的其余部分应该按照指南工作。

    http://docs.sencha.com/ext-js/4-0/#!/guide/localization

    【讨论】:

      猜你喜欢
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 2012-03-26
      • 1970-01-01
      • 2011-02-18
      • 2011-07-11
      • 2011-02-23
      相关资源
      最近更新 更多