-------- Original Message --------
Subject: [MVC-Programmers] [Struts Tips] #4 - Formatting output with 
MessageResources
Date: Mon, 01 Apr 2002 10:09:20 -0500
From: Ted Husted <[EMAIL PROTECTED]>
Reply-To: [EMAIL PROTECTED]
Organization: Apache Software Foundation / Jakarta
Newsgroups: MVC-Programmers

A common requirement of the presentation layer is to format information
in a human-friendly way. While a numeral rendered as 1000000 works well
for the software, most humans can't turn that data into information at a
glance. Humans expect a numeral to be rendered as 1,000,000  - which
they can interpret at a glance to be the value one million.

At least to Americans.

People in other countries might find the name rather strange, since it
includes two decimal markers, or what an American would render as
1.000.000.

Since many Struts applications are localized, this are important issues.
When a page in a Struts application renders a number, or a date, or a
percentage, it should be able to do so according to each user's locale.

Struts already has localized messaging built-in, which most developers
use for error messages, prompts, and field label names. A Struts
application refers to messages like this using a symbolic key, which is
used to retrieve the actual message from the Struts application resource
bundle. The messages can also be used as templates, and the application
can pass custom values to them at runtime. Often, there is a default
template for an entire class of message, and the field name or other
value is merged into the static text at runtime. Each locale can have
it's own resource properties file, which makes localization a relatively
simple process.

Now, it's important to note that Struts didn't invent any of this.
Resource bundles, message templates, locales, and the rest of it are all
part of the Java internationalization framework. Struts makes Java i18n
an intregal part of its own framework and provides support classes
making it easier to use. But the core functionality is provided by the
Java internationalization framework.

Including the abilty to apply formatting to a value a runtime.

Most message templates are created by just indicating the placeholder,
like

ordering.authorized.range.staff=Staff is only authorized to requisition
supplies that cost less than ${0}

It then becomes the application's responsiblility to supply whatever
string is needed to replace {0}, which will be inserted into the message
template, as-is.

However, you can also pass more than one parameter as the placeholder
and also include one of the standard message format classes: number,
currency, percent - along with a default mask. This means you can just
as easily render the template as

ordering.authorized.range.staff=Staff is only authorized to requisition
supplies that cost less than {0,currency}

which will insert the appropriate thousands-separator or
decimal-separator for the user's locale. This example would also insert
the monetary symbol for the user's country. If everything should be
expressed in a single currency, then you'd use number instead

ordering.authorized.range.staff=Staff is only authorized to requisition
supplies that cost less than ${0,number}  USD

The same technique can be applied to dates and percentages. See the Java
Tutorial's page on the MessageFormat class for more

http://java.sun.com/docs/books/tutorial/i18n/format/messageFormat.html

While the Struts application resource bundle is most often used when
generating error messages, the <bean:message> tag can be used to access
message templates throughout your application, and pass the replacement
parameters at runtime to generate a customized page.

Here's the same example message coded for <bean:message>

    <bean:message key="ordering.authorized.range.staff" arg0='<%=
config.getStaffAuthAmnt() %>'/>

where there is a bean in some scope named "config" that can return the
amount in question.

HTH, Ted.

_______________________________________________
MVC-Programmers mailing list
[EMAIL PROTECTED]
http://www.basebeans.com:8081/mailman/listinfo/mvc-programmers

Reply via email to