Using the plugin for international, is there a "proper" way to pluralize words dynamically, or select the plural version of a property?
You can use an embedded ChoiceFormat
in the i18n message. Using an example from the java.text.MessageFormat
Javadocs you could define something like:
numfiles.message = "There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}."
Then in your GSP:
<g:message code="numfiles.message" args="[numfiles]"/>
Where numfiles
is an integer value.
As @overzealous had noticed in comments, not all languages have the same simple plural rules as English language has. English has just two forms: one
(quantity is 1, e.g. 1 car, 1 hour) and other
(quantity is more than 1, e.g. 3 cars, 11 hours). In this case pluralization can be handled pretty simple in Grails using ChoiseFormat
. However, some languages have more than two forms, for example Polish has 4 forms: one
(e.g. 1 auto), few
(e.g. 2 auta), many
(e.g. 6 aut) and other
. Here is a link to the table describing plural rules for all languages.
I've also wrote a plugin for Grails which adds rich pluralization abilities using table above. Using it you can just write in messages.properties
msgcode={0} {0, plural, one{auto}few{auta}many{aut}other{aut}}
and then use it:
<g:message code="msgcode" args="[3]"/>
will print 3 auta
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With