English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Before we begin, we need to explain several important concepts:
If you want to build a globalized website, you need to be concerned about a series of projects. This chapter will explain in detail how to handle internationalization issues and provide some examples to enhance understanding.
The JSP container can provide the correct page version based on the locale attribute of the request. The syntax for obtaining the Locale object through the request object is given next:
java.util.Locale request.getLocale()
The following table lists some important methods in the Locale object, used to detect the region, language, and locale of the request object. All these methods will display the country name and language name in the browser:
Number | Method & Description |
---|---|
1 | String getCountry() Returns the country/The English uppercase region code, or ISO 3166 2-Letter formatted region |
2 | String getDisplayCountry() Returns the country name to be displayed to the user |
3 | String getLanguage() Returns the language code in lowercase English, or ISO 639 Formatted region |
4 | String getDisplayLanguage() Returns the language name to be displayed to the user |
5 | String getISO3Country() Returns the country name of3Abbreviation |
6 | String getISO3Language() Returns the language name of3Abbreviation |
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>
JSP can use Western European languages to output a page, such as English, Spanish, German, French, Italian, and so on. From this, it can be seen that setting the Content-It is very important to set the Language information header correctly to display all characters.
The second point is that it is necessary to use HTML character entities to display special characters, such as "ñ" represents the ñ, "¡" 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 = "In Spanish"; %> <html> <head> <title><% out.print(title); %></title> </head> <body> <center> <h1><% out.print(title); %></h1> </center> <div align="center"> <p>In Spanish</p> <p>?Hola Mundo!</p> </div> </body> </html>
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 based on 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( )); %> <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>
You can use the java.text.NumberFormat class and its static method getCurrencyInstance() to format numbers. For example, in locale-specific currencies for long and double types. The following example demonstrates how to format currency based on 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); %> <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>
You can use the java.text.NumberFormat class and its static method getPercentInstance() to format percentages. The following example shows how to format percentages based on 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); %> <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>