1--- 2layout: default 3title: Locale Examples 4nav_order: 1 5parent: Locales and Resources 6--- 7<!-- 8© 2020 and later: Unicode, Inc. and others. 9License & terms of use: http://www.unicode.org/copyright.html 10--> 11 12# Locale Examples 13 14## Locale Currency Conventions 15 16Application programs should not reset the default locale as a way of requesting 17an international object, because resetting default locale affects the other 18programs running in the same process. Use one of the factory methods instead, 19e.g. `Collator::createInstance(Locale)`. 20 21In general, a locale object or locale string is used for specifying the locale. 22Here is an example to specify the Belgium French with Euro currency locale: 23 24**C++** 25 26```c++ 27Locale loc("fr", "BE"); 28Locale loc2("fr_BE"); 29``` 30 31**C** 32 33```c 34const char *loc = "fr_BE"; 35``` 36 37**Java** 38 39```java 40ULocale loc = new ULocale("fr_BE"); 41``` 42 43> :point_right: **Note**: **Java** does **not** support the form `Locale("xx_yy_ZZ")`, 44> instead use the form `Locale("xx","yy","ZZ")`. 45 46## Locale Constants 47 48A `Locale` is the mechanism for identifying the kind of object (`NumberFormat`) that 49you would like to get. The locale is just a mechanism for identifying objects, 50not a container for the objects themselves. For example, the following creates 51various number formatters for the "Germany" locale: 52 53**C++** 54 55```c++ 56UErrorCode status = U_ZERO_ERROR; 57NumberFormat *nf; 58nf = NumberFormat::createInstance(Locale::getGermany(), status); 59delete nf; 60nf = NumberFormat::createCurrencyInstance(Locale::getGermany(), status); 61delete nf; 62nf = NumberFormat::createPercentInstance(Locale::getGermany(), status); 63delete nf; 64``` 65 66**C** 67 68```c 69UErrorCode success = U_ZERO_ERROR; 70UChar *pattern; 71UNumberFormat *nf; 72UParseError *pe; 73nf = unum_open( UNUM_DEFAULT, pattern, 0, "fr_FR", pe, &success ); 74unum_close(nf); 75nf = unum_open( UNUM_CURRENCY, pattern, 0, "fr_FR", pe, &success ); 76unum_close(nf); 77nf = unum_open( UNUM_PERCENT, pattern, 0, "fr_FR", pe, &success ); 78unum_close(nf); 79``` 80 81**Java** 82 83```java 84NumberFormat nf = NumberFormat.getInstance(ULocale.GERMANY); 85NumberFormat currencyInstance = NumberFormat.getCurrencyInstance(ULocale.GERMANY); 86NumberFormat percentInstance = NumberFormat.getPercentInstance(ULocale.GERMANY); 87``` 88 89## Querying Locale 90 91Each class that performs locale-sensitive operations allows you to get all the 92available objects of that type. You can sift through these objects by language, 93country, or variant, and use the display names to present a menu to the user. 94For example, you can create a menu of all the collation objects suitable for a 95given language. For example, the following shows the display name of all 96available locales in English (US): 97 98**C++** 99 100```c++ 101int32_t count; 102const Locale* list = NULL; 103UnicodeString result; 104list = Locale::getAvailableLocales(count); 105for (int i = 0; i < count; i++) { 106 list[i].getDisplayName(Locale::getUS(), result); 107 /* print result */ 108} 109``` 110 111**C** 112 113```c 114int32_t count; 115UChar result[100]; 116int i = 0; 117UErrorCode status = U_ZERO_ERROR; 118count = uloc_countAvailable(); 119for (i = 0; i < count; i++) { 120 uloc_getDisplayName(uloc_getAvailable(i), "en_US", result, 100, &status); 121 /* print result */ 122} 123``` 124 125**Java** 126 127```java 128import com.ibm.icu.util.*; 129public class TestLocale { 130 public void run() { 131 ULocale l[] = ULocale.getAvailableLocales(); 132 int n = l.length; 133 for(int i=0; i<n; ++i) { 134 ULocale locale = l[i]; 135 System.out.println(); 136 System.out.println("The base name of this locale is: " + locale.getBaseName()); 137 System.out.println("Locale's country name: " + locale.getDisplayCountry()); 138 System.out.println("Locale's script name: " + locale.getDisplayScript()); 139 System.out.println("Locale's language: " + locale.getDisplayLanguage()); 140 System.out.println("Locale's variant: " + locale.getDisplayVariant()); 141 } 142 } 143 public static void main(String args[]) { 144 new TestLocale().run(); 145 } 146} 147``` 148