// © 2016 and later: Unicode, Inc. and others. // License & terms of use: http://www.unicode.org/copyright.html#License /* ****************************************************************************** * Copyright (C) 2003-2012, International Business Machines Corporation and * * others. All Rights Reserved. * ****************************************************************************** */ package com.ibm.icu.util; import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.ParseException; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Map.Entry; import java.util.MissingResourceException; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; import com.ibm.icu.impl.ICUCache; import com.ibm.icu.impl.LocaleIDParser; import com.ibm.icu.impl.LocaleIDs; import com.ibm.icu.impl.LocaleUtility; import com.ibm.icu.impl.SimpleCache; import com.ibm.icu.impl.locale.AsciiUtil; import com.ibm.icu.impl.locale.BaseLocale; import com.ibm.icu.impl.locale.Extension; import com.ibm.icu.impl.locale.InternalLocaleBuilder; import com.ibm.icu.impl.locale.LanguageTag; import com.ibm.icu.impl.locale.LocaleExtensions; import com.ibm.icu.impl.locale.LocaleSyntaxException; import com.ibm.icu.impl.locale.ParseStatus; import com.ibm.icu.impl.locale.UnicodeLocaleExtension; /** * {@icuenhanced java.util.Locale}.{@icu _usage_} * * A class analogous to {@link java.util.Locale} that provides additional * support for ICU protocol. In ICU 3.0 this class is enhanced to support * RFC 3066 language identifiers. * *
Many classes and services in ICU follow a factory idiom, in * which a factory method or object responds to a client request with * an object. The request includes a locale (the requested * locale), and the returned object is constructed using data for that * locale. The system may lack data for the requested locale, in * which case the locale fallback mechanism will be invoked until a * populated locale is found (the valid locale). Furthermore, * even when a populated locale is found (the valid locale), * further fallback may be required to reach a locale containing the * specific data required by the service (the actual locale). * *
ULocale performs 'normalization' and 'canonicalization' of locale ids. * Normalization 'cleans up' ICU locale ids as follows: *
canonicalize
can be called to convert the id
* to canonical form, or the canonicalInstance
factory method
* can be called.
*
* This class provides selectors {@link #VALID_LOCALE} and {@link
* #ACTUAL_LOCALE} intended for use in methods named
* getLocale(). These methods exist in several ICU classes,
* including {@link com.ibm.icu.util.Calendar}, {@link
* com.ibm.icu.util.Currency}, {@link com.ibm.icu.text.UFormat},
* {@link com.ibm.icu.text.BreakIterator},
* Collator
,
* {@link com.ibm.icu.text.DateFormatSymbols}, and {@link
* com.ibm.icu.text.DecimalFormatSymbols} and their subclasses, if
* any. Once an object of one of these classes has been created,
* getLocale() may be called on it to determine the valid and
* actual locale arrived at during the object's construction.
*
*
Note: The actual locale is returned correctly, but the valid
* locale is not, in most cases.
*
* @see java.util.Locale
* @author weiv
* @author Alan Liu
* @author Ram Viswanadha
* @stable ICU 2.8
*/
public final class ULocale implements Serializable {
// using serialver from jdk1.4.2_05
private static final long serialVersionUID = 3715177670352309217L;
/**
* Useful constant for language.
* @stable ICU 3.0
*/
public static final ULocale ENGLISH = new ULocale("en", Locale.ENGLISH);
/**
* Useful constant for language.
* @stable ICU 3.0
*/
public static final ULocale FRENCH = new ULocale("fr", Locale.FRENCH);
/**
* Useful constant for language.
* @stable ICU 3.0
*/
public static final ULocale GERMAN = new ULocale("de", Locale.GERMAN);
/**
* Useful constant for language.
* @stable ICU 3.0
*/
public static final ULocale ITALIAN = new ULocale("it", Locale.ITALIAN);
/**
* Useful constant for language.
* @stable ICU 3.0
*/
public static final ULocale JAPANESE = new ULocale("ja", Locale.JAPANESE);
/**
* Useful constant for language.
* @stable ICU 3.0
*/
public static final ULocale KOREAN = new ULocale("ko", Locale.KOREAN);
/**
* Useful constant for language.
* @stable ICU 3.0
*/
public static final ULocale CHINESE = new ULocale("zh", Locale.CHINESE);
/**
* Useful constant for language.
* @stable ICU 3.0
*/
public static final ULocale SIMPLIFIED_CHINESE = new ULocale("zh_Hans", Locale.CHINESE);
/**
* Useful constant for language.
* @stable ICU 3.0
*/
public static final ULocale TRADITIONAL_CHINESE = new ULocale("zh_Hant", Locale.CHINESE);
/**
* Useful constant for country/region.
* @stable ICU 3.0
*/
public static final ULocale FRANCE = new ULocale("fr_FR", Locale.FRANCE);
/**
* Useful constant for country/region.
* @stable ICU 3.0
*/
public static final ULocale GERMANY = new ULocale("de_DE", Locale.GERMANY);
/**
* Useful constant for country/region.
* @stable ICU 3.0
*/
public static final ULocale ITALY = new ULocale("it_IT", Locale.ITALY);
/**
* Useful constant for country/region.
* @stable ICU 3.0
*/
public static final ULocale JAPAN = new ULocale("ja_JP", Locale.JAPAN);
/**
* Useful constant for country/region.
* @stable ICU 3.0
*/
public static final ULocale KOREA = new ULocale("ko_KR", Locale.KOREA);
/**
* Useful constant for country/region.
* @stable ICU 3.0
*/
public static final ULocale CHINA = new ULocale("zh_Hans_CN", Locale.CHINA);
/**
* Useful constant for country/region.
* @stable ICU 3.0
*/
public static final ULocale PRC = CHINA;
/**
* Useful constant for country/region.
* @stable ICU 3.0
*/
public static final ULocale TAIWAN = new ULocale("zh_Hant_TW", Locale.TAIWAN);
/**
* Useful constant for country/region.
* @stable ICU 3.0
*/
public static final ULocale UK = new ULocale("en_GB", Locale.UK);
/**
* Useful constant for country/region.
* @stable ICU 3.0
*/
public static final ULocale US = new ULocale("en_US", Locale.US);
/**
* Useful constant for country/region.
* @stable ICU 3.0
*/
public static final ULocale CANADA = new ULocale("en_CA", Locale.CANADA);
/**
* Useful constant for country/region.
* @stable ICU 3.0
*/
public static final ULocale CANADA_FRENCH = new ULocale("fr_CA", Locale.CANADA_FRENCH);
/**
* Handy constant.
*/
private static final String EMPTY_STRING = "";
// Used in both ULocale and LocaleIDParser, so moved up here.
private static final char UNDERSCORE = '_';
// default empty locale
private static final Locale EMPTY_LOCALE = new Locale("", "");
// special keyword key for Unicode locale attributes
private static final String LOCALE_ATTRIBUTE_KEY = "attribute";
/**
* The root ULocale.
* @stable ICU 2.8
*/
public static final ULocale ROOT = new ULocale("", EMPTY_LOCALE);
/**
* Enum for locale categories. These locale categories are used to get/set the default locale for
* the specific functionality represented by the category.
* @stable ICU 49
*/
public enum Category {
/**
* Category used to represent the default locale for displaying user interfaces.
* @stable ICU 49
*/
DISPLAY,
/**
* Category used to represent the default locale for formatting date, number and/or currency.
* @stable ICU 49
*/
FORMAT
}
private static final SimpleCache This constructor does not canonicalize the localeID. So, for
* example, "zh__pinyin" remains unchanged instead of converting
* to "zh@collation=pinyin". By default ICU only recognizes the
* latter as specifying pinyin collation. Use {@link #createCanonical}
* or {@link #canonicalize} if you need to canonicalize the localeID.
*
* @param localeID string representation of the locale, e.g:
* "en_US", "sy_Cyrl_YU", "zh__pinyin", "es_ES@currency=EUR;collation=traditional"
* @stable ICU 2.8
*/
public ULocale(String localeID) {
this.localeID = getName(localeID);
}
/**
* Convenience overload of ULocale(String, String, String) for
* compatibility with java.util.Locale.
* @see #ULocale(String, String, String)
* @stable ICU 3.4
*/
public ULocale(String a, String b) {
this(a, b, null);
}
/**
* Constructs a ULocale from a localeID constructed from the three 'fields' a, b, and
* c. These fields are concatenated using underscores to form a localeID of the form
* a_b_c, which is then handled like the localeID passed to Java locale strings consisting of language, country, and
* variant will be handled by this form, since the country code
* (being shorter than four letters long) will not be interpreted
* as a script code. If a script code is present, the final
* argument ('c') will be interpreted as the country code. It is
* recommended that this constructor only be used to ease porting,
* and that clients instead use the single-argument constructor
* when constructing a ULocale from a localeID.
* @param a first component of the locale id
* @param b second component of the locale id
* @param c third component of the locale id
* @see #ULocale(String)
* @stable ICU 3.0
*/
public ULocale(String a, String b, String c) {
localeID = getName(lscvToID(a, b, c, EMPTY_STRING));
}
/**
* {@icu} Creates a ULocale from the id by first canonicalizing the id.
* @param nonCanonicalID the locale id to canonicalize
* @return the locale created from the canonical version of the ID.
* @stable ICU 3.0
*/
public static ULocale createCanonical(String nonCanonicalID) {
return new ULocale(canonicalize(nonCanonicalID), (Locale)null);
}
private static String lscvToID(String lang, String script, String country, String variant) {
StringBuilder buf = new StringBuilder();
if (lang != null && lang.length() > 0) {
buf.append(lang);
}
if (script != null && script.length() > 0) {
buf.append(UNDERSCORE);
buf.append(script);
}
if (country != null && country.length() > 0) {
buf.append(UNDERSCORE);
buf.append(country);
}
if (variant != null && variant.length() > 0) {
if (country == null || country.length() == 0) {
buf.append(UNDERSCORE);
}
buf.append(UNDERSCORE);
buf.append(variant);
}
return buf.toString();
}
/**
* {@icu} Converts this ULocale object to a {@link java.util.Locale}.
* @return a JDK locale that either exactly represents this object
* or is the closest approximation.
* @stable ICU 2.8
*/
public Locale toLocale() {
if (locale == null) {
locale = JDKLocaleHelper.toLocale(this);
}
return locale;
}
private static ICUCache
* By setting the default ULocale with this method, all of the default categoy locales
* are also set to the specified default ULocale.
* @param newLocale the new default locale
* @throws SecurityException if a security manager exists and its
* Note: The valid locale will be returned correctly in ICU
* 3.0 or later. In ICU 2.8, it is not returned correctly.
* @draft ICU 2.8 (retain)
* @provisional This API might change or be removed in a future release.
*/
public static Type VALID_LOCALE = new Type();
/**
* Opaque selector enum for getLocale().
* @see com.ibm.icu.util.ULocale
* @see com.ibm.icu.util.ULocale#ACTUAL_LOCALE
* @see com.ibm.icu.util.ULocale#VALID_LOCALE
* @draft ICU 2.8 (retainAll)
* @provisional This API might change or be removed in a future release.
*/
public static final class Type {
private Type() {}
}
/**
* {@icu} Based on a HTTP formatted list of acceptable locales, determine an available
* locale for the user. NullPointerException is thrown if acceptLanguageList or
* availableLocales is null. If fallback is non-null, it will contain true if a
* fallback locale (one not in the acceptLanguageList) was returned. The value on
* entry is ignored. ULocale will be one of the locales in availableLocales, or the
* ROOT ULocale if if a ROOT locale was used as a fallback (because nothing else in
* availableLocales matched). No ULocale array element should be null; behavior is
* undefined if this is the case.
* @param acceptLanguageList list in HTTP "Accept-Language:" format of acceptable locales
* @param availableLocales list of available locales. One of these will be returned.
* @param fallback if non-null, a 1-element array containing a boolean to be set with
* the fallback status
* @return one of the locales from the availableLocales list, or null if none match
* @stable ICU 3.4
*/
public static ULocale acceptLanguage(String acceptLanguageList, ULocale[] availableLocales,
boolean[] fallback) {
if (acceptLanguageList == null) {
throw new NullPointerException();
}
ULocale acceptList[] = null;
try {
acceptList = parseAcceptLanguage(acceptLanguageList, true);
} catch (ParseException pe) {
acceptList = null;
}
if (acceptList == null) {
return null;
}
return acceptLanguage(acceptList, availableLocales, fallback);
}
/**
* {@icu} Based on a list of acceptable locales, determine an available locale for the
* user. NullPointerException is thrown if acceptLanguageList or availableLocales is
* null. If fallback is non-null, it will contain true if a fallback locale (one not
* in the acceptLanguageList) was returned. The value on entry is ignored. ULocale
* will be one of the locales in availableLocales, or the ROOT ULocale if if a ROOT
* locale was used as a fallback (because nothing else in availableLocales matched).
* No ULocale array element should be null; behavior is undefined if this is the case.
* @param acceptLanguageList list of acceptable locales
* @param availableLocales list of available locales. One of these will be returned.
* @param fallback if non-null, a 1-element array containing a boolean to be set with
* the fallback status
* @return one of the locales from the availableLocales list, or null if none match
* @stable ICU 3.4
*/
public static ULocale acceptLanguage(ULocale[] acceptLanguageList, ULocale[]
availableLocales, boolean[] fallback) {
// fallbacklist
int i,j;
if(fallback != null) {
fallback[0]=true;
}
for(i=0;i If this Language: If language is empty, or not well-formed
* (for example "a" or "e2"), it will be emitted as "und" (Undetermined).
*
* Script: If script is not well-formed (for example "12"
* or "Latin"), it will be omitted.
*
* Country: If country is not well-formed (for example "12"
* or "USA"), it will be omitted.
*
* Variant: If variant is well-formed, each sub-segment
* (delimited by '-' or '_') is emitted as a subtag. Otherwise:
* Note: Although the language tag created by this
* method is well-formed (satisfies the syntax requirements
* defined by the IETF BCP 47 specification), it is not
* necessarily a valid BCP 47 language tag. For example,
* If the specified language tag contains any ill-formed subtags,
* the first such subtag and all following subtags are ignored. Compare
* to {@link ULocale.Builder#setLanguageTag} which throws an exception
* in this case.
*
* The following conversions are performed: This implements the 'Language-Tag' production of BCP47, and
* so supports grandfathered (regular and irregular) as well as
* private use language tags. Stand alone private use tags are
* represented as empty language and extension 'x-whatever',
* and grandfathered tags are converted to their canonical replacements
* where they exist.
*
* Grandfathered tags with canonical replacements are as follows:
*
* Grandfathered tags with no modern replacement will be
* converted as follows:
*
* For a list of all grandfathered tags, see the
* IANA Language Subtag Registry (search for "Type: grandfathered").
*
* Note: there is no guarantee that Note: The The following example shows how to create a Builders can be reused; All fields of the locale must be well-formed, see {@link Locale}.
*
* Locales with any ill-formed fields cause
* The typical language value is a two or three-letter language
* code as defined in ISO639.
*
* @param language the language
* @return This builder.
* @throws IllformedLocaleException if The typical script value is a four-letter script code as defined by ISO 15924.
*
* @param script the script
* @return This builder.
* @throws IllformedLocaleException if The typical region value is a two-letter ISO 3166 code or a
* three-digit UN M.49 area code.
*
* The country value in the Note: This method checks if Note: The key {@link ULocale#UNICODE_LOCALE_EXTENSION
* UNICODE_LOCALE_EXTENSION} ('u') is used for the Unicode locale extension.
* Setting a value for this key replaces any existing Unicode locale key/type
* pairs with those defined in the extension.
*
* Note: The key {@link ULocale#PRIVATE_USE_EXTENSION
* PRIVATE_USE_EXTENSION} ('x') is used for the private use code. To be
* well-formed, the value for this key needs only to have subtags of one to
* eight alphanumeric characters, not two to eight as in the general case.
*
* @param key the extension key
* @param value the extension value
* @return This builder.
* @throws IllformedLocaleException if Keys and types are converted to lower case.
*
* Note:Setting the 'u' extension via {@link #setExtension}
* replaces all Unicode locale keywords with those defined in the
* extension.
*
* @param key the Unicode locale key
* @param type the Unicode locale type
* @return This builder.
* @throws IllformedLocaleException if Attribute comparision for removal is case-insensitive.
*
* @param attribute the attribute
* @return This builder.
* @throws NullPointerException if ULocale(String
* localeID)
.
*
* checkPermission
method doesn't allow the operation.
* @throws NullPointerException if newLocale
is null
* @see SecurityManager#checkPermission(java.security.Permission)
* @see java.util.PropertyPermission
* @see ULocale#setDefault(Category, ULocale)
* @stable ICU 3.0
*/
public static synchronized void setDefault(ULocale newLocale){
defaultLocale = newLocale.toLocale();
Locale.setDefault(defaultLocale);
defaultULocale = newLocale;
// This method also updates all category default locales
for (Category cat : Category.values()) {
setDefault(cat, newLocale);
}
}
/**
* Returns the current default ULocale for the specified category.
*
* @param category the category
* @return the default ULocale for the specified category.
* @stable ICU 49
*/
public static ULocale getDefault(Category category) {
synchronized (ULocale.class) {
int idx = category.ordinal();
if (defaultCategoryULocales[idx] == null) {
// Just in case this method is called during ULocale class
// initialization. Unlike getDefault(), we do not have
// cyclic dependency for category default.
return ULocale.ROOT;
}
if (JDKLocaleHelper.isJava7orNewer()) {
Locale currentCategoryDefault = JDKLocaleHelper.getDefault(category);
if (!defaultCategoryLocales[idx].equals(currentCategoryDefault)) {
defaultCategoryLocales[idx] = currentCategoryDefault;
defaultCategoryULocales[idx] = forLocale(currentCategoryDefault);
}
} else {
// java.util.Locale.setDefault(Locale) in Java 7 updates
// category locale defaults. On Java 6 or older environment,
// ICU4J checks if the default locale has changed and update
// category ULocales here if necessary.
// Note: When java.util.Locale.setDefault(Locale) is called
// with a Locale same with the previous one, Java 7 still
// updates category locale defaults. On Java 6 or older env,
// there is no good way to detect the event, ICU4J simply
// check if the default Java Locale has changed since last
// time.
Locale currentDefault = Locale.getDefault();
if (!defaultLocale.equals(currentDefault)) {
defaultLocale = currentDefault;
defaultULocale = forLocale(currentDefault);
for (Category cat : Category.values()) {
int tmpIdx = cat.ordinal();
defaultCategoryLocales[tmpIdx] = currentDefault;
defaultCategoryULocales[tmpIdx] = forLocale(currentDefault);
}
}
// No synchronization with JDK Locale, because category default
// is not supported in Java 6 or older versions
}
return defaultCategoryULocales[idx];
}
}
/**
* Sets the default ULocale
for the specified Category
.
* This also sets the default Locale
for the specified Category
* of the JVM. If the caller does not have write permission to the
* user.language property, a security exception will be thrown,
* and the default ULocale for the specified Category will remain unchanged.
*
* @param category the specified category to set the default locale
* @param newLocale the new default locale
* @see SecurityManager#checkPermission(java.security.Permission)
* @see java.util.PropertyPermission
* @stable ICU 49
*/
public static synchronized void setDefault(Category category, ULocale newLocale) {
Locale newJavaDefault = newLocale.toLocale();
int idx = category.ordinal();
defaultCategoryULocales[idx] = newLocale;
defaultCategoryLocales[idx] = newJavaDefault;
JDKLocaleHelper.setDefault(category, newJavaDefault);
}
/**
* This is for compatibility with Locale-- in actuality, since ULocale is
* immutable, there is no reason to clone it, so this API returns 'this'.
* @stable ICU 3.0
*/
public Object clone() {
return this;
}
/**
* Returns the hashCode.
* @stable ICU 3.0
*/
public int hashCode() {
return localeID.hashCode();
}
/**
* Returns true if the other object is another ULocale with the
* same full name, or is a String localeID that matches the full name.
* Note that since names are not canonicalized, two ULocales that
* function identically might not compare equal.
*
* @return true if this Locale is equal to the specified object.
* @stable ICU 3.0
*/
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof String) {
return localeID.equals((String)obj);
}
if (obj instanceof ULocale) {
return localeID.equals(((ULocale)obj).localeID);
}
return false;
}
/**
* {@icunote} Unlike the Locale API, this returns an array of ULocale
,
* not Locale
. Returns a list of all installed locales.
* @stable ICU 3.0
*/
public static ULocale[] getAvailableLocales() {
//#com.ibm.icu.base
if (availableLocales == null) {
synchronized (ULocale.class) {
if (availableLocales == null) {
Locale[] locales = Locale.getAvailableLocales();
availableLocales = new ULocale[locales.length];
for (int i = 0; i < locales.length; i++) {
availableLocales[i] = ULocale.forLocale(locales[i]);
}
}
}
}
return availableLocales.clone();
}
private static volatile ULocale[] availableLocales = null;
/**
* Returns a list of all 2-letter country codes defined in ISO 3166.
* Can be used to create Locales.
* @stable ICU 3.0
*/
public static String[] getISOCountries() {
return LocaleIDs.getISOCountries();
}
/**
* Returns a list of all 2-letter language codes defined in ISO 639.
* Can be used to create Locales.
* [NOTE: ISO 639 is not a stable standard-- some languages' codes have changed.
* The list this function returns includes both the new and the old codes for the
* languages whose codes have changed.]
* @stable ICU 3.0
*/
public static String[] getISOLanguages() {
return LocaleIDs.getISOLanguages();
}
/**
* Returns the language code for this locale, which will either be the empty string
* or a lowercase ISO 639 code.
* @see #getDisplayLanguage()
* @see #getDisplayLanguage(ULocale)
* @stable ICU 3.0
*/
public String getLanguage() {
return getLanguage(localeID);
}
/**
* Returns the language code for the locale ID,
* which will either be the empty string
* or a lowercase ISO 639 code.
* @see #getDisplayLanguage()
* @see #getDisplayLanguage(ULocale)
* @stable ICU 3.0
*/
public static String getLanguage(String localeID) {
return new LocaleIDParser(localeID).getLanguage();
}
/**
* {@icu} Returns the script code for this locale, which might be the empty string.
* @see #getDisplayScript()
* @see #getDisplayScript(ULocale)
* @stable ICU 3.0
*/
public String getScript() {
return getScript(localeID);
}
/**
* {@icu} Returns the script code for the specified locale, which might be the empty
* string.
* @see #getDisplayScript()
* @see #getDisplayScript(ULocale)
* @stable ICU 3.0
*/
public static String getScript(String localeID) {
return new LocaleIDParser(localeID).getScript();
}
/**
* Returns the country/region code for this locale, which will either be the empty string
* or an uppercase ISO 3166 2-letter code.
* @see #getDisplayCountry()
* @see #getDisplayCountry(ULocale)
* @stable ICU 3.0
*/
public String getCountry() {
return getCountry(localeID);
}
/**
* Returns the country/region code for this locale, which will either be the empty string
* or an uppercase ISO 3166 2-letter code.
* @param localeID The locale identification string.
* @see #getDisplayCountry()
* @see #getDisplayCountry(ULocale)
* @stable ICU 3.0
*/
public static String getCountry(String localeID) {
return new LocaleIDParser(localeID).getCountry();
}
/**
* Returns the variant code for this locale, which might be the empty string.
* @see #getDisplayVariant()
* @see #getDisplayVariant(ULocale)
* @stable ICU 3.0
*/
public String getVariant() {
return getVariant(localeID);
}
/**
* Returns the variant code for the specified locale, which might be the empty string.
* @see #getDisplayVariant()
* @see #getDisplayVariant(ULocale)
* @stable ICU 3.0
*/
public static String getVariant(String localeID) {
return new LocaleIDParser(localeID).getVariant();
}
/**
* {@icu} Returns the fallback locale for the specified locale, which might be the
* empty string.
* @stable ICU 3.2
*/
public static String getFallback(String localeID) {
return getFallbackString(getName(localeID));
}
/**
* {@icu} Returns the fallback locale for this locale. If this locale is root,
* returns null.
* @stable ICU 3.2
*/
public ULocale getFallback() {
if (localeID.length() == 0 || localeID.charAt(0) == '@') {
return null;
}
return new ULocale(getFallbackString(localeID), (Locale)null);
}
/**
* Returns the given (canonical) locale id minus the last part before the tags.
*/
private static String getFallbackString(String fallback) {
int extStart = fallback.indexOf('@');
if (extStart == -1) {
extStart = fallback.length();
}
int last = fallback.lastIndexOf('_', extStart);
if (last == -1) {
last = 0;
} else {
// truncate empty segment
while (last > 0) {
if (fallback.charAt(last - 1) != '_') {
break;
}
last--;
}
}
return fallback.substring(0, last) + fallback.substring(extStart);
}
/**
* {@icu} Returns the (normalized) base name for this locale.
* @return the base name as a String.
* @stable ICU 3.0
*/
public String getBaseName() {
return getBaseName(localeID);
}
/**
* {@icu} Returns the (normalized) base name for the specified locale.
* @param localeID the locale ID as a string
* @return the base name as a String.
* @stable ICU 3.0
*/
public static String getBaseName(String localeID){
if (localeID.indexOf('@') == -1) {
return localeID;
}
return new LocaleIDParser(localeID).getBaseName();
}
/**
* {@icu} Returns the (normalized) full name for this locale.
*
* @return String the full name of the localeID
* @stable ICU 3.0
*/
public String getName() {
return localeID; // always normalized
}
/**
* Gets the shortest length subtag's size.
*
* @param localeID
* @return The size of the shortest length subtag
**/
private static int getShortestSubtagLength(String localeID) {
int localeIDLength = localeID.length();
int length = localeIDLength;
boolean reset = true;
int tmpLength = 0;
for (int i = 0; i < localeIDLength; i++) {
if (localeID.charAt(i) != '_' && localeID.charAt(i) != '-') {
if (reset) {
reset = false;
tmpLength = 0;
}
tmpLength++;
} else {
if (tmpLength != 0 && tmpLength < length) {
length = tmpLength;
}
reset = true;
}
}
return length;
}
/**
* {@icu} Returns the (normalized) full name for the specified locale.
*
* @param localeID the localeID as a string
* @return String the full name of the localeID
* @stable ICU 3.0
*/
public static String getName(String localeID){
String tmpLocaleID;
// Convert BCP47 id if necessary
if (localeID != null && !localeID.contains("@") && getShortestSubtagLength(localeID) == 1) {
tmpLocaleID = forLanguageTag(localeID).getName();
if (tmpLocaleID.length() == 0) {
tmpLocaleID = localeID;
}
} else {
tmpLocaleID = localeID;
}
String name = nameCache.get(tmpLocaleID);
if (name == null) {
name = new LocaleIDParser(tmpLocaleID).getName();
nameCache.put(tmpLocaleID, name);
}
return name;
}
/**
* Returns a string representation of this object.
* @stable ICU 3.0
*/
public String toString() {
return localeID;
}
/**
* {@icu} Returns an iterator over keywords for this locale. If there
* are no keywords, returns null.
* @return iterator over keywords, or null if there are no keywords.
* @stable ICU 3.0
*/
public Iteratorftp://dkuug.dk/i18n/iso-639-2.txt
* @exception MissingResourceException Throws MissingResourceException if the
* three-letter language abbreviation is not available for this locale.
* @stable ICU 3.0
*/
public String getISO3Language(){
return getISO3Language(localeID);
}
/**
* Returns a three-letter abbreviation for this locale's language. If the locale
* doesn't specify a language, returns the empty string. Otherwise, returns
* a lowercase ISO 639-2/T language code.
* The ISO 639-2 language codes can be found on-line at
* ftp://dkuug.dk/i18n/iso-639-2.txt
* @exception MissingResourceException Throws MissingResourceException if the
* three-letter language abbreviation is not available for this locale.
* @stable ICU 3.0
*/
public static String getISO3Language(String localeID) {
return LocaleIDs.getISO3Language(getLanguage(localeID));
}
/**
* Returns a three-letter abbreviation for this locale's country/region. If the locale
* doesn't specify a country, returns the empty string. Otherwise, returns
* an uppercase ISO 3166 3-letter country code.
* @exception MissingResourceException Throws MissingResourceException if the
* three-letter country abbreviation is not available for this locale.
* @stable ICU 3.0
*/
public String getISO3Country() {
return getISO3Country(localeID);
}
/**
* Returns a three-letter abbreviation for this locale's country/region. If the locale
* doesn't specify a country, returns the empty string. Otherwise, returns
* an uppercase ISO 3166 3-letter country code.
* @exception MissingResourceException Throws MissingResourceException if the
* three-letter country abbreviation is not available for this locale.
* @stable ICU 3.0
*/
public static String getISO3Country(String localeID) {
return LocaleIDs.getISO3Country(getCountry(localeID));
}
// display names
/**
* Returns this locale's language localized for display in the default DISPLAY
locale.
* @return the localized language name.
* @see Category#DISPLAY
* @stable ICU 3.0
*/
public String getDisplayLanguage() {
return getDisplayLanguageInternal(this, getDefault(Category.DISPLAY), false);
}
/**
* {@icu} Returns this locale's language localized for display in the provided locale.
* @param displayLocale the locale in which to display the name.
* @return the localized language name.
* @stable ICU 3.0
*/
public String getDisplayLanguage(ULocale displayLocale) {
return getDisplayLanguageInternal(this, displayLocale, false);
}
/**
* Returns a locale's language localized for display in the provided locale.
* This is a cover for the ICU4C API.
* @param localeID the id of the locale whose language will be displayed
* @param displayLocaleID the id of the locale in which to display the name.
* @return the localized language name.
* @stable ICU 3.0
*/
public static String getDisplayLanguage(String localeID, String displayLocaleID) {
return getDisplayLanguageInternal(new ULocale(localeID), new ULocale(displayLocaleID),
false);
}
/**
* Returns a locale's language localized for display in the provided locale.
* This is a cover for the ICU4C API.
* @param localeID the id of the locale whose language will be displayed.
* @param displayLocale the locale in which to display the name.
* @return the localized language name.
* @stable ICU 3.0
*/
public static String getDisplayLanguage(String localeID, ULocale displayLocale) {
return getDisplayLanguageInternal(new ULocale(localeID), displayLocale, false);
}
/**
* {@icu} Returns this locale's language localized for display in the default DISPLAY
locale.
* If a dialect name is present in the data, then it is returned.
* @return the localized language name.
* @see Category#DISPLAY
* @stable ICU 4.4
*/
public String getDisplayLanguageWithDialect() {
return getDisplayLanguageInternal(this, getDefault(Category.DISPLAY), true);
}
/**
* {@icu} Returns this locale's language localized for display in the provided locale.
* If a dialect name is present in the data, then it is returned.
* @param displayLocale the locale in which to display the name.
* @return the localized language name.
* @stable ICU 4.4
*/
public String getDisplayLanguageWithDialect(ULocale displayLocale) {
return getDisplayLanguageInternal(this, displayLocale, true);
}
/**
* {@icu} Returns a locale's language localized for display in the provided locale.
* If a dialect name is present in the data, then it is returned.
* This is a cover for the ICU4C API.
* @param localeID the id of the locale whose language will be displayed
* @param displayLocaleID the id of the locale in which to display the name.
* @return the localized language name.
* @stable ICU 4.4
*/
public static String getDisplayLanguageWithDialect(String localeID, String displayLocaleID) {
return getDisplayLanguageInternal(new ULocale(localeID), new ULocale(displayLocaleID),
true);
}
/**
* {@icu} Returns a locale's language localized for display in the provided locale.
* If a dialect name is present in the data, then it is returned.
* This is a cover for the ICU4C API.
* @param localeID the id of the locale whose language will be displayed.
* @param displayLocale the locale in which to display the name.
* @return the localized language name.
* @stable ICU 4.4
*/
public static String getDisplayLanguageWithDialect(String localeID, ULocale displayLocale) {
return getDisplayLanguageInternal(new ULocale(localeID), displayLocale, true);
}
private static String getDisplayLanguageInternal(ULocale locale, ULocale displayLocale,
boolean useDialect) {
//#com.ibm.icu.base
// No dialect support
return locale.toLocale().getDisplayLanguage(displayLocale.toLocale());
}
/**
* {@icu} Returns this locale's script localized for display in the default DISPLAY
locale.
* @return the localized script name.
* @see Category#DISPLAY
* @stable ICU 3.0
*/
public String getDisplayScript() {
return getDisplayScriptInternal(this, getDefault(Category.DISPLAY));
}
/**
* {@icu} Returns this locale's script localized for display in the provided locale.
* @param displayLocale the locale in which to display the name.
* @return the localized script name.
* @stable ICU 3.0
*/
public String getDisplayScript(ULocale displayLocale) {
return getDisplayScriptInternal(this, displayLocale);
}
/**
* {@icu} Returns a locale's script localized for display in the provided locale.
* This is a cover for the ICU4C API.
* @param localeID the id of the locale whose script will be displayed
* @param displayLocaleID the id of the locale in which to display the name.
* @return the localized script name.
* @stable ICU 3.0
*/
public static String getDisplayScript(String localeID, String displayLocaleID) {
return getDisplayScriptInternal(new ULocale(localeID), new ULocale(displayLocaleID));
}
/**
* {@icu} Returns a locale's script localized for display in the provided locale.
* @param localeID the id of the locale whose script will be displayed.
* @param displayLocale the locale in which to display the name.
* @return the localized script name.
* @stable ICU 3.0
*/
public static String getDisplayScript(String localeID, ULocale displayLocale) {
return getDisplayScriptInternal(new ULocale(localeID), displayLocale);
}
// displayLocaleID is canonical, localeID need not be since parsing will fix this.
private static String getDisplayScriptInternal(ULocale locale, ULocale displayLocale) {
//#com.ibm.icu.base
String dispScript = null;
try {
// Calling Locale#getDisplayScript on Java 7 or later
Method mGetDisplayScript = Locale.class.getMethod("getDisplayScript", Locale.class);
dispScript = (String) mGetDisplayScript.invoke(locale.toLocale(), displayLocale.toLocale());
} catch (NoSuchMethodException e) {
} catch (InvocationTargetException e) {
} catch (IllegalAccessException e) {
}
if (dispScript == null) {
dispScript = locale.getScript();
}
return dispScript;
}
/**
* Returns this locale's country localized for display in the default DISPLAY
locale.
* @return the localized country name.
* @see Category#DISPLAY
* @stable ICU 3.0
*/
public String getDisplayCountry() {
return getDisplayCountryInternal(this, getDefault(Category.DISPLAY));
}
/**
* Returns this locale's country localized for display in the provided locale.
* @param displayLocale the locale in which to display the name.
* @return the localized country name.
* @stable ICU 3.0
*/
public String getDisplayCountry(ULocale displayLocale){
return getDisplayCountryInternal(this, displayLocale);
}
/**
* Returns a locale's country localized for display in the provided locale.
* This is a cover for the ICU4C API.
* @param localeID the id of the locale whose country will be displayed
* @param displayLocaleID the id of the locale in which to display the name.
* @return the localized country name.
* @stable ICU 3.0
*/
public static String getDisplayCountry(String localeID, String displayLocaleID) {
return getDisplayCountryInternal(new ULocale(localeID), new ULocale(displayLocaleID));
}
/**
* Returns a locale's country localized for display in the provided locale.
* This is a cover for the ICU4C API.
* @param localeID the id of the locale whose country will be displayed.
* @param displayLocale the locale in which to display the name.
* @return the localized country name.
* @stable ICU 3.0
*/
public static String getDisplayCountry(String localeID, ULocale displayLocale) {
return getDisplayCountryInternal(new ULocale(localeID), displayLocale);
}
// displayLocaleID is canonical, localeID need not be since parsing will fix this.
private static String getDisplayCountryInternal(ULocale locale, ULocale displayLocale) {
//#com.ibm.icu.base
return locale.toLocale().getDisplayCountry(displayLocale.toLocale());
}
/**
* Returns this locale's variant localized for display in the default DISPLAY
locale.
* @return the localized variant name.
* @see Category#DISPLAY
* @stable ICU 3.0
*/
public String getDisplayVariant() {
return getDisplayVariantInternal(this, getDefault(Category.DISPLAY));
}
/**
* Returns this locale's variant localized for display in the provided locale.
* @param displayLocale the locale in which to display the name.
* @return the localized variant name.
* @stable ICU 3.0
*/
public String getDisplayVariant(ULocale displayLocale) {
return getDisplayVariantInternal(this, displayLocale);
}
/**
* Returns a locale's variant localized for display in the provided locale.
* This is a cover for the ICU4C API.
* @param localeID the id of the locale whose variant will be displayed
* @param displayLocaleID the id of the locale in which to display the name.
* @return the localized variant name.
* @stable ICU 3.0
*/
public static String getDisplayVariant(String localeID, String displayLocaleID){
return getDisplayVariantInternal(new ULocale(localeID), new ULocale(displayLocaleID));
}
/**
* Returns a locale's variant localized for display in the provided locale.
* This is a cover for the ICU4C API.
* @param localeID the id of the locale whose variant will be displayed.
* @param displayLocale the locale in which to display the name.
* @return the localized variant name.
* @stable ICU 3.0
*/
public static String getDisplayVariant(String localeID, ULocale displayLocale) {
return getDisplayVariantInternal(new ULocale(localeID), displayLocale);
}
private static String getDisplayVariantInternal(ULocale locale, ULocale displayLocale) {
//#com.ibm.icu.base
return locale.toLocale().getDisplayVariant(displayLocale.toLocale());
}
/**
* {@icu} Returns a keyword localized for display in the default DISPLAY
locale.
* @param keyword the keyword to be displayed.
* @return the localized keyword name.
* @see #getKeywords()
* @see Category#DISPLAY
* @stable ICU 3.0
*/
public static String getDisplayKeyword(String keyword) {
return getDisplayKeywordInternal(keyword, getDefault(Category.DISPLAY));
}
/**
* {@icu} Returns a keyword localized for display in the specified locale.
* @param keyword the keyword to be displayed.
* @param displayLocaleID the id of the locale in which to display the keyword.
* @return the localized keyword name.
* @see #getKeywords(String)
* @stable ICU 3.0
*/
public static String getDisplayKeyword(String keyword, String displayLocaleID) {
return getDisplayKeywordInternal(keyword, new ULocale(displayLocaleID));
}
/**
* {@icu} Returns a keyword localized for display in the specified locale.
* @param keyword the keyword to be displayed.
* @param displayLocale the locale in which to display the keyword.
* @return the localized keyword name.
* @see #getKeywords(String)
* @stable ICU 3.0
*/
public static String getDisplayKeyword(String keyword, ULocale displayLocale) {
return getDisplayKeywordInternal(keyword, displayLocale);
}
private static String getDisplayKeywordInternal(String keyword, ULocale displayLocale) {
//#com.ibm.icu.base
// No localization
return keyword;
}
/**
* {@icu} Returns a keyword value localized for display in the default DISPLAY
locale.
* @param keyword the keyword whose value is to be displayed.
* @return the localized value name.
* @see Category#DISPLAY
* @stable ICU 3.0
*/
public String getDisplayKeywordValue(String keyword) {
return getDisplayKeywordValueInternal(this, keyword, getDefault(Category.DISPLAY));
}
/**
* {@icu} Returns a keyword value localized for display in the specified locale.
* @param keyword the keyword whose value is to be displayed.
* @param displayLocale the locale in which to display the value.
* @return the localized value name.
* @stable ICU 3.0
*/
public String getDisplayKeywordValue(String keyword, ULocale displayLocale) {
return getDisplayKeywordValueInternal(this, keyword, displayLocale);
}
/**
* {@icu} Returns a keyword value localized for display in the specified locale.
* This is a cover for the ICU4C API.
* @param localeID the id of the locale whose keyword value is to be displayed.
* @param keyword the keyword whose value is to be displayed.
* @param displayLocaleID the id of the locale in which to display the value.
* @return the localized value name.
* @stable ICU 3.0
*/
public static String getDisplayKeywordValue(String localeID, String keyword,
String displayLocaleID) {
return getDisplayKeywordValueInternal(new ULocale(localeID), keyword,
new ULocale(displayLocaleID));
}
/**
* {@icu} Returns a keyword value localized for display in the specified locale.
* This is a cover for the ICU4C API.
* @param localeID the id of the locale whose keyword value is to be displayed.
* @param keyword the keyword whose value is to be displayed.
* @param displayLocale the id of the locale in which to display the value.
* @return the localized value name.
* @stable ICU 3.0
*/
public static String getDisplayKeywordValue(String localeID, String keyword,
ULocale displayLocale) {
return getDisplayKeywordValueInternal(new ULocale(localeID), keyword, displayLocale);
}
// displayLocaleID is canonical, localeID need not be since parsing will fix this.
private static String getDisplayKeywordValueInternal(ULocale locale, String keyword,
ULocale displayLocale) {
//#com.ibm.icu.base
keyword = AsciiUtil.toLowerString(keyword.trim());
String value = locale.getKeywordValue(keyword);
// No localization
return value;
}
/**
* Returns this locale name localized for display in the default DISPLAY
locale.
* @return the localized locale name.
* @see Category#DISPLAY
* @stable ICU 3.0
*/
public String getDisplayName() {
return getDisplayNameInternal(this, getDefault(Category.DISPLAY));
}
/**
* Returns this locale name localized for display in the provided locale.
* @param displayLocale the locale in which to display the locale name.
* @return the localized locale name.
* @stable ICU 3.0
*/
public String getDisplayName(ULocale displayLocale) {
return getDisplayNameInternal(this, displayLocale);
}
/**
* Returns the locale ID localized for display in the provided locale.
* This is a cover for the ICU4C API.
* @param localeID the locale whose name is to be displayed.
* @param displayLocaleID the id of the locale in which to display the locale name.
* @return the localized locale name.
* @stable ICU 3.0
*/
public static String getDisplayName(String localeID, String displayLocaleID) {
return getDisplayNameInternal(new ULocale(localeID), new ULocale(displayLocaleID));
}
/**
* Returns the locale ID localized for display in the provided locale.
* This is a cover for the ICU4C API.
* @param localeID the locale whose name is to be displayed.
* @param displayLocale the locale in which to display the locale name.
* @return the localized locale name.
* @stable ICU 3.0
*/
public static String getDisplayName(String localeID, ULocale displayLocale) {
return getDisplayNameInternal(new ULocale(localeID), displayLocale);
}
private static String getDisplayNameInternal(ULocale locale, ULocale displayLocale) {
//#com.ibm.icu.base
return locale.toLocale().getDisplayName(displayLocale.toLocale());
}
/**
* {@icu} Returns this locale name localized for display in the default DISPLAY
locale.
* If a dialect name is present in the locale data, then it is returned.
* @return the localized locale name.
* @see Category#DISPLAY
* @stable ICU 4.4
*/
public String getDisplayNameWithDialect() {
return getDisplayNameWithDialectInternal(this, getDefault(Category.DISPLAY));
}
/**
* {@icu} Returns this locale name localized for display in the provided locale.
* If a dialect name is present in the locale data, then it is returned.
* @param displayLocale the locale in which to display the locale name.
* @return the localized locale name.
* @stable ICU 4.4
*/
public String getDisplayNameWithDialect(ULocale displayLocale) {
return getDisplayNameWithDialectInternal(this, displayLocale);
}
/**
* {@icu} Returns the locale ID localized for display in the provided locale.
* If a dialect name is present in the locale data, then it is returned.
* This is a cover for the ICU4C API.
* @param localeID the locale whose name is to be displayed.
* @param displayLocaleID the id of the locale in which to display the locale name.
* @return the localized locale name.
* @stable ICU 4.4
*/
public static String getDisplayNameWithDialect(String localeID, String displayLocaleID) {
return getDisplayNameWithDialectInternal(new ULocale(localeID),
new ULocale(displayLocaleID));
}
/**
* {@icu} Returns the locale ID localized for display in the provided locale.
* If a dialect name is present in the locale data, then it is returned.
* This is a cover for the ICU4C API.
* @param localeID the locale whose name is to be displayed.
* @param displayLocale the locale in which to display the locale name.
* @return the localized locale name.
* @stable ICU 4.4
*/
public static String getDisplayNameWithDialect(String localeID, ULocale displayLocale) {
return getDisplayNameWithDialectInternal(new ULocale(localeID), displayLocale);
}
private static String getDisplayNameWithDialectInternal(ULocale locale, ULocale displayLocale) {
//#com.ibm.icu.base
// No dialect handling
return locale.toLocale().getDisplayName(displayLocale.toLocale());
}
/**
* {@icu} Returns this locale's layout orientation for characters. The possible
* values are "left-to-right", "right-to-left", "top-to-bottom" or
* "bottom-to-top".
* @return The locale's layout orientation for characters.
* @stable ICU 4.0
*/
public String getCharacterOrientation() {
//#com.ibm.icu.base
// Hardcoded
String lang = getLanguage();
if (lang.equals("ar") || lang.equals("fa") || lang.equals("he") || lang.equals("ps") || lang.equals("ur")) {
return "right-to-left";
}
String script = getScript();
if (script.equals("Arab")) {
return "right-to-left";
}
return "left-to-right";
}
/**
* {@icu} Returns this locale's layout orientation for lines. The possible
* values are "left-to-right", "right-to-left", "top-to-bottom" or
* "bottom-to-top".
* @return The locale's layout orientation for lines.
* @stable ICU 4.0
*/
public String getLineOrientation() {
//#com.ibm.icu.base
return "top-to-bottom";
}
/**
* {@icu} Selector for getLocale() indicating the locale of the
* resource containing the data. This is always at or above the
* valid locale. If the valid locale does not contain the
* specific data being requested, then the actual locale will be
* above the valid locale. If the object was not constructed from
* locale data, then the valid locale is null.
*
* @draft ICU 2.8 (retain)
* @provisional This API might change or be removed in a future release.
*/
public static Type ACTUAL_LOCALE = new Type();
/**
* {@icu} Selector for getLocale() indicating the most specific
* locale for which any data exists. This is always at or above
* the requested locale, and at or below the actual locale. If
* the requested locale does not correspond to any resource data,
* then the valid locale will be above the requested locale. If
* the object was not constructed from locale data, then the
* actual locale is null.
*
*
*
* http://www.unicode.org/reports/tr35/#Likely_Subtags
*
* If the provided ULocale instance is already in the minimal form, or there
* is no data available for minimization, it will be returned. Since the
* minimization algorithm relies on proper maximization, see the comments
* for addLikelySubtags for reasons why there might not be any data.
*
* Examples:
*
* "en_Latn_US" minimizes to "en"
*
* "de_Latn_US" minimizes to "de"
*
* "sr_Cyrl_RS" minimizes to "sr"
*
* "zh_Hant_TW" minimizes to "zh_TW" (The region is preferred to the
* script, and minimizing to "zh" would imply "zh_Hans_CN".)
*
* @param loc The ULocale to minimize
* @return The minimized ULocale instance.
* @stable ICU 4.0
*/
public static ULocale minimizeSubtags(ULocale loc) {
//#com.ibm.icu.base
return loc;
}
/**
* A trivial utility function that checks for a null
* reference or checks the length of the supplied String.
*
* @param string The string to check
*
* @return true if the String is empty, or if the reference is null.
*/
private static boolean isEmptyString(String string) {
return string == null || string.length() == 0;
}
/**
* Append a tag to a StringBuilder, adding the separator if necessary.The tag must
* not be a zero-length string.
*
* @param tag The tag to add.
* @param buffer The output buffer.
**/
private static void appendTag(String tag, StringBuilder buffer) {
if (buffer.length() != 0) {
buffer.append(UNDERSCORE);
}
buffer.append(tag);
}
/**
* Create a tag string from the supplied parameters. The lang, script and region
* parameters may be null references.
*
* If any of the language, script or region parameters are empty, and the alternateTags
* parameter is not null, it will be parsed for potential language, script and region tags
* to be used when constructing the new tag. If the alternateTags parameter is null, or
* it contains no language tag, the default tag for the unknown language is used.
*
* @param lang The language tag to use.
* @param script The script tag to use.
* @param region The region tag to use.
* @param trailing Any trailing data to append to the new tag.
* @param alternateTags A string containing any alternate tags.
* @return The new tag string.
**/
private static String createTagString(String lang, String script, String region,
String trailing, String alternateTags) {
LocaleIDParser parser = null;
boolean regionAppended = false;
StringBuilder tag = new StringBuilder();
if (!isEmptyString(lang)) {
appendTag(
lang,
tag);
}
else if (isEmptyString(alternateTags)) {
/*
* Append the value for an unknown language, if
* we found no language.
*/
appendTag(
UNDEFINED_LANGUAGE,
tag);
}
else {
parser = new LocaleIDParser(alternateTags);
String alternateLang = parser.getLanguage();
/*
* Append the value for an unknown language, if
* we found no language.
*/
appendTag(
!isEmptyString(alternateLang) ? alternateLang : UNDEFINED_LANGUAGE,
tag);
}
if (!isEmptyString(script)) {
appendTag(
script,
tag);
}
else if (!isEmptyString(alternateTags)) {
/*
* Parse the alternateTags string for the script.
*/
if (parser == null) {
parser = new LocaleIDParser(alternateTags);
}
String alternateScript = parser.getScript();
if (!isEmptyString(alternateScript)) {
appendTag(
alternateScript,
tag);
}
}
if (!isEmptyString(region)) {
appendTag(
region,
tag);
regionAppended = true;
}
else if (!isEmptyString(alternateTags)) {
/*
* Parse the alternateTags string for the region.
*/
if (parser == null) {
parser = new LocaleIDParser(alternateTags);
}
String alternateRegion = parser.getCountry();
if (!isEmptyString(alternateRegion)) {
appendTag(
alternateRegion,
tag);
regionAppended = true;
}
}
if (trailing != null && trailing.length() > 1) {
/*
* The current ICU format expects two underscores
* will separate the variant from the preceeding
* parts of the tag, if there is no region.
*/
int separators = 0;
if (trailing.charAt(0) == UNDERSCORE) {
if (trailing.charAt(1) == UNDERSCORE) {
separators = 2;
}
}
else {
separators = 1;
}
if (regionAppended) {
/*
* If we appended a region, we may need to strip
* the extra separator from the variant portion.
*/
if (separators == 2) {
tag.append(trailing.substring(1));
}
else {
tag.append(trailing);
}
}
else {
/*
* If we did not append a region, we may need to add
* an extra separator to the variant portion.
*/
if (separators == 1) {
tag.append(UNDERSCORE);
}
tag.append(trailing);
}
}
return tag.toString();
}
/**
* Create a tag string from the supplied parameters. The lang, script and region
* parameters may be null references.If the lang parameter is an empty string, the
* default value for an unknown language is written to the output buffer.
*
* @param lang The language tag to use.
* @param script The script tag to use.
* @param region The region tag to use.
* @param trailing Any trailing data to append to the new tag.
* @return The new String.
**/
static String createTagString(String lang, String script, String region, String trailing) {
return createTagString(lang, script, region, trailing, null);
}
// --------------------------------
// BCP47/OpenJDK APIs
// --------------------------------
/**
* {@icu} The key for the private use locale extension ('x').
*
* @see #getExtension(char)
* @see Builder#setExtension(char, String)
*
* @draft ICU 4.2
* @provisional This API might change or be removed in a future release.
*/
public static final char PRIVATE_USE_EXTENSION = 'x';
/**
* {@icu} The key for Unicode locale extension ('u').
*
* @see #getExtension(char)
* @see Builder#setExtension(char, String)
*
* @draft ICU 4.2
* @provisional This API might change or be removed in a future release.
*/
public static final char UNICODE_LOCALE_EXTENSION = 'u';
/**
* {@icu} Returns the extension (or private use) value associated with
* the specified key, or null if there is no extension
* associated with the key. To be well-formed, the key must be one
* of [0-9A-Za-z]
. Keys are case-insensitive, so
* for example 'z' and 'Z' represent the same extension.
*
* @param key the extension key
* @return The extension, or null if this locale defines no
* extension for the specified key.
* @throws IllegalArgumentException if key is not well-formed
* @see #PRIVATE_USE_EXTENSION
* @see #UNICODE_LOCALE_EXTENSION
*
* @draft ICU 4.2
* @provisional This API might change or be removed in a future release.
*/
public String getExtension(char key) {
if (!LocaleExtensions.isValidKey(key)) {
throw new IllegalArgumentException("Invalid extension key: " + key);
}
return extensions().getExtensionValue(key);
}
/**
* {@icu} Returns the set of extension keys associated with this locale, or the
* empty set if it has no extensions. The returned set is unmodifiable.
* The keys will all be lower-case.
*
* @return the set of extension keys, or the empty set if this locale has
* no extensions
* @draft ICU 4.2
* @provisional This API might change or be removed in a future release.
*/
public Setkey
is null
*
* @draft ICU 4.4
* @provisional This API might change or be removed in a future release.
*/
public String getUnicodeLocaleType(String key) {
if (!LocaleExtensions.isValidUnicodeLocaleKey(key)) {
throw new IllegalArgumentException("Invalid Unicode locale key: " + key);
}
return extensions().getUnicodeLocaleType(key);
}
/**
* {@icu} Returns the set of Unicode locale keys defined by this locale, or the empty set if
* this locale has none. The returned set is immutable. Keys are all lower case.
*
* @return The set of Unicode locale keys, or the empty set if this locale has
* no Unicode locale keywords.
*
* @draft ICU 4.4
* @provisional This API might change or be removed in a future release.
*/
public SetULocale
has a language, script, country, or
* variant that does not satisfy the IETF BCP 47 language tag
* syntax requirements, this method handles these fields as
* described below:
*
*
*
*
*
* [0-9a-zA-Z]{1,8}
* (for example "WIN" or "Oracle_JDK_Standard_Edition"), the first
* ill-formed sub-segment and all following will be appended to
* the private use subtag. The first appended subtag will be
* "lvariant", followed by the sub-segments in order, separated by
* hyphen. For example, "x-lvariant-WIN",
* "Oracle-x-lvariant-JDK-Standard-Edition".
*
* [0-9a-zA-Z]{1,8}
, the variant will be truncated
* and the problematic sub-segment and all following sub-segments
* will be omitted. If the remainder is non-empty, it will be
* emitted as a private use subtag as above (even if the remainder
* turns out to be well-formed). For example,
* "Solaris_isjustthecoolestthing" is emitted as
* "x-lvariant-Solaris", not as "solaris".
* new Locale("xx", "YY").toLanguageTag();
*
* will return "xx-YY", but the language subtag "xx" and the
* region subtag "YY" are invalid because they are not registered
* in the IANA Language Subtag Registry.
*
* @return a BCP47 language tag representing the locale
* @see #forLanguageTag(String)
*
* @draft ICU 4.2
* @provisional This API might change or be removed in a future release.
*/
public String toLanguageTag() {
BaseLocale base = base();
LocaleExtensions exts = extensions();
if (base.getVariant().equalsIgnoreCase("POSIX")) {
// special handling for variant POSIX
base = BaseLocale.getInstance(base.getLanguage(), base.getScript(), base.getRegion(), "");
if (exts.getUnicodeLocaleType("va") == null) {
// add va-posix
InternalLocaleBuilder ilocbld = new InternalLocaleBuilder();
try {
ilocbld.setLocale(BaseLocale.ROOT, exts);
ilocbld.setUnicodeLocaleKeyword("va", "posix");
exts = ilocbld.getLocaleExtensions();
} catch (LocaleSyntaxException e) {
// this should not happen
throw new RuntimeException(e);
}
}
}
LanguageTag tag = LanguageTag.parseLocale(base, exts);
StringBuilder buf = new StringBuilder();
String subtag = tag.getLanguage();
if (subtag.length() > 0) {
buf.append(LanguageTag.canonicalizeLanguage(subtag));
}
subtag = tag.getScript();
if (subtag.length() > 0) {
buf.append(LanguageTag.SEP);
buf.append(LanguageTag.canonicalizeScript(subtag));
}
subtag = tag.getRegion();
if (subtag.length() > 0) {
buf.append(LanguageTag.SEP);
buf.append(LanguageTag.canonicalizeRegion(subtag));
}
List
*
*
* ULocale loc;
* loc = ULocale.forLanguageTag("en-US-x-lvariant-icu4j);
* loc.getVariant(); // returns "ICU4J"
* loc.getExtension('x'); // returns null
*
* loc = Locale.forLanguageTag("de-icu4j-x-URP-lvariant-Abc-Def");
* loc.getVariant(); // returns "ICU4J_ABC_DEF"
* loc.getExtension('x'); // returns "urp"
*
*
*
* ULocale.forLanguageTag("ar-aao").getLanguage(); // returns "aao"
* ULocale.forLanguageTag("en-abc-def-us").toString(); // returns "abc_US"
*
*
*
*
*
*
*
* grandfathered tag modern replacement
* art-lojban jbo
* i-ami ami
* i-bnn bnn
* i-hak hak
* i-klingon tlh
* i-lux lb
* i-navajo nv
* i-pwn pwn
* i-tao tao
* i-tay tay
* i-tsu tsu
* no-bok nb
* no-nyn nn
* sgn-BE-FR sfb
* sgn-BE-NL vgt
* sgn-CH-DE sgg
* zh-guoyu cmn
* zh-hakka hak
* zh-min-nan nan
*
* zh-xiang hsn
*
*
*
*
* grandfathered tag converts to
* cel-gaulish xtg-x-cel-gaulish
* en-GB-oed en-GB-x-oed
* i-default en-x-i-default
* i-enochian und-x-i-enochian
* i-mingo see-x-i-mingo
*
* zh-min nan-x-zh-min toLanguageTag
* and forLanguageTag
will round-trip.
*
* @param languageTag the language tag
* @return The locale that best represents the language tag.
* @throws NullPointerException if languageTag
is null
* @see #toLanguageTag()
* @see ULocale.Builder#setLanguageTag(String)
*
* @draft ICU 4.2
* @provisional This API might change or be removed in a future release.
*/
public static ULocale forLanguageTag(String languageTag) {
LanguageTag tag = LanguageTag.parse(languageTag, null);
InternalLocaleBuilder bldr = new InternalLocaleBuilder();
bldr.setLanguageTag(tag);
return getInstance(bldr.getBaseLocale(), bldr.getLocaleExtensions());
}
/**
* Builder
is used to build instances of ULocale
* from values configured by the setters. Unlike the ULocale
* constructors, the Builder
checks if a value configured by a
* setter satisfies the syntax requirements defined by the ULocale
* class. A ULocale
object created by a Builder
is
* well-formed and can be transformed to a well-formed IETF BCP 47 language tag
* without losing information.
*
* ULocale
class does not provide any
* syntactic restrictions on variant, while BCP 47 requires each variant
* subtag to be 5 to 8 alphanumerics or a single numeric followed by 3
* alphanumerics. The method setVariant
throws
* IllformedLocaleException
for a variant that does not satisfy
* this restriction. If it is necessary to support such a variant, use a
* ULocale constructor. However, keep in mind that a ULocale
* object created this way might lose the variant information when
* transformed to a BCP 47 language tag.
*
* Locale
object
* with the Builder
.
*
*
*
*
* ULocale aLocale = new Builder().setLanguage("sr").setScript("Latn").setRegion("RS").build();
*
* clear()
resets all
* fields to their default values.
*
* @see ULocale#toLanguageTag()
*
* @draft ICU 4.2
* @provisional This API might change or be removed in a future release.
*/
public static final class Builder {
private final InternalLocaleBuilder _locbld;
/**
* Constructs an empty Builder. The default value of all
* fields, extensions, and private use information is the
* empty string.
*
* @draft ICU 4.2
* @provisional This API might change or be removed in a future release.
*/
public Builder() {
_locbld = new InternalLocaleBuilder();
}
/**
* Resets the Builder
to match the provided
* locale
. Existing state is discarded.
*
* IllformedLocaleException
to be thrown.
*
* @param locale the locale
* @return This builder.
* @throws IllformedLocaleException if locale
has
* any ill-formed fields.
* @throws NullPointerException if locale
is null.
*
* @draft ICU 4.2
* @provisional This API might change or be removed in a future release.
*/
public Builder setLocale(ULocale locale) {
try {
_locbld.setLocale(locale.base(), locale.extensions());
} catch (LocaleSyntaxException e) {
throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
}
return this;
}
/**
* Resets the Builder to match the provided IETF BCP 47
* language tag. Discards the existing state. Null and the
* empty string cause the builder to be reset, like {@link
* #clear}. Grandfathered tags (see {@link
* ULocale#forLanguageTag}) are converted to their canonical
* form before being processed. Otherwise, the language tag
* must be well-formed (see {@link ULocale}) or an exception is
* thrown (unlike ULocale.forLanguageTag
, which
* just discards ill-formed and following portions of the
* tag).
*
* @param languageTag the language tag
* @return This builder.
* @throws IllformedLocaleException if languageTag
is ill-formed
* @see ULocale#forLanguageTag(String)
*
* @draft ICU 4.2
* @provisional This API might change or be removed in a future release.
*/
public Builder setLanguageTag(String languageTag) {
ParseStatus sts = new ParseStatus();
LanguageTag tag = LanguageTag.parse(languageTag, sts);
if (sts.isError()) {
throw new IllformedLocaleException(sts.getErrorMessage(), sts.getErrorIndex());
}
_locbld.setLanguageTag(tag);
return this;
}
/**
* Sets the language. If language
is the empty string or
* null, the language in this Builder
is removed. Otherwise,
* the language must be well-formed
* or an exception is thrown.
*
* language
is ill-formed
*
* @draft ICU 4.2
* @provisional This API might change or be removed in a future release.
*/
public Builder setLanguage(String language) {
try {
_locbld.setLanguage(language);
} catch (LocaleSyntaxException e) {
throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
}
return this;
}
/**
* Sets the script. If script
is null or the empty string,
* the script in this Builder
is removed.
* Otherwise, the script must be well-formed or an exception is thrown.
*
* script
is ill-formed
*
* @draft ICU 4.2
* @provisional This API might change or be removed in a future release.
*/
public Builder setScript(String script) {
try {
_locbld.setScript(script);
} catch (LocaleSyntaxException e) {
throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
}
return this;
}
/**
* Sets the region. If region is null or the empty string, the region
* in this Builder
is removed. Otherwise,
* the region must be well-formed or an exception is thrown.
*
* Locale
created by the
* Builder
is always normalized to upper case.
*
* @param region the region
* @return This builder.
* @throws IllformedLocaleException if region
is ill-formed
*
* @draft ICU 4.2
* @provisional This API might change or be removed in a future release.
*/
public Builder setRegion(String region) {
try {
_locbld.setRegion(region);
} catch (LocaleSyntaxException e) {
throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
}
return this;
}
/**
* Sets the variant. If variant is null or the empty string, the
* variant in this Builder
is removed. Otherwise, it
* must consist of one or more well-formed subtags, or an exception is thrown.
*
* variant
* satisfies the IETF BCP 47 variant subtag's syntax requirements,
* and normalizes the value to lowercase letters. However,
* the ULocale
class does not impose any syntactic
* restriction on variant. To set such a variant,
* use a ULocale constructor.
*
* @param variant the variant
* @return This builder.
* @throws IllformedLocaleException if variant
is ill-formed
*
* @draft ICU 4.2
* @provisional This API might change or be removed in a future release.
*/
public Builder setVariant(String variant) {
try {
_locbld.setVariant(variant);
} catch (LocaleSyntaxException e) {
throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
}
return this;
}
/**
* Sets the extension for the given key. If the value is null or the
* empty string, the extension is removed. Otherwise, the extension
* must be well-formed or an exception is thrown.
*
* key
is illegal
* or value
is ill-formed
* @see #setUnicodeLocaleKeyword(String, String)
*
* @draft ICU 4.2
* @provisional This API might change or be removed in a future release.
*/
public Builder setExtension(char key, String value) {
try {
_locbld.setExtension(key, value);
} catch (LocaleSyntaxException e) {
throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
}
return this;
}
/**
* Sets the Unicode locale keyword type for the given key. If the type
* is null, the Unicode keyword is removed. Otherwise, the key must be
* non-null and both key and type must be well-formed or an exception
* is thrown.
*
* key
or type
* is ill-formed
* @throws NullPointerException if key
is null
* @see #setExtension(char, String)
*
* @draft ICU 4.4
* @provisional This API might change or be removed in a future release.
*/
public Builder setUnicodeLocaleKeyword(String key, String type) {
try {
_locbld.setUnicodeLocaleKeyword(key, type);
} catch (LocaleSyntaxException e) {
throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
}
return this;
}
/**
* Adds a unicode locale attribute, if not already present, otherwise
* has no effect. The attribute must not be null and must be well-formed
* or an exception is thrown.
*
* @param attribute the attribute
* @return This builder.
* @throws NullPointerException if attribute
is null
* @throws IllformedLocaleException if attribute
is ill-formed
* @see #setExtension(char, String)
*
* @draft ICU 4.6
* @provisional This API might change or be removed in a future release.
*/
public Builder addUnicodeLocaleAttribute(String attribute) {
try {
_locbld.addUnicodeLocaleAttribute(attribute);
} catch (LocaleSyntaxException e) {
throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
}
return this;
}
/**
* Removes a unicode locale attribute, if present, otherwise has no
* effect. The attribute must not be null and must be well-formed
* or an exception is thrown.
*
* attribute
is null
* @throws IllformedLocaleException if attribute
is ill-formed
* @see #setExtension(char, String)
*
* @draft ICU 4.6
* @provisional This API might change or be removed in a future release.
*/
public Builder removeUnicodeLocaleAttribute(String attribute) {
try {
_locbld.removeUnicodeLocaleAttribute(attribute);
} catch (LocaleSyntaxException e) {
throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
}
return this;
}
/**
* Resets the builder to its initial, empty state.
*
* @return this builder
*
* @draft ICU 4.2
* @provisional This API might change or be removed in a future release.
*/
public Builder clear() {
_locbld.clear();
return this;
}
/**
* Resets the extensions to their initial, empty state.
* Language, script, region and variant are unchanged.
*
* @return this builder
* @see #setExtension(char, String)
*
* @draft ICU 4.2
* @provisional This API might change or be removed in a future release.
*/
public Builder clearExtensions() {
_locbld.clearExtensions();
return this;
}
/**
* Returns an instance of ULocale
created from the fields set
* on this builder.
*
* @return a new Locale
*
* @draft ICU 4.4
* @provisional This API might change or be removed in a future release.
*/
public ULocale build() {
return getInstance(_locbld.getBaseLocale(), _locbld.getLocaleExtensions());
}
}
private static ULocale getInstance(BaseLocale base, LocaleExtensions exts) {
String id = lscvToID(base.getLanguage(), base.getScript(), base.getRegion(),
base.getVariant());
Set