您可以創建語言文件爲對象,如果您使用的需要,例如:
locale/en_gb.js
define({
buttons: {
ok: 'Ok',
error: 'Error',
prompts: {
saveChanges: 'You have unsaved changes. Would you like to save them?',
quit: 'Are you sure you want to quit?'
}
},
pages: {
home: 'Home'
contact: 'Contact'
}
});
您有不同的語言,語言文件。
在您的擴展視圖,您可以創建這樣一些方法:
localeBase: null,
currentLocale: null,
loadLocale: function(localeName) {
require('locale/'+localeName, function(Locale) {
this.currentLocale = Locale;
});
},
l: function(lookupStr) {
// Lookup 'lookupStr' in 'this.currentLocale'
}
的l
方法採取.
標註的查詢字符串,如:
"pages.home"
回報"Home"
或者,如果localeBase
設爲例如:
"buttons"
這時如果字符串啓動帶.
:
".prompts.quit"
將相對查找到localeBase
(如果它不與點查找從開始AKA絕對查找開始)。
請注意,localeBase
不是一個必要的概念,它取決於您的偏好。
在你View
render
方法,您通過視圖作爲template context
:
var html = myTemplateRenderer_Function(this);
this.el.html(html);
你的模板文件,然後可以訪問l
方法,所以你可以做的東西,如:
<button value="<%= l('buttons.ok') %>" />