Latest web development tutorials

JSP Internationalization

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

  • Internationalization (i18n): indicates that a page based on the visitor's language or country presented different translations.
  • Localization (l10n): add resources to the site, in order to adapt it to different regions and cultures. For example, the Indian language versions of the site.
  • Area: This is a specific region or culture, generally considered to be a sign language and national symbols are connected by an underscore. Such as "en_US" for US English regions.

If you want to create a global website, you need to be concerned about a number of projects. This chapter will show you in detail how to deal with international issues, and gives some examples to deepen understanding.

JSP container can request according to the locale attribute to provide the correct version of the page. Next we show how to obtain a Locale object by request object syntax:

java.util.Locale request.getLocale() 

Detection Locale

The following table lists a Locale object more important method for detecting request object region, language, and region. All of these methods will display the name of the country and the language name in the browser:

No. Method & description
1 String getCountry ()

Back Country region / area code of the capital, or ISO 3166 2-letter format
2 String getDisplayCountry ()

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

Returns the language code English lowercase, or regional ISO 639 formats
4 String getDisplayLanguage ()

Back to give the user to see the name of the language
5 String getISO3Country ()

Returns the name of the country's three-letter abbreviation
6 String getISO3Language ()

3 returns the language name initials

Examples Demo

This example demonstrates how to display the language and country in the JSP:

<%@ page import="java.io.*,java.util.Locale" %>
<%@ page import="javax.servlet.*,javax.servlet.http.* "%>
<%
   //获取客户端本地化信息
   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 be used to output a page for Western European languages, such as English, Spanish, German, French, Italian and so on. Thus, set the Content-Language header information correctly displays all characters are very important.

The second point is, we need to use HTML character entities to display special characters, such as "& # 241;" represents "& # 161;" represents the "?": "?"

<%@ 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>

Regional specific dates

You can use java.text.DateFormat class and its static methods getDateTimeInstance () to format date and time. This next example shows how to format according to the specified area of ​​the date and time:

<%@ 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 currencies

You can use java.text.NumberFormat class and its static methods getCurrencyInstance () to format numbers. For example, in a particular region of the long currency type and double type. The following example shows how to format according to the specified area Currency:

<%@ 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>

A certain percentage of the area

You can use java.text.NumberFormat class and its static methods getPercentInstance () to format percentages. The following example demonstrates how to format according to the specified percentage of the area:

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