Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet205/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   201   202   203   204   205   206   207   208   ...   245
Bog'liq
Java 17 Recipes

 How It Works
The Locale class gives locale-sensitive classes the context to perform culturally 
appropriate data formatting and parsing. Some of the locale-sensitive classes include the 
following.
• java.text.NumberFormat
• java.text.DateFormat
• java.util.Calendar
A Locale instance identifies a specific language and can be finely tuned to identify 
languages written in a particular script or spoken in a specific world region. It is an 
important and necessary element for creating anything that depends on language or 
regional influences.
Chapter 11 UniCode, internationalization, and CUrrenCy Codes 


405
The Java Locale class is always being enhanced to support modern BCP 47 language 
tags. BCP 47 defines the best common practices for using ISO standards for language, 
region, script, and variant identifiers. Although the existing Locale constructors 
continue to be compatible with prior versions of the Java platform, the constructors 
do not support the additional script tags. For example, only the more recently added 
Locale.Builder class and Locale.forLanguageTag() method support the newer 
functionality that identifies scripts. Because the Locale constructors do not enforce 
strict BCP 47 compliance, you should avoid the constructors in any new code. Instead, 
developers should use the Builder class and the forLanguageTag() method.
A Locale.Builder instance has a variety of setter methods that help you configure it 
to create a valid, BCP 47–compliant Locale instance.
• public Locale.BuildersetLanguage(String language)
• public Locale.BuildersetRegion(String region)
• public Locale.BuildersetScript(String script)
Each of these methods throws a java.util.IllFormedLocaleException if its 
argument is not a well-formed element of the BCP 47 standard. The language parameter 
must be a valid two- or three-letter ISO 639 language identifier. The region parameter must 
be a valid two-letter ISO 3166 region code or a three-digit M.49 United Nations “area” code. 
Finally, the script parameter must be a valid four-letter ISO 15924 script code.
The Builder lets you configure it to create a specific BCP 47–compliant locale. Once 
you set all the configurations, the build() method creates and returns a Locale instance. 
Notice that all the setters can be chained together for a single statement. The Builder 
pattern works by having each configuration method return a reference to the current 
instance, on which further configuration methods may be called.
Locale aLocale = new Builder().setLanguage("fr").setRegion("FR").build();
The BCP 47 document and the standards that comprise it can be found at the 
following locations.
• ISO 3166 (region identifiers): 
www.iso.org/iso-3166-country-
codes.html
• ISO 15924 (script identifiers): 
https://unicode.org/iso15924/
• United Nations M.49 (area identifiers): 
https://unstats.un.org/
unsd/methods/m49/m49.htm
Chapter 11 UniCode, internationalization, and CUrrenCy Codes 


406

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   201   202   203   204   205   206   207   208   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling