Home  >  Article  >  Java  >  Internationalization in Java

Internationalization in Java

WBOY
WBOYOriginal
2024-08-30 15:41:371032browse

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.

Syntax of Internationalization in Java

The following classes can be used to implement internationalization:

  • Locale
  • NumberFormat
  • DateFormat

1. Locale

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:

  • Locale. UK
  • Locale.ITALY
  • Locale.US

Functions of the Locale Class:

  • pubic static Locale getDefault(): This method is used to gets the instance of the current locale.
  • public static Locale[] getAvailableLocales(): This method is used to gets an array of present locales.
  • public String getDisplayLanguage(Locale l): This method is used to gets the language name of the passed locale object.
  • public String getDisplayVariant(Locale l): This method is used to gets the variant code for the passed locale object.
  • public String getDisplayCountry(Locale l): This method is used to gets the country name of the passed locale object.
  • public static getISO3Language(): This method is used to gets the three-letter abbreviation of the current language code of the locale.
  • public static getISO3Country(): This method is used to gets the three-letter abbreviation of the current country of the locale.

2. NumberFormat

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:

  • public static NumberFormat getInstance(): This method is used to gets the object of default Locale.
  • public static NumberFormat getInstance(Locale l): This method is used to gets its object for the passed Locale object.
  • public static NumberFormat getCurrencyInstance(): This method is used to gets the object of default Locale to show in specific currency.
  • public static format(long l):This method is used to convert passed number to locale object.

3. DateFormat

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:

  • DateFormat.LONG
  • DateFormat.MINUTE_FIELD
  • DateFormat.MINUTE_FIELD

Functions of the DateFormat Class:

  • final String format(Date date): This method converts and returns the specified Date object into a string.
  • static final DateFormat getInstance(): This method is used to gets a date-time formatter with a short formatting style for date and time.
  • static Locale[] getAvailableLocales(): This method is used to gets an array of present locales.
  • static final DateFormat getTimeInstance(): This method is used to gets time formatter with default formatting style for the default locale.
  • static final DateFormat getTimeInstance(int style): This method is used to gets time formatter with the specified formatting style for the default locale.
  • static final DateFormat getTimeInstance(int style, Locale locale): This method is used to gets time formatter with the specified formatting style for the given locale.
  • TimeZone getTimeZone(): This method is used to gets an object of TimeZone for this DateFormat instance.
  • static final DateFormat getDateInstance(): This method is used to gets date formatter with default formatting style for the default locale.
  • static final DateFormat getDateInstance(int style): This method is used to gets date formatter with the specified formatting style for the default locale.
  • static final DateFormat getDateInstance(int style, Locale locale): This method is used to gets date formatter with the specified formatting style for the given locale.
  • static final DateFormat getDateTimeInstance(): This method is used to gets date or time formatter with default formatting style for the default locale.
  • NumberFormat getNumberFormat(): This method is used to gets an object of NumberFormat for this DateFormat instance.
  • Calendar getCalendar(): This method is used to gets an object of the Calendar for this DateFormat instance.

Examples of Internationalization in Java

Given below are the examples mentioned:

Example #1

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:

Internationalization in Java

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 #2

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:

Internationalization in Java

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 #3

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:

Internationalization in Java

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.

Conclusion

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Native Methods in JavaNext article:Native Methods in Java