问题描述
我想要做一些基本的国际化在我的应用
I want to do some basic i18n in my application
我装了i18n的插件(从require.js)到我的应用程序并创建了一个目录 NLS
中,我有几个文件,例如 project.js
I loaded the i18n plugin (from require.js) to my Application and created a directory nls
in which i have several files e.g. project.js
我 main.js
设置默认文件位置像
config : {
i18n : {
locale : 'de-de'
}
}
我现在想用在我看来/模板的文件。谁能向我解释如何做到这一点?模板是在一个template.js文件设置
I now want to use the files in my View / Template. Can somebody explain to me how this is done? The templates are setup in one template.js file
我的观点:
define(['marionette', 'templates', 'i18n!nls/project'], function (marionette, templates, msg) {
return marionette.CompositeView.extend({
template : templates.project
});
});
我的模板:
<div class="container">
<div class="well">
<h2>Projects</h2>
</div>
</div>
有人能向我解释如何使用该文件在我看来/模板?在此先感谢!
Can someone explain to me how to use the file in my View / Template? Thanks in advance!
编辑:
我想通了一些尝试和放大器解决方案;错误。由于我是通过第三方物流require.js装载模板!插件我不需要编译它们,如果我通过称他们需要('TPL!模板/ dashboard.tmpl')
。我可以简单地通过我通过国际化!NLS /仪表盘
加载的i18n文件。在木偶认为是默认渲染,所以我这样做:
I figured out the solution by some try&error. As i am loading the templates via the require.js tpl! plugin i dont need to compile them if i call them by require('tpl!templates/dashboard.tmpl')
. I can simply pass the i18n file i loaded by 'i18n!nls/dashboard'
. In Marionette the view are rendered by default, so i did this:
define(['marionette', 'templates', 'i18n!nls/dashboard'], function (Marionette, templates, msg) {
return Marionette.CompositeView.extend({
template : function () {
return templates.dashboard(msg);
},
initialize : function() {
}
});
});
为国际化插件的文件以及在这里解释说:
我不得不做这一步一步来,首先我错过了根,那么我不知道为什么我的德语语言环境没有加载,但我只是忘了设置去德:真
在根文件。现在,一切工作就像一个魅力
I had to do this step by step, first i missed the root, then i wondered why my german locale did not load, but i simply forgot to set de-de : true
in the root file. Now everything is working like a charm
推荐答案
在装入通过require.js到您的视图的I18文件。
我用的车把模板在本实施例。
first you load the i18 file via require.js to your view.I use the handlebars templates in this example.
define([
'marionette',
'handlebars',
'text!modules/tableModule/templates/myTmpl.html',
'i18n!nls/dashboard',
],
function(Marionette, Handlebars, tmpl, locals) { ...
然后编译并与I18对象加载您的模板。
then you compile and load your template with the i18 object.
var template = Handlebars.compile(tmpl);
this.template = template(locals.myVar);
你可以去和做复杂的组合以及
you can go and do complex combinations as well
var template = Handlebars.compile(tmpl);
var data =_.extend(options, {lang:locals});
this.template = template(data);
您的NLS文件看起来像这样
your nls file will look like this
define({
"root": {
"myVar" : "some text in",
"canBeAnObjectTo": {
"title" : "my title ",
"contact" : "Contact",
}
和你的看法会是这样的:
and your view will be something like this:
<div class="cssClass">
<div class="table-caption pull-left">{{this.myVar}}</div>
</div>
希望帮助
这篇关于木偶+国际化模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!