Open In App

Servlets – Internationalization(I18N)

Internationalization(I18N) is the process of designing web applications in such a way that it provides support for various countries, various languages, and various currencies automatically without performing any change in the application is called Internationalization(I18N). It is known as I18N because between I and N; there are 18 characters; that’s why I18N. Internationalization is one of the powerful concepts of java if you are developing an application and want to display messages, currencies, dates, time, etc., according to the specific region or language. Rather deep-diving into the concept, let’s first discuss a few topics which will be helpful for understanding the concepts:

Initiating Locale



java.util.Locale request.getLocale()

Methods Used



  1. getCountry(): It returns the country/region code in upper case for this locale in ISO3166 2-letter format.
  2. getDisplayCountry(): It returns the name for the locale’s country which is appropriate for display to the user.
  3. getLanguage(): It returns the language code in lower case for this locale in ISO 639 format.
  4. getDisplayLanguage(): It returns the name for the locale’s language that is appropriate for display to the user.
  5. getISO3Country(): It returns a three-letter abbreviation for this locale’s country.
  6. getISO3Language(): It returns a three-letter abbreviation for this locale’s language.

Example

This example shows how you display a language and associated country for a request




import java.io.*;
import java.util.Locale;
import javax.servlet.*;
import javax.servlet.http.*;
  
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");
        PrintWriter out = response.getWriter();
        String title = "Detecting Locale";
  
        String docType =
  
            "<!doctype html public \"-//w3c//dtd html 4.0 "
            + "transitional//en\">\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

Locale Specific Dates

You can use the java.text.DateFormat class and its static getDateTimeInstance() method to format date and time specific to locale. Following is the example which shows how to format dates specific to a given locale −




import java.io.*;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import javax.servlet.*;
import javax.servlet.http.*;
  
public class DateLocale extends HttpServlet {
  
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
  
        throws ServletException, IOException
    {
  
        // Set response content type
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
        // Get the client's Locale
        Locale locale = request.getLocale();
        String date = DateFormat
                          .getDateTimeInstance(
                              DateFormat.FULL,
                              DateFormat.SHORT, locale)
                          .format(new Date());
  
        String title = "Locale Specific Dates";
        String docType =
  
            "<!doctype html public \"-//w3c//dtd html 4.0 "
            + "transitional//en\">\n";
  
        out.println(docType +
  
                        "<html>\n"
                        +
  
                        "<head><title>" + title
                        + "</title></head>\n"
                        +
  
                        "<body bgcolor = \"#f0f0f0\">\n"
                        +
  
                        "<h1 align = \"center\">" + date
                        + "</h1>\n"
                        +
  
                        "</body>
  
                    < / html > "
  
        );
    }
}


Article Tags :