26 - JSP Internationalization

26.1 Overview of JSP Internationalization

Internationalization (i18n) is way to serve users in different languages including formatting of numbers, adjustment to date and time etc. Web applications are accessible across the world and each country has its own language and own formatting standards like some country uses comma(,) as thousand separator whereas in other countries dot (.) or space is the separator.

Think of the situation where if our web application needs to support multiple languages then we need to create a new version of each JSP for every language. But with the help of Internationalization support we can create our web application to adjust itself according to the locale which includes formatting of currency or numbers , all text of website in country specific language.

26.2 Locale Introduction

Locale is used to represent a geographical or cultural region to localize the text, number, date etc. A Locale object may contain a country, region, language, and also a variant of a language.

Java provides a Locale implementation as java.util.Locale class.

The Locale instance is handed to components that need localization.

A Locale instance contains the following subparts:

· Language -This is 2 or 3 characters language code (valid ISO 639)

· Country - The country code is a 2 character code following the ISO 3166 standard

· Variant - The variant part describes a variant (dialect) of a language

Locale object can be created by

a) Constructors –

· Locale(String language)

· Locale(String language, String country)

· Locale(String language, String country, String variant)

b) Constants Locale class has several constants for countries like Locale.UK, Locale.US which can be directly used

Locale object provides several useful methods like getCountry(), getLanguage() etc

26.3 Locale object in JSP

We can create Locale object with the approaches mentioned above but it is directly available using “request” class. We can get it like


26.4 JSP Tags to achieve Internationalization

JSP provides a format tag (as a part of JSTL) to support internationalization.

· URI for format tag library is http://java.sun.com/jsp/jstl/fmt

· To use format tag library on JSP page we need to import its tag library using

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

26.4.1 <fmt:setBundle>

Resource bundle is nothing but a collection of messages in a form of key – value pair. We can define one file per locale and all file will have same keys but corresponding values will be locale specific. This tag is used to load the resource bundle in any scoped variable OR in bundle configuration variable.

Syntax of tag is –

<fmt:setBundle basename="bundle_base_name" var="variable_name" scope=”page| request | application| session”/>ession”/>

· basename- is mandatory attribute and used to specify base name of bundle.

· var- is an optional attribute to store the bundle

· scope- is the scope of variable defined in var tag. Default value is page

26.4.2 <fmt:setLocale>

This tag is used to store the locale in locale configuration variable.

Syntax of this tag is-

<fmt:setLocale value="valid_locale" variant="variant" scope=”page| request | application| session”/>cation| session”/>

· valueis the mandatory attribute and is used for specifying a locale. To specify a locale code it must have made from the two-letter (lower case) language code (according to ISO-639) and/or the two-letter (upper-case) of country code (according to the ISO-3166). And if both the language and country code are used for specifying the locale the locale code must be separated by either hyphen (-) or underscore (_).

· variant : is an optional attribute and is used for specifying the browser or vendor specific variant.

· scope : is an optional attribute and specifies the scope of locale configuration variable. Default value is Page

26.4.3 <fmt:message>

This tag is used to display the messages from bundle based on the locale.

Syntax of this tag is-

 <fmt:message key="key_in_bundle_file" bundle="bundle_variable" scope=”page| request | application| session” var=”variable_name”/> 

· var- is an optional attribute to store the resolved localized message.

· scope- is the scope of variable defined in var tag. Default value is page

· key- is the key in resource bundle whose value to display

· bundle- variable in which bundle is stored and this bundle will be used.

Note : If var is used, then message will not be displayed, instead will be stored in a variable for a later use.

26.5 JSP Internationalization Examples

26.5.1 Language Specific Text Example

Create resource bundle in two locale (default – without locale and another in Spanish locale (es_US).

Resource bundle is simply a properties file and for all files , base name will remain same and all locales will be appended to base name.

1)Create Following two files in com.sample.jsp.tutorial.bundle


title=Title in Default Locale
usernameLbl=Username in default Locale


title=Title in Spanish Locale
usernameLbl=Username in Spanish 
passwordLbl=Password- in Spanish

2)Create fmtlocale.jsp

title=Title in Default Locale
usernameLbl=Username in default Locale
   <title> fmt:locale , fmt:message and fm:setBundle tag example </title>
  <%@ taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core" %>
  <%@ taglib prefix="fmt"   uri="http://java.sun.com/jsp/jstl/fmt" %>
    <fmt:setBundle basename="com.sample.jsp.tutorial.bundle.myBundle" var="resourceBundle"/>
    Title in default Locale :: <fmt:message key="title" bundle="${resourceBundle}"/>
    <fmt:message key="usernameLbl" var="userMsg" bundle="${resourceBundle}"/>
    Username in Default Locale :: <c:out value="${userMsg}"/>
    <fmt:setLocale value="es_US"/>
    <fmt:setBundle basename="com.sample.jsp.tutorial.bundle.myBundle" var="resourceBundle"/>
    Title in Spanish Locale :: <fmt:message key="title" bundle="${resourceBundle}"/>
    Username in Spanish Locale :: <fmt:message key="usernameLbl" bundle="${resourceBundle}"/>

Access fmtlocale.jsp by hitting http://localhost:8080/jsp-tutorial/fmtlocale.jsp

26.5.2 Date and Number Formatting Example

Java provides a DateFormat and NumberFormat classes that can format the given number currency, and dates based on the locale. Lets create a jsp that will format and number and date based on locale

Create fmtFormat.jsp

<%@page import="java.text.NumberFormat"%>
<%@page import="java.text.DateFormat"%>
    <title> Format date and number  </title>
    <%@page import="java.util.*" %>
    <h3> Display Current Date and Number based on client request locale </h3>
     Locale locale = request.getLocale();
     Date currentDate= new Date();
     DateFormat dateFormat= DateFormat.getDateInstance(DateFormat.FULL, locale);
     NumberFormat numberFormat =  NumberFormat.getNumberInstance(locale);
    Locale is  <%= locale.getDisplayCountry() %>
    Current date ::  
   Number (100045.34) ::

Access fmtformat.jsp by hitting http://localhost:8080/jsp-tutorial/fmtformat.jsp

Now change the browser locale to France like –

Go to Internet Options àLanguage


Add France locale and move it to top


Access the page again and you can see the date and number in French locale like below


Note : We did not change anything in code and application adapts itself to locale

Like us on Facebook