Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there standard way in grails to manage plurals in international strings from property files?

Using the plugin for international, is there a "proper" way to pluralize words dynamically, or select the plural version of a property?

like image 242
Andrew Avatar asked Apr 22 '11 19:04

Andrew


2 Answers

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.

like image 55
Rob Fletcher Avatar answered Oct 23 '22 01:10

Rob Fletcher


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.

like image 1
jasp Avatar answered Oct 23 '22 00:10

jasp