Java 17 Recipes
-2. Creating and Working with Locales
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
11-2. Creating and Working with Locales
Problem You want to display numbers, dates, and time in a user-friendly way that conforms to your customers’ language and cultural expectations. Solution The display format for numbers, dates, and time varies across the world and depends on your user’s language and cultural region. Additionally, text collation rules vary by language. The java.util.Locale final class represents a specific language and region of the world. By determining and using your customer’s locale, you can apply that locale to a variety of format classes, which can create user-visible data in expected forms. Classes that use Locale instances to modify their behavior for a particular language or region are called locale-sensitive classes. You can learn more about locale-sensitive classes in Chapter 4 . That chapter shows you how to use Locale instances in the NumberFormat and DateFormat classes. In this recipe, however, you learn different options for creating these Locale instances. You can create a Locale instance in any of the following ways. • Use the Locale.Builder class to configure and build a Locale object. • Use the static Locale.forLanguageTag() method. • Use the Locale constructors to create an object. • Use preconfigured static Locale objects. The Java Locale.Builder class has setter methods to create locales that can be transformed into well-formed Best Common Practices (BCP) 47 language tags. The “How It Works” section describes the BCP 47 standard in more detail. For now, you should simply understand that a Builder creates Locale instances that comply with that standard. The following code snippet from the org.java17recipes.chapter11.recipe11_02 .Recipe11_2 class demonstrates how to create Builder and Locale instances. You create locales in locale-sensitive classes to produce culturally correct display formats. Chapter 11 UniCode, internationalization, and CUrrenCy Codes 402 private static final long number = 123456789L; private static final Date now = new Date(); public static void main(String[] args) { Recipe11_2 app = new Recipe11_2(); app.run(); } public void run() { createFromBuilder(); createFromLanguageTag(); createFromConstructor(); createFromStatics(); } private void createFromBuilder() { System.out.printf("Creating from Builder...\n\n"); String[][] langRegions = {{"fr", "FR"}, {"ja", "JP"}, {"en", "US"}}; Builder builder = new Builder(); Locale l = null; NumberFormat nf = null; DateFormat df = null; for (String[] lr: langRegions) { builder.clear(); builder.setLanguage(lr[0]).setRegion(lr[1]); l = builder.build(); nf = NumberFormat.getInstance(l); df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat. LONG, l); System.out.printf("Locale: %s\nNumber: %s\nDate: %s\n\n", l.getDisplayName(), nf.format(number), df.format(now)); } Chapter 11 UniCode, internationalization, and CUrrenCy Codes 403 The previous code prints the following to the standard console. Creating from Builder... Locale: French (France) Number: 123 456 789 Date: 14 septembre 2016 00:08:06 PDT Locale: Japanese (Japan) Number: 123,456,789 Date: 2016/09/14 0:08:06 PDT Locale: English (United States) Number: 123,456,789 Date: September 14, 2016 12:08:06 AM PDT Another way to create Locale instances is by using the static Locale.forLanguageTag() method. This method allows you to use BCP 47 language tag arguments. The following code uses the forLanguageTag() method to create three locales from their corresponding language tags. ... System.out.printf("Creating from BCP 47 language tags...\n\n"); String[] bcp47LangTags= {"fr-FR", "ja-JP", "en-US"}; Locale l = null; NumberFormat nf = null; DateFormat df = null; for (String langTag: bcp47LangTags) { l = Locale.forLanguageTag(langTag); nf = NumberFormat.getInstance(l); df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, l); System.out.printf("Locale: %s\nNumber: %s\nDate: %s\n\n", l.getDisplayName(), nf.format(number), df.format(now)); } ... Chapter 11 UniCode, internationalization, and CUrrenCy Codes 404 The output is similar to the results created from the Builder-generated Locale instance. Creating from BCP 47 language tags... Locale: French (France) Number: 123 456 789 Date: 14 septembre 2016 01:07:22 PDT ... You can also use constructors to create instances. The following code shows how to do this. Locale l = new Locale("fr", "FR"); Other constructors allow you to pass fewer or more arguments. The argument parameters can include language, region, and optional variant codes. Finally, the Locale class has many predefined static instances for some commonly used cases. Because the instances are predefined, your code needs to reference only the static instances. For example, the following example shows how to reference existing static instances representing fr-FR, ja-JP, and en-US locales. Locale frenchInFrance = Locale.FRANCE; Locale japaneseInJapan = Locale.JAPAN; Locale englishInUS = Locale.US; Download 3.2 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling