Internationalization in Java

Internationalization in Java with Examples

In this article, I am going to discuss Internationalization in Java with Examples. Please read our previous article where we discussed JDBC in Java. At the end of this article, you will understand what is Internationalization in Java and when and how to use this in Java applications.

What is Internationalization in Java?

Internationalization is a mechanism to make such an application that will be adapted to different languages and regions. The method of designing an internet application such it supports various countries, various languages without performing any changes within the application is named Internationalization. It’s referred to as I18N because between I and N there are 18 characters that’s why I18N.

Localization is additionally abbreviated as I10N because there are a total of 10 characters between the primary letter ‘L’ and last letter ‘N’. Localization is the mechanism to make such an application that will be adapted to a selected language and region by adding locale-specific text and components.

We can implement Internationalization by using the following classes. They are:

  1. Locale
  2. NumberFormat
  3. DateFormat

Locale in Java

A Locale object can be used to represent a geographic (Country) location (or) Language. Locale class is present in java.util Language. It is a final class and direct child class of Object implements Cloneable and Serializable Interfaces.

How to create a Locale Object?

We can create a Locale object by using the following constructors of Locale Class.
Locale l = new Locale(String language);
Locale l = new Locale(String language, String country);

Locale class already defines some predefined Locale constants. We can use these constants directly.
Example:
Locale. UK
Locale.US
Locale.ITALY

Methods of Locale Class
  1. pubic static Locale getDefault(): Returns the instance of the current locale.
  2. public String getDisplayLanguage(Locale l): Returns the language name of this locale object.
  3. public String getDisplayCountry(Locale l): Returns the country name of this locale object.
  4. public String getDisplayVariant(Locale l): Returns the variant code for this locale object.
  5. public static Locale[] getAvailableLocales(): Returns an array of available locales.
  6. public static getISO3Country(): Returns the three-letter abbreviation for the current locale’s country.
  7. public static getISO3Language(): Returns the three-letter abbreviation for the current locale’s language.
Sample Program for Locale Class
import java.util.*;
public class LocaleClassDemo 
{
    public static void main (String[]args)
    {
        Locale l1 = Locale.getDefault ();
        //System.out.println(l1.getDisplayCountry()+"....."+l1.getDisplayLanguage());
        Locale l2 = new Locale ("pa", "IN");
        Locale.setDefault (l2);
        
        String[] s3 = Locale.getISOLanguages ();
        for (String s4:s3)
        {
         //System.out.println("ISO Language is :");
         // System.out.println(s4);
        }
        
        String[] s4 = Locale.getISOCountries ();
        for (String s5:s4)
        {
         System.out.println ("ISO Country is:");
         System.out.println (s5);
        }
        
        Locale[]s = Locale.getAvailableLocales ();
        for (Locale s1:s)
        {
         //System.out.println("Available Locales is : ");
         //System.out.println(s1.getDisplayCountry()+"....."+s1.getDisplayLanguage());
        }
    }
}

Output

Internationalization in Java with Examples

Number Format in Java

Various countries follow various styles to represent the number. The representation of the numbers differs from one locale to a different. Internationalizing the numbers is a good approach for the application that displays the information according to the locales.

The NumberFormat class is employed to format the amount consistent with the precise locale. To get the instance of the NumberFormat class, we’d like to call either getInstance() or getNumberInstance() methods.

Example:
1,23,456.789———-INDIA
123,456.789———-US
123.456,789———-ITALY

By using NumberFormat class we can format a number according to a particular Locale. The NumberFormat class present in java.Text package and it is an abstract class. Hence, we can’t create an object by using a constructor.

NumberFormat nf = new NumberFormat(); ———Invalid

Getting NumberFormat object for the default Locale

NumberFormat class defines the following methods for this:
public static NumberFormat getInstance();
public static NumberFormat getCurrencyInstance();
public static NumberFormat getPercentInstance();
public static NumberFormat getNumberInstance();

Getting the NumberFormat object for the specific Locale:

The methods are exactly the same but we have to pass the corresponding Locale object as an argument.
Example: public static NumberFormat getNumberInstance(Locale l);

Once we got NumberFormat object we can call the following methods to format and parse numbers.
public String format(long l);
public String format(double d);

To convert a number from Java form to Locale specific form.
public Number parse(String source) throws ParseException

Sample Program To convert from Locale specific String from to Java-specific form.
import java.util.*;
import java.text.*;
public class NumberFormatDemo1
{
    public static void main (String[]args)
    {
        double d = 123456.789;
        NumberFormat nf = NumberFormat.getInstance (Locale.ITALY);
        System.out.println ("ITALY form is :" + nf.format (d));
    }
}

Output: Internationalization in Java with Examples

Write a Program to print a Java number in INDIA, UK, US, and ITALY currency formats.
import java.util.*;
import java.text.*;
public class NumberFormatDemo2
{
    public static void main (String[]args)
    {
        double d = 123456.789;
        Locale INDIA = new Locale ("pa", "IN");
        NumberFormat nf = NumberFormat.getCurrencyInstance (INDIA);
            System.out.println ("INDIA notation is :" + nf.format (d));
        NumberFormat nf1 = NumberFormat.getCurrencyInstance (Locale.UK);
            System.out.println ("UK notation is : " + nf1.format (d));
        NumberFormat nf2 = NumberFormat.getCurrencyInstance (Locale.US);
            System.out.println ("US notation is : " + nf2.format (d));
        NumberFormat nf3 = NumberFormat.getCurrencyInstance (Locale.ITALY);
            System.out.println ("ITALY notation is : " + nf3.format (d));
    }
}

Output

Program to print a Java number in INDIA, UK, US, and ITALY currency formats

Setting Minimum, Maximum, Fraction and Integer digits:

NumberFormat class defines the following methods for this purpose:

  1. public void setMaximumFractionDigits(int n);
  2. public void setMinimumFractionDigits(int n);
  3. public void setMaximumIntegerDigits(int n);
  4. public void setMinimumIntegerDigits(int n);
Example:
import java.text.*;
public class NumberFormatExample
{
    public static void main (String[]args)
    {
        NumberFormat nf = NumberFormat.getCurrencyInstance ();
        nf.setMaximumFractionDigits (3);
        System.out.println (nf.format (123.4));
        System.out.println (nf.format (123.4567));
        nf.setMinimumFractionDigits (3);
        System.out.println (nf.format (123.4));
        System.out.println (nf.format (123.4567));
        nf.setMaximumIntegerDigits (3);
        System.out.println (nf.format (1.234));
        System.out.println (nf.format (123456.789));
        nf.setMinimumIntegerDigits (3);
        System.out.println (nf.format (1.234));
        System.out.println (nf.format (123456.789));
    }
}

Output

Setting Minimum, Maximum, Fraction and Integer digits

DateFormat in Java

The format of the dates differs from one region to a different that’s why we internationalize the dates. Various countries follow various styles to represent Date. We will format the date consistent with a specific Locale by using DateFromat class. DateFormat class present in java.text package and it’s an abstract class. We will internationalize the date by using the getDateInstance() method of the DateFormat class. The DateFormat class receives the locale object as a parameter and returns the instance of the DateFormat class.

Getting DateFormat object for default Locale:

DateFromat class defines the following methods for this purpose.
public static DateFormat getInstance();
public static DateFormat getDateInstance();
public static DateFormat geDatetInstance(int style);

Here, int style are:
DateFormat.Full ———-> 0
DateFormat.LONG ———-> 1
DateFormat.MEDIUM ———-> 2
DateFormat.SHORT ———-> 3

Getting DateFormat object for the specific Locale

public static DateFormat getDateInstance(int style, Locale l);
Once we got DateFormat object we can format and parse Date by using the following methods.
public String format(Date date); To convert the date from java locale to specific string form.
public Date parse (String source) throws ParseExceptionl; To convert the date from Locale specific form to java form.

Write a Program to represent the current system date in all possible styles of US format.
import java.text.*;
import java.util.*;
public class DateFormatDemo1
{
    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

Program to represent the current system date in all possible styles of US format

Write a Program to represent the current system date in the UK, US, and ITALY styles.
import java.text.*;
import java.util.*;
public class DateFormatDemo2
{
    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

Program to represent the current system date in the UK, US, and ITALY styles

Getting DateFormat object to get both DATE and TIME

DateFormat class defines the following methods:
public static DateFormat getDateTimeInstance();
public static DateFormat getDateTimeInstance(int dateStyle, int timeStyle);
public static DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale l);

Sample Program
import java.text.*;
import java.util.*;
public class DateFormatDemo3
{
    public static void main (String[]args)
    {
        DateFormat df = DateFormat.getDateTimeInstance (DateFormat.FULL, DateFormat.FULL);
        System.out.println (df.format (new Date ()));
    }
}

Output: Wednesday, August 26, 2020 2:31:15 AM UTC

In the next article, I am going to discuss the Regular Expression in Java with Examples. Here, in this article, I try to explain Internationalization in Java with Examples. I hope you enjoy this Internationalization in Java with Examples article.

Leave a Reply

Your email address will not be published. Required fields are marked *