Wednesday, December 2, 2015

INTERNATIONALIZATION(I18N) IN JAVA

In this post we will discuss and learn about Internationalization in Java with examples.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.


Examples:

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();

Example:
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();

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));
}
}
Output:



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(); -> Compile time Error

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()));
}
}
Output:







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

Output:






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()));
}
}
Output:



I hope you people enjoy this post and share this to your friends and also in social websites,write your comments on this topic,keep follow me for latest updates.

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...