Internationalization in JSP|i18n| l10n

Internationalization in JSP |i18n| l10n with Examples

In this article, I am going to discuss Internationalization in JSP with Examples. Please read our previous article where we discussed JavaBeans in JSP Application. Internationalization helps us to enable the website to provide different versions of content translated into visitor’s language or nationality. It is a way to serve users in different languages. It also includes formatting of numbers, date and time, etc. As each country has its own language and formatting standards, here we are using internationalization so the web applications are accessible across the world.

Locale in JSP

Locale is representing a geographical region. It is also used to represent the cultural region to localize the text, number, dates, etc. It is an object that contains a country, region, language, etc. It can be implemented as java.util.Locale class. We can get the Locale object as follows: request.getLocale();

JSP tags to achieve Internationalization

<fmt:setLocale>: It is used to represent the particular Locale.
Syntax: <fmt:setLocale value=”—”/>, where value is an attribute which takes Locale parameters like en_US, it_IT, etc.

<fmt:formatNumber>: It is used to represent number w.r.t. the specified Locale.
Syntax: <fmt:formatNumber var=”—” value”—”/>, where var is an attribute that takes a variable to hold up the formatted number and value is an attribute that takes a number.

<fmt:formatDate>: It is used to format the present system date with respect to a particular Locale.
Syntax: <fmt:formatDate var=”—” value”—”/>, where var is an attribute that takes a variable to hold up the formatted date and value is an attribute that takes the reference of the Date object.

<fmt:setBundle>: It is used to prepare the ResourceBundle object on the basis of a particular properties file.
Syntax: <fmt:setBundle var=”—” basename=”—”/>, where var is an attribute that takes a variable to hold up ResourceBundle object reference and basename is an attribute that takes the base name of the properties file.

<fmt:message>: It can be used to get the message from the ResourceBundle object on the basis of the provided key.
Syntax: <fmt:message var=”—” key=”—”/>, where var is an attribute that takes a variable to hold up the message and the key is an attribute that takes the key of the message defined in the respective properties file.

Different Locale Methods to detect the requester’s Locale, Location, and Language
  1. getCountry(): It gives the country/region code in the upper case for this locale in ISO 3166 2-letter format.
  2. getDisplayCountry(): It gives the locale’s country that is appropriate for display to the user.
  3. getLanguage(): It gives the language code in lower case for this locale in ISO 639 format.
  4. getDisplayLanguage(): It gives the name for the locale’s language that is appropriate for display to the user.
  5. getISO3Conutry(): It gives a three-letter abbreviation for this locale’s country.
  6. getISO3Language(): It gives a three-letter abbreviation for this locale’s language.
Detecting Locale in JSP

In this example, we will show how to display a language and associated country for a request in a JSP.

country.jsp
<%@ page import="java.io.*,java.util.Locale"%>
<%@ page import="javax.servlet.*,javax.servlet.http.* "%>
<%
//Get the client's Locale
Locale locale = request.getLocale();
String language = locale.getLanguage();
String country = locale.getCountry();
%>
<html>
<head>
<title>Detecting Locale</title>
</head>
<body>
 <h1>Detecting Locale</h1>
 <p>
  <%
  out.println("Language : " + language + "<br />");
  out.println("Country  : " + country + "<br />");
  %>
 </p>
</body>
</html>
Output

Detecting Locale in JSP

Languages setting in JSP

In this example, we will set the Content-Language header to display all the characters properly as a JSP can output a page in Western European Languages such as English, Spanish, German, etc. so it is important to set the language.

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

<%
// Set response content type
response.setContentType("text/html");

// Set spanish language code.
response.setHeader("Content-Language", "es");
String title = "En Español";
%>
<html>
<head>
<title>
 <%
 out.print(title);
 %>
</title>
</head>
<body>
  <h1>
   <%
   out.print(title);
   %>
  </h1>
 <div>
  <p>En Español</p>
  <p>¡Hola Mundo!</p>
 </div>
</body>
</html>
Output

Internationalization in JSP |i18n| l10n with Examples

Locale Specific Dates in JSP

In this example, we will show how to format dates specific to a given locale. Here we are using java.text.DateFormat class and its static getDateTimeIntance() method to format date and time specific to the locale.

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

<%
String title = "Locale Specific Dates";

//Get the client's Locale
Locale locale = request.getLocale();

String date = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, locale).format(new Date());
%>

<html>
<head>
<title>
 <%
 out.print(title);
 %>
</title>
</head>

<body>
 <h1>
  <%
  out.print(title);
  %>
 </h1>
 <div>
  <p>
   Local Date:
   <%
  out.print(date);
  %>
  </p>
 </div>
</body>
</html>
Output

Locale Specific Dates in JSP

Locale Specific Currency in JSP

In this example, we will show how o format currency specific to a given locale. Here we are using java.txt.NumberFormat class and its static getCurrencyInstance() method to format a number in a locale-specific currency.

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

<%
String title = "Locale Specific Currency";

//Get the client's Locale
Locale locale = request.getLocale();

NumberFormat nft = NumberFormat.getCurrencyInstance(locale);
String formattedCurr = nft.format(1000000);
%>

<html>
<head>
<title>
 <%
 out.print(title);
 %>
</title>
</head>

<body>
 <h1>
  <%
  out.print(title);
  %>
 </h1>
 <div>
  <p>
   Formatted Currency:
   <%
  out.print(formattedCurr);
  %>
  </p>
 </div>
</body>
</html>
Output

Locale Specific Currency in JSP

Locale Specific Percentage in JSP

In this example, we will show to format percentage specific to a given locale. Here we are using java.txt.NumberFormat class and its static getPercentInstance() method to get locale-specific percentage.

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

<%
String title = "Locale Specific Percentage";

//Get the client's Locale
Locale locale = request.getLocale();

NumberFormat nft = NumberFormat.getPercentInstance(locale);
String formattedPerc = nft.format(0.51);
%>

<html>
<head>
<title>
 <%
 out.print(title);
 %>
</title>
</head>
<body>
 <h1>
  <%
  out.print(title);
  %>
 </h1>
 <div>
  <p>
   Formatted Percentage:
   <%
  out.print(formattedPerc);
  %>
  </p>
 </div>
</body>
</html>
Output

Internationalization in JSP Applications with Examples

In the next article, I am going to discuss MVC in JSP Application. Here, in this article, I try to explain Internationalization in JSP Applications with Examples and I hope you enjoy the Internationalization in JSP with Examples article.

Leave a Reply

Your email address will not be published.