Friday, June 12, 2009

Tips: Spring MVC and messages.properties

Tip #1:

If a message in your messages.properties file contains single quotes, you may find that these do not display properly. The trick is to use Unicode hex values instead. That said, avoid using \u0027, the Unicode hex value for apostrophe, as it may not work correctly. (It didn't for me.) Use \u2018 and \u2019 instead. These are, respectively, the left single quotation mark and the right single quotation mark.

So, for example, instead of...
errors.required.parameter=Required parameter '{0}' is not present.
... use...
errors.required.parameter=Required parameter \u2018{0}\u2019 is not present.

Tip #2:

If you pass a number parameter into a message in your messages.properties file, it will automatically be formatted as a number and commas will be inserted. For example, 12300 will be displayed as 12,300. If this number is an identifier, and you don't want commas to be inserted, convert the number to a string before passing it to your translation mechanism.

Example:

The message definition is:
errors.proposal.not.found=Proposal {0} not found.

Code:
Long proposalId = 227128;
String errorMessage= org.springframework.context.MessageSource.getMessage
("errors.proposal.not.found", new Object[] {
proposalId}, Locale.getDefault());

Error message:
Proposal 227,128 not found.

Change your code to:
org.springframework.context.MessageSource.getMessage
("errors.proposal.not.found", new Object[] {
proposalId.toString()}, Locale.getDefault());

Now the error message is:
Proposal 227128 not found.

1 comment:

Unknown said...

or ... double the '
like "I''m talking about things"