Translate

Monday, November 5, 2012

INTERNATIONALIZATION(I18N)


INTERNATIONALIZATION(I18N)

Introduction

It is the process of developing web application so that it can be used in any environment. That supports various languages and various countries, with out making any changes in the application we can achieve this by using following classes.

1) Locale: To represent a particular region.
2) NumberFormat: For formatting Numbers.
3) DateFormat: For formatting Dates.

Locale Class

A Locale object represents a particular region with respect to country (or) language. It is a final class available in java.util package and implements Serializable and Clonable interfaces.

Construction of Locale Objects

We can construct the Locale Object by using the following Locale class constructor.

1) Locale l = new Locale(String Language);
2) Locale l = new Locale(String Language, String Country);

Locale class already defined some standard locale objects in the form of constants.

Ex: public static final Locale UK;
public static final Locale ITALY;

Important methods of Locale Class

1) public tatic Locale getDefault(): Returns the default locale configure in JVM.
2) public static void setDefault(Locale l) To set our own Locale.
3) public String getCountry();
4) public String getDisplayCountry();
5) public String getLanguages();
6) public String getDisplayLanguages();
7) public static String[] getISOCountries()
Returns ISO countries supported by the JVM.
8) public static String[] getISOLanguages();
9) public static Locale[] getAvailableLocales();

Ex

import java.util.*;
class LocaleDemo1
{
public static void main(String arg[])
{
Locale l = Locale.getDefault();
System.out.println(l.getCountry()+"..."+l.getLanguage());
System.out.println(l.getDisplayCountry()+"..."+l.getDisplayLanguage());
Locale l2 = new Locale("pa","IN");
Locale.setDefault(l2);
String s3[] = Locale.getISOLanguages();
for (String s4: s3)
{
System.out.println(s4);
}
String s5[] = Locale.getISOCountries();
for (String s6: s5)
{
System.out.println(s6);
}
Locale l3[] = Locale.getAvailableLocales();
for (Locale l4: l3)
{
System.out.println(l4);
System.out.println(l4.getDisplayCountry()+"....."+l4.getDisplayLanguage());
}
}
}
NumberFormat Class

We can use this class for formatting the numbers according to a particular Locale. This class is available in java.text package.
Getting Number Format object for default locate NumberFormat class contains the following factory methods to get NumberFormat Object
.
Important methods of NumberFormat Class

1) public static NumberFormat getInstance();
2) public static NumberFormat getCurrencyInstance();
3) public static NumberFormat getPercentInstance();
4) public static NumberFormat getNumberInstance();

Getting NumberFormat Object for a particular locale we have to pass the corresponding locale object as the argument to the above methods. public static NumberFormat getCurrencyInstance(Locale l)
NumberFormat class contains the following methods for converting a java number to the Locale Specific number form.
àString format(long l)
à String format(double d)
NumberFormat class contains the following method for converting Locale specific form to java
NumberForm
Number parse(String s) throws parseException.
Consider the java number 123456.789 represents this no in ITALY, US, CHINA, Specific Forms

Ex:
import java.text.*;
import java.util.*;
class NumberFormatDemo
{
public static void main(String[] args)
{
double d1 = 123456.789;
Locale india = new Locale("pa","IN");
NumberFormat nf = NumberFormat.getCurrencyInstance(india);
System.out.println("India Notation is ....." + nf.format(d1));
NumberFormat nf1 =
NumberFormat.getCurrencyInstance(Locale.ITALY);
System.out.println("Italy Notation is ....." + nf1.format(d1));
NumberFormat nf2 =
  NumberFormat.getCurrencyInstance(Locale.CHINA);
System.out.println("China Notation is ....." +                  nf2.format(d1));
NumberFormat nf3 =
                     NumberFormat.getCurrencyInstance(Locale.US);
System.out.println("US Notation is ....." + nf3.format(d1));
}
}
O/P:- India Notation is ------ INR123,456.79
         Italy Notation is --------C 123.456.79
         China Notation is ------?123,456.79
          US Notation is ---------$123.456.79


Setting max/min integer and fraction digits:

NumberFormat class contains the following methods for specifying max and min fraction and integer digits.

1) public void setMaximamIntegerDigits(int n);
2) public void setMinimumIntegerDigits(int n);
3) public void setMaximamFractionDigits(int n);
4) public void setMinimumFractionDigits(int n);
NumberFormat nf = NumberFormat.getInstance();

Case1:-

nf.setMaximumIntegerDigits(4);
System.out.println(nf.format(123456.789)); à 3,456.789
Case2:-
nf.setMinimumIntegerDigits(4);
System.out.println(nf.format(12.456)); à 0012.456
Case3:-
nf.setMaximumFractionDigits(2);
System.out.println(nf.format(123456.789)); à 123,456.79
Case4:-
nf.setMinimumFractionDigits(3);
System.out.println(nf.format(123.4)); à 123.400
DateFormat Class
DateFormat class can be used for formatting the dates according to a particular locale. DateFormat class is available in java.text package. DateFormat class is an abstract class we can’t create an object by using the constructor.
DateFormat df = new DateFormat(); à C.E

Creation of DateFormat Object for a default loacale

DateFormat class contains the following methods to get DateFormat Objects.

public static DateFormat getDateInstance();
public static DateFormat getDateInstance();
public static DateFormat getDateInstance(int style)
Where style is DateFormats
FULL – 0
LONG-1
MEDIUM-2
SHORT-3

Creation of DateFormat Object for a specific Locale

public static DateFormat getDateInstance(int style, Locale l)
DateFormat class contain the following method for converting java Date form to Locale specific form.
String format(Date d)
DateFormat class contain the following method for converting Locale Specific form to java Date form.
Date parse(String s)throws parseException

The program to display the current system date in all possible styles according to US Locale

Ex:

import java.text.*;
import java.util.*;
class DateFormatDemo
{
public static void main(String[] args)
{
System.out.println("Full form is --" +
DateFormat.getDateInstance(0).format(new Date()));
System.out.println("Long form is --" +
DateFormat.getDateInstance(1).format(new Date()));
System.out.println("Medium form is --" +
DateFormat.getDateInstance(2).format(new Date()));
System.out.println("Short form is --" +
DateFormat.getDateInstance(3).format(new Date()));
}
}
O/P:-
Full form is – Monday, November 05, 2012
Long form is – November 05, 2012
Medium form is – Nov 05, 2012
Short form is – 11/05/12

The Demo program for displaying the current date according to UK,US and ITALY specific ways.

import java.text.*;
import java.util.*;
class DateFormatDemo
{
public static void main(String[] args)
{
DateFormat UK =    DateFormat.getDateInstance(0,Locale.UK);
DateFormat US = DateFormat.getDateInstance(0,Locale.US);
DateFormat ITALY =               DateFormat.getDateInstance(0,Locale.ITALY);
System.out.println("UK Style is --" + UK.format(new Date()));
System.out.println("US Style is --" + US.format(new Date()));
System.out.println("ITALY Style is --" + ITALY.format(new Date()));
}
}
O/P:-
UK Style is – Monday, 05 November 2012
US Style is – Monday, November 05, 2012
ITALY Style is – marted~ n…… 2012





Creation of DateFormat Object for representing both Date and Time

The following are the constructors for representing both Data and Time.
public static DateFormat getDateTimeInstance();
public static DateFormat getDateTimeInstance(int dateStyle, int timeStyle);
public static DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale l)

Ex:

import java.text.*;
import java.util.*;
class DateFormatDemo
{
public static void main(String[] args)
{
DateFormat ITALY = DateFormat.getDateTimeInstance(0,0,Locale.ITALY);
System.out.println("ITALY Style is --" + ITALY.format(new Date()));
}
}
O/P:-
         ITALY Style is –marted~ 24 febbraio 2009 13.54.06 IST
   

No comments: