Javatpoint Logo
Javatpoint Logo

Java Localization

Internationalization is the process of developing a software application so that it can be altered to various languages and regions without modifying the application. Developing an application locale specific increases, the cost of the application, also require lots of maintenance.

Localization is the process of adapting internationalized software for a specific region or country by translating text and adding locale-specific components.

So, it is good to develop an application using the concept of internationalization. It saves time and reduces development cost and require less efforts to maintain it.

Before understanding the term Localization, it is important to understand a concept that is often read along with it, "Internationalization".

Internationalization

Internationalization is the process of developing an application that supports various languages, number formats, currency, date, time and other formats. It is an integral part of any modern multilingual system. There is a very popular abbreviation (perhaps more popular than the real name) for international production - "I18N" because of the 18 characters between 'i' and 'n'.

Modern business systems need to serve people from different regions/ countries of the world or many cultural areas. Different cultural or linguistic regions determine not only the specific meanings of a language but also determine the type of currency, numerical representation, and even variation of date and time.

For example, have a look at the decimal and thousand separators conventions for different countries:

  1. 504,200.12 (United States)
  2. 504 200,12 (Poland)
  3. 200,12 (Germany)

Similarly, the date and time formats are also different. For example:

  1. Thursday, March 3, 2022, 16:30:14 PM CET (United States)
  2. lundi 1 mardi 2022 16 h 30 CET (France)
  3. 2022年1月1日 星期一 下午16时30分14秒 CET (China)

What's more, we know that there are unique currency symbols for different countries:

  1. £5,4000.80 (United Kingdom)
  2. €400,80 (Italy)
  3. 7 400,80 € (France)
  4. $7,400.80 (United States)

Please note that even if some countries have same currency and currency symbol like that of France and Italy, the position of their currency symbol could be different.

Localization

Within Java, we have a fantastic feature at our disposal called the Locale class. It allows us to quickly differentiate between cultural locales and format our content appropriately. Similar to I18N, Localization also has its acronym - "I10N" because of the 10 letters between 'I' and 'N'. The main reason for using Locale is that all required local-specific formatting can be accessed without recompilation. The application can handle multiple locales at once, so support for a new language is straightforward.

Localization can also be defined as the process of adapting a product's content to a specific place or language. Translating words is a well-known part of this process, but it is not the only part. Locale practices often include:

  • Numbers
  • Dates
  • Currencies
  • Images and sounds
  • Layouts

Even in countries with the same languages, you can find differences, for example, American English vs. British English.

Benefits of Localization

  1. With the addition of localized data, the same executable can run worldwide.
  2. Text elements, such as status messages and GUI component labels, have no solid codes in the system. Instead, they are stored without source code and retrieved dynamically.
  3. Support for new languages does not need to be re-integrated.
  4. Custom-based data, such as dates and currency types, appears in the formats that correspond to the region and the language of the end user.

Let's understand how to implement localization in programming.

Locale.Builder

Another option is Locale.Builder. Locale.Builder has five set methods that we can use to build an object and at the same time validate those values:

The String representation of the above Locale is fr_CA_POSIX_#Latn.

It is good to know that the 'variant' set may be a bit tricky as there are no legal limits to different values, although the set method requires compliance with BCP-47. If not, it will throw IllformedLocaleException.

In a situation where we need to use a value that does not exceed the guarantee, we may use Locale constructors as they do not validate values.

Importance of Locale Class

An object of Locale class represents a geographical or cultural region. This object can be used to get the locale specific information such as country name, language, variant etc.

Fields of Locale Class

There are the following fields of Java Locale class:

  • public static final Locale ENGLISH
  • public static final Locale FRENCH
  • public static final Locale GERMAN
  • public static final Locale ITALIAN
  • public static final Locale JAPANESE
  • public static final Locale KOREAN
  • public static final Locale CHINESE
  • public static final Locale SIMPLIFIED_CHINESE
  • public static final Locale TRADITIONAL_CHINESE
  • public static final Locale FRANCE
  • public static final Locale GERMANY
  • public static final Locale ITALY
  • public static final Locale JAPAN
  • public static final Locale KOREA
  • public static final Locale CHINA
  • public static final Locale PRC
  • public static final Locale TAIWAN
  • public static final Locale UK
  • public static final Locale US
  • public static final Locale CANADA
  • public static final Locale CANADA_FRENCH
  • public static final Locale ROOT

Constructors of the Java Locale Class

There are the following three constructors of the Locale class, as follows:

  1. Locale( String language )
  2. Locale( String language, String country )
  3. Locale( String language, String country, String variant )

Methods of the Java Locale Class

There are the following methods used to localize a Java application.

A. getDefault()

Detects the current default location for this Java Virtual Machine event.

Java Virtual Machine sets the default location during implementation based on the host location. It uses many locale-sensitive methods in an area when there is no explicit location. It can be changed using the setDefault( ) method.

Returns:

The default locale for this instance of the Java Virtual Machine.

B. setDefault()

Sets the default location for this Java Virtual Machine event. This does not affect the location of the host.

If there is a security manager, its checkPermission method is called, PropertyPermission (" user.language ", " write ") before the default location is changed.

Java Virtual Machine sets the default location during implementation based on the host location. It uses many sensitive methods in an area when there is no explicit location.

Since changing location can affect many different operating environments, this method should only be used if the caller is ready to reset the locale-sensitive code running within the same Java Virtual machine.

By setting the default location in this way, all the default regions for each Phase are also set to the specified location.

Parameters:

newLocale: the new default locale

Throws:

SecurityException: if a security manager exists and its checkPermission method doesn't allow the operation.

NullPointerException: if newLocale is null

C. getISOCountries()

Returns a list of all two-letter country codes defined in ISO 3166. Can be used to create locales.

Note: The Locale class also supports other country/region codes, such as the UN M.49 3-digit city code. Therefore, the list returned by this method does not contain all valid code that can be used to create a locale.

Returns:

An array of ISO 3166 two-letter country codes.

D. getISOLanguages()

Synatx:

It returns a list of all two-letter language codes defined in ISO 639. It can be used to create locales.

Note: ISO 639 is not a stable standard. Some language codes have been changed. The list returned by this function contains both new and old code for languages where the code has changed.

The Locale class also supports language codes up to 8 characters long. Therefore, the list returned by this method does not contain all valid code that can be used to create a locale.

Returns:

An array of ISO 639 two-letter language codes.

E. getScript()

Syntax:

It returns the script for this locale, which must be an empty string or a 4-digit ISO 15924 script code. The first letter is uppercase and the rest are lowercase ( eg "Latin", "Kyrl" ).

Returns:

The script code, or the empty string if none is defined.

F. getCountry()

Synatx:

Returns the country/region code for this locale, which should either be the empty string, an uppercase ISO 3166 2-letter code, or a UN M.49 3-digit code.

Returns:

The country/region code, or the empty string if none is defined.

G. toString()

Syntax:

It returns the string representation of this locale object consisting of language, country, variant, script, and extension as shown below.

Overrides:

toString() method of the Object class.

Returns:

A string representation of the Locale, for debugging.

Note that the language is always lower case, country is always upper case, script is always title case, and extensions are always lower case. When the locale has neither script nor extensions, the result is the same as in Java 6 and prior.

The function will return the empty string if both the language and country fields are missing, even if the variant, script, or extensions field is present (you can't have a locale with just a variant, the variant must accompany a well-formed language or country code). If script or extensions are present and variant is missing, no underscore is added before the "#".

The behaviour supports debugging and compatibility with older uses for toString where only language, country, and variant fields were expected. Use toLanguageTag() to represent a Locale as a string for exchange purposes.

Examples:

  • en
  • de_DE
  • _GB
  • en_US_WIN
  • de__POSIX
  • zh_CN_#Hans
  • zh_TW_#Hant-x-java
  • th_TH_TH_#u-nu-thai

H. getISO3Language()

It returns the three-letter language abbreviation for the given locale. If a language matches a two-letter ISO 6391 code, the corresponding three-letter ISO 6392/T lowercase code is returned. ISO 6392 language codes can be found on the Internet. If the locale specifies a three-letter language, that language is returned as it is. If no language is specified for the locale, an empty string is returned.

Returns:

A three-letter abbreviation of this locale's language.

Throws:

MissingResourceException: Throws MissingResourceException if three-letter language abbreviation is not available for this locale.

I. getISO3Country()

Returns the three-letter abbreviation for the country of this locale. If a country matches an ISO 31661 alpha2 code, the corresponding ISO 31661 alpha3 code in uppercase is returned. If you do not specify a country for the locale, it will be an empty string. The ISO 3166-1 codes can be found on-line.

Returns:

A three-letter abbreviation of this locale's country.

J. getDisplayLanguage()

Returns the name of the local language that should be displayed to the user. If possible, the restored name is made locally in the default DISPLAY location. For example, if the region says fr_FR and the DISPLAY default location says en_US, getDisplayLanguage( ) returns "French". If the location is en_US and DISPLAY default location says fr_FR, getDisplayLanguage( ) returns "inglais". If the restored name cannot be localized in the DISPLAY default area (assuming there is no Japanese Croatian name), this function uses the English name and uses the ISO code as a last resort. If the location does not specify a language, this function returns an empty character unit.

Returns:

The name of the display language.

K. getDisplayScript()

Returns a locale script name suitable for display to the user. If possible, the name is localized to the default DISPLAY locale. If this locale does not specify a script code, it returns an empty string.

Returns:

The display name of the script code for the current default DISPLAY locale

L. getDisplayCountry()

Returns the country name of a region suitable for display to the user. If possible, the returned name is localized to the default DISPLAY locale. For example, if the locale is fr_FR and the default DISPLAY locale is en_US, then getDisplayCountry() returns "France". If the locale is en_US and the default DISPLAY locale is fr_FR, then getDisplayCountry() returns "EtatsUnis". If the returned name cannot be localized to the default DISPLAY locale (assuming there is no Japanese name for Croatia), this function uses the English name and uses the ISO code as a last resort. If no country is specified for the locale, this function returns an empty string.

Returns:

The name of the country appropriate to the locale.

M. getDisplayVariant()

Returns the name of the locale variant code suitable for display to the user. If possible, the name is localized to the default DISPLAY locale. If the locale does not specify a variant code, this function returns an empty string.

Returns:

The name of the display variant code appropriate to the locale.

N. getDisplayName()

Returns the name of a locale suitable for display to the user. This will be the value returned by getDisplayLanguage(), getDisplayScript(), getDisplayCountry(), and getDisplayVariant() combined into a single string. Non-empty values are used in sequence with the second and subsequent names in parentheses. Example:

  • Language (Script, Country, Variant)
  • Language (Country)
  • Language (Variant)
  • Script (Country)

Country depends on the field specified in the locale. If the Language, Script, Country, and Variant fields are empty, this function returns an empty string.

Returns:

The name of the locale appropriate to display.

Let's implement the localization in an application.

LocaleExample.java

Output:

United States
English
English (United States)
USA
eng
en
US






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA