public static final class Locale.Builder extends Object
Builderis used to build instances of
Localefrom values configured by the setters. Unlike the
Builderchecks if a value configured by a setter satisfies the syntax requirements defined by the
Localeobject created by a
Builderis well-formed and can be transformed to a well-formed IETF BCP 47 language tag without losing information.
Locale 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
IllformedLocaleException for a variant that does not satisfy
this restriction. If it is necessary to support such a variant, use a
Locale constructor. However, keep in mind that a
object created this way might lose the variant information when
transformed to a BCP 47 language tag.
The following example shows how to create a
Locale aLocale = new Builder().setLanguage("sr").setScript("Latn").setRegion("RS").build();
Builders can be reused;
clear() resets all
fields to their default values.
|Constructor and Description|
Constructs an empty Builder.
|Modifier and Type||Method and Description|
Adds a unicode locale attribute, if not already present, otherwise has no effect.
Returns an instance of
Resets the builder to its initial, empty state.
Resets the extensions to their initial, empty state.
Removes a unicode locale attribute, if present, otherwise has no effect.
Sets the extension for the given key.
Sets the language.
Resets the Builder to match the provided IETF BCP 47 language tag.
Sets the region.
Sets the script.
Sets the Unicode locale keyword type for the given key.
Sets the variant.
public Locale.Builder setLocale(Locale locale)
Builderto match the provided
locale. Existing state is discarded.
All fields of the locale must be well-formed, see
Locales with any ill-formed fields cause
IllformedLocaleException to be thrown, except for the
following three cases which are accepted for compatibility
public Locale.Builder setLanguageTag(String languageTag)
clear(). Grandfathered tags (see
Locale.forLanguageTag(java.lang.String)) are converted to their canonical form before being processed. Otherwise, the language tag must be well-formed (see
Locale) or an exception is thrown (unlike
Locale.forLanguageTag, which just discards ill-formed and following portions of the tag).
public Locale.Builder setLanguage(String language)
languageis the empty string or null, the language in this
Builderis 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.
language- the language
public Locale.Builder setScript(String script)
scriptis null or the empty string, the script in this
Builderis 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.
script- the script
public Locale.Builder setRegion(String region)
Builderis 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
Locale created by the
Builder is always normalized to upper case.
region- the region
public Locale.Builder setVariant(String variant)
Builderis removed. Otherwise, it must consist of one or more well-formed subtags, or an exception is thrown.
Note: This method checks if
satisfies the IETF BCP 47 variant subtag's syntax requirements,
and normalizes the value to lowercase letters. However,
Locale class does not impose any syntactic
restriction on variant, and the variant value in
Locale is case sensitive. To set such a variant,
use a Locale constructor.
variant- the variant
public Locale.Builder setExtension(char key, String value)
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.
public Locale.Builder setUnicodeLocaleKeyword(String key, String type)
Keys and types are converted to lower case.
Note:Setting the 'u' extension via
replaces all Unicode locale keywords with those defined in the
public Locale.Builder addUnicodeLocaleAttribute(String attribute)
public Locale.Builder removeUnicodeLocaleAttribute(String attribute)
Attribute comparision for removal is case-insensitive.
public Locale.Builder clear()
public Locale.Builder clearExtensions()
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2020, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.