Page 7 of 8
Again follow the same steps we create
another properties file. This time name it resource_bundle_[language].properties
where the language is actually the language codes for our translations. For
this tutorial, let�s name it resource_bundle_id.properties. Please notice that
the suffix is �id� which means that this is the resource bundle for Indonesian language. You may use any
suffix as you prefer at this stage i.e. resource_bundle_es.properties (Spanish)
or resource_bundle_zh.properties (Mandarin) and etc.
The new properties file, the one with
suffix will be automatically nested under our default �resource_bundle.properties�
file.
Put some keys in our default properties
file.
title=My Company
welcome=Welcome!
And the translated version of keys for our
suffixed properties file.
title=Perusahaanku
welcome=Selamat Datang!
We
are done with the resource bundle properties files. Let�s see how they are
working. Back to our Jstl_Fmt_Tags.jsp, add below codes to test our
internationalization support from JSTL tag.
<fmt:setBundle
basename="resource_bundle" />
<fmt:message
key="title" />
<fmt:message
key="welcome" />
<fmt:bundle
basename="resource_bundle_id" >
<fmt:message key="title"
/>
<fmt:message
key="welcome" />
</fmt:bundle>
The
first and second paragraph does the same thing. The first paragraph, we are
using <fmt:setBundle> while the second we are using <fmt:bundle>
tag. They are doing the same thing except <fmt:bundle> works only within
its tag. The <fmt:message> outside of the <fmt:bundle> should not
be able to access the properties files. Therefore we need to define the opening
and closing tag for <fmt:bundle> tag. While <fmt:setBundle> works
in higher level and we can specify it in what scope the tag
<fmt:setBundle> will have an effect.
The
basename attribute in the <fmt:bundle> and <fmt:setBundle> are the name
of the specific properties file while <fmt:message> is used to look up
for matching available keys message in our properties file.
Next
part is about date time parsing and formatting. Continue with our current JSP.
Put this code.
<jsp:useBean
id="now" class="java.util.Date" />
This
tag is to get to java.util.Date class.
<fmt:setLocale value="en_GB"
/><strong>U.K :</strong>
<br>
<fmt:formatDate
value="${now}" type="both" dateStyle="full"
timeStyle="default" />
<br>
<fmt:timeZone
value="GMT">
<fmt:formatDate
value="${now}" type="both" pattern="EEEE,
dd/MMMM/yyyy, HH:mm:ss z" />
</fmt:timeZone>
<br>
<fmt:formatNumber
value="${now.time}" />
<br>
<fmt:formatNumber
value="8000" type="currency"
minFractionDigits="2" maxIntegerDigits="10" />
How
do the above codes work? First we set the locale value to desired value (in
this case, UK).
Simply refer to Language Preference in
our browser for some locale values.
The
tag <fmt:formatDate> will format the date value which retrieved via �now�
bean id. The type could be date, time or both. The dateStyle and timeStyle
could be in short, medium, long, full or default. The default value for this is
�short�. Pattern is used if we want the date time format to be in our specific
way. Pattern will always override dateStyle and timeStyle.
The tag
<fmt:timeZone> will adjust the current time to GMT based time. We may
want to use <fmt:setTimeZone> in certain conditions. The value attribute
for this tag could be in standard abbreviation
(e.g., "PST," "GMT"), a full name (e.g.,"Europe/Stockholm"),
or a GMT offset (e.g., "GMT+1").
The tag
<fmt:formatNumber> is to format the numerical given values. The type
could be number, currency or percentage. Here are more details about it. minFractionDigits
is a minimum number of digits in fractional portion. maxIntegerDigits is a
maximum number of digits in integer portion. Other attributes are currencyCode
and currencySymbol. These 2 are valid while the type is currency. This can be useful when you need to show prices expressed in a
fixed currency, but you want the amount to be formatted according to the
selected locale.
<fmt:parseDate value="25-12-2006"
pattern="dd-MM-yyyy"/>
<fmt:parseNumber value="$8,000.00"
pattern="$#,###.##" />
<fmt:parseDate> and <fmt:parseNumber> is to get the
actual value from unparsed string. These tags are useful in the situation where
we need to get users� input from a User Interface (UI) form. We can parse the
values in form of what we want before inserting or updating to a database.
Now build and run the
project. We will get something like this.
And
this is what we get when we click the �JSTL Formatting Tags�.
|
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.