2013-02-10 110 views
1

我想要做一些基本的國際化在我的應用程序木偶+國際化

我裝了i18n的插件(從require.js)到我的應用程序和創建的目錄nls其中我有幾個文件,例如project.js

main.js文件中設置的默認位置像

config : { 
    i18n : { 
     locale : 'de-de' 
    } 
} 

我現在想用在我看來/模板文件。有人可以向我解釋這是如何完成的嗎?這些模板安裝在一個文件template.js

筆者認爲:

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> 

有人能向我解釋如何使用文件在我看來/模板?提前致謝!

編輯:

我想通了一些解決方案嘗試&錯誤。正如我通過require.js tpl加載模板!插件我不需要編譯它們,如果我打電話給他們require('tpl!templates/dashboard.tmpl')。我可以簡單地通過我加載的i18n文件'i18n!nls/dashboard'。在木偶認爲是默認渲染,所以我這樣做:

define(['marionette', 'templates', 'i18n!nls/dashboard'], function (Marionette, templates, msg) { 

    return Marionette.CompositeView.extend({ 

     template : function() { 
      return templates.dashboard(msg); 
     }, 

     initialize : function() { 

     } 
    }); 

}); 

的國際化插件的文件以及這裏解釋: http://requirejs.org/docs/api.html#i18n

我不得不做這一步一步來,首先我錯過根,然後我想知道爲什麼我的德語區域設置沒有加載,但我只是忘了在根文件中設置de-de : true。現在一切都像魅力

回答

5

首先你通過require.js加載i18文件到你的視圖。 我在這個例子中使用了把手模板。

define([ 
    'marionette', 
    'handlebars', 
    'text!modules/tableModule/templates/myTmpl.html', 
    'i18n!nls/dashboard', 
], 
function(Marionette, Handlebars, tmpl, locals) { ... 

然後你編譯並加載你的模板與i18對象。

var template = Handlebars.compile(tmpl); 
this.template = template(locals.myVar); 

,你可以去和做複雜的組合以及

var template = Handlebars.compile(tmpl); 
    var data =_.extend(options, {lang:locals}); 
    this.template = template(data); 

您的NLS文件看起來像這樣

define({ 

    "root": { 
     "myVar" : "some text in", 
     "canBeAnObjectTo": { 
         "title" : "my title ", 
         "contact" : "Contact", 
      } 

,你的看法會是這樣的:

<div class="cssClass"> 
<div class="table-caption pull-left">{{this.myVar}}</div> 
    </div> 

希望能幫到

+0

非常感謝你,我不得不做的事情有點不同,它幫助了我很多,我會在原始帖子中發佈我的結果 – pfried 2013-02-11 08:51:20