- java.lang.Object
-
- java.util.Locale.Builder
-
- Enclosing class:
- Locale
public static final class Locale.Builder extends Object
Builder
is used to build instances ofLocale
from values configured by the setters. Unlike theLocale
constructors, theBuilder
checks if a value configured by a setter satisfies the syntax requirements defined by theLocale
class. ALocale
object created by aBuilder
is well-formed and can be transformed to a well-formed IETF BCP 47 language tag without losing information.Note: The
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 methodsetVariant
throwsIllformedLocaleException
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 aLocale
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
object with theBuilder
.Locale aLocale = new Builder().setLanguage("sr").setScript("Latn").setRegion("RS").build();
Builders can be reused;
clear()
resets all fields to their default values.- Since:
- 1.7
- See Also:
Locale.forLanguageTag(java.lang.String)
-
-
Constructor Summary
Constructors Constructor and Description Locale.Builder()
Constructs an empty Builder.
-
Method Summary
Methods Modifier and Type Method and Description Locale.Builder
addUnicodeLocaleAttribute(String attribute)
Adds a unicode locale attribute, if not already present, otherwise has no effect.Locale
build()
Returns an instance ofLocale
created from the fields set on this builder.Locale.Builder
clear()
Resets the builder to its initial, empty state.Locale.Builder
clearExtensions()
Resets the extensions to their initial, empty state.Locale.Builder
removeUnicodeLocaleAttribute(String attribute)
Removes a unicode locale attribute, if present, otherwise has no effect.Locale.Builder
setExtension(char key, String value)
Sets the extension for the given key.Locale.Builder
setLanguage(String language)
Sets the language.Locale.Builder
setLanguageTag(String languageTag)
Resets the Builder to match the provided IETF BCP 47 language tag.Locale.Builder
setLocale(Locale locale)
Resets theBuilder
to match the providedlocale
.Locale.Builder
setRegion(String region)
Sets the region.Locale.Builder
setScript(String script)
Sets the script.Locale.Builder
setUnicodeLocaleKeyword(String key, String type)
Sets the Unicode locale keyword type for the given key.Locale.Builder
setVariant(String variant)
Sets the variant.
-
-
-
Constructor Detail
-
Locale.Builder
public Locale.Builder()
Constructs an empty Builder. The default value of all fields, extensions, and private use information is the empty string.
-
-
Method Detail
-
setLocale
public Locale.Builder setLocale(Locale locale)
Resets theBuilder
to 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
IllformedLocaleException
to be thrown, except for the following three cases which are accepted for compatibility reasons:- Locale("ja", "JP", "JP") is treated as "ja-JP-u-ca-japanese"
- Locale("th", "TH", "TH") is treated as "th-TH-u-nu-thai"
- Locale("no", "NO", "NY") is treated as "nn-NO"
- Parameters:
locale
- the locale- Returns:
- This builder.
- Throws:
IllformedLocaleException
- iflocale
has any ill-formed fields.NullPointerException
- iflocale
is null.
-
setLanguageTag
public Locale.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()
. Grandfathered tags (seeLocale.forLanguageTag(java.lang.String)
) are converted to their canonical form before being processed. Otherwise, the language tag must be well-formed (seeLocale
) or an exception is thrown (unlikeLocale.forLanguageTag
, which just discards ill-formed and following portions of the tag).- Parameters:
languageTag
- the language tag- Returns:
- This builder.
- Throws:
IllformedLocaleException
- iflanguageTag
is ill-formed- See Also:
Locale.forLanguageTag(String)
-
setLanguage
public Locale.Builder setLanguage(String language)
Sets the language. Iflanguage
is the empty string or null, the language in thisBuilder
is 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
- iflanguage
is ill-formed
-
setScript
public Locale.Builder setScript(String script)
Sets the script. Ifscript
is null or the empty string, the script in thisBuilder
is 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
- ifscript
is ill-formed
-
setRegion
public Locale.Builder setRegion(String region)
Sets the region. If region is null or the empty string, the region in thisBuilder
is 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 theBuilder
is always normalized to upper case.- Parameters:
region
- the region- Returns:
- This builder.
- Throws:
IllformedLocaleException
- ifregion
is ill-formed
-
setVariant
public Locale.Builder setVariant(String variant)
Sets the variant. If variant is null or the empty string, the variant in thisBuilder
is removed. Otherwise, it must consist of one or more well-formed subtags, or an exception is thrown.Note: This method checks if
variant
satisfies the IETF BCP 47 variant subtag's syntax requirements, and normalizes the value to lowercase letters. However, theLocale
class does not impose any syntactic restriction on variant, and the variant value inLocale
is case sensitive. To set such a variant, use a Locale constructor.- Parameters:
variant
- the variant- Returns:
- This builder.
- Throws:
IllformedLocaleException
- ifvariant
is ill-formed
-
setExtension
public Locale.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
- ifkey
is illegal orvalue
is ill-formed- See Also:
setUnicodeLocaleKeyword(String, String)
-
setUnicodeLocaleKeyword
public Locale.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
- ifkey
ortype
is ill-formedNullPointerException
- ifkey
is null- See Also:
setExtension(char, String)
-
addUnicodeLocaleAttribute
public Locale.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
- ifattribute
is nullIllformedLocaleException
- ifattribute
is ill-formed- See Also:
setExtension(char, String)
-
removeUnicodeLocaleAttribute
public Locale.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 comparision for removal is case-insensitive.
- Parameters:
attribute
- the attribute- Returns:
- This builder.
- Throws:
NullPointerException
- ifattribute
is nullIllformedLocaleException
- ifattribute
is ill-formed- See Also:
setExtension(char, String)
-
clear
public Locale.Builder clear()
Resets the builder to its initial, empty state.- Returns:
- This builder.
-
clearExtensions
public Locale.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)
-
build
public Locale build()
Returns an instance ofLocale
created from the fields set on this builder.This applies the conversions listed in
Locale.forLanguageTag(java.lang.String)
when constructing a Locale. (Grandfathered tags are handled insetLanguageTag(java.lang.String)
.)- Returns:
- A Locale.
-
-
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 11/06/2005 gemaakt, de laatste keer de 04/03/2020 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/java-api-rf-java/util/locale.builder.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.