Home >Java >javaTutorial >Internationalization in Java
The following article provides an outline for Internationalization in Java. Internationalization is the process of creating web applications in such a way that they automatically support several countries, languages, and currencies without requiring any changes to the application. It is also called I18N because there are 18 characters between the letters I and N. The worldwide marketplace is an important factor when designing software or websites today. Companies must create products that engage with users in their local regions and languages as the expansion of software applications for global markets continues.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Developers working on software for international markets should be aware of the customs and differences in each culture. Language, punctuation, currency, dates, times, numerals, and time zones are all examples of differences. Localization is also shortened as I10N because there are ten characters between the initial letter ‘L’ and the last letter ‘N,’. Localization is the process of adding locale-specific text and components to an application so that it may be tailored to a given language and place.
The following classes can be used to implement internationalization:
A Locale object can be used to represent a geographical location or a language. The java.util package includes a Locale class.
Constructors of the Locale Class:
Locale l = new Locale(String language);
Locale l = new Locale(String language, String country);
Constants of the Locale Class:
Some Locale constants are already declared in the Locale class.
These constants can be used directly, few constants are shown below:
Functions of the Locale Class:
We can format a number according to a specific locale by using the NumberFormat class. The NumberFormat class present in java.Text package and is an abstract class because of which we can not create an object by using its constructor.
Functions of the Locale Class:
We internationalize the date format because the format of dates varies from one location to the next. We can use the DateFromat class to format the date in accordance with a given Locale. DateFormat is an abstract class in java.text package.
Constants of the Locale Class:
Some DateFormat constants are already declared in the DateFormat class.
These constants can be used directly, few constants are shown below:
Functions of the DateFormat Class:
Given below are the examples mentioned:
Example for the internationalization in Java to create different country locale.
Code:
// The program can be tested in Eclipse IDE, JAVA 11 package jex; import java.util.Locale; public class ex { public static void main(String[] args) { Locale[] locales = { new Locale("en", "US"), new Locale("it", "IT"), new Locale("es", "ES") }; for (int l=0; l< locales.length; l++) { String Language = locales[l].getDisplayLanguage(locales[l]); System.out.println(locales[l].toString() + ": " + Language); } } }
Output:
As in the above program, the Locale class objects are created and store in the array. Next, used the for loop to iterate each locale object and display its name and its language, as we can see in the above output.
Example for the internationalization in Java to show the number in different formats for the different countries.
Code:
// The program can be tested in Eclipse IDE, JAVA 11 package jex; import java.util.*; import java.text.*; public class ex { public static void main (String[]args) { double n = 45273.8956; NumberFormat f1 = NumberFormat.getInstance (Locale.US); NumberFormat f2 = NumberFormat.getInstance (Locale.ITALY); NumberFormat f3 = NumberFormat.getInstance (Locale.CHINA); System.out.println ("The number format in US is :" + f1.format (n)); System.out.println ("The number format in ITALY is:" + f2.format (n)); System.out.println ("The number format in CHINA is :" + f3.format (n)); } }
Output:
As in the above program, three different NumberFormat class objects are created using the Locale class. Next, using the format() method of the NumberFormat class, the given number is printing in the specific format of the country, as we can see in the above output.
Example for the internationalization in Java to show the date in different formats for the different countries.
Code:
// The program can be tested in Eclipse IDE, JAVA 11 package jex; import java.text.DateFormat; import java.util.Date; import java.util.Locale; public class ex { public static void main (String[]args) { DateFormat d1 = DateFormat.getDateInstance (0, Locale.US); DateFormat d2 = DateFormat.getDateInstance (0, Locale.ITALY); DateFormat d3 = DateFormat.getDateInstance (0, Locale.CHINA); System.out.println ("The date format in US is :" + d1.format (new Date ())); System.out.println ("The date format in ITALY is : " + d2.format (new Date ())); System.out.println ("The date format in CHINA is : " + d3.format (new Date ())); } }
Output:
As in the above program, three different DateFormat class objects are created using the Locale class. Next, using the format() method of the DateFormat class, the return date of the Date() method is printing in the specific format of the country, as we can see in the above output.
Internationalization is also called I18N because there are 18 characters between the letters I and N. It is the process of creating web applications in such a way that they automatically support several countries, languages, and currencies without requiring any changes to the application.
The above is the detailed content of Internationalization in Java. For more information, please follow other related articles on the PHP Chinese website!