Servlet Internationalization
Before we begin, let's look at three important terms:
Internationalization (i18n): This means a website provides different versions translated into the visitor's language or nationality.
Localization (l10n): This means adding resources to a website to make it adaptable to a specific geographic or cultural region, such as translating the website into Hindi.
Locale: This is a specific cultural or geographic region. It usually refers to a language symbol followed by an underscore and a country symbol. For example, "en_US" represents the English locale for the US.
When building a global website, there are some considerations to keep in mind. This tutorial will not cover the full details of these considerations, but it will demonstrate through a good example how to make web pages appear in different languages by differentiating the locale.
A Servlet can pick the appropriate version of the website based on the requester's locale and provide the corresponding website version according to the local language, culture, and needs. Below is the method in the request object that returns the Locale object.
java.util.Locale request.getLocale()
Detecting Locale
The following lists important locale methods that you can use to detect the requester's geographic location, language, and locale. All of the methods below show the country name and language name set in the requester's browser.
Number | Method & Description |
---|---|
1 | String getCountry() <br>This method returns the country/region code of this locale in the ISO 3166 two-letter uppercase format. |
2 | String getDisplayCountry() <br>This method returns a name for the locale's country that is appropriate for display to the user. |
3 | String getLanguage() <br>This method returns the language code of this locale in the ISO 639 lowercase format. |
4 | String getDisplayLanguage() <br>This method returns a name for the locale's language that is appropriate for display to the user. |
5 | String getISO3Country() <br>This method returns a three-letter abbreviation for the locale's country. |
6 | String getISO3Language() <br>This method returns a three-letter abbreviation for the locale's language. |
Example
This example demonstrates how to display the language and associated country of a request:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;
public class GetLocale extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Get the client's locale
Locale locale = request.getLocale();
String language = locale.getLanguage();
String country = locale.getCountry();
// Set response content type
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String title = "Detecting Locale";
String docType = "<!DOCTYPE html> \n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + language + "</h1>\n" +
"<h2 align=\"center\">" + country + "</h2>\n" +
"</body></html>");
}
}
Language Settings
Servlets can output pages written in Western European languages such as English, Spanish, German, French, Italian, Dutch, etc. Here, it is important to set the Content-Language header to correctly display all characters. The second point is to display all special characters using HTML entities. For example, "ñ" represents "ñ" and "¡" represents "¡", as shown below:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;
public class DisplaySpanish extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Set response content type
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// Set Spanish language code
response.setHeader("Content-Language", "es");
String title = "En Español";
String docType = "<!DOCTYPE html> \n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1>" + "En Español:" + "</h1>\n" +
"<h1>" + "¡Hola Mundo!" + "</h1>\n" +
"</body></html>");
}
}
Locale-Specific Date
You can use the java.text.DateFormat class and its static method getDateTimeInstance() to format locale-specific dates and times. The following example demonstrates how to format a date for a given locale:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;
import java.text.DateFormat;
import java.util.Date;
public class DateLocale extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Set response content type
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// Get client's locale
Locale locale = request.getLocale();
String date = DateFormat.getDateTimeInstance(
DateFormat.FULL,
DateFormat.SHORT,
locale).format(new Date());
String title = "Locale-Specific Date";
String docType = "<!DOCTYPE html> \n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + date + "</h1>\n" +
"</body></html>");
}
}
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;
import java.text.NumberFormat;
import java.util.Date;
public class CurrencyLocale extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Set response content type
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// Get client's locale
Locale locale = request.getLocale();
NumberFormat nft = NumberFormat.getCurrencyInstance(locale);
String formattedCurr = nft.format(1000000);
String title = "Locale Specific Currency";
String docType = "<!DOCTYPE html> \n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + formattedCurr + "</h1>\n" +
"</body></html>");
}
}
Locale Specific Percentage
You can use the java.text.NumberFormat
class and its static method getPercentInstance()
to format locale-specific percentages. The following example demonstrates how to format a percentage for a given locale:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;
import java.text.NumberFormat;
import java.util.Date;
public class PercentageLocale extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Set response content type
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// Get client's locale
Locale locale = request.getLocale();
NumberFormat nft = NumberFormat.getPercentInstance(locale);
String formattedPerc = nft.format(0.51);
String title = "Locale Specific Percentage";
String docType = "<!DOCTYPE html> \n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + formattedPerc + "</h1>\n" +
"</body></html>");
}
}
"<head><title>" + title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n" + "<h1 align=\"center\">" + formattedPerc + "</h1>\n" + "</body></html>"); } }