Pr.Pg Next Pg

Servlets Internationalization

  • The process of preparing an application to support more than one language and data format is called internationalization.

  • Localization is the process of adapting an internationalized application to support a specific region or locale.

  • A Locale class object represents a specific geographical, political, or cultural region. An operation that requires a Locale to perform its task is called locale-sensitive and uses the Locale to tailor information for the user.

  • For example, displaying a number is a locale-sensitive operation the number should be formatted according to the customs/conventions of the user's native country, region, or culture.

  • Locale-sensitive data is stored in a java.util.ResourceBundle. A resource bundle contains key-value pairs, where the key uniquely identifies a locale-specific object in the bundle.

  • A resource bundle can be backed by a text file properties resource bundle or a class list resource bundle containing the pairs.

  • You construct a resource bundle instance by appending a locale string representation to a base name.

  • Below are the method use for detecting the client location, language and locale:

 

 Return Type

Method

Description

Object

clone() 

Overrides Cloneable

boolean

equals(Object obj) 

Returns true if this Locale is equal to another object.

static Locale[]

getAvailableLocales() 

Returns an array of all installed locales.

String

getCountry() 

Returns the country/region code for this locale, which will either be the empty string or an uppercase ISO 3166 2-letter code.

static Locale

getDefault() 

Gets the current value of the default locale for this instance of the Java Virtual Machine.

String

getDisplayCountry() 

Returns a name for the locale's country that is appropriate for display to the user.

String

getDisplayCountry(Locale inLocale) 

Returns a name for the locale's country that is appropriate for display to the user.

String

getDisplayLanguage() 

Returns a name for the locale's language that is appropriate for display to the user.

String

getDisplayLanguage(Locale inLocale) 

Returns a name for the locale's language that is appropriate for display to the user.

String

getDisplayName() 

Returns a name for the locale that is appropriate for display to the user.

String

getLanguage()

Returns the language code for this locale, which will either be the empty string or a lowercase ISO 639 code.

 

Languages Setting

  • The best way to generate a page containing multiple languages is to output 16-bit Unicode characters to the client.

  • There are two common ways to do this: UCS-2 and UTF-8. UCS-2 (Universal Character Set, 2-byte form) sends Unicode characters in what could be called their natural format, two bytes per character.

  • All characters, including US-ASCII characters, require two bytes. UTF-8 (UCS Transformation Format, 8-bit form) is a variable-length encoding. With UTF-8, a Unicode character is transformed into a 1, 2, or 3-byte representation.

  • In general, UTF-8 tends to be more efficient than UCS-2 because it can encode a character from the US-ASCII charset using just 1 byte. 

 

Locale Specific Dates

  • Date Format helps you to format and parse dates for any locale. Your code can be completely independent of the locale conventions for months, days of the week, or even the calendar format: lunar vs. solar.

  • Use static factory method getDateTimeInstance() for formatting date and time specific to locate.

 

Locale Specific Currency

  • NumberFormat helps you to format and parse numbers for any locale. Your code can be completely independent of the locale conventions for decimal points, thousands-separators, or even the particular decimal digits used, or whether the number format is even decimal.

  • Use getCurrencyInstance to get the currency number formate of any specific given locale.

 

Locale Specific Percentage

  • You can use getPercentInstance() method of NumberFormat class to format percentages of different locale

 

 

Example: Servlet program how to display language and associated country

Sam Sir

//Program name InternationalizationServlet.java

// Servlet program how to display language and associated country

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.util.Locale;

 

public class InternationalizationServlet extends HttpServlet{

 

public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException

{

Locale locale = request.getLocale();

String language = locale.getLanguage();

String country = locale.getCountry();

 

response.setContentType("text/html");

PrintWriter out = response.getWriter();

 

out.println(language + ":" + country);

}

}

Output

Servlet Language setting

 

 

Example: Servlet program to display Hello World! in different language

Sam Sir

//Program name languages.java

// Program to display Hello World! in different language

import java.io.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

 

public class languages extends HttpServlet {

 

public void doGet(HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException {

Locale locale;

res.setContentType("text/plain; charset=UTF-8");

PrintWriter out = res.getWriter();

 

locale = new Locale("en", "US");

 

out.println("In English appropriate for the US:");

out.println("Hello World!");

out.println();

locale = new Locale("es", "");

 

out.println("En Espa\u00f1ol:");

out.println("Hola Mundo!");

out.println();

 

locale = new Locale("ja", "");

 

out.println("In Japanese:");

out.println("\u4eca\u65e5\u306f\u4e16\u754c");

 

locale = new Locale("zh", "");

}

}

Output

Servlet internationalization language setting

 


 

Example: Servlet program to display date time format of different location

Sam Sir

//Program name datetimeformat.java

// Program to display date time format of different location

import java.io.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.text.*;

public class datetimeformat extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException {

Locale locale;

DateFormat full;

res.setContentType("text/plain; charset=UTF-8");

PrintWriter out = res.getWriter();

 

locale = new Locale("en", "US");

full = DateFormat.getDateTimeInstance(DateFormat.LONG,

DateFormat.LONG,

locale);

out.println("US date time format");

out.println(full.format(new Date()));

out.println();

 

locale = new Locale("es", "");

full = DateFormat.getDateTimeInstance(DateFormat.LONG,

DateFormat.LONG,

locale);

out.println("Espanol date time format");

out.println(full.format(new Date()));

out.println();

 

locale = new Locale("ja", "");

full = DateFormat.getDateTimeInstance(DateFormat.LONG,

DateFormat.LONG,

locale);

out.println("Japan date time format");

out.println(full.format(new Date()));

out.println();

}

}

Output

Servlet internationalization date and time setting

 

 

 

Example: Servlet program to get the the currency of different location and itís format

Sam Sir

//Program name currencylocale.java

// Program to get the the currency of different location and itís format

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.util.Locale;

import java.text.NumberFormat;

public class currencylocale extends HttpServlet{

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException

{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

Locale defaultcurr = request.getLocale( );

NumberFormat nft = NumberFormat.getCurrencyInstance(defaultcurr);

String formattedCurr = nft.format(2000);

String title = "Locale Specific Currency";

out.println("<H3><Center>"+title+"</Center></H3><BR>");

out.println("Default US Doller : "+formattedCurr+"<BR>");

Locale swedish = new Locale("sv", "SE");

nft=NumberFormat.getCurrencyInstance(swedish);

out.println("Swedish: " + nft.format(2000)+"<BR>");

Locale Canada = new Locale("fr", "CA");

nft=NumberFormat.getCurrencyInstance(Canada);

out.println("Canada: " + nft.format(2000)+"<BR>");

}

}

Output

Servlet internationalization currency setting

 


 

Example:Servlet program to get the percentage format of different locale

Sam Sir

//Program name LocalPercentageformat.java

//Program to get the percentage format of different locale

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.util.*;

import java.text.*;

public class LocalPercentageformat extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

double percentage=75.5;

out.println("<Center><H3>Percentage formate of different locale </H3></Center>");

Locale China =Locale.CHINA;

Locale US=Locale.US;

Locale Italy=Locale.ITALY;

String Chin=NumberFormat.getPercentInstance(China).format(percentage);

String Us=NumberFormat.getPercentInstance(US).format(percentage);

String Ita=NumberFormat.getPercentInstance(Italy).format(percentage);

out.println("Chiness Percentage format: "+Chin +"<BR>");

out.println("US Percentage format: "+Us +"<BR>");

out.println("Italy Percentage format: "+Ita+"<BR>");

}

}

Output

Servlet internationalization percentage formate display of different country

Pr.Pg border                                              Next Pg