有沒有任何助手讓模板知道何時使用複數單詞?你如何處理Ember中的多元化?
在下面的例子中,你如何使模板輸出「2狗有...」?
代碼:
Ember.View.create({dog_count: 2})
模板:
{{dog_count}} (dog has)/(dogs have) gone for a walk.
有沒有任何助手讓模板知道何時使用複數單詞?你如何處理Ember中的多元化?
在下面的例子中,你如何使模板輸出「2狗有...」?
代碼:
Ember.View.create({dog_count: 2})
模板:
{{dog_count}} (dog has)/(dogs have) gone for a walk.
我知道這是舊的,但我今天需要它,所以在這裏。
Ember.Handlebars.registerBoundHelper('pluralize', function(number, opts) {
var single = opts.hash['s'];
Ember.assert('pluralize requires a singular string (s)', single);
var plural = opts.hash['p'] || single + 's';
return (number == 1) ? single : plural;
});
用法:
{{questions.length}} {{pluralize questions.length s="Question"}}
或
{{dog_count}} {{pluralize dog_count s="dog has" p="dogs have"}} gone for a walk.
複數(P =)選項只在必要的時候,你不想標準+行爲。
您贏得正確簡單答案的獎品。這些其他人努力嘗試,但那是'registerBoundHelper'存在之前的日子。 – hekevintran
這不是一個好的選擇,更正確的方法是@ Giovanni's。但是因爲ember-inflector現在自己註冊了一個複數幫助器,所以你可以直接使用它(參見https://github.com/stefanpenner/ember-inflector/issues/18)。 –
我已經更新了這個工作在Ember 2.x,[這裏](https://gist.github.com/finferflu/8eeeff014acec7525f466aa72d0ab560),但由於我不知道如何命名參數在那裏工作,我已經選擇位置參數。隨意改善它! – finferflu
我不知道任何灰燼特定的功能,會爲你做到這一點。但是,通常當您複數詞時,單個版本僅在計數爲1時才顯示。
一個例子來看看這個:http://jsfiddle.net/6VN56/
function pluralize(count, single, plural) {
return count + " " + (count == 1 ? single : plural);
}
pluralize(1, 'dog', 'dogs') // 1 dog
pluralize(10, 'dog', 'dogs') // 10 dogs
pluralize(0, 'dog', 'dogs') // 0 dogs
有一個圖書館的I18n爲灰燼:zendesk/ember-i18n。
有一個車把幫手t
通過從Em.I18n.translations
查找字符串處理國際化:
Em.I18n.translations = {
'dog.walk.one': '1 dog has gone for a walk.',
'dog.walk.other': '{{count}} dogs have gone for a walk.'
};
然後你就可以通過使用字符串在把手的模板:
{{t dog.walk countBinding="dogCount"}}
代碼以上未經測試,僅取自README中的文檔。
另一個JS的I18n庫,我發現是亞歷克斯·塞克斯頓的messageformat.js。
這取決於你複雜的應用程序,但你也可以使用一個計算的屬性,看http://jsfiddle.net/pangratz666/pzg4c/:
把手:
<script type="text/x-handlebars" data-template-name="dog" >
{{dogCountString}}
</script>
的JavaScript:
Ember.View.create({
templateName: 'dog',
dogCountString: function() {
var dogCount = this.get('dogCount');
var dogCountStr = (dogCount === 1) ? 'dog has' : 'dogs have';
return '%@ %@ gone for a walk.'.fmt(dogCount, dogCountStr);
}.property('dogCount')
}).append();
它不應該是一個更好的解決方案,直接創建一個handlebars helper而不是一個計算屬性嗎? – louiscoquio
我發現了一個Imber的庫,它有一個把手幫手。 – pangratz
+1,它看起來也很棒! – louiscoquio
看起來你got an answer from wycats himself,但我沒有看到它在這個線程提及,所以這裏是:
Handlebars.registerHelper('pluralize', function(number, single, plural) {
if (number === 1) { return single; }
else { return plural; }
});
我最近發現這個庫http://slexaxton.github.com/Jed/這似乎是JS國際化一個很好的工具。我想你可以很容易地創建自己的實現,通過註冊一個使用這個庫的句柄助手。
如果您使用Ember Data,您可以使用Ember.Inflector
。
var inflector = new Ember.Inflector(Ember.Inflector.defaultRules);
inflector.pluralize('person') //=> 'people'
你可以註冊一個新的幫手:在http://emberjs.com/api/data/classes/Ember.Inflector.html
ember-inflector現在註冊一個複數幫助程序本身,您可以直接使用它(請參閱https://github.com/stefanpenner/ember-inflector/issues/18)。 –
更多細節看看http://stackoverflow.com/questions/48726/best-javascript-i18n -techniques-ajax-dates-times-numbers-currency – Reactormonk