Skip to content
Related Articles

Related Articles

Servlets – Internationalization(I18N)

Improve Article
Save Article
Like Article
  • Last Updated : 30 Dec, 2021

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:

  • Internationalization(I18N): it is the task of creating a system that is flexible enough to work in any environment.
  • Localization (I10N): It is the process of arranging for a program to run during a specific locale.
  • Locale: It represents a selected geographical, political, or cultural region. The string representation of a locale consists of the international standard two-character abbreviation for language and country and an optional variant, separated by underscore (_) characters.

Initiating Locale

  • Web application either retrieves the locale from the request using the getLocale() method or allows the user to explicitly select the locale to get the right strings for a given user.
  • An operation that needs a Locale to perform its task is named locale-sensitive and it uses the Locale to tailor information for the user.
  • No validity check is performed once you construct a Locale because a Locale object is simply an identifier for a neighborhood.

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

Java




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

  • A servlet outputs a page written in a Western European language such as English, Spanish, German, French, Italian, Dutch, Norwegian, Finnish, or Swedish. Notice the utilization of the special characters “ñ” and “¡”. Characters like these, while scarce in English, are prevalent in Western European languages. Servlets have two ways to get these characters: with HTML character entities or Unicode escape sequences.
  • The ability for specific sequences of characters in an HTML page to be displayed as a single character. The sequences, called character entities, begin with an ampersand (&) and end with a semi-colon (;). Character entities can either be named or numbered.
  • The Unicode Worldwide Escape Sequences is a character coding system designed to support the interchange, processing, and display of the written texts of the diverse languages of the modern world. In addition, it supports classical and historical texts of the many written languages.

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 −

Java




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 > "
  
        );
    }
}

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!