Internationalizing JSP Pages

Introduction

 
The rapid development of the web has given birth to the necessity of making web applications available to a wide range of users. It is sometimes required that these applications be available to the users of a particular region. The formatting of messages, such as dates, currencies and percentages is required to be done as per the user’s region or culture. Web applications in JSP for international users needs formatting, which can be done using the internationalizing tag library in the JSP Standard Tag Libraries (JSTL).
 

JSTL Internationalization Tag Library

 
We need to apply various internationalizations formatting while creating a web application using JSP. For this, JSP Standard Tag Library (JSTL) provides a set of internationalization or i18N tags that are used for applying various internationalization formats. The internationalization or i18n tag library helps in reducing and managing the complexities of internationalized applications.
 
There are several tags available in i18n tag library.
 
formatDate
 
This tag formats a date value as a date using a locale. A style or pattern for the presentation of the date can be specified.
 
formatNumber
 
This tag formats a number as a currency using a locale. The default text is used if the currency value is null. If no locale is specified, then the parent <i18n:locale> tag is used.
 
message
 
This tag retrieves a message from a resource bundle and optionally uses the java.util.MessageFormat class to format messages. The arguments to MessgaeFormat can be supplied in the form of an object array or as sub tags within the message tag body.
 

Formatting Dates in JSP

 
The tag <fmt:formatDate> is used for formatting dates and/or time in JSP for internationalization. The value attribute or the body content of the <fmt:formatDate> tag formats the date value. The formatted date is written to JSP writer. The value can also be stored in a string named var and an optional scope attribute.
 
Syntax
  1. <fmt: formatDate value=”date” [type=”{time|date|both}”]  
  2. [dateStyle=”{default|short|medium|long|full}”]  
  3. [timeStyle=”{default|short|medium|long|full}”]  
  4. [pattern=”customPattern”]  
  5. [timeZone=”timeZone”]  
  6. [var=”varName”]  
  7. [scope=”{page|request|session|application}”] 
Where,
 
value represents the Date and/or time to be formatted.
  • type: specifies whether the time, the date. Or both are to be formatted.
  • dateStyle: Is the predefined formatting style for dates.
  • timeStyle: Is the predefined formatting style for times.
  • pattern: Is custom formatting style for dates and times.
  • timeZone: Is the time zone in which to represent the formatted time.
var represents scope of the variable
 
For example : The Source Code
  1. <c:set var=”FinsihDate” value=”<%=Date() %>”/>  
  2. <fmt:formatDate value=”${FinishDate}” type=”date” dateStyle=”full” />  

Formatting Currencies in JSP

 
The currencies can be formatted in JSP for internationalization using JSTL i18N tags. The <fmt:setLocale> stores the specified locale in the javax.servlet.jsp.jstl.fmt.locale configuration variable. The formatting is done as per the set locale. The tag <fmt:formatNumber> can be used to format the currencies.
 
The number is specified to be formatted either with an EL expression in value attribute or as the tag’s body content. The desired formatting is specified by the type attribute.
 
Syntax of <fmt:setLocale> tag
  1. <fmt:setLocale>  
  2. <fmt:setLocale value=”locale” [variant=”variant”]  
  3. [scope=”{page|request|session|application}”]/>   
Where value: is a string value, which is interpreted as the printable representation of a locale. Language and country codes must be separated by hypen(‘-‘) or underscore(‘_’)
Variant: is vendor- or browser-specific variant.
 
scope: is the scope of the locale configuration variable.
 
Syntax of the <fmt:formatNumber> tag
  1. <fmt:formatNumber value=”numericValue”  
  2. [type=”{numbe|currency|percent}|]  
  3. [pattern=”customPattern”]  
  4. [currencyCode=”currencyCode”]  
  5. [currencySymbol=”currencySymbol”]  
  6. [groupingUsed=”{true|false}”]  
  7. [maxIntegerDigits=”maxIntegerDigits”]  
  8. [minIntegerDigits=”minIntegerDigits”]  
  9. [maxFractionDigits=”maxFractionDigits”]  
  10. [minFractionDigits=”minFractionDigits”]  
  11. [var=”varName”]  
  12. [scope=”{page|request|session|application}”]/>  
Where,
  • value represents the numeric value to be formatted.
  • type represents whether the value is to be formatted as number, currency or percentage
  • pattern represents the custom formatting pattern
  • currencyCode is ISO4217 currency code. Applied only when formatting currencies ; ignored otherwise.
  • currencySymbol is the currency symbol . Applied only when formatting currencies, ignored otherwise.
  • groupingUsed represents whether the formatted output will contain any grouping separators.
  • maxIntegerDigits represents the maximum number of digits in the integer portion.
  • minIntegerDigits represents the minimum number of digits in the integer portion.
  • maxFractionDigits represents the maximum number of digits in the fractional portion.
  • minFractionDigits represents the minimum number of digits in the fractional portion.
  • var is the name of exported scoped variable.
scope: scope of the variable.
 
For example,
  1. <fmt:setLocale value=”en_GB”/>   
Formatting Salary with Locale <B>en_GB</B> becomes,
  1. <c:set var=”salary” value=”${5000}”/>  
  2. <fmt:formatNumber type=”currency” value=”${salary}” /><BR>   

Formatting Percentages in JSP

 
The <fmt:formatNumber> tag formats a number in integer, decimal, currency and percentage . By specifying the type attribute in <fmt:formatNumber> percentage of a number can be obtained. This will happen when the value is multiplied by hundred.
  1. <fmt:formatNumber value=”0.82” type=”percentage”/>   

Formatting Messages in JSP

 
The <fmt:message> tag retrieves a message from a resource bundle & uses the java.util.MessageFormat class to format the message. The key attribute specifies the message key. If the <fmt:message> tag occurs with in a <fmt:bundle> tag, the key is appended to the bundle prefix, if there is one.
 
If <fmt:message> occurs outside of a <fmt:setbundle>, the bundle attribute must be present & must be an expression that evaluates to a LocalizationContext object. Most often, a variable is initialized with the <fmt:setBundle>
 
Syntax
  1. <fmt:message key=”messageKey”  
  2. [bundle=”resourceBundle”][var=”varName”]  
  3. [scope=”{page|request|session|application}”]/>   
Where,
  • key represents the message key to be looked up.
  • bundle is the localization context in which resource bundle the message key is looked up.
  • var represents the name of the exported scoped variable string
  • scope represents the scope of the variable.
If the scope is specified , var must also be specified.
 
For example,
  1. // this code formats the message//  
  2. <c:set var=”userNameString” value=”Ashish”/>  
  3. <fmt:message key=”Welcome”>  
  4. <fmt:param value=”${userNameString}” />  
  5. </fmt:message>  

Summary

 
Resource bundles contain data which are specific to a particular region, be it political or geographical. JSP Standard Tag Library (JSTL) provides a set of internationalization or i18N tags that are used for applying various internationalization formats. JSTL also provides tags that are catering to localization formats.