Internationalizing Servlets

Introduction

 
With the rapid development of the web and exponential growth in the user base, web applications must be able to adapt themselves to the languages and formats of the users. This means that the web pages should be customized for the clients, based on their language and cultural formatting conventions. Internationalization can be defined as designing an application, which can be adapted to the region or to the language, without going through a massive change in the technology. The abbreviated form of internationalization is i18n, because there are 18 letters between the first “i” and the last “n”.
 
The adapting of software by adding components, which will help to cater the content in the region or language-specific manner, is called localization. The term localization is abbreviated as l10n, because there are 10 letters between “l” and “n”.
 
The process of making a page available in numerous languages is known as Internationalization, but making the page available in a specific language is termed Localization.
Often web applications are used by users of different political or geographical regions. The formatting of the output should be based on the locale. Servlets should be internationalized to adapt to the language and formatting conventions of the clients. Internationalization leads the servlets to tailer the page content according to the client language and cultural formatting.
 

Using Resource Bundle in Servlets

 
A resource bundle is a piece of software that holds application data separate from the program. The modification and updating of the user interface are convenient as there is no need for recompilation. By putting all text used in the program into the resource bundle, it becomes enormously easier to translate the software into other languages. The identification of the specific language and geographic region is done by the locales. These locales help in formatting information, such as date, time, number, and currency. Its constructor takes an ISO language code and an ISO country code. It is simply an identifier and holds no information about how the country and language should be represented. The java.util.Locale class is used to customized and format the data to be presented to the user. The java.util.Locale object contains only a few important members, such as language code, country or region code, and variant code.
 
A locale is denoted by the standard format xx_YY . The xx stands for two-letter language code in lower case and YY stands for two-letter country code. There are several variations for locales, such as hyphen between the language and country code also in lowercase. It is absolutely necessary to separate the language from the country as more than one country might use the same language but might following different conventions for date, time and currency formats. For example: zh_CN for Shanghai, China, ko_KR for Seoul, Korea, en_US for English, US.
 
The resource bundling helps to build server-side code that gives output based on the location and language of the user. It makes the job easier by avoiding the writing of multiple version of a class for different locales. Several methods in ResourceBundle class.
 
getBundle(String baseName)
 
Method represents to gets a resource bundle by using the base name, the default locale, with the caller’s class loader.
 
Syntax:
  1. public static final ResourceBundle getBundle(String baseName)  
where baseName represents here the name of resource bundle.
 
getBundle(String baseName, Locale locale)
 
Method represents getting a resource bundle with a base name with locale, and the caller’s class loader.
 
Syntax:
  1. public static final ResourceBundle getBundle(String baseName, Locale locale)  
where baseName represents here the name of resource bundle with a fully qualified class name.
 
locale - represents the locale for which a resource bundle is desired.
 
getBundle(String baseName, Locale locale, ClassLoader loader)
 
The method used to get a resource bundle using the base name, locale, and class loader.
 
Syntax:
  1. public static ResourceBundle getBundle(String baseName, Locale locale, ClassLoader loader)  
Where baseName represents here the name of resource bundle with a fully qualified class name.
 
locale : represents the locale for which a resource bundle is desired.
 
loader: is the class loader from which to load the resource bundle
 
For example:
  1. public static void iterateKeys(Locale currentLocale) {  
  2.     ResourceBundle labels = ResourceBundle.getBundle(“LabelsBundle”, currentLocale);  
  3.     Enumeration bundleKeys = labels.getKeys();  
  4.     while (bundleKeys.hasMoreElements()) {  
  5.         String keys = (String) bundleKeys.nextElement();  
  6.         String value = labels.getString(keys);  
  7.         System.out.println(“Key: “+keys + “, “+“value: ”+value);  
  8.     }  
  9. }  
In this source code, the method getkeys() returns an enumeration of the locale keys.
 
getLocale()
 
Method returns the locale of this resource bundle.
 
For example,
  1. Locale locale = request.getLocale();  
  2. ResourceBundle bundle = ResourceBundle.getBundle(“i18n.WelcomeDemoBundle”, locale);  
  3. String welcome = bundle.getString(“DemoWelcome”);  
getObject(String key)
 
Method gets an object for the given key from the resource bundle or one of its parents.
 
Syntax :
  1. public final Object getObject(String key)   
Where key represents the key for the desired object.
 
For example:
  1. int[] myDemoIntegers=(int[]) myDemoResources.getObject(“intList”);  
getString(String key)
 
Method gets a string from this resource bundle or one of its parents.
 
Syntax:
  1. public final String getString(String key)   
where key is the key for the desired string.
 
getStringArray(String key)
 
Method gets a string array for the given key from this resource bundle also one of its parents.
 
Syntax is,
  1. public final String[] getStringArray(String key)   
where key is the key for the desired string array.
 
handleGetObject(String key)
 
Method gets an object for the given key from the resource bundle.
 
Syntax:
  1. protected abstract Object handleGetObject(String key)   
where key is the key for the desired object.
 
setParent(ResourceBundle parent)
 
The method sets the parent bundle of this bundle.
 
Syntax:
  1. protected void setParent(ResourceBundle parent)  
where parent is the current bundle’s parent bundle.
 
Formatting Dates in Servlets
 
Formatting dates helps the programmer to present the data as per the locale or the region of the user. Several formats in which dates can be displayed are as follows:
 
Using Predefined Formats
 
The DateFormat style is pre-defined and locale-specific also easy to use.
 
The Styles are,
  • SHORT- It is completely numeric, such as 11.12.50 or 3:30PM
  • MEDIUM- It is Longer, such as Jan12, 2021
  • LONG- Is is longer, such as January 12, 2021 or 12:01:00 PM
  • FULL- It is completed specified , such as Sunday, January 12, 2021 AD or 12:01:00 PM PST.
There are two steps inn formatting of date using the DateFormat class. The first step is to create a formatted with the getDateInstance() method. The second step consists of invoking the format method, which returns a formatted date in the form of a string.
 
Customizing Formats
 
Most of the time, the predefined formats are not enough and customized formats are required. To do that the SimpleDateFormat  class is used.
 
A SimpleDateFormat object is created with specified pattern strings. The date format is determined by the contents of the pattern string.
 
The DateFormat class
 
There are several classes, such as DateFormat , for formatting dates, The DateFormat is an abstract class, which formats and parses the dates and time in a language-independent manner.
 
Some of the methods are:
 
getDateInstance()
 
This method retrieves the date formatter for the default locale with the default style.
 
The syntax:
  1. public static final DateFormat getDateInstance()   
format()
 
This method formats an object of the Date class into String type.
 
The syntax:
  1. public final String format(Date date)   
where the date is the date or time value to be formatted to a string.
 
getDateTimeInstance()
 
This method retrieves the date or time formatter for the default locale with the default style.
 
The syntax:
  1. public static final DateFormat getDateTimeInstance()   
getTimeInstance()
 
This method retrieves the time formatter for the default locale with the default style.
 
Syntax:
  1. public static final DateFormat getTime Instance(int style)   
where style represents the given formatting style.
 
parse()
 
This method converts a given string to a Date type object.
 
Syntax:
  1. public Date parse(String text) throws ParseException   
where text represents the date/time string to be converted.
 
For example:
  1. Date today;  
  2. String dateOut;  
  3. DateFormat dateFormatter;  
  4. Locale currentLocale;  
  5. dateFormatter=DateFormat.getDateInstance(DateFormat.DEFAULT, currentLocale);  
  6. today=new Date();  
  7. dateOut=dateFormatter.format(today);  
  8. System.out.println(dateOut + “ ”+currentLocale.toString());  

Summary

 
Internationalization can be defined as designing a Web application, which can be adapted to the region or the language without going through any massive change in the technology. The Process of making a page available as per the region or language is termed as localization. A locale is denoted by the standard format xx_YY . The xx stands for two-letter language code in lower case and YY stands for two-letter country code. The java.util.Locale object contains only a few important members, such as language code, country or region code, and variant code.


Similar Articles