Class ULocale.Builder
- java.lang.Object
-
- com.ibm.icu.util.ULocale.Builder
-
- Enclosing class:
- ULocale
public static final class ULocale.Builder extends Object
Builderis used to build instances ofULocalefrom values configured by the setters. Unlike theULocaleconstructors, theBuilderchecks if a value configured by a setter satisfies the syntax requirements defined by theULocaleclass. AULocaleobject created by aBuilderis well-formed and can be transformed to a well-formed IETF BCP 47 language tag without losing information.Note: The
ULocaleclass 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 methodsetVariantthrowsIllformedLocaleExceptionfor 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 aULocaleobject created this way might lose the variant information when transformed to a BCP 47 language tag.The following example shows how to create a
Localeobject with theBuilder.ULocale aLocale = new Builder().setLanguage("sr").setScript("Latn").setRegion("RS").build();Builders can be reused;
clear()resets all fields to their default values.- See Also:
ULocale.toLanguageTag()- Status:
- Stable ICU 4.2.
-
-
Constructor Summary
Constructors Constructor Description Builder()Constructs an empty Builder.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ULocale.BuilderaddUnicodeLocaleAttribute(String attribute)Adds a unicode locale attribute, if not already present, otherwise has no effect.ULocalebuild()Returns an instance ofULocalecreated from the fields set on this builder.ULocale.Builderclear()Resets the builder to its initial, empty state.ULocale.BuilderclearExtensions()Resets the extensions to their initial, empty state.ULocale.BuilderremoveUnicodeLocaleAttribute(String attribute)Removes a unicode locale attribute, if present, otherwise has no effect.ULocale.BuildersetExtension(char key, String value)Sets the extension for the given key.ULocale.BuildersetLanguage(String language)Sets the language.ULocale.BuildersetLanguageTag(String languageTag)Resets the Builder to match the provided IETF BCP 47 language tag.ULocale.BuildersetLocale(ULocale locale)Resets theBuilderto match the providedlocale.ULocale.BuildersetRegion(String region)Sets the region.ULocale.BuildersetScript(String script)Sets the script.ULocale.BuildersetUnicodeLocaleKeyword(String key, String type)Sets the Unicode locale keyword type for the given key.ULocale.BuildersetVariant(String variant)Sets the variant.
-
-
-
Method Detail
-
setLocale
public ULocale.Builder setLocale(ULocale locale)
Resets theBuilderto match the providedlocale. Existing state is discarded.All fields of the locale must be well-formed, see
Locale.Locales with any ill-formed fields cause
IllformedLocaleExceptionto be thrown.- Parameters:
locale- the locale- Returns:
- This builder.
- Throws:
IllformedLocaleException- iflocalehas any ill-formed fields.NullPointerException- iflocaleis null.- Status:
- Stable ICU 4.2.
-
setLanguageTag
public ULocale.Builder setLanguageTag(String languageTag)
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, likeclear(). Legacy tags (seeULocale.forLanguageTag(java.lang.String)) are converted to their canonical form before being processed. Otherwise, the language tag must be well-formed (seeULocale) or an exception is thrown (unlikeULocale.forLanguageTag, which just discards ill-formed and following portions of the tag).- Parameters:
languageTag- the language tag- Returns:
- This builder.
- Throws:
IllformedLocaleException- iflanguageTagis ill-formed- See Also:
ULocale.forLanguageTag(String)- Status:
- Stable ICU 4.2.
-
setLanguage
public ULocale.Builder setLanguage(String language)
Sets the language. Iflanguageis the empty string or null, the language in thisBuilderis removed. Otherwise, the language must be well-formed or an exception is thrown.The typical language value is a two or three-letter language code as defined in ISO639.
- Parameters:
language- the language- Returns:
- This builder.
- Throws:
IllformedLocaleException- iflanguageis ill-formed- Status:
- Stable ICU 4.2.
-
setScript
public ULocale.Builder setScript(String script)
Sets the script. Ifscriptis null or the empty string, the script in thisBuilderis removed. Otherwise, the script must be well-formed or an exception is thrown.The typical script value is a four-letter script code as defined by ISO 15924.
- Parameters:
script- the script- Returns:
- This builder.
- Throws:
IllformedLocaleException- ifscriptis ill-formed- Status:
- Stable ICU 4.2.
-
setRegion
public ULocale.Builder setRegion(String region)
Sets the region. If region is null or the empty string, the region in thisBuilderis removed. Otherwise, the region must be well-formed or an exception is thrown.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
Localecreated by theBuilderis always normalized to upper case.- Parameters:
region- the region- Returns:
- This builder.
- Throws:
IllformedLocaleException- ifregionis ill-formed- Status:
- Stable ICU 4.2.
-
setVariant
public ULocale.Builder setVariant(String variant)
Sets the variant. If variant is null or the empty string, the variant in thisBuilderis removed. Otherwise, it must consist of one or more well-formed subtags, or an exception is thrown.Note: This method checks if
variantsatisfies the IETF BCP 47 variant subtag's syntax requirements, and normalizes the value to lowercase letters. However, theULocaleclass does not impose any syntactic restriction on variant. To set such a variant, use a ULocale constructor.- Parameters:
variant- the variant- Returns:
- This builder.
- Throws:
IllformedLocaleException- ifvariantis ill-formed- Status:
- Stable ICU 4.2.
-
setExtension
public ULocale.Builder setExtension(char key, String value)
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.Note: The key
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
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.- Parameters:
key- the extension keyvalue- the extension value- Returns:
- This builder.
- Throws:
IllformedLocaleException- ifkeyis illegal orvalueis ill-formed- See Also:
setUnicodeLocaleKeyword(String, String)- Status:
- Stable ICU 4.2.
-
setUnicodeLocaleKeyword
public ULocale.Builder setUnicodeLocaleKeyword(String key, String type)
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.Keys and types are converted to lower case.
Note:Setting the 'u' extension via
setExtension(char, java.lang.String)replaces all Unicode locale keywords with those defined in the extension.- Parameters:
key- the Unicode locale keytype- the Unicode locale type- Returns:
- This builder.
- Throws:
IllformedLocaleException- ifkeyortypeis ill-formedNullPointerException- ifkeyis null- See Also:
setExtension(char, String)- Status:
- Stable ICU 4.4.
-
addUnicodeLocaleAttribute
public ULocale.Builder addUnicodeLocaleAttribute(String attribute)
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.- Parameters:
attribute- the attribute- Returns:
- This builder.
- Throws:
NullPointerException- ifattributeis nullIllformedLocaleException- ifattributeis ill-formed- See Also:
setExtension(char, String)- Status:
- Stable ICU 4.6.
-
removeUnicodeLocaleAttribute
public ULocale.Builder removeUnicodeLocaleAttribute(String attribute)
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 comparison for removal is case-insensitive.
- Parameters:
attribute- the attribute- Returns:
- This builder.
- Throws:
NullPointerException- ifattributeis nullIllformedLocaleException- ifattributeis ill-formed- See Also:
setExtension(char, String)- Status:
- Stable ICU 4.6.
-
clear
public ULocale.Builder clear()
Resets the builder to its initial, empty state.- Returns:
- this builder
- Status:
- Stable ICU 4.2.
-
clearExtensions
public ULocale.Builder clearExtensions()
Resets the extensions to their initial, empty state. Language, script, region and variant are unchanged.- Returns:
- this builder
- See Also:
setExtension(char, String)- Status:
- Stable ICU 4.2.
-
build
public ULocale build()
Returns an instance ofULocalecreated from the fields set on this builder.- Returns:
- a new Locale
- Status:
- Stable ICU 4.4.
-
-