[英]ember-i18n - Internalization Support
我目前正在使用jamesarosen / ember-i18n在我的ember 1.5.1 app上進行內部化支持。
我有兩種語言。 英語和法語。
Em.I18n.translations = {
en: {
animal: {
cat: 'cat',
dog: 'dog'
}
},
fr: {
animal: {
cat: 'chat',
dog: 'chien'
}
}
};
在我的車把模板中,我有: -
{{t animal.cat}}
但是我得到的消息是:
缺少翻譯:animal.cat。
如果我放置它會工作:
{{t en.animal.cat}} or {{t fr.animal.cat}}
讓這個工作並使其在兩種語言之間自動切換的最佳做法是什么? 我已經嘗試將此設置在我的文件頂部:
Em.I18n.locale = 'fr';
我找到了答案:
添加此幫助器:
Ember.Handlebars.registerHelper('i18n', function(property, options) {
var params = options.hash,
self = this;
// Support variable interpolation for our string
Object.keys(params).forEach(function (key) {
params[key] = Em.Handlebars.get(self, params[key], options);
});
property = Em.I18n.locale + '.' + property;
return Em.I18n.t(property, params);
});
並確保設置:
Em.I18n.locale = 'en';
使用更新的車把參考:
{{i18n animal.cat}}
@湯姆的解決方案很好,但只適用於助手。
更一般地說,如果要使用例如Em.I18n.TranslateableProperties
,則必須單獨處理它們。
在我看來,一旦你想改變語言環境,就可以改變Em.I18n.translations
的全部內容。
這需要刷新頁面,因為你沒有在Ember.I18n.locale上設置任何監聽器。 它可以動態地完成,但它會變得有點復雜。
除此之外,最好的選擇可能是翻譯來自后端。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.