]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/unicode/locid.h
2 ******************************************************************************
4 * Copyright (C) 1996-2003, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
11 * Created by: Helena Shih
13 * Modification History:
15 * Date Name Description
16 * 02/11/97 aliu Changed gLocPath to fgLocPath and added methods to
18 * 04/02/97 aliu Made operator!= inline; fixed return value of getName().
19 * 04/15/97 aliu Cleanup for AIX/Win32.
20 * 04/24/97 aliu Numerous changes per code review.
21 * 08/18/98 stephen Added tokenizeString(),changed getDisplayName()
22 * 09/08/98 stephen Moved definition of kEmptyString for Mac Port
23 * 11/09/99 weiv Added const char * getName() const;
24 * 04/12/00 srl removing unicodestring api's and cached hash code
25 * 08/10/01 grhoten Change the static Locales to accessor functions
26 ******************************************************************************
32 #include "unicode/utypes.h"
33 #include "unicode/uobject.h"
34 #include "unicode/unistr.h"
35 #include "unicode/putil.h"
36 #include "unicode/uloc.h"
40 * \brief C++ API: Locale ID object.
44 * A <code>Locale</code> object represents a specific geographical, political,
45 * or cultural region. An operation that requires a <code>Locale</code> to perform
46 * its task is called <em>locale-sensitive</em> and uses the <code>Locale</code>
47 * to tailor information for the user. For example, displaying a number
48 * is a locale-sensitive operation--the number should be formatted
49 * according to the customs/conventions of the user's native country,
52 * The Locale class is not suitable for subclassing.
55 * You create a <code>Locale</code> object using the constructor in
59 * . Locale( const char* language,
60 * . const char* country,
61 * . const char* variant);
64 * The first argument to the constructors is a valid <STRONG>ISO
65 * Language Code.</STRONG> These codes are the lower-case two-letter
66 * codes as defined by ISO-639.
67 * You can find a full list of these codes at a number of sites, such as:
68 * <BR><a href ="http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt">
69 * http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt</a>
72 * The second argument to the constructors is a valid <STRONG>ISO Country
73 * Code.</STRONG> These codes are the upper-case two-letter codes
74 * as defined by ISO-3166.
75 * You can find a full list of these codes at a number of sites, such as:
76 * <BR><a href="http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html">
77 * http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html</a>
80 * The third constructor requires a third argument--the <STRONG>Variant.</STRONG>
81 * The Variant codes are vendor and browser-specific.
82 * For example, use WIN for Windows, MAC for Macintosh, and POSIX for POSIX.
83 * Where there are two variants, separate them with an underscore, and
84 * put the most important one first. For
85 * example, a Traditional Spanish collation might be referenced, with
86 * "ES", "ES", "Traditional_WIN".
89 * Because a <code>Locale</code> object is just an identifier for a region,
90 * no validity check is performed when you construct a <code>Locale</code>.
91 * If you want to see whether particular resources are available for the
92 * <code>Locale</code> you construct, you must query those resources. For
93 * example, ask the <code>NumberFormat</code> for the locales it supports
94 * using its <code>getAvailableLocales</code> method.
95 * <BR><STRONG>Note:</STRONG> When you ask for a resource for a particular
96 * locale, you get back the best available match, not necessarily
97 * precisely what you asked for. For more information, look at
98 * <code>ResourceBundle</code>.
101 * The <code>Locale</code> class provides a number of convenient constants
102 * that you can use to create <code>Locale</code> objects for commonly used
103 * locales. For example, the following refers to a <code>Locale</code> object
104 * for the United States:
112 * Once you've created a <code>Locale</code> you can query it for information about
113 * itself. Use <code>getCountry</code> to get the ISO Country Code and
114 * <code>getLanguage</code> to get the ISO Language Code. You can
115 * use <code>getDisplayCountry</code> to get the
116 * name of the country suitable for displaying to the user. Similarly,
117 * you can use <code>getDisplayLanguage</code> to get the name of
118 * the language suitable for displaying to the user. Interestingly,
119 * the <code>getDisplayXXX</code> methods are themselves locale-sensitive
120 * and have two versions: one that uses the default locale and one
121 * that takes a locale as an argument and displays the name or country in
122 * a language appropriate to that locale.
125 * The TIFC provides a number of classes that perform locale-sensitive
126 * operations. For example, the <code>NumberFormat</code> class formats
127 * numbers, currency, or percentages in a locale-sensitive manner. Classes
128 * such as <code>NumberFormat</code> have a number of convenience methods
129 * for creating a default object of that type. For example, the
130 * <code>NumberFormat</code> class provides these three convenience methods
131 * for creating a default <code>NumberFormat</code> object:
134 * UErrorCode success = U_ZERO_ERROR;
138 * nf = NumberFormat::createInstance( success ); delete nf;
139 * nf = NumberFormat::createCurrencyInstance( success ); delete nf;
140 * nf = NumberFormat::createPercentInstance( success ); delete nf;
143 * Each of these methods has two variants; one with an explicit locale
144 * and one without; the latter using the default locale.
147 * nf = NumberFormat::createInstance( myLocale, success ); delete nf;
148 * nf = NumberFormat::createCurrencyInstance( myLocale, success ); delete nf;
149 * nf = NumberFormat::createPercentInstance( myLocale, success ); delete nf;
152 * A <code>Locale</code> is the mechanism for identifying the kind of object
153 * (<code>NumberFormat</code>) that you would like to get. The locale is
154 * <STRONG>just</STRONG> a mechanism for identifying objects,
155 * <STRONG>not</STRONG> a container for the objects themselves.
158 * Each class that performs locale-sensitive operations allows you
159 * to get all the available objects of that type. You can sift
160 * through these objects by language, country, or variant,
161 * and use the display names to present a menu to the user.
162 * For example, you can create a menu of all the collation objects
163 * suitable for a given language. Such classes implement these
164 * three class methods:
167 * static Locale* getAvailableLocales(int32_t& numLocales)
168 * static UnicodeString& getDisplayName(const Locale& objectLocale,
169 * const Locale& displayLocale,
170 * UnicodeString& displayName)
171 * static UnicodeString& getDisplayName(const Locale& objectLocale,
172 * UnicodeString& displayName)
177 * @see ResourceBundle
180 class U_COMMON_API Locale
: public UObject
{
182 /** Useful constant for this language. @stable ICU 2.0 */
183 static const Locale
&getEnglish(void);
184 /** Useful constant for this language. @stable ICU 2.0 */
185 static const Locale
&getFrench(void);
186 /** Useful constant for this language. @stable ICU 2.0 */
187 static const Locale
&getGerman(void);
188 /** Useful constant for this language. @stable ICU 2.0 */
189 static const Locale
&getItalian(void);
190 /** Useful constant for this language. @stable ICU 2.0 */
191 static const Locale
&getJapanese(void);
192 /** Useful constant for this language. @stable ICU 2.0 */
193 static const Locale
&getKorean(void);
194 /** Useful constant for this language. @stable ICU 2.0 */
195 static const Locale
&getChinese(void);
196 /** Useful constant for this language. @stable ICU 2.0 */
197 static const Locale
&getSimplifiedChinese(void);
198 /** Useful constant for this language. @stable ICU 2.0 */
199 static const Locale
&getTraditionalChinese(void);
201 /** Useful constant for this country/region. @stable ICU 2.0 */
202 static const Locale
&getFrance(void);
203 /** Useful constant for this country/region. @stable ICU 2.0 */
204 static const Locale
&getGermany(void);
205 /** Useful constant for this country/region. @stable ICU 2.0 */
206 static const Locale
&getItaly(void);
207 /** Useful constant for this country/region. @stable ICU 2.0 */
208 static const Locale
&getJapan(void);
209 /** Useful constant for this country/region. @stable ICU 2.0 */
210 static const Locale
&getKorea(void);
211 /** Useful constant for this country/region. @stable ICU 2.0 */
212 static const Locale
&getChina(void);
213 /** Useful constant for this country/region. @stable ICU 2.0 */
214 static const Locale
&getPRC(void);
215 /** Useful constant for this country/region. @stable ICU 2.0 */
216 static const Locale
&getTaiwan(void);
217 /** Useful constant for this country/region. @stable ICU 2.0 */
218 static const Locale
&getUK(void);
219 /** Useful constant for this country/region. @stable ICU 2.0 */
220 static const Locale
&getUS(void);
221 /** Useful constant for this country/region. @stable ICU 2.0 */
222 static const Locale
&getCanada(void);
223 /** Useful constant for this country/region. @stable ICU 2.0 */
224 static const Locale
&getCanadaFrench(void);
228 * Construct a default locale object, a Locale for the default locale ID.
231 * @see uloc_getDefault
237 * Construct a locale from language, country, variant.
238 * If an error occurs, then the constructed object will be "bogus"
239 * (isBogus() will return TRUE).
241 * @param language Lowercase two-letter or three-letter ISO-639 code.
242 * This parameter can instead be an ICU style C locale (e.g. "en_US"),
243 * but the other parameters must not be used.
244 * This parameter can be NULL; if so,
245 * the locale is initialized to match the current default locale.
246 * (This is the same as using the default constructor.)
247 * Please note: The Java Locale class does NOT accept the form
248 * 'new Locale("en_US")' but only 'new Locale("en","US")'
250 * @param country Uppercase two-letter ISO-3166 code. (optional)
251 * @param variant Uppercase vendor and browser specific code. See class
252 * description. (optional)
255 * @see uloc_getDefault
258 Locale( const char * language
,
259 const char * country
= 0,
260 const char * variant
= 0);
263 * Initializes a Locale object from another Locale object.
265 * @param other The Locale object being copied in.
268 Locale(const Locale
& other
);
278 * Replaces the entire contents of *this with the specified value.
280 * @param other The Locale object being copied in.
284 Locale
& operator=(const Locale
& other
);
287 * Checks if two locale keys are the same.
289 * @param other The locale key object to be compared with this.
290 * @return True if the two locale keys are the same, false otherwise.
293 UBool
operator==(const Locale
& other
) const;
296 * Checks if two locale keys are not the same.
298 * @param other The locale key object to be compared with this.
299 * @return True if the two locale keys are not the same, false
303 UBool
operator!=(const Locale
& other
) const;
306 * Common methods of getting the current default Locale. Used for the
307 * presentation: menus, dialogs, etc. Generally set once when your applet or
308 * application is initialized, then never reset. (If you do reset the
309 * default locale, you probably want to reload your GUI, so that the change
310 * is reflected in your interface.)
312 * More advanced programs will allow users to use different locales for
313 * different fields, e.g. in a spreadsheet.
315 * Note that the initial setting will match the host system.
316 * @return a reference to the Locale object for the default locale ID
320 static const Locale
& getDefault(void);
323 * Sets the default. Normally set once at the beginning of a process,
325 * setDefault() only changes ICU's default locale ID, <strong>not</strong>
326 * the default locale ID of the runtime environment.
328 * @param newLocale Locale to set to.
329 * @param success The error code.
333 static void setDefault(const Locale
& newLocale
,
334 UErrorCode
& success
);
338 * Creates a locale which has had minimal canonicalization
339 * as per uloc_getName().
340 * @param name The name to create from. If name is null,
341 * the default Locale is used.
342 * @return new locale object
346 static Locale
createFromName(const char *name
);
350 * Returns the locale's ISO-639 language code.
351 * @return An alias to the code
354 inline const char * getLanguage( ) const;
357 * Returns the locale's ISO-3166 country code.
358 * @return An alias to the code
361 inline const char * getCountry( ) const;
364 * Returns the locale's variant code.
365 * @return An alias to the code
368 inline const char * getVariant( ) const;
371 * Returns the programmatic name of the entire locale, with the language,
372 * country and variant separated by underbars. If a field is missing, up
373 * to two leading underbars will occur. Example: "en", "de_DE", "en_US_WIN",
374 * "de__POSIX", "fr__MAC", "__MAC", "_MT", "_FR_EURO"
375 * @return A pointer to "name".
378 inline const char * getName() const;
381 * returns the locale's three-letter language code, as specified
382 * in ISO draft standard ISO-639-2..
383 * @return An alias to the code, or NULL
386 const char * getISO3Language() const;
389 * Fills in "name" with the locale's three-letter ISO-3166 country code.
390 * @return An alias to the code, or NULL
393 const char * getISO3Country() const;
396 * Returns the Windows LCID value corresponding to this locale.
397 * This value is stored in the resource data for the locale as a one-to-four-digit
398 * hexadecimal number. If the resource is missing, in the wrong format, or
399 * there is no Windows LCID value that corresponds to this locale, returns 0.
402 uint32_t getLCID(void) const;
405 * Fills in "dispLang" with the name of this locale's language in a format suitable for
406 * user display in the default locale. For example, if the locale's language code is
407 * "fr" and the default locale's language code is "en", this function would set
408 * dispLang to "French".
409 * @param dispLang Receives the language's display name.
410 * @return A reference to "dispLang".
413 UnicodeString
& getDisplayLanguage(UnicodeString
& dispLang
) const;
416 * Fills in "dispLang" with the name of this locale's language in a format suitable for
417 * user display in the locale specified by "inLocale". For example, if the locale's
418 * language code is "en" and inLocale's language code is "fr", this function would set
419 * dispLang to "Anglais".
420 * @param inLocale Specifies the locale to be used to display the name. In other words,
421 * if the locale's language code is "en", passing Locale::getFrench() for
422 * inLocale would result in "Anglais", while passing Locale::getGerman()
423 * for inLocale would result in "Englisch".
424 * @param dispLang Receives the language's display name.
425 * @return A reference to "dispLang".
428 UnicodeString
& getDisplayLanguage( const Locale
& inLocale
,
429 UnicodeString
& dispLang
) const;
432 * Fills in "dispCountry" with the name of this locale's country in a format suitable
433 * for user display in the default locale. For example, if the locale's country code
434 * is "FR" and the default locale's language code is "en", this function would set
435 * dispCountry to "France".
436 * @param dispCountry Receives the country's display name.
437 * @return A reference to "dispCountry".
440 UnicodeString
& getDisplayCountry( UnicodeString
& dispCountry
) const;
443 * Fills in "dispCountry" with the name of this locale's country in a format suitable
444 * for user display in the locale specified by "inLocale". For example, if the locale's
445 * country code is "US" and inLocale's language code is "fr", this function would set
446 * dispCountry to "Etats-Unis".
447 * @param inLocale Specifies the locale to be used to display the name. In other
448 * words, if the locale's country code is "US", passing
449 * Locale::getFrench() for inLocale would result in "États-Unis", while
450 * passing Locale::getGerman() for inLocale would result in
451 * "Vereinigte Staaten".
452 * @param dispCountry Receives the country's display name.
453 * @return A reference to "dispCountry".
456 UnicodeString
& getDisplayCountry( const Locale
& inLocale
,
457 UnicodeString
& dispCountry
) const;
460 * Fills in "dispVar" with the name of this locale's variant code in a format suitable
461 * for user display in the default locale.
462 * @param dispVar Receives the variant's name.
463 * @return A reference to "dispVar".
466 UnicodeString
& getDisplayVariant( UnicodeString
& dispVar
) const;
469 * Fills in "dispVar" with the name of this locale's variant code in a format
470 * suitable for user display in the locale specified by "inLocale".
471 * @param inLocale Specifies the locale to be used to display the name.
472 * @param dispVar Receives the variant's display name.
473 * @return A reference to "dispVar".
476 UnicodeString
& getDisplayVariant( const Locale
& inLocale
,
477 UnicodeString
& dispVar
) const;
480 * Fills in "name" with the name of this locale in a format suitable for user display
481 * in the default locale. This function uses getDisplayLanguage(), getDisplayCountry(),
482 * and getDisplayVariant() to do its work, and outputs the display name in the format
483 * "language (country[,variant])". For example, if the default locale is en_US, then
484 * fr_FR's display name would be "French (France)", and es_MX_Traditional's display name
485 * would be "Spanish (Mexico,Traditional)".
486 * @param name Receives the locale's display name.
487 * @return A reference to "name".
490 UnicodeString
& getDisplayName( UnicodeString
& name
) const;
493 * Fills in "name" with the name of this locale in a format suitable for user display
494 * in the locale specfied by "inLocale". This function uses getDisplayLanguage(),
495 * getDisplayCountry(), and getDisplayVariant() to do its work, and outputs the display
496 * name in the format "language (country[,variant])". For example, if inLocale is
497 * fr_FR, then en_US's display name would be "Anglais (États-Unis)", and no_NO_NY's
498 * display name would be "norvégien (Norvège,NY)".
499 * @param inLocale Specifies the locale to be used to display the name.
500 * @param name Receives the locale's display name.
501 * @return A reference to "name".
504 UnicodeString
& getDisplayName( const Locale
& inLocale
,
505 UnicodeString
& name
) const;
508 * Generates a hash code for the locale.
511 int32_t hashCode(void) const;
514 * Sets the locale to bogus
515 * A bogus locale represents a non-existing locale associated
516 * with services that can be instantiated from non-locale data
517 * in addition to locale (for example, collation can be
518 * instantiated from a locale and from a rule set).
524 * Gets the bogus state. Locale object can be bogus if it doesn't exist
525 * @return FALSE if it is a real locale, TRUE if it is a bogus locale
528 UBool
isBogus(void) const;
531 * Returns a list of all installed locales.
532 * @param count Receives the number of locales in the list.
533 * @return A pointer to an array of Locale objects. This array is the list
534 * of all locales with installed resource files. The called does NOT
535 * get ownership of this list, and must NOT delete it.
538 static const Locale
* getAvailableLocales(int32_t& count
);
541 * Gets a list of all available 2-letter country codes defined in ISO 639. This is a
542 * pointer to an array of pointers to arrays of char. All of these pointers are
543 * owned by ICU-- do not delete them, and do not write through them. The array is
544 * terminated with a null pointer.
545 * @return a list of all available country codes
548 static const char* const* getISOCountries();
551 * Gets a list of all available language codes defined in ISO 639. This is a pointer
552 * to an array of pointers to arrays of char. All of these pointers are owned
553 * by ICU-- do not delete them, and do not write through them. The array is
554 * terminated with a null pointer.
555 * @return a list of all available language codes
558 static const char* const* getISOLanguages();
561 * ICU "poor man's RTTI", returns a UClassID for the actual class.
565 virtual inline UClassID
getDynamicClassID() const;
568 * ICU "poor man's RTTI", returns a UClassID for this class.
572 static inline UClassID
getStaticClassID();
574 protected: /* only protected for testing purposes. DO NOT USE. */
576 * Set this from a single POSIX style locale string.
579 void setFromPOSIXID(const char *posixID
);
583 * Initialize the locale object with a new name.
584 * Was deprecated - used in implementation - moved internal
586 * @param cLocaleID The new locale name.
588 Locale
& init(const char* cLocaleID
);
591 * Internal constructor to allow construction of a locale object with
592 * NO side effects. (Default constructor tries to get
593 * the default locale.)
601 * Initialize the locale cache for commonly used locales
603 static Locale
*getLocaleCache(void);
605 char language
[ULOC_LANG_CAPACITY
];
606 char country
[ULOC_COUNTRY_CAPACITY
];
607 int32_t variantBegin
;
609 char fullNameBuffer
[ULOC_FULLNAME_CAPACITY
];
614 * The address of this static class variable serves as this class's ID
615 * for ICU "poor man's RTTI".
617 static const char fgClassID
;
619 static const Locale
&getLocale(int locid
);
621 friend void locale_set_default_internal(const char *);
625 Locale::getStaticClassID()
626 { return (UClassID
)&fgClassID
; }
629 Locale::getDynamicClassID() const
630 { return Locale::getStaticClassID(); }
633 Locale::operator!=(const Locale
& other
) const
635 return !operator==(other
);
639 Locale::getCountry() const
645 Locale::getLanguage() const
651 Locale::getVariant() const
653 return &fullName
[variantBegin
];
657 Locale::getName() const
663 Locale::isBogus(void) const {