Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Named Parameters rather than positional for Spring MessageSource.getMessage()

we are using Spring MessageSource to build error messages in our app.

We populate our error messages like this

dobInvalid = The DOB supplied {0} is invalid

We want to use named parameters so we can do

dobInvalid = The DOB supplied {dob} is invalid

Looking in the Api docs for getMessage it appears to suggest you can do this http://static.springsource.org/spring/docs/1.2.x/api/org/springframework/context/MessageSource.html

args - Array of arguments that will be filled in for params within the message (params look like "{0}", "{1,date}", "{2,time}" within a message), or null if none.

Obviously we can write our own but was wondering if spring can do it and if anyone can provide an example or using named parameters rather positional parameters.

Cheers Mark

like image 300
Mark Hanlon Avatar asked Sep 25 '12 14:09

Mark Hanlon


1 Answers

AIUI, Spring MessageSource works with JDK MessageFormat, so there is no such a named parameter. {1,date} is an example, where "date" refers to formatType, no to an arbitrary named parameter.

The general form of a parameter is:

{ ArgumentIndex , FormatType , FormatStyle }
like image 80
Camilo Silva Avatar answered Sep 24 '22 23:09

Camilo Silva