Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plural/singular #64

Open
thany opened this issue Feb 6, 2013 · 2 comments
Open

Plural/singular #64

thany opened this issue Feb 6, 2013 · 2 comments

Comments

@thany
Copy link

thany commented Feb 6, 2013

How to write in the template a conditional block to display a plural or singular label? Say, my json is something like this:
{ "title": "...", "responses": 4 }

I want my template to be something like this:
{title}, {responses} responses.

But for that, if there's 1 response (in this example) I have to determine in the template if the value {responses} is 0, 1, or many. For multilingual purposes, I need all three labels "no responses", "one response" and "X responses" to be in the template.

But how?

@thany
Copy link
Author

thany commented Feb 6, 2013

Okay, these helpers will do what I need:

$.extend(dust.helpers, {
  zero: function(chunk, context, bodies, args) { return Number(args.what) === 0 ? bodies.block(chunk, context) : chunk; },
   one: function(chunk, context, bodies, args) { return Number(args.what) === 1 ? bodies.block(chunk, context) : chunk; },
  many: function(chunk, context, bodies, args) { return Number(args.what) > 1 ? bodies.block(chunk, context) : chunk; }
});

And then in the template:

{@zero what=responses}<span>no responses</span>{/zero}
{@one what=responses}<span>1 response</span>{/one}
{@many what=responses}<span>{responses} responses</span>{/many}

But this kind of functionality is so basic, that I fully expect this to be in the core... If it isn't, then consider this a request ;)

@sethkinast
Copy link

You could use some of the logic helpers in https://github.com/linkedin/dustjs-helpers along with LinkedIn's Dust fork. So using the native functionality you could do

<span>
{@eq key=responses value=1}{responses} response{:else}{responses} responses{/eq}
</span>

However the best way would be to create a helper that calls out to something like FormatJS and uses real MessageFormat syntax, because different languages have different pluralization needs. FormatJS even supports Dust templates :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants