Class Locale in Java

digitization, transformation, binary

Class Locale in Java

The Locale class represents a locale: a political, geographical, or cultural region that typically has a distinct language and distinct customs and conventions for such things as formatting dates, times, and numbers.

The Locale class is used for internationalization. Instances of Locale specify language and formatting customs by identifying a language and a country. A Locale object may also specify a platform-specific variant.

Ads code goes here

Other classes throughout the JDK use Locale objects to determine how to represent themselves to the user. The tasks performed by these classes are called locale-sensitive tasks; the tasks should be done in a way that conforms with the conventions of a particular country and language.

There are a number of classes provided with Java that have static methods that create instances of locale-specific subclasses. For example, the NumberFormat class contains static methods named getInstance() that create and return locale-specific instances of subclasses of NumberFormat. A particular NumberFormat instance knows how to format numbers, currency values, and percentages appropriately for a particular locale. Note that it is the responsibility of a class like NumberFormat to implement the logic needed to translate locale-identifying information into actual subclass instances.

Classes like NumberFormat that can create locale-specific instances are expected to follow certain conventions:

  • Methods like getInstance() in NumberFormat are expected to have two variants: one that takes a Locale argument and one that does not. The variant that does not take a locale argument is expected to use the default locale, which is normally determined by calling Locale.getDefault().
  • Classes that can create a variety of locale-specific instances are expected to implement a method that has the following signature:
public static Locale[] getAvailableLocales()

This requirement is not specified through an interface declaration because interfaces cannot declare static methods. The purpose of this method is to facilitate presenting the user with a list or menu of locale choices. The getAvailableLocales() method should return an array of Locale objects that identifies all of the locales for which the class can create locale-specific instances.

Two additional methods are recommended for helping to display the locale choices:

public static final String getDisplayName(Locale objectLocale);
public static String getDisplayName(Locale objectLocale,Locale displayLocale);

The first form of getDisplayName() should return a description of objectLocale that is suitable for display in the default locale. The second form should return a description of objectLocale that is suitable for display in the locale specified by displayLocale. Implementations of these methods generally call the getDisplayName() method of the Locale object.

The language, country and variant information that is encapsulated by a Locale object are specified to a constructor as strings. The language for a Locale should be specified as one of the two-letter lowercase language codes defined by ISO-639.

The country for a Locale object should be specified as either “” to indicate that no country is specified, or as one of the two-letter uppercase country codes defined by ISO-3166.

Variant codes are platform-specific.
Although the Locale is constructed from these three types of codes, human-readable names can be obtained by
calling getDisplayLanguage(), getDisplayCountry(), and getDisplayVariant().

The Locale class defines a number of constant Locale objects that represent some of the major languages and countries of the world.

The Locale class defines a number of constants that represent commonly used locales. The locale also defines a static getDefault() method that returns the default Locale object, which represents a locale value inherited from the host system. Furthermore, many locale-sensitive classes, such as DateFormat, provide getAvailableLocales() methods that return a list of supported Locale objects. If none of these methods for obtaining a Locale object is suitable, we can also explicitly create our own Locale object.

READ  Hire Java Builders With Working experience in Top 10 Java Frameworks

To do this, we must specify a language code, a country code, and an optional variant string. The Locale class does not implement any internationalization behavior itself; it merely serves as a locale identifier for those classes that can localize their behavior. Given a Locale object, we can invoke the various getDisplay methods to obtain a description of the locale suitable for display to a user. Note that these methods may themselves take a Locale argument so that the names of languages and countries can be localized as appropriate.

The structure of the class Locale is given by

public final class java.util.Locale extends java.lang.Object implements java.lang.Cloneable, java.io.Serializable {
// Public Constructors
public Locale(String language, String country, String variant);
public Locale(String language, String country);
// Constants
public static final Locale CANADA;
public static final Locale CANADA_FRENCH;
public static final Locale CHINA;
public static final Locale CHINESE;
public static final Locale ENGLISH;
public static final Locale FRANCE;
public static final Locale FRENCH;
public static final Locale GERMAN;
public static final Locale GERMANY;
public static final Locale ITALIAN;
public static final Locale ITALY;
public static final Locale JAPAN;
public static final Locale JAPANESE;
public static final Locale KOREA;
public static final Locale KOREAN;
public static final Locale PRC;
public static final Locale SIMPLIFIED_CHINESE;
public static final Locale TAIWAN;
public static final Locale TRADITIONAL_CHINESE;
public static final Locale UK;
public static final Locale US;
// Class Methods
public static synchronized Locale getDefault();
public static synchronized void setDefault(Locale newLocale);
// Public Instance Methods
public Object clone(); // Overrides Object
public boolean equals(Object obj); // Overrides Object
public String getCountry();
public final String getDisplayCountry();
public String getDisplayCountry(Locale inLocale);
public final String getDisplayLanguage();
public String getDisplayLanguage(Locale inLocale);
public final String getDisplayName();
public String getDisplayName(Locale inLocale);
public final String getDisplayVariant();
public String getDisplayVariant(Locale inLocale);
public String getISO3Country() throws MissingResourceException;
public String getISO3Language() throws MissingResourceException;
public String getLanguage();
public String getVariant();
public synchronized int hashCode(); // Overrides Object
public final String toString(); // Overrides Object
}

The details of the class structure are given as follows:

public final static Locale CANADA;

public final static Locale CANADA represents a locale that represents English-speaking Canada.

public final static Locale CANADA_FRENCH;

public final static Locale CANADA_FRENCH represents a locale that represents French-speaking Canada.

public final static Locale CHINA;

public final static Locale CHINA represents a locale that represents China.

public final static Locale CHINESE;

public final static Locale CHINESE represents a locale that represents the Chinese language.

public final static Locale ENGLISH;

public final static Locale ENGLISH represents a locale that represents the English language.

public final static Locale FRANCE;

public final static Locale FRANCE represents a locale that represents France.

public final static Locale FRENCH;

public final static Locale FRENCH represents a locale that represents the French language.

public final static Locale GERMAN;

public final static Locale GERMAN represents a locale that represents the German language.

public final static Locale GERMANY;

public final static Locale GERMANY represents a locale that represents Germany.

public final static Locale ITALIAN;

public final static Locale ITALIAN represents a locale that represents the Italian language.

public final static Locale ITALY;

public final static Locale ITALY represents a locale that represents Italy.

public final static Locale JAPAN;

public final static Locale JAPAN represents a locale that represents Japan.

public final static Locale JAPANESE;

public final static Locale JAPANESE represents a locale that represents the Japanese language.

public final static Locale KOREA;

public final static Locale KOREA represents a locale that represents Korea.

public final static Locale KOREAN;

public final static Locale KOREAN represents a locale that represents the Korean language.

public final static Locale PRC;

public final static Locale PRC represents a locale that represents the People’s Republic of China. It is equivalent to CHINA.

public final static Locale SIMPLIFIED_CHINESE;

public final static Locale SIMPLIFIED_CHINESE represents a locale that represents the Chinese language as used in mainland China.

public final static Locale TAIWAN;

public final static Locale TAIWAN represents a locale that represents Taiwan.

public final static Locale TRADITIONAL_CHINESE;

public final static Locale TRADITIONAL_CHINESE represents a locale that represents the Chinese language as used in Taiwan.

public final static Locale UK;

public final static Locale UK represents a locale that represents the United Kingdom.

public final static Locale US;

public final static Locale US represents a locale that represents the United States.

public Locale(String language, String country);

public Locale(String language, String country) constructor creates a Locale that represents the given language and country.

READ  Abstract Class ResourceBundle in Java

Parameter
language – A two-letter ISO-639 language code.
country – A two-letter ISO-3166 country code or “” to omit the country specification.

public Locale(String language, String country, String variant);

public Locale(String language, String country, String variant) constructor creates a Locale that represents the given language, country, and variant.

Parameter
language – A two-letter ISO-639 language code.
country – A two-letter ISO-3166 country code or “” to omit the country specification.
variant – A vendor-specific variant code.

public static synchronized Locale getDefault();

public static synchronized Locale getDefault() method returns the current default Locale. An application or applet uses this method to find out how to present locale-sensitive information, such as textual strings and numbers. The method is generally called during application initialization to get the default Locale. Once the locale is set, it almost never changes.

If we do change the locale, we should probably reload the GUI for your application, so that any locale-sensitive information in the interface is changed. The initial default Locale is set by the host system.

This method returns the default Locale.

public static synchronized void setDefault(Locale newLocale);

public static synchronized void setDefault(Locale newLocale) method changes the current default locale to newLocale. Note that calling setDefault() does not change the default locale of the host system.

Parameter
newLocale – The new default locale.

public Object clone();

public Object clone() method creates a copy of this Locale and returns it.

This method returns a copy of this Locale.

public boolean equals(Object obj);

public boolean equals(Object obj) method returns true if obj is an instance of Locale, and it contains the same value as the object this method is associated with.

This method returns true if the objects are equal; false if they are not.

Parameter
obj – The object to be compared with this object.

public String getCountry();

public String getCountry() method returns a String that represents the country of this Locale. This String is the same String that was passed to the constructor of this Locale object. The String is normally a two-letter ISO-3166 country code.

This method returns the country of this Locale.

public final String getDisplayCountry();

public final String getDisplayCountry() method returns the country of this Locale as a country name in a form appropriate for this Locale. If the country name cannot be found, this method returns the same value as getCountry().

This method returns the country of this Locale.

public String getDisplayCountry(Locale inLocale);

public String getDisplayCountry(Locale inLocale) method returns the country of this Locale as a country name in a form appropriate for inLocale. For example, Locale.ITALY.getDisplayCountry(Locale.GERMAN) returns the German name
for Italy, Italien.

This method returns the country of this Locale, localized to the given locale.

Parameter
inLocale – The locale to use when finding the country name.

public final String getDisplayLanguage();

public final String getDisplayLanguage() method returns the language of this Locale as a language name in a form appropriate for this Locale. If the language name cannot be found, this method returns the same value as getLanguage().
This method returns the language of this Locale.

public String getDisplayLanguage(Locale inLocale);

public String getDisplayLanguage(Locale inLocale) methodreturns the language of this Locale as a language name in a form appropriate for inLocale. For example, Locale.ITALY.getDisplayLanguage(Locale.GERMAN) returns the German name for the Italian language, Italienisch.

READ  Class SecurityManager in Java

This method returns the language of this Locale, localized to the given locale.
Parameter
inLocale – The locale to use when finding the language name.

public final String getDisplayName();

public final String getDisplayName() method constructs a string that represents this Locale by calling getDisplayLanguage(), getDisplayCountry(), and getDisplayVariant(). In other words, the method returns a string that contains the country name, language name, and variant in a form appropriate for this Locale. If any of the names cannot be found, the String that was passed to the constructor of this Locale object is used instead. These strings are normally two-letter ISO codes.

This method returns a string that represents this Locale.

Parameter

public String getDisplayName(Locale inLocale);

public String getDisplayName(Locale inLocale) method constructs a string that represents this Locale by calling
getDisplayLanguage(inLocale), getDisplayCountry(inLocale), and getDisplayVariant(inLocale). In other words, the method returns a string that contains the country name, language name, and variant in a form appropriate for inLocale. If any of the names cannot be found, the String that was passed to the constructor of this Locale object is used instead. These strings are normally two-letter ISO codes.

This method returns a string that represents this Locale.

Parameter
inLocale – The locale to use when constructing the string representation.

public final String getDisplayVariant();

public final String getDisplayVariant() method returns the variant of this Locale as a human-readable string in a form appropriate for this Locale. If the variant name cannot be found, this method returns the same value as getVariant().

This method returns the variant of this Locale.

public String getDisplayVariant(Locale inLocale);

public String getDisplayVariant(Locale inLocale) method returns the variant of this Locale as a human-readable string in a form appropriate for inLocale.

This method returns the variant of this Locale, localized to the given locale.

Parameter
inLocale – The locale to use when finding the variant name.

public String getISO3Country();

public String getISO3Country() method returns the country of this Locale as a three-letter ISO country code. The country code is obtained from a ResourceBundle for this Locale.

This method returns the ISO three-letter country code of this Locale.

public String getISO3Language();

public String getISO3Language() method returns the language of this Locale as a three-letter ISO language code. The language code is obtained from a ResourceBundle for this Locale.

This method returns the ISO three-letter language code of this Locale.

public String getLanguage();

public String getLanguage() method returns a String that represents the language of this Locale. This String is the same String that was passed to the constructor of this Locale object. The String is normally a two-letter ISO-639 language code.

This method returns the language of this Locale.

public String getVariant();

public String getVariant() method returns the variant code of this Locale. If no variant code is specified for this Locale, an empty string is returned.

This method returns the variant of this Locale.

public synchronized int hashCode();

public synchronized int hashCode() method returns a hashcode for this object.

This method returns a hashcode for this Locale.

public final String toString();

public final String toString() method returns a string representation of this Locale, constructed from the language code, country code, and variant code. The various codes are separated by underscore characters. If a code is missing, it is omitted.

This method returns a string representation of this Locale.

Apart from these Locale class also has inherited methods from class- Object. They are as follows:

  • finalize()
  • notifyAll()
  • wait()
  • wait(long, int)
  • getClass()
  • notify()
  • wait(long)

Share and Enjoy !

Leave a Comment

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