Handlebars.js: How do you handle pluralisation in Handlebars?

Created on 30 May 2012  路  11Comments  路  Source: handlebars-lang/handlebars.js

Is it possible to write a helper for pluralizing words that can be used like this?

{{dog_count}} {{pluralize dog_count "dog" "dogs"}} {{pluralize dog_count "has" "have"}} gone for a walk.

Most helpful comment

I made my own Handlebars helper for this. It's pretty versatile. For example, all of these compile to "bagel" when numVar === 1 and to "bagels" for everything else:

{{plural numVar 'bagel'}}
{{plural numVar 'bagel(s)'}}
{{plural numVar 'bagel/bagels'}}
bagel{{plural numVar}}

It can also be used for switching between singular and plural grammar as appropriate:

{{numVar}} bagel{{plural numVar ' was/s were'}} eaten.

Here's the source: https://gist.github.com/drmercer/c3e0b2e174718787dac5a181b2be83f6

All 11 comments

This is essentially a generic request for binding-aware helpers in Ember, right?

No not really. It's a generic request for advice on how to pluralise things in Handlebars.

Oh shit. I completely missed the context of this request :P

Something like this would work:

Handlebars.registerHelper('pluralize', function(number, single, plural) {
  if (number === 1) { return single; }
  else { return plural; }
});

logic-less require a lot of foresight....

@ahoward what do you mean by that?

I used two separate functions to fit my use case a little better:

Handlebars.registerHelper('pluralize', function(number, singular, plural) {
    if (number === 1)
        return singular;
    else
        return (typeof plural === 'string' ? plural : singular + 's');
});

Handlebars.registerHelper('pluralCount', function(number, singular, plural) {
    return number+' '+Handlebars.helpers.pluralize.apply(this, arguments);
});

The idea being you can use it like this:

{{pluralCount dogCount 'dog'}} {{pluralize dogCount 'has' 'have'}} gone for a walk

or like this:

The dog was on {{pluralCount couchCount 'couch' 'couches'}}

very nice @lazd thank you much...

It's not that handy.
I prefer to use messageformat, which allow the translator to manage himself pluralization, gender and so on. As is, the developper just need one handlebars tag:

``` .javascript
{{i18n "doginfo" dogCount}}

This can call the i18n helper which get the "doginfo" label in an external en_UK.json file (for example),
which may contain:

``` .json
{
    "doginfo": "{ dogCount, plural,
                      one {The dog has}
                      other {The dogs have}
                } gone for walk"
}

This implies using another syntax, right. But it's not that complicated, and free the translator to move words order.

I made my own Handlebars helper for this. It's pretty versatile. For example, all of these compile to "bagel" when numVar === 1 and to "bagels" for everything else:

{{plural numVar 'bagel'}}
{{plural numVar 'bagel(s)'}}
{{plural numVar 'bagel/bagels'}}
bagel{{plural numVar}}

It can also be used for switching between singular and plural grammar as appropriate:

{{numVar}} bagel{{plural numVar ' was/s were'}} eaten.

Here's the source: https://gist.github.com/drmercer/c3e0b2e174718787dac5a181b2be83f6

Also consider using grunt-locales to manage your translations.

Was this page helpful?
0 / 5 - 0 ratings