How to translate for one or more items: Pluralism made easy

It is quite easy to pluralise .yml translated strings in SilverStripe 4 these days. Lets take a simple example where I want to pluralise some search results.

In SS4 you can define single and plural versions of strings with “one” and “other”. The shortcut in templates is:

<%t SEARCH.ResultsCountQuery 'Your search for "{query}" returned {count} result|{count} results' query=$Query count=$Results.Matches.TotalItems %>


Note the “|” in the string. This is where “one” and “other” come into effect.

If count is exactly one item, the left part of the pipe is displayed (Your search for "foo" returned 1 result), if you have zero or more than one results, the right part of the pipe (e.g. "23 results") is displayed, if you don't overwrite this in your yml files per language.

ResultsCountQuery:

  one: 'Your search for "{query}" returned {count} result'

  other: 'Your search for "{query}" returned {count} results'

More information and examples can be found in the i18n deveoper documentation.

Rate this post

Post your comment

Comments

No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments