mardi 17 février 2015

using variables in resource files with requirejs/i18n


Hello everyone,


I'd like to use the i18n (for require.js) library to load my stored strings in resource files based on the user's language. I have used this aproach, since I'm using both backbone and require.js in my project.


Let's say this is my template where I'd like to use the translated string.



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


The first line is evaluated with the data taken from the resource file. But the second line , is where I'd like to use data from different source.


(default resource file)



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


now there is the part where I'd like to render this to my view.



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;
});


I'd like to achieve this output:



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


but I'm not sure how do I put two sources of data for into one template.





Aucun commentaire:

Enregistrer un commentaire