WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JSP internationalization


Before you begin, you need to explain a few important concepts:

  • Internationalization (i18n): indicates that a page renders different translations according to the language or country of the visitor.
  • Localization (l10n): Add resources to your website to adapt it to different regions and cultures. For example, the Hindi version of the website.
  • Region: This is a specific region or culture that is generally considered to be a language sign and a country sign connected by an underscore. For example, "en_US" stands for American English.

If you want to build a global website, you need to care about a series of projects. This chapter will show you how to deal with internationalization in detail, and give some examples to deepen your understanding.

The JSP container can provide the correct page version based on the locale attribute of the request. The syntax of how to get the Locale object from the request object is given next:

java.util.Locale request.getLocale() 

Detection of Locale

The following table lists the more important methods in the Locale object, which are used to detect the region, language, and region of the request object. All these methods will display the country name and language name in the browser:

Serial number method & Description
1 String getCountry ()

Return the country code in English capitals, or ISO 3166 2-letter format region
2 String getDisplayCountry ()

Returns the name of the country to be displayed to the user
3 String getLanguage ()

Returns the lowercase English of the language code, or a locale in ISO 639 format
4 String getDisplayLanguage ()

Returns the name of the language to be shown to the user
5 String getISO3Country ()

Return 3-letter abbreviation of country name
6 String getISO3Language ()

Return 3-letter abbreviation for language name

Demonstration

This example tells us how to display language and country in JSP:

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

Language settings

JSP can output a page in Western European languages, such as English, Spanish, German, French, Italian, and so on. From this, it is important to set the Content-Language header to display all characters correctly.

The second point is that you need to use HTML character entities to display special characters, such as "&#241;" Stands for ñ,"&#161;"Stands for ¡ :

<%@ 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>
<center>
<h1><%  out.print(title); %></h1>
</center>
<div align="center">
<p>En Espa?ol</p>
<p>?Hola Mundo!</p>
</div>
</body>
</html>

Region-specific date

You can use the java.text.DateFormat class and its static method getDateTimeInstance () to format dates and times. The following example shows how to format a date and time based on a specified region:

<%@ 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>
<center>
<h1><% out.print(title); %></h1>
</center>
<div align="center">
<p>Local Date: <%  out.print(date); %></p>
</div>
</body>
</html>

Region-specific currency

You can use the java.text.NumberFormat class and its static method getCurrencyInstance () to format numbers. For example, long and double in region-specific currencies. The following example shows how to format a currency based on a specified region:

<%@ 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>
<center>
<h1><% out.print(title); %></h1>
</center>
<div align="center">
<p>Formatted Currency: <%  out.print(formattedCurr); %></p>
</div>
</body>
</html>

Region-specific percentage

You can use the java.text.NumberFormat class and its static method getPercentInstance () to format the percentage. The following example tells us how to format the percentage based on the specified area: