Recent Posts

Wednesday, 31 August 2016

Internationalization Tutorial


Internationalization
    The process of designing a web application such that it supports various countries, various languages without performing any changes in the application is called Internationalization.
     If the request is coming from India then the response should be in India specific form , and if the request is from US then the response should be in US specific form. We can implement Internationalization by using the following classes.
1. Locale
     To represent a particular region.
2. NumberFormat
     For formatting Numbers.
3. DateFormat
     For formatting Dates.

1. 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.
E.g
public static final Locale UK;
public static final Locale ITALY;

Important methods of Locale Class
1. public static 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();
E.g
package com.ashok.internationalization;

import java.util.Locale;

public class MyLocale {
   public static void main(String arg[]) {
      Locale loc = Locale.getDefault();
      System.out.println("Country is : " + loc.getCountry() + "Language is -->" + loc.getLanguage());
      System.out.println("Display Country is : " + loc.getDisplayCountry() + "Display Language is -->" + loc.getDisplayLanguage());
      Locale loc2 = new Locale("pa", "IN");
      Locale.setDefault(loc2);
      String str[] = Locale.getISOLanguages();
      System.out.println("ISO Languages are : ");
      for (String str1 : str) {
         System.out.println(str1);
      }
      String str2[] = Locale.getISOCountries();
      System.out.println("ISO Countries are : ");
      for (String str3 : str2) {
         System.out.println(str3);
      }
      Locale loc3[] = Locale.getAvailableLocales();
      System.out.println("Available Locales are : ");
      for (Locale loc4 : loc3) {
         System.out.println("Display Country is : " + loc4.getDisplayCountry() + "Display Language is -->" + loc4.getDisplayLanguage());
      }
   }
}
2. NumberFormat Class
     We can use this class for formatting the numbers according to a particular Locale. This class is available in java.text package and it is an abstract class.
Hence we can't create an object by using constructor.
NumberFormat nf=new NumberFormat(); // invalid

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 loc)

     NumberFormat class contains the following methods for converting a java number to the Locale Specific number form.
1. String format(long l)
2. 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, UK, Specific Forms.
package com.ashok.internationalization;

import java.text.NumberFormat;
import java.util.Locale;

public class MyNumberFormat {
   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.UK);
      System.out.println("UK Notation is --->" + nf2.format(d1));
      NumberFormat nf3 = NumberFormat.getCurrencyInstance(Locale.US);
      System.out.println("US Notation is --->" + nf3.format(d1));
   }
}
Output
India Notation is --->INR 123,456.79
Italy Notation is --->€ 123.456,79
UK 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();

Case 1
nf.setMaximumIntegerDigits(4);
System.out.println(nf.format(123456.789)); // 3,456.789

Case 2
nf.setMinimumIntegerDigits(4);
System.out.println(nf.format(12.456)); // 0012.456

Case 3
nf.setMaximumFractionDigits(2);
System.out.println(nf.format(123456.789)); // 123,456.79

Case 4
nf.setMinimumFractionDigits(3);
System.out.println(nf.format(123.4)); // 123.400

3. 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 getInstance();
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
package com.ashok.internationalization;

import java.text.DateFormat;
import java.util.Date;

public class MyDateFormat {
   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
Full form is --Thursday, September 1, 2016
Long form is --September 1, 2016
Medium form is --Sep 1, 2016
Short form is --9/1/16
Program for displaying the current date according to UK,US and ITALY specific ways.
package com.ashok.internationalization;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

class MyDateFormat {
   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
UK Style is --> Thursday, 1 September 2016
US Style is --> Thursday, September 1, 2016
ITALY Style is --> giovedì 1 settembre 2016
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)
E.g
package com.ashok.internationalization;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

class MyDateFormat {
   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
ITALY Style is --> giovedì 1 settembre 2016 16.47.06 IST

Next Tutorial  Enum Tutorial

Previous Tutorial  Inner Classes Tutorial Part 3

No comments:

Post a Comment