Internationalization

What and Why ?

Preparing Java applications as per the users locality or as per the users local conventions is called Internationalization, in short I18N.

To provide Services as per the users local conventions , first we have to divide all the users into the number of groups , for this we have to use the following parameters.

  1. Language: To represent users’ Languages we will use two lowercase letters.

EX: en, it, hi,....

  1. Country: To represent Users Country we will use two uppercase letters.

EX: US, IT, IN,.....

  1. System Variant[OS]: To represent System variant we will use three lower case letters.

EX: win, lin, uni,…. 

Note: In general, to divide all the users into the groups we will use language and country parameters only, we will not use System Variant.

To represent a group of users on the basis of the above parameters we have a predefined class in the form of java.util.Locale.

EX:
Locale l1 = new Locale(“en”);
Locale l2 = new Locale(“en”,”US”);
Locale l3 = new Locale(“en”,”US”,”win”);


I18N Services

After representing all the users in the form of groups we have to provide the following services as part of the internationalization.

  1. Number Formations
  2. Date Formations
  3. Message Formations

Number Formations

From Language to language , country to country number representations are varied, to represent a particular number w.r.t a particular Locale we have to use a predefined class in the form of java.text.NumberFormat.

To create a NumberFormat object we have to use the following static factory method.

public static NumberFormat getInstance(Locale l)

EX: 
Locale l = new Locale(”en”, “US”);
NumberFormat nf = NumberFormat.getInstance(l);

To format a number we have to use the following method from the NumberFormat class.

public xxx format(xxx value)

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


public class Main {
public static void main(String[] args) {
Locale locale = new Locale("en","US");
NumberFormat numberFormat = NumberFormat.getInstance(locale);
System.out.println(numberFormat.format(123456789.34567));


Locale locale1 = new Locale("it","IT");
NumberFormat numberFormat1 = NumberFormat.getInstance(locale1);
System.out.println(numberFormat1.format(123456789.34567));
}
}

123,456,789.346
123.456.789,346

Date Formations

From Language to language , country to country date representations are varied, to represent a particular date w.r.t a particular Locale we have to use a predefined class in the form of java.text.DateFormat.

To create a DateFormat object we have to use the following static factory method.

public static DateFormat getDateInstance(int style, Locale l)
where Date Styles vary from 0 to 3.

To format a particular date w.r.t a particular locale we have to use the following method from the DateFormat class.

public String format(Date date)

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


public class Main {
public static void main(String[] args) {
Locale locale = new Locale("en","US");
DateFormat dateFormat = DateFormat.getDateInstance(3, locale);
System.out.println(dateFormat.format(new Date()));


Locale locale1 = new Locale("it","IT");
DateFormat dateFormat1 = DateFormat.getDateInstance(3, locale1);
System.out.println(dateFormat1.format(new Date()));


}
}

Date Style: 0
Sunday, November 19, 2023
domenica 19 novembre 2023

Date Style : 1
November 19, 2023
19 novembre 2023

Date Style: 2
Nov 19, 2023
19 nov 2023
Date Style: 3
11/19/23
19/11/23

Message Formations

From Language to language , country to country message representations are varied, to represent a particular message w.r.t a particular Locale we have to use a predefined class in the form of java.util.ResourceBundle.

ResourceBundle is an object that is able to represent the data of a particular properties file.

The format of the properties file : baseName_lang_country.properties

EX:
abc_en_US.properties
welcome = Welcome To en US Users.

abc_it_IT.properties
welcome = Welcomeo Teo it IT Userseo.

abc_hi_IN.properties
welcome = Aap ka Swagth hai.

Note: in the properties file we have to provide the messages in the form of key-value pairs, where keys must be in English and values must be in the local language.

To represent a particular properties file data we have to create a ResourceBundle object by using the following static factory method.

ResourceBundle rb = ResourceBundle.getBundle(“abc”,locale);

To get the value from the ResourceBundle we have to use the following method.

public String getString(String key)

abc_en_US.properties
welcome = Welcome To en US USers.

abc_it_IT.properties
welcome = Welcomeo Teo it IT Userseo.

abc_hi_IN.properties
welcome = Aap ka Swagat hai.

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


public class Main {
public static void main(String[] args) {
Locale locale1 = new Locale("en","US");
ResourceBundle resourceBundle1 =
ResourceBundle.getBundle("abc", locale1);
System.out.println(resourceBundle1.getString("welcome"));

Locale locale2 = new Locale("it","IT");
ResourceBundle resourceBundle2 =
ResourceBundle.getBundle("abc", locale2);
System.out.println(resourceBundle2.getString("welcome"));

Locale locale3 = new Locale("hi","IN");
ResourceBundle resourceBundle3 =
ResourceBundle.getBundle("abc", locale3);
System.out.println(resourceBundle3.getString("welcome"));
}
}

Welcome To en US USers.
Welcomeo Teo it IT Userseo.
Aap ka Swagat hai.