cblanto7
cblanto7

Reputation: 169

issue with arguments to messages.properties, all numbers except zero work correctly

In my Grails 2.4.4 app I am using messages.properties for internationalization, with the following value:

my.app.thing.allowance(s)={0} Allowance(s)

and it is being using in a gsp like so:

<g:message code="my.app.thing.allowance(s)" args="${item.allowances.size()}"/>

the message is correctly displayed for any values greater than 0, for example if item.allowances.size() == 4 then the message displayed is 4 Allowances(s)

the issue is that if item.allowances.size() == 0 then the message displayed is {0} Allowance(s)

I have tried to write the args in several different ways, for example:

<g:message code="my.app.thing.allowance(s)" args="${item.allowances.isEmpty() ? 0.intValue() : item.allowances.size()}"/>

I have debugged things and I am sure that item.allowances.size() == 0 but for some reason it can not handle a value of 0 properly. What is the correct way to pass an argument with an int value of 0 to messages.properties?

Upvotes: 0

Views: 339

Answers (2)

Bharat Satija
Bharat Satija

Reputation: 362

In g.message arguments are always passed as an List.

From: http://docs.grails.org/3.0.17/ref/Tags/message.html

args (optional) - A list of argument values to apply to the message when code is used.

Try this code instead:

<g:message code="my.app.thing.allowance(s)" args="[item.allowances.size()]"/>

Upvotes: 6

Koloritnij
Koloritnij

Reputation: 1217

The Bharat's answer is correct, but I want to add why it happened so: You have passed args=0 And here it is code from message tag lib:

List args = []
if (attrs.args) {
   args = attrs.encodeAs ? attrs.args as List : encodeArgsIfRequired(attrs.args)
   }

In groovy 0 is false, that's why you didn't have filled in message in case of ZERO

Upvotes: 1

Related Questions