Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JSP Internationalization

JSP Internationalization

Before we begin, a few important concepts need to be explained:

If you want to build a globalized website, you need to care about a series of items. This chapter will explain in detail how to handle internationalization issues and provides some examples to deepen your understanding.

The JSP container can provide the correct page version based on the locale attribute of the request. The following gives the syntax for obtaining a Locale object through the request object:



java.util.Locale request.getLocale() 


Detecting Locale

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

No. Method** & **Description
1 String getCountry()

Returns the uppercase English country/region code, or the region in ISO 3166 2-letter format
2 String getDisplayCountry()

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

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

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

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

Returns the 3-letter abbreviation of the language name

Example Demonstration

This example shows how to display the language and country in 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();

%>





Detecting Locale





Detecting Locale

<% out.println("Language : " + language + "
"); out.println("Country : " + country + "
"); %>


Language Settings

JSP can output a page using Western European languages, such as English, Spanish, German, French, Italian, and so on. Thus, 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, for example, "ñ" stands for the letter n with a tilde, and "¡" stands for the inverted exclamation mark:



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



%>





<%  out.print(title); %>





<% out.print(title); %>

En Espa?ol

?Hola Mundo!


Locale-Specific Dates

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 dates and times according to a specified locale:



<%@ 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( ));

%>





<% out.print(title); %>





<% out.print(title); %>

Local Date: <% out.print(date); %>


Locale-Specific Currencies

You can use the java.text.NumberFormat class and its static method getCurrencyInstance() to format numbers, such as long and double values in locale-specific currencies. The following example shows how to format currency according to a specified locale:



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

%>





<% out.print(title); %>





<% out.print(title); %>

Formatted Currency: <% out.print(formattedCurr); %>


Locale-Specific Percentages

You can use the java.text.NumberFormat class and its static method getPercentInstance() to format percentages. The following example shows how to format percentages according to a specified locale:



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

%>





<% out.print(title); %>





<% out.print(title); %>

Formatted Percentage: <% out.print(formattedPerc); %>