您可以创建语言文件为对象,如果您使用的需要,例如:
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') %>" />