Java Locale Class



Introduction

The Java Locale class object represents a specific geographical, political, or cultural region. .Following are the important points about Locale −

  • An operation that requires a Locale to perform its task is called locale-sensitive and uses the Locale to form information for the user.

  • Locale is a mechanism for identifying objects, not a container for the objects themselves.

Class declaration

Following is the declaration for java.util.Locale class −

public final class Locale
   extends Object
   implements Cloneable, Serializable

Nested Classes

Following are the nested classes for java.util.Locale class −

  • static class Locale.Builder − Builder is used to build instances of Locale from values configured by the setters.

  • static class Locale.Category − Enum for locale categories.

    static class Locale.FilteringMode − This enum provides constants to select a filtering mode for locale matching.

    static class Locale.IsoCountryCode − Enum for specifying the type defined in ISO 3166.

    static class Locale.LanguageRange − This class expresses a Language Range defined in RFC 4647 Matching of Language Tags.

Field

Following are the fields for java.util.Locale class −

  • static Locale CANADA − This is the constant for country.

  • static Locale CANADA FRENCH − This is the constant for country.

  • static Locale CHINA − This is the constant for country.

  • static Locale CHINESE − This is the constant for language.

  • static Locale ENGLISH − This is the constant for language.

  • static Locale FRANCE − This is the constant for country.

  • static Locale FRENCH − This is the constant for language.

  • static Locale GERMAN − This is the constant for language.

  • static Locale GERMANY − This is the constant for country.

  • static Locale ITALIAN − This is the constant for language.

  • static Locale ITALY − This is the constant for country.

  • static Locale JAPAN − This is the constant for country.

  • static Locale JAPANESE − This is the constant for language.

  • static Locale KOREA − This is the constant for country.

  • static Locale KOREAN − This is the constant for language.

  • static Locale PRC − This is the constant for country.

  • static char PRIVATE_USE_EXTENSION − The key for the private use extension ('x').

  • static Locale ROOT − This is the constant for root locale.

  • static Locale SIMPLIFIED CHINESE − This is the constant for language.

  • static Locale TAIWAN − This is the constant for country.

  • static Locale TRADITIONAL CHINESE − This is the constant for language.

  • static Locale UK − This is the constant for country.

  • static char UNICODE_LOCALE_EXTENSION − The key for Unicode locale extension ('u').

  • static Locale US − This is the constant for country.

Class constructors

Sr.No. Constructor & Description
1

Locale(String language)

This constructs a locale from a language code.

2

Locale(String language, String country)

This constructs a locale from a language code.

3

Locale(String language, String country, String variant)

This constructs a locale from language, country, variant.

Class methods

Sr.No. Method & Description
1 Object clone()

This method overrides Cloneable

2 boolean equals(Object obj)

This method returns true if this Locale is equal to another object.

3 static List<Locale> filter​(List<Locale.LanguageRange> priorityList, Collection<Locale> locales)

This method returns a list of matching Locale instances using the filtering mechanism defined in RFC 4647.

4 static List<String> filterTags​(List<Locale.LanguageRange> priorityList, Collection<String> tags)

This method returns a list of matching languages tags using the basic filtering mechanism defined in RFC 4647.

5 static Locale forLanguageTag​(String languageTag)

This method returns a locale for the specified IETF BCP 47 language tag string.

6 static Locale[] getAvailableLocales()

This method returns an array of all installed locales.

7 String getCountry()

This method returns the country/region code for this locale, which will either be the empty string or an uppercase ISO 3166 2-letter code.

8 static Locale getDefault()

This method gets the current value of the default locale for this instance of the Java Virtual Machine.

9 String getDisplayCountry()

This method returns a name for the locale's country that is appropriate for display to the user.

10 String getDisplayLanguage()

This method returns a name for the locale's language that is appropriate for display to the user.

11 String getDisplayName()

This method returns a name for the locale that is appropriate for display to the user.

12 String getDisplayScript()

This method returns a name for the locale's script that is appropriate for display to the user.

13 String getDisplayVariant()

This method returns a name for the locale's variant code that is appropriate for display to the user.

14 String getExtension​(char key)

This method returns the extension (or private use) value associated with the specified key, or null if there is no extension associated with the key.

15 Set<Character> getExtensionKeys()

Returns the set of extension keys associated with this locale, or the empty set if it has no extensions.

16 String getISO3Country()

This method returns a three-letter abbreviation for this locale's country.

17 String getISO3Language()

This method returns returns a three-letter abbreviation for this locale's language.

18 static String[] getISOCountries()

This method returns a list of all 2-letter country codes defined in ISO 3166.

19 static String[] getISOLanguages()

This method returns a list of all 2-letter language codes defined in ISO 639.

20 String getLanguage()

This method returns the language code for this locale, which will either be the empty string or a lowercase ISO 639 code.

21 String getScript()

This method returns the script for this locale, which should either be the empty string or an ISO 15924 4-letter script code.

22 Set<String> getUnicodeLocaleAttributes()

This method returns the set of unicode locale attributes associated with this locale, or the empty set if it has no attributes.

23 Set<String> getUnicodeLocaleKeys()

This method returns the set of Unicode locale keys defined by this locale, or the empty set if this locale has none.

24 String getUnicodeLocaleType​(String key)

This method returns the Unicode locale type associated with the specified Unicode locale key for this locale.

25 String getVariant()

This method returns the variant code for this locale.

26 boolean hasExtensions()

This method returns true if this Locale has any extensions.

27 int hashCode()

This method override hashCode.

28 static Locale lookup​(List<Locale.LanguageRange> priorityList, Collection<Locale> locales)

This method returns a Locale instance for the best-matching language tag using the lookup mechanism defined in RFC 4647.

29 static String lookupTag​(List<Locale.LanguageRange> priorityList, Collection<String> tags)

This method returns the best-matching language tag using the lookup mechanism defined in RFC 4647.

30 static void setDefault(Locale newLocale)

This method sets the default locale for this instance of the Java Virtual Machine.

31 Locale stripExtensions()

This method returns a copy of this Locale with no extensions.

32 String toLanguageTag()

This method returns a well-formed IETF BCP 47 language tag representing this locale.

33 String toString()

This method is the getter for the programmatic name of the entire locale, with the language, country and variant separated by underbars.

Methods inherited

This class inherits methods from the following classes −

  • java.util.Object

Getting List of All Available Locales Example

The following example shows the usage of Java Locale getAvailableLocales() method. We're creating an array of available locales and then those locales are printed.

package com.tutorialspoint;

import java.util.Locale;

public class LocaleDemo {
   public static void main(String[] args) {

      // create a new array and get all installed locales
      Locale[] locales = Locale.getAvailableLocales();

      // print locales
      System.out.println("Installed locales are:");
      
      for (int i = 0; i < locales.length; i++) {
         System.out.println(i + ":" + locales[i]);
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Installed locales are:
0:
1:nn
2:ar_JO
3:bg
4:kea
5:nds
6:zu
7:am_ET
8:fr_DZ
...
741:mas_TZ
742:ti
743:kok
744:ewo
745:ms_BN
746:ccp_IN
747:br_FR
Advertisements