2015-02-17 63 views
0

我想使用i18n(for require.js)库根据用户的语言在资源文件中加载存储的字符串。 我已经使用this的方法,因为我在我的项目中使用backbone和require.js。对下划线模板使用多个数据源

假设这是我的模板,我想使用翻译后的字符串。

<h1><%= test.options.test %></h1> 
    <b> user_ud: <%= data.id %> </b> 

第一行使用从资源文件获取的数据进行评估。 但第二行,我想使用来自不同来源的数据。

(默认的资源文件)

define({ 
    'root': { 
     'options': { 
      'test': 'Yellow' 
     } 
    }, 
    "en-us": true 
}); 

现在存在这样的情况,我想使这个我的看法的一部分。

define(['underscore', 'backbone', 'models/model', 'templates/template' , 'i18n!nls/resource'], function (_, Backbone, tModel, template, resource) { 
    var TooltipView = Backbone.View.extend({ 
     el : $('#test'), 

     initialize: function(options){ 
      this.model = new tModel(); 
     }, 

     render: function(){ 
      var $el = this.$el; 
       if(template.length != 0){ 
        var compiledTemplate = template['test'](resource) /// loads pre-compiled template ///   
        $el.html(compiledTemplate); 
       }else{ 
        console.log(" [e] No template found. "); 
       } 
      }); 
     } 
    }); 

    return TooltipView; 
}); 

我想实现这个输出:

<h1> Yellow </h1> 
<b> id: 14 </b> 

,但我不知道该怎么办,我把两个数据源合并成一个模板。

回答

1

你可以用resource和模型数据到一个新的对象,这将成为在模板中使用根对象:

var compiledTemplate = template['test']({ 
    i18n: resource, 
    data: this.model.toJSON() 
}); 

然后在模板

<h1><%= i18n.test.options.test %></h1> 
<b> user_ud: <%= data.id %> </b> 
+0

实际上是相同的弹出的访问它们在我关掉计算机之后,我想在今天早上删除我的问题,但是你的速度更快,你的答案确实是正确的。谢谢! – Wracker 2015-02-18 06:37:12