Pluralization in Visualforce

What are best practices for pluralization in Visualforce pages? I find myself doing a lot of this, which feels clunky and makes the code hard to read:

{!numUnread} unread message{!IF(numUnread > 1,'s', '')}

Answer

If you are using standard object labels, you just use the label:

$ObjectType.Account.label -> singular label: Account
$Objecttype.Account.labelplural -> plural label: Accounts

Otherwise, I would go with two separate custom labels:

$Label.UnitSingular  -> "Stock Unit"
$Label.UnitPlural   -> "Stock Units"

The reason I would go with the above instead of hard coding an “s” is because the way something is pluralized may be different. For instance you might get a business requirement to change the terminology around the word “Unit” above. Someone might get the idea that they like “Merchandise”. This will break the code that you have in your question. Whereas using a custom label, an administrator can simply go in and provide the new text value for the plural and singular word and your code still works.

Your code might actually end up being harder to read, but it will require less maintenance.

Attribution
Source : Link , Question Author : Benj , Answer Author : pchittum

Leave a Comment