]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/unicode/locid.h
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / common / unicode / locid.h
1 /*
2 ******************************************************************************
3 *
4 * Copyright (C) 1996-2003, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 ******************************************************************************
8 *
9 * File locid.h
10 *
11 * Created by: Helena Shih
12 *
13 * Modification History:
14 *
15 * Date Name Description
16 * 02/11/97 aliu Changed gLocPath to fgLocPath and added methods to
17 * get and set it.
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 ******************************************************************************
27 */
28
29 #ifndef LOCID_H
30 #define LOCID_H
31
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"
37
38 /**
39 * \file
40 * \brief C++ API: Locale ID object.
41 */
42
43 /**
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,
50 * region, or culture.
51 *
52 * The Locale class is not suitable for subclassing.
53 *
54 * <P>
55 * You create a <code>Locale</code> object using the constructor in
56 * this class:
57 * <blockquote>
58 * <pre>
59 * . Locale( const char* language,
60 * . const char* country,
61 * . const char* variant);
62 * </pre>
63 * </blockquote>
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>
70 *
71 * <P>
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>
78 *
79 * <P>
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".
87 *
88 * <P>
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>.
99 *
100 * <P>
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:
105 * <blockquote>
106 * <pre>
107 * Locale::getUS()
108 * </pre>
109 * </blockquote>
110 *
111 * <P>
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.
123 *
124 * <P>
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:
132 * <blockquote>
133 * <pre>
134 * UErrorCode success = U_ZERO_ERROR;
135 * Locale myLocale;
136 * NumberFormat *nf;
137 *
138 * nf = NumberFormat::createInstance( success ); delete nf;
139 * nf = NumberFormat::createCurrencyInstance( success ); delete nf;
140 * nf = NumberFormat::createPercentInstance( success ); delete nf;
141 * </pre>
142 * </blockquote>
143 * Each of these methods has two variants; one with an explicit locale
144 * and one without; the latter using the default locale.
145 * <blockquote>
146 * <pre>
147 * nf = NumberFormat::createInstance( myLocale, success ); delete nf;
148 * nf = NumberFormat::createCurrencyInstance( myLocale, success ); delete nf;
149 * nf = NumberFormat::createPercentInstance( myLocale, success ); delete nf;
150 * </pre>
151 * </blockquote>
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.
156 *
157 * <P>
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:
165 * <blockquote>
166 * <pre>
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)
173 * </pre>
174 * </blockquote>
175 *
176 * @stable ICU 2.0
177 * @see ResourceBundle
178 */
179 U_NAMESPACE_BEGIN
180 class U_COMMON_API Locale : public UObject {
181 public:
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);
200
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);
225
226
227 /**
228 * Construct a default locale object, a Locale for the default locale ID.
229 *
230 * @see getDefault
231 * @see uloc_getDefault
232 * @stable ICU 2.0
233 */
234 Locale();
235
236 /**
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).
240 *
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")'
249 *
250 * @param country Uppercase two-letter ISO-3166 code. (optional)
251 * @param variant Uppercase vendor and browser specific code. See class
252 * description. (optional)
253 *
254 * @see getDefault
255 * @see uloc_getDefault
256 * @stable ICU 2.0
257 */
258 Locale( const char * language,
259 const char * country = 0,
260 const char * variant = 0);
261
262 /**
263 * Initializes a Locale object from another Locale object.
264 *
265 * @param other The Locale object being copied in.
266 * @stable ICU 2.0
267 */
268 Locale(const Locale& other);
269
270
271 /**
272 * Destructor
273 * @stable ICU 2.0
274 */
275 ~Locale() ;
276
277 /**
278 * Replaces the entire contents of *this with the specified value.
279 *
280 * @param other The Locale object being copied in.
281 * @return *this
282 * @stable ICU 2.0
283 */
284 Locale& operator=(const Locale& other);
285
286 /**
287 * Checks if two locale keys are the same.
288 *
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.
291 * @stable ICU 2.0
292 */
293 UBool operator==(const Locale& other) const;
294
295 /**
296 * Checks if two locale keys are not the same.
297 *
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
300 * otherwise.
301 * @stable ICU 2.0
302 */
303 UBool operator!=(const Locale& other) const;
304
305 /**
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.)
311 *
312 * More advanced programs will allow users to use different locales for
313 * different fields, e.g. in a spreadsheet.
314 *
315 * Note that the initial setting will match the host system.
316 * @return a reference to the Locale object for the default locale ID
317 * @system
318 * @stable ICU 2.0
319 */
320 static const Locale& getDefault(void);
321
322 /**
323 * Sets the default. Normally set once at the beginning of a process,
324 * then never reset.
325 * setDefault() only changes ICU's default locale ID, <strong>not</strong>
326 * the default locale ID of the runtime environment.
327 *
328 * @param newLocale Locale to set to.
329 * @param success The error code.
330 * @system
331 * @stable ICU 2.0
332 */
333 static void setDefault(const Locale& newLocale,
334 UErrorCode& success);
335
336
337 /**
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
343 * @stable ICU 2.0
344 * @see uloc_getName
345 */
346 static Locale createFromName(const char *name);
347
348
349 /**
350 * Returns the locale's ISO-639 language code.
351 * @return An alias to the code
352 * @stable ICU 2.0
353 */
354 inline const char * getLanguage( ) const;
355
356 /**
357 * Returns the locale's ISO-3166 country code.
358 * @return An alias to the code
359 * @stable ICU 2.0
360 */
361 inline const char * getCountry( ) const;
362
363 /**
364 * Returns the locale's variant code.
365 * @return An alias to the code
366 * @stable ICU 2.0
367 */
368 inline const char * getVariant( ) const;
369
370 /**
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".
376 * @stable ICU 2.0
377 */
378 inline const char * getName() const;
379
380 /**
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
384 * @stable ICU 2.0
385 */
386 const char * getISO3Language() const;
387
388 /**
389 * Fills in "name" with the locale's three-letter ISO-3166 country code.
390 * @return An alias to the code, or NULL
391 * @stable ICU 2.0
392 */
393 const char * getISO3Country() const;
394
395 /**
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.
400 * @stable ICU 2.0
401 */
402 uint32_t getLCID(void) const;
403
404 /**
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".
411 * @stable ICU 2.0
412 */
413 UnicodeString& getDisplayLanguage(UnicodeString& dispLang) const;
414
415 /**
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".
426 * @stable ICU 2.0
427 */
428 UnicodeString& getDisplayLanguage( const Locale& inLocale,
429 UnicodeString& dispLang) const;
430
431 /**
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".
438 * @stable ICU 2.0
439 */
440 UnicodeString& getDisplayCountry( UnicodeString& dispCountry) const;
441
442 /**
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".
454 * @stable ICU 2.0
455 */
456 UnicodeString& getDisplayCountry( const Locale& inLocale,
457 UnicodeString& dispCountry) const;
458
459 /**
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".
464 * @stable ICU 2.0
465 */
466 UnicodeString& getDisplayVariant( UnicodeString& dispVar) const;
467
468 /**
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".
474 * @stable ICU 2.0
475 */
476 UnicodeString& getDisplayVariant( const Locale& inLocale,
477 UnicodeString& dispVar) const;
478
479 /**
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".
488 * @stable ICU 2.0
489 */
490 UnicodeString& getDisplayName( UnicodeString& name) const;
491
492 /**
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".
502 * @stable ICU 2.0
503 */
504 UnicodeString& getDisplayName( const Locale& inLocale,
505 UnicodeString& name) const;
506
507 /**
508 * Generates a hash code for the locale.
509 * @stable ICU 2.0
510 */
511 int32_t hashCode(void) const;
512
513 /**
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).
519 * @stable ICU 2.1
520 */
521 void setToBogus();
522
523 /**
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
526 * @stable ICU 2.1
527 */
528 UBool isBogus(void) const;
529
530 /**
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.
536 * @stable ICU 2.0
537 */
538 static const Locale* getAvailableLocales(int32_t& count);
539
540 /**
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
546 * @stable ICU 2.0
547 */
548 static const char* const* getISOCountries();
549
550 /**
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
556 * @stable ICU 2.0
557 */
558 static const char* const* getISOLanguages();
559
560 /**
561 * ICU "poor man's RTTI", returns a UClassID for the actual class.
562 *
563 * @draft ICU 2.2
564 */
565 virtual inline UClassID getDynamicClassID() const;
566
567 /**
568 * ICU "poor man's RTTI", returns a UClassID for this class.
569 *
570 * @draft ICU 2.2
571 */
572 static inline UClassID getStaticClassID();
573
574 protected: /* only protected for testing purposes. DO NOT USE. */
575 /**
576 * Set this from a single POSIX style locale string.
577 * @internal
578 */
579 void setFromPOSIXID(const char *posixID);
580
581 private:
582 /**
583 * Initialize the locale object with a new name.
584 * Was deprecated - used in implementation - moved internal
585 *
586 * @param cLocaleID The new locale name.
587 */
588 Locale& init(const char* cLocaleID);
589
590 /*
591 * Internal constructor to allow construction of a locale object with
592 * NO side effects. (Default constructor tries to get
593 * the default locale.)
594 */
595 enum ELocaleType {
596 eBOGUS
597 };
598 Locale(ELocaleType);
599
600 /**
601 * Initialize the locale cache for commonly used locales
602 */
603 static Locale *getLocaleCache(void);
604
605 char language[ULOC_LANG_CAPACITY];
606 char country[ULOC_COUNTRY_CAPACITY];
607 int32_t variantBegin;
608 char* fullName;
609 char fullNameBuffer[ULOC_FULLNAME_CAPACITY];
610
611 UBool fIsBogus;
612
613 /**
614 * The address of this static class variable serves as this class's ID
615 * for ICU "poor man's RTTI".
616 */
617 static const char fgClassID;
618
619 static const Locale &getLocale(int locid);
620
621 friend void locale_set_default_internal(const char *);
622 };
623
624 inline UClassID
625 Locale::getStaticClassID()
626 { return (UClassID)&fgClassID; }
627
628 inline UClassID
629 Locale::getDynamicClassID() const
630 { return Locale::getStaticClassID(); }
631
632 inline UBool
633 Locale::operator!=(const Locale& other) const
634 {
635 return !operator==(other);
636 }
637
638 inline const char *
639 Locale::getCountry() const
640 {
641 return country;
642 }
643
644 inline const char *
645 Locale::getLanguage() const
646 {
647 return language;
648 }
649
650 inline const char *
651 Locale::getVariant() const
652 {
653 return &fullName[variantBegin];
654 }
655
656 inline const char *
657 Locale::getName() const
658 {
659 return fullName;
660 }
661
662 inline UBool
663 Locale::isBogus(void) const {
664 return fIsBogus;
665 }
666
667 U_NAMESPACE_END
668
669 #endif