Wednesday, March 4, 2009

Locale example tutorial

Internationalization(i18n):
The process of developing web application that can be used any environment that supports various languages and regions with out making any changes in the application.
We can achive this i18n by using java.util.Locale class or localeNumber format and dateformat classes.
A locale object represents a specific geographical or political or cultural regions.
Locale class present in java.util. package .It is a final class.We are not allowed to create the child class .The locale class implements serializable and clonable interface.
Constructor for the locale object:
Locale l=new Locale(String lan)
Locale l=new Locale(String lan, String country);
Methods in Locale class:
Public Static Locale getDefault();
Return the default locale of the JVM mostly it is US locale(JVM)
Public static void setDefault(Locale New Locale);
Public String getCountry();
Public String getDisplayCountry();
Public String getLanguage();
Public String getDisplayLanguage();
Public static String [] getISOCountries();
Return an array of two letter codes for all the countries defined by ISO-31 slandered which is supported by JVM
Public static String [] getISOLanguages();
Return an array of two letter codes for all the countries defined by ISO-639 slandered which is supported by JVM

Public static Locale[] get availbleLocales();

Locale Demo:
Import java.util.*;
Class Sample
{
public static void main(Strring arg[])
{
Locale l =Locale.getDefault();
System.out.println(l.getCountry()); //us
System.out.println(l.getDisplayCountry()); //united states

System.out.println(l.getLanguage()); //en

System.out.println(l.getDisplayLanguage()); //English
Locale l=new Locale (“pa”,”IN”);
Locale.setDefault(l1);
String [] s=Locale.getISOLanguages();
for(String s1:s)
{
System.out.println(s1);
}
String[] s2=Locale.getISOCountries();
for(String s3:s2)
{
System.out.println(s3);
}
Locale[] l3=Locale.getAvailableLocales();
for(Locale l4: l3)
{
System.out.println(l4.getCountry+”…..”+l4.getLanguage());
}
}

Locale class contain several constants which represents locale objects.
Ex: US,UK,ITALY..........

0 comments:

Post a Comment