Table of contents
1.
Introduction
2.
Internationalization in JSP
3.
Locale instance
4.
Internationalization in JSP - Example
4.1.
Language-Specific Text Example
4.2.
Locale Specific Dates
4.3.
Locale Specific Currency
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Internationalization in JSP

Author Tanay kumar Deo
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Let's consider a situation where our JSP application needs to support multiple languages. We can solve this issue most effectively by using Internationalization in JSP. 

Internationalization ( i18n ) serves the website to users with different languages, including adjustment to date and time, formatting of numbers, etc. Web apps are accessible across the globe, and each country has a different language and formatting standards. For example, in some countries, the date is formatted as DD-MM-YYYY, whereas in other countries, it is written as MM-DD-YYYY.

Internationalization in JSP allows our application to adjust itself based on the locale, including formatting of currency, numbers, date and time, etc.

Internationalization in JSP

This article will discuss Internationalization in JSP. But before moving ahead, let's understand its common concepts:

  • Internationalization (i18n):− This means that the website will modify itself based on the region and country by translating its contents into the local language.
  • Localization (l10n): This means adding different resources to the website to adapt it to any particular region, such as Hindi/English translation.
  • Locale:− Locale is a particular geographical or cultural region. It is generally referred to as a language symbol followed by the country symbol separated by an underscore. For example, "hi_IN" represents the Hindi locale for India.

With JSP, we can pick up the appropriate version of our website based on the user's locale and provide the proper website version with local language, date and time format. Below is the method to request an object which returns the Locale object.

java.util.Locale request.getLocale()
You can also try this code with Online Java Compiler
Run Code

Locale instance

A Locale instance contains the following subparts:

  • Language -This is a 2 - 3 character language code ( valid ISO 639).
  • Country The country code is two characters code following the ISO 3166 standard.
  • Variant - The variant part describes the variant (dialect) of a language.

 

There are multiple methods that we may use to detect the requester's locale:

Methods

Description

String getCountry() This method will return the country code in upper case for the locale in ISO 3166 2-letter format.
String getDisplayCountry() This method will return a name for the locale's country (appropriate to display to the user).
String getLanguage() This method will return the language code in lower-case for the locale in ISO 639 format.
String getDisplayLanguage() This method will return a name for the locale's language (appropriate to display to the user).
String getISO3Country() This method will return a three-letter abbreviation for this locale's country.
String getISO3Language() This method will return a three-letter abbreviation for this locale's language.

 

Example to display the name and language of the user’s locale:

<%@ page import = "javax.servlet.*,javax.servlet.http.* "%>
<%@ page import = "java.io.*,java.util.Locale" %>
<%
   // Get the user's Locale
   Locale locale = request.getLocale();
   String languageName = locale.getLanguage();
   String countryName = locale.getCountry();
%>

<html>
   <head>
      <title>Detecting Locale in JSP</title>
   </head>
   <body>
      <h1>Detecting Locale in JSP</h1>
      <p>
         <%
            out.println("Country Name : " + countryName  + "<br />");
            out.println("Language : " + languageName + "<br />");
         %>
      </p>
   </body>
</html>
You can also try this code with Online Java Compiler
Run Code

 

In this example for detecting locale in JSP, we successfully printed the County Name and Language Name as shown in the image below:

Output

Internationalization in JSP - Example

In this section, we will see different examples of Internationalization in JSP.

Language-Specific Text Example

With JSP we can output a page written in any Western European language such as Spanish, English, French, German, Dutch, Italian, etc. Let’s understand this with the example given below:

<%@ page import = "javax.servlet.*,javax.servlet.http.* "%>
<%@ page import = "java.io.*,java.util.Locale" %>
<%
   String titleInSpanish = “Hola estudiantes”;
  
   // Set response content type
   response.setContentType("text/html");

   // Set spanish language code.
   response.setHeader("Content-Language", "es")
%>

<html>
   <head>
      <title><%  out.print(titleInSpanish); %></title>
   </head>
   <body>
      <h1><%  out.print(titleInSpanish); %></h1>
      <p>En Español</p>
   </body>
</html>
You can also try this code with Online Java Compiler
Run Code

 

In the above example, we have written the content of our web app in the Spanish language. Here, we have set the Content-Language header as Spanish to display all the characters correctly. The output produced by the above example in India Locale is given below:

Output

Here the output produced is the English translation of “Hola estudiantes” to “Hello students” and “En Español” to “in Spanish”.

Locale Specific Dates

We may use the java.text.DateFormat class and getDateTimeInstance() method to format date and time specific to different locale. Let’s understand how to format dates to any locale with the example given below:

<%@ page import = "javax.servlet.*,javax.servlet.http.* "%>
<%@ page import = "java.io.*,java.util.Locale" %>
<%@page import="java.text.DateFormat"%>


<%
   String titleString = "Date formatting in different locale";
   
   // Get the user’s Locale
   Locale locale = request.getLocale( );
   
   String date = DateFormat.getDateTimeInstance(
      DateFormat.FULL,
      DateFormat.SHORT,
      locale).format(new Date( ));
%>


<html>
   <head>
      <title><%  out.print(titleString); %></title>
   </head>
   <body>
      <h1><%  out.print(titleString); %></h1>
      <p>Current Date: <%  out.print(date); %></p>
   </body>
</html>
You can also try this code with Online Java Compiler
Run Code

 

In the above example, we have written the code to format the date based on the requester's locale. The output produced by the above example in United State Locale is given below:

Output

Locale Specific Currency

We may use the java.txt.NumberFormat class and getCurrencyInstance()method to format a number such as  to different locale. Let’s understand how to format dates to any locale with the example given below:

<%@ page import = "javax.servlet.*,javax.servlet.http.* "%>
<%@ page import = "java.io.*,java.util.Locale" %>
<%@ page import = "java.text.NumberFormat,java.util.Date" %>


<%
   String titleString = "Currency in different Locale";
   
   //Get the client's Locale
   Locale locale = request.getLocale( );
   
   NumberFormat amount = NumberFormat.getCurrencyInstance(locale);
   String formattedCurr = amount.format(1000000);
%>


<html>
   <head>
      <title><%  out.print(titleString); %></title>
   </head>
   <body>
      <h1><%  out.print(titleString); %></h1>
      <p>Amount : <%  out.print(amount); %></p>
   </body>
</html>
You can also try this code with Online Java Compiler
Run Code

 

In the above example, we have written the code to format currency based on the requester's locale. The output produced by the above example in United State Locale is given below:

Output

FAQs

  1. Name the tools to help with the Internationalization of Strings in JSP?
    Few tools available in JSP to help with the internationalization of string is listed below:
    WebGettext: It provides a method for localizing strings in JSP in the Gettext style (i.e., keeping English in the source JSP).
    Regex2PotTask: Provides an Ant task that can find such strings in JSP and generate a Gettext POT (portable object template) file.
     
  2. Important points to consider for Internationalization in JSP?
    Multiple items should be taken care of while building up a global Website. A few of them are listed below:
    We should always detect the user's locale and display contents appropriately.
    We should always format numbers, dates and times in appropriate format.
    We should always display the special characters using the HTML entities. For example, "&#161;" represents "¡", and "&#241;" represents "ñ".
     
  3. How to create a locale object in JSP?
    We can create locale objects in JSP directly by using the “request” class. For example,
request.getLocale();
You can also try this code with Online Java Compiler
Run Code

Key Takeaways

We learned about Internationalization in JSP in this article. We discussed the following things:

  • Internationalization (i18n), Localization (l10n), and Locale instance.
  • Different methods to detect the requester’s locale.
  • Different examples for Internationalization in JSP.

 

Don't stop here. Check out the blogs Introduction to JSPJSP - Custom TagsJSP Architecture and Lifecycle, and JSP Syntax and First App.

We hope you found this blog helpful. Liked the blog? Then feel free to upvote and share it.

Live masterclass