]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/unicode/locid.h
f25c212e80820c1ead26acbe92bb0a25a7faf67b
[apple/icu.git] / icuSources / common / unicode / locid.h
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 ******************************************************************************
5 *
6 * Copyright (C) 1996-2015, International Business Machines
7 * Corporation and others. All Rights Reserved.
8 *
9 ******************************************************************************
10 *
11 * File locid.h
12 *
13 * Created by: Helena Shih
14 *
15 * Modification History:
16 *
17 * Date Name Description
18 * 02/11/97 aliu Changed gLocPath to fgLocPath and added methods to
19 * get and set it.
20 * 04/02/97 aliu Made operator!= inline; fixed return value of getName().
21 * 04/15/97 aliu Cleanup for AIX/Win32.
22 * 04/24/97 aliu Numerous changes per code review.
23 * 08/18/98 stephen Added tokenizeString(),changed getDisplayName()
24 * 09/08/98 stephen Moved definition of kEmptyString for Mac Port
25 * 11/09/99 weiv Added const char * getName() const;
26 * 04/12/00 srl removing unicodestring api's and cached hash code
27 * 08/10/01 grhoten Change the static Locales to accessor functions
28 ******************************************************************************
29 */
30
31 #ifndef LOCID_H
32 #define LOCID_H
33
34 #include "unicode/bytestream.h"
35 #include "unicode/localpointer.h"
36 #include "unicode/strenum.h"
37 #include "unicode/stringpiece.h"
38 #include "unicode/utypes.h"
39 #include "unicode/uobject.h"
40 #include "unicode/putil.h"
41 #include "unicode/uloc.h"
42
43 /**
44 * \file
45 * \brief C++ API: Locale ID object.
46 */
47
48 #if U_SHOW_CPLUSPLUS_API
49 U_NAMESPACE_BEGIN
50
51 // Forward Declarations
52 void U_CALLCONV locale_available_init(); /**< @internal */
53
54 class StringEnumeration;
55 class UnicodeString;
56
57 /**
58 * A <code>Locale</code> object represents a specific geographical, political,
59 * or cultural region. An operation that requires a <code>Locale</code> to perform
60 * its task is called <em>locale-sensitive</em> and uses the <code>Locale</code>
61 * to tailor information for the user. For example, displaying a number
62 * is a locale-sensitive operation--the number should be formatted
63 * according to the customs/conventions of the user's native country,
64 * region, or culture.
65 *
66 * The Locale class is not suitable for subclassing.
67 *
68 * <P>
69 * You can create a <code>Locale</code> object using the constructor in
70 * this class:
71 * \htmlonly<blockquote>\endhtmlonly
72 * <pre>
73 * Locale( const char* language,
74 * const char* country,
75 * const char* variant);
76 * </pre>
77 * \htmlonly</blockquote>\endhtmlonly
78 * The first argument to the constructors is a valid <STRONG>ISO
79 * Language Code.</STRONG> These codes are the lower-case two-letter
80 * codes as defined by ISO-639.
81 * You can find a full list of these codes at:
82 * <BR><a href ="http://www.loc.gov/standards/iso639-2/">
83 * http://www.loc.gov/standards/iso639-2/</a>
84 *
85 * <P>
86 * The second argument to the constructors is a valid <STRONG>ISO Country
87 * Code.</STRONG> These codes are the upper-case two-letter codes
88 * as defined by ISO-3166.
89 * You can find a full list of these codes at a number of sites, such as:
90 * <BR><a href="http://www.iso.org/iso/en/prods-services/iso3166ma/index.html">
91 * http://www.iso.org/iso/en/prods-services/iso3166ma/index.html</a>
92 *
93 * <P>
94 * The third constructor requires a third argument--the <STRONG>Variant.</STRONG>
95 * The Variant codes are vendor and browser-specific.
96 * For example, use REVISED for a language's revised script orthography, and POSIX for POSIX.
97 * Where there are two variants, separate them with an underscore, and
98 * put the most important one first. For
99 * example, a Traditional Spanish collation might be referenced, with
100 * "ES", "ES", "Traditional_POSIX".
101 *
102 * <P>
103 * Because a <code>Locale</code> object is just an identifier for a region,
104 * no validity check is performed when you construct a <code>Locale</code>.
105 * If you want to see whether particular resources are available for the
106 * <code>Locale</code> you construct, you must query those resources. For
107 * example, ask the <code>NumberFormat</code> for the locales it supports
108 * using its <code>getAvailableLocales</code> method.
109 * <BR><STRONG>Note:</STRONG> When you ask for a resource for a particular
110 * locale, you get back the best available match, not necessarily
111 * precisely what you asked for. For more information, look at
112 * <code>ResourceBundle</code>.
113 *
114 * <P>
115 * The <code>Locale</code> class provides a number of convenient constants
116 * that you can use to create <code>Locale</code> objects for commonly used
117 * locales. For example, the following refers to a <code>Locale</code> object
118 * for the United States:
119 * \htmlonly<blockquote>\endhtmlonly
120 * <pre>
121 * Locale::getUS()
122 * </pre>
123 * \htmlonly</blockquote>\endhtmlonly
124 *
125 * <P>
126 * Once you've created a <code>Locale</code> you can query it for information about
127 * itself. Use <code>getCountry</code> to get the ISO Country Code and
128 * <code>getLanguage</code> to get the ISO Language Code. You can
129 * use <code>getDisplayCountry</code> to get the
130 * name of the country suitable for displaying to the user. Similarly,
131 * you can use <code>getDisplayLanguage</code> to get the name of
132 * the language suitable for displaying to the user. Interestingly,
133 * the <code>getDisplayXXX</code> methods are themselves locale-sensitive
134 * and have two versions: one that uses the default locale and one
135 * that takes a locale as an argument and displays the name or country in
136 * a language appropriate to that locale.
137 *
138 * <P>
139 * ICU provides a number of classes that perform locale-sensitive
140 * operations. For example, the <code>NumberFormat</code> class formats
141 * numbers, currency, or percentages in a locale-sensitive manner. Classes
142 * such as <code>NumberFormat</code> have a number of convenience methods
143 * for creating a default object of that type. For example, the
144 * <code>NumberFormat</code> class provides these three convenience methods
145 * for creating a default <code>NumberFormat</code> object:
146 * \htmlonly<blockquote>\endhtmlonly
147 * <pre>
148 * UErrorCode success = U_ZERO_ERROR;
149 * Locale myLocale;
150 * NumberFormat *nf;
151 *
152 * nf = NumberFormat::createInstance( success ); delete nf;
153 * nf = NumberFormat::createCurrencyInstance( success ); delete nf;
154 * nf = NumberFormat::createPercentInstance( success ); delete nf;
155 * </pre>
156 * \htmlonly</blockquote>\endhtmlonly
157 * Each of these methods has two variants; one with an explicit locale
158 * and one without; the latter using the default locale.
159 * \htmlonly<blockquote>\endhtmlonly
160 * <pre>
161 * nf = NumberFormat::createInstance( myLocale, success ); delete nf;
162 * nf = NumberFormat::createCurrencyInstance( myLocale, success ); delete nf;
163 * nf = NumberFormat::createPercentInstance( myLocale, success ); delete nf;
164 * </pre>
165 * \htmlonly</blockquote>\endhtmlonly
166 * A <code>Locale</code> is the mechanism for identifying the kind of object
167 * (<code>NumberFormat</code>) that you would like to get. The locale is
168 * <STRONG>just</STRONG> a mechanism for identifying objects,
169 * <STRONG>not</STRONG> a container for the objects themselves.
170 *
171 * <P>
172 * Each class that performs locale-sensitive operations allows you
173 * to get all the available objects of that type. You can sift
174 * through these objects by language, country, or variant,
175 * and use the display names to present a menu to the user.
176 * For example, you can create a menu of all the collation objects
177 * suitable for a given language. Such classes implement these
178 * three class methods:
179 * \htmlonly<blockquote>\endhtmlonly
180 * <pre>
181 * static Locale* getAvailableLocales(int32_t& numLocales)
182 * static UnicodeString& getDisplayName(const Locale& objectLocale,
183 * const Locale& displayLocale,
184 * UnicodeString& displayName)
185 * static UnicodeString& getDisplayName(const Locale& objectLocale,
186 * UnicodeString& displayName)
187 * </pre>
188 * \htmlonly</blockquote>\endhtmlonly
189 *
190 * @stable ICU 2.0
191 * @see ResourceBundle
192 */
193 class U_COMMON_API Locale : public UObject {
194 public:
195 /** Useful constant for the Root locale. @stable ICU 4.4 */
196 static const Locale &U_EXPORT2 getRoot(void);
197 /** Useful constant for this language. @stable ICU 2.0 */
198 static const Locale &U_EXPORT2 getEnglish(void);
199 /** Useful constant for this language. @stable ICU 2.0 */
200 static const Locale &U_EXPORT2 getFrench(void);
201 /** Useful constant for this language. @stable ICU 2.0 */
202 static const Locale &U_EXPORT2 getGerman(void);
203 /** Useful constant for this language. @stable ICU 2.0 */
204 static const Locale &U_EXPORT2 getItalian(void);
205 /** Useful constant for this language. @stable ICU 2.0 */
206 static const Locale &U_EXPORT2 getJapanese(void);
207 /** Useful constant for this language. @stable ICU 2.0 */
208 static const Locale &U_EXPORT2 getKorean(void);
209 /** Useful constant for this language. @stable ICU 2.0 */
210 static const Locale &U_EXPORT2 getChinese(void);
211 /** Useful constant for this language. @stable ICU 2.0 */
212 static const Locale &U_EXPORT2 getSimplifiedChinese(void);
213 /** Useful constant for this language. @stable ICU 2.0 */
214 static const Locale &U_EXPORT2 getTraditionalChinese(void);
215
216 /** Useful constant for this country/region. @stable ICU 2.0 */
217 static const Locale &U_EXPORT2 getFrance(void);
218 /** Useful constant for this country/region. @stable ICU 2.0 */
219 static const Locale &U_EXPORT2 getGermany(void);
220 /** Useful constant for this country/region. @stable ICU 2.0 */
221 static const Locale &U_EXPORT2 getItaly(void);
222 /** Useful constant for this country/region. @stable ICU 2.0 */
223 static const Locale &U_EXPORT2 getJapan(void);
224 /** Useful constant for this country/region. @stable ICU 2.0 */
225 static const Locale &U_EXPORT2 getKorea(void);
226 /** Useful constant for this country/region. @stable ICU 2.0 */
227 static const Locale &U_EXPORT2 getChina(void);
228 /** Useful constant for this country/region. @stable ICU 2.0 */
229 static const Locale &U_EXPORT2 getPRC(void);
230 /** Useful constant for this country/region. @stable ICU 2.0 */
231 static const Locale &U_EXPORT2 getTaiwan(void);
232 /** Useful constant for this country/region. @stable ICU 2.0 */
233 static const Locale &U_EXPORT2 getUK(void);
234 /** Useful constant for this country/region. @stable ICU 2.0 */
235 static const Locale &U_EXPORT2 getUS(void);
236 /** Useful constant for this country/region. @stable ICU 2.0 */
237 static const Locale &U_EXPORT2 getCanada(void);
238 /** Useful constant for this country/region. @stable ICU 2.0 */
239 static const Locale &U_EXPORT2 getCanadaFrench(void);
240
241
242 /**
243 * Construct a default locale object, a Locale for the default locale ID.
244 *
245 * @see getDefault
246 * @see uloc_getDefault
247 * @stable ICU 2.0
248 */
249 Locale();
250
251 /**
252 * Construct a locale from language, country, variant.
253 * If an error occurs, then the constructed object will be "bogus"
254 * (isBogus() will return TRUE).
255 *
256 * @param language Lowercase two-letter or three-letter ISO-639 code.
257 * This parameter can instead be an ICU style C locale (e.g. "en_US"),
258 * but the other parameters must not be used.
259 * This parameter can be NULL; if so,
260 * the locale is initialized to match the current default locale.
261 * (This is the same as using the default constructor.)
262 * Please note: The Java Locale class does NOT accept the form
263 * 'new Locale("en_US")' but only 'new Locale("en","US")'
264 *
265 * @param country Uppercase two-letter ISO-3166 code. (optional)
266 * @param variant Uppercase vendor and browser specific code. See class
267 * description. (optional)
268 * @param keywordsAndValues A string consisting of keyword/values pairs, such as
269 * "collation=phonebook;currency=euro"
270 *
271 * @see getDefault
272 * @see uloc_getDefault
273 * @stable ICU 2.0
274 */
275 Locale( const char * language,
276 const char * country = 0,
277 const char * variant = 0,
278 const char * keywordsAndValues = 0);
279
280 /**
281 * Initializes a Locale object from another Locale object.
282 *
283 * @param other The Locale object being copied in.
284 * @stable ICU 2.0
285 */
286 Locale(const Locale& other);
287
288 #ifndef U_HIDE_DRAFT_API
289 /**
290 * Move constructor; might leave source in bogus state.
291 * This locale will have the same contents that the source locale had.
292 *
293 * @param other The Locale object being moved in.
294 * @draft ICU 63
295 */
296 Locale(Locale&& other) U_NOEXCEPT;
297 #endif // U_HIDE_DRAFT_API
298
299 /**
300 * Destructor
301 * @stable ICU 2.0
302 */
303 virtual ~Locale() ;
304
305 /**
306 * Replaces the entire contents of *this with the specified value.
307 *
308 * @param other The Locale object being copied in.
309 * @return *this
310 * @stable ICU 2.0
311 */
312 Locale& operator=(const Locale& other);
313
314 #ifndef U_HIDE_DRAFT_API
315 /**
316 * Move assignment operator; might leave source in bogus state.
317 * This locale will have the same contents that the source locale had.
318 * The behavior is undefined if *this and the source are the same object.
319 *
320 * @param other The Locale object being moved in.
321 * @return *this
322 * @draft ICU 63
323 */
324 Locale& operator=(Locale&& other) U_NOEXCEPT;
325 #endif // U_HIDE_DRAFT_API
326
327 /**
328 * Checks if two locale keys are the same.
329 *
330 * @param other The locale key object to be compared with this.
331 * @return True if the two locale keys are the same, false otherwise.
332 * @stable ICU 2.0
333 */
334 UBool operator==(const Locale& other) const;
335
336 /**
337 * Checks if two locale keys are not the same.
338 *
339 * @param other The locale key object to be compared with this.
340 * @return True if the two locale keys are not the same, false
341 * otherwise.
342 * @stable ICU 2.0
343 */
344 inline UBool operator!=(const Locale& other) const;
345
346 /**
347 * Clone this object.
348 * Clones can be used concurrently in multiple threads.
349 * If an error occurs, then NULL is returned.
350 * The caller must delete the clone.
351 *
352 * @return a clone of this object
353 *
354 * @see getDynamicClassID
355 * @stable ICU 2.8
356 */
357 Locale *clone() const;
358
359 #ifndef U_HIDE_SYSTEM_API
360 /**
361 * Common methods of getting the current default Locale. Used for the
362 * presentation: menus, dialogs, etc. Generally set once when your applet or
363 * application is initialized, then never reset. (If you do reset the
364 * default locale, you probably want to reload your GUI, so that the change
365 * is reflected in your interface.)
366 *
367 * More advanced programs will allow users to use different locales for
368 * different fields, e.g. in a spreadsheet.
369 *
370 * Note that the initial setting will match the host system.
371 * @return a reference to the Locale object for the default locale ID
372 * @system
373 * @stable ICU 2.0
374 */
375 static const Locale& U_EXPORT2 getDefault(void);
376
377 /**
378 * Sets the default. Normally set once at the beginning of a process,
379 * then never reset.
380 * setDefault() only changes ICU's default locale ID, <strong>not</strong>
381 * the default locale ID of the runtime environment.
382 *
383 * @param newLocale Locale to set to. If NULL, set to the value obtained
384 * from the runtime environment.
385 * @param success The error code.
386 * @system
387 * @stable ICU 2.0
388 */
389 static void U_EXPORT2 setDefault(const Locale& newLocale,
390 UErrorCode& success);
391 #endif /* U_HIDE_SYSTEM_API */
392
393 #ifndef U_HIDE_DRAFT_API
394 /**
395 * Returns a Locale for the specified BCP47 language tag string.
396 * If the specified language tag contains any ill-formed subtags,
397 * the first such subtag and all following subtags are ignored.
398 * <p>
399 * This implements the 'Language-Tag' production of BCP47, and so
400 * supports grandfathered (regular and irregular) as well as private
401 * use language tags. Private use tags are represented as 'x-whatever',
402 * and grandfathered tags are converted to their canonical replacements
403 * where they exist. Note that a few grandfathered tags have no modern
404 * replacement, these will be converted using the fallback described in
405 * the first paragraph, so some information might be lost.
406 * @param tag the input BCP47 language tag.
407 * @param status error information if creating the Locale failed.
408 * @return the Locale for the specified BCP47 language tag.
409 * @draft ICU 63
410 */
411 static Locale U_EXPORT2 forLanguageTag(StringPiece tag, UErrorCode& status);
412
413 /**
414 * Returns a well-formed language tag for this Locale.
415 * <p>
416 * <b>Note</b>: Any locale fields which do not satisfy the BCP47 syntax
417 * requirement will be silently omitted from the result.
418 *
419 * If this function fails, partial output may have been written to the sink.
420 *
421 * @param sink the output sink receiving the BCP47 language
422 * tag for this Locale.
423 * @param status error information if creating the language tag failed.
424 * @draft ICU 63
425 */
426 void toLanguageTag(ByteSink& sink, UErrorCode& status) const;
427
428 /**
429 * Returns a well-formed language tag for this Locale.
430 * <p>
431 * <b>Note</b>: Any locale fields which do not satisfy the BCP47 syntax
432 * requirement will be silently omitted from the result.
433 *
434 * @param status error information if creating the language tag failed.
435 * @return the BCP47 language tag for this Locale.
436 * @draft ICU 63
437 */
438 template<typename StringClass>
439 inline StringClass toLanguageTag(UErrorCode& status) const;
440 #endif // U_HIDE_DRAFT_API
441
442 /**
443 * Creates a locale which has had minimal canonicalization
444 * as per uloc_getName().
445 * @param name The name to create from. If name is null,
446 * the default Locale is used.
447 * @return new locale object
448 * @stable ICU 2.0
449 * @see uloc_getName
450 */
451 static Locale U_EXPORT2 createFromName(const char *name);
452
453 /**
454 * Creates a locale from the given string after canonicalizing
455 * the string by calling uloc_canonicalize().
456 * @param name the locale ID to create from. Must not be NULL.
457 * @return a new locale object corresponding to the given name
458 * @stable ICU 3.0
459 * @see uloc_canonicalize
460 */
461 static Locale U_EXPORT2 createCanonical(const char* name);
462
463 /**
464 * Returns the locale's ISO-639 language code.
465 * @return An alias to the code
466 * @stable ICU 2.0
467 */
468 inline const char * getLanguage( ) const;
469
470 /**
471 * Returns the locale's ISO-15924 abbreviation script code.
472 * @return An alias to the code
473 * @see uscript_getShortName
474 * @see uscript_getCode
475 * @stable ICU 2.8
476 */
477 inline const char * getScript( ) const;
478
479 /**
480 * Returns the locale's ISO-3166 country code.
481 * @return An alias to the code
482 * @stable ICU 2.0
483 */
484 inline const char * getCountry( ) const;
485
486 /**
487 * Returns the locale's variant code.
488 * @return An alias to the code
489 * @stable ICU 2.0
490 */
491 inline const char * getVariant( ) const;
492
493 /**
494 * Returns the programmatic name of the entire locale, with the language,
495 * country and variant separated by underbars. If a field is missing, up
496 * to two leading underbars will occur. Example: "en", "de_DE", "en_US_WIN",
497 * "de__POSIX", "fr__MAC", "__MAC", "_MT", "_FR_EURO"
498 * @return A pointer to "name".
499 * @stable ICU 2.0
500 */
501 inline const char * getName() const;
502
503 /**
504 * Returns the programmatic name of the entire locale as getName() would return,
505 * but without keywords.
506 * @return A pointer to "name".
507 * @see getName
508 * @stable ICU 2.8
509 */
510 const char * getBaseName() const;
511
512 #ifndef U_HIDE_DRAFT_API
513 /**
514 * Add the likely subtags for this Locale, per the algorithm described
515 * in the following CLDR technical report:
516 *
517 * http://www.unicode.org/reports/tr35/#Likely_Subtags
518 *
519 * If this Locale is already in the maximal form, or not valid, or there is
520 * no data available for maximization, the Locale will be unchanged.
521 *
522 * For example, "und-Zzzz" cannot be maximized, since there is no
523 * reasonable maximization.
524 *
525 * Examples:
526 *
527 * "en" maximizes to "en_Latn_US"
528 *
529 * "de" maximizes to "de_Latn_US"
530 *
531 * "sr" maximizes to "sr_Cyrl_RS"
532 *
533 * "sh" maximizes to "sr_Latn_RS" (Note this will not reverse.)
534 *
535 * "zh_Hani" maximizes to "zh_Hans_CN" (Note this will not reverse.)
536 *
537 * @param status error information if maximizing this Locale failed.
538 * If this Locale is not well-formed, the error code is
539 * U_ILLEGAL_ARGUMENT_ERROR.
540 * @draft ICU 63
541 */
542 void addLikelySubtags(UErrorCode& status);
543
544 /**
545 * Minimize the subtags for this Locale, per the algorithm described
546 * in the following CLDR technical report:
547 *
548 * http://www.unicode.org/reports/tr35/#Likely_Subtags
549 *
550 * If this Locale is already in the minimal form, or not valid, or there is
551 * no data available for minimization, the Locale will be unchanged.
552 *
553 * Since the minimization algorithm relies on proper maximization, see the
554 * comments for addLikelySubtags for reasons why there might not be any
555 * data.
556 *
557 * Examples:
558 *
559 * "en_Latn_US" minimizes to "en"
560 *
561 * "de_Latn_US" minimizes to "de"
562 *
563 * "sr_Cyrl_RS" minimizes to "sr"
564 *
565 * "zh_Hant_TW" minimizes to "zh_TW" (The region is preferred to the
566 * script, and minimizing to "zh" would imply "zh_Hans_CN".)
567 *
568 * @param status error information if maximizing this Locale failed.
569 * If this Locale is not well-formed, the error code is
570 * U_ILLEGAL_ARGUMENT_ERROR.
571 * @draft ICU 63
572 */
573 void minimizeSubtags(UErrorCode& status);
574 #endif // U_HIDE_DRAFT_API
575
576 /**
577 * Gets the list of keywords for the specified locale.
578 *
579 * @param status the status code
580 * @return pointer to StringEnumeration class, or NULL if there are no keywords.
581 * Client must dispose of it by calling delete.
582 * @see getKeywords
583 * @stable ICU 2.8
584 */
585 StringEnumeration * createKeywords(UErrorCode &status) const;
586
587 #ifndef U_HIDE_DRAFT_API
588
589 /**
590 * Gets the list of Unicode keywords for the specified locale.
591 *
592 * @param status the status code
593 * @return pointer to StringEnumeration class, or NULL if there are no keywords.
594 * Client must dispose of it by calling delete.
595 * @see getUnicodeKeywords
596 * @draft ICU 63
597 */
598 StringEnumeration * createUnicodeKeywords(UErrorCode &status) const;
599
600 /**
601 * Gets the set of keywords for this Locale.
602 *
603 * A wrapper to call createKeywords() and write the resulting
604 * keywords as standard strings (or compatible objects) into any kind of
605 * container that can be written to by an STL style output iterator.
606 *
607 * @param iterator an STL style output iterator to write the keywords to.
608 * @param status error information if creating set of keywords failed.
609 * @draft ICU 63
610 */
611 template<typename StringClass, typename OutputIterator>
612 inline void getKeywords(OutputIterator iterator, UErrorCode& status) const;
613
614 /**
615 * Gets the set of Unicode keywords for this Locale.
616 *
617 * A wrapper to call createUnicodeKeywords() and write the resulting
618 * keywords as standard strings (or compatible objects) into any kind of
619 * container that can be written to by an STL style output iterator.
620 *
621 * @param iterator an STL style output iterator to write the keywords to.
622 * @param status error information if creating set of keywords failed.
623 * @draft ICU 63
624 */
625 template<typename StringClass, typename OutputIterator>
626 inline void getUnicodeKeywords(OutputIterator iterator, UErrorCode& status) const;
627
628 #endif // U_HIDE_DRAFT_API
629
630 /**
631 * Gets the value for a keyword.
632 *
633 * This uses legacy keyword=value pairs, like "collation=phonebook".
634 *
635 * ICU4C doesn't do automatic conversion between legacy and Unicode
636 * keywords and values in getters and setters (as opposed to ICU4J).
637 *
638 * @param keywordName name of the keyword for which we want the value. Case insensitive.
639 * @param buffer The buffer to receive the keyword value.
640 * @param bufferCapacity The capacity of receiving buffer
641 * @param status Returns any error information while performing this operation.
642 * @return the length of the keyword value
643 *
644 * @stable ICU 2.8
645 */
646 int32_t getKeywordValue(const char* keywordName, char *buffer, int32_t bufferCapacity, UErrorCode &status) const;
647
648 #ifndef U_HIDE_DRAFT_API
649 /**
650 * Gets the value for a keyword.
651 *
652 * This uses legacy keyword=value pairs, like "collation=phonebook".
653 *
654 * ICU4C doesn't do automatic conversion between legacy and Unicode
655 * keywords and values in getters and setters (as opposed to ICU4J).
656 *
657 * @param keywordName name of the keyword for which we want the value.
658 * @param sink the sink to receive the keyword value.
659 * @param status error information if getting the value failed.
660 * @draft ICU 63
661 */
662 void getKeywordValue(StringPiece keywordName, ByteSink& sink, UErrorCode& status) const;
663
664 /**
665 * Gets the value for a keyword.
666 *
667 * This uses legacy keyword=value pairs, like "collation=phonebook".
668 *
669 * ICU4C doesn't do automatic conversion between legacy and Unicode
670 * keywords and values in getters and setters (as opposed to ICU4J).
671 *
672 * @param keywordName name of the keyword for which we want the value.
673 * @param status error information if getting the value failed.
674 * @return the keyword value.
675 * @draft ICU 63
676 */
677 template<typename StringClass>
678 inline StringClass getKeywordValue(StringPiece keywordName, UErrorCode& status) const;
679
680 /**
681 * Gets the Unicode value for a Unicode keyword.
682 *
683 * This uses Unicode key-value pairs, like "co-phonebk".
684 *
685 * ICU4C doesn't do automatic conversion between legacy and Unicode
686 * keywords and values in getters and setters (as opposed to ICU4J).
687 *
688 * @param keywordName name of the keyword for which we want the value.
689 * @param sink the sink to receive the keyword value.
690 * @param status error information if getting the value failed.
691 * @draft ICU 63
692 */
693 void getUnicodeKeywordValue(StringPiece keywordName, ByteSink& sink, UErrorCode& status) const;
694
695 /**
696 * Gets the Unicode value for a Unicode keyword.
697 *
698 * This uses Unicode key-value pairs, like "co-phonebk".
699 *
700 * ICU4C doesn't do automatic conversion between legacy and Unicode
701 * keywords and values in getters and setters (as opposed to ICU4J).
702 *
703 * @param keywordName name of the keyword for which we want the value.
704 * @param status error information if getting the value failed.
705 * @return the keyword value.
706 * @draft ICU 63
707 */
708 template<typename StringClass>
709 inline StringClass getUnicodeKeywordValue(StringPiece keywordName, UErrorCode& status) const;
710 #endif // U_HIDE_DRAFT_API
711
712 /**
713 * Sets or removes the value for a keyword.
714 *
715 * For removing all keywords, use getBaseName(),
716 * and construct a new Locale if it differs from getName().
717 *
718 * This uses legacy keyword=value pairs, like "collation=phonebook".
719 *
720 * ICU4C doesn't do automatic conversion between legacy and Unicode
721 * keywords and values in getters and setters (as opposed to ICU4J).
722 *
723 * @param keywordName name of the keyword to be set. Case insensitive.
724 * @param keywordValue value of the keyword to be set. If 0-length or
725 * NULL, will result in the keyword being removed. No error is given if
726 * that keyword does not exist.
727 * @param status Returns any error information while performing this operation.
728 *
729 * @stable ICU 49
730 */
731 void setKeywordValue(const char* keywordName, const char* keywordValue, UErrorCode &status);
732
733 #ifndef U_HIDE_DRAFT_API
734 /**
735 * Sets or removes the value for a keyword.
736 *
737 * For removing all keywords, use getBaseName(),
738 * and construct a new Locale if it differs from getName().
739 *
740 * This uses legacy keyword=value pairs, like "collation=phonebook".
741 *
742 * ICU4C doesn't do automatic conversion between legacy and Unicode
743 * keywords and values in getters and setters (as opposed to ICU4J).
744 *
745 * @param keywordName name of the keyword to be set.
746 * @param keywordValue value of the keyword to be set. If 0-length or
747 * NULL, will result in the keyword being removed. No error is given if
748 * that keyword does not exist.
749 * @param status Returns any error information while performing this operation.
750 * @draft ICU 63
751 */
752 void setKeywordValue(StringPiece keywordName, StringPiece keywordValue, UErrorCode& status);
753
754 /**
755 * Sets or removes the Unicode value for a Unicode keyword.
756 *
757 * For removing all keywords, use getBaseName(),
758 * and construct a new Locale if it differs from getName().
759 *
760 * This uses Unicode key-value pairs, like "co-phonebk".
761 *
762 * ICU4C doesn't do automatic conversion between legacy and Unicode
763 * keywords and values in getters and setters (as opposed to ICU4J).
764 *
765 * @param keywordName name of the keyword to be set.
766 * @param keywordValue value of the keyword to be set. If 0-length or
767 * NULL, will result in the keyword being removed. No error is given if
768 * that keyword does not exist.
769 * @param status Returns any error information while performing this operation.
770 * @draft ICU 63
771 */
772 void setUnicodeKeywordValue(StringPiece keywordName, StringPiece keywordValue, UErrorCode& status);
773 #endif // U_HIDE_DRAFT_API
774
775 /**
776 * returns the locale's three-letter language code, as specified
777 * in ISO draft standard ISO-639-2.
778 * @return An alias to the code, or an empty string
779 * @stable ICU 2.0
780 */
781 const char * getISO3Language() const;
782
783 /**
784 * Fills in "name" with the locale's three-letter ISO-3166 country code.
785 * @return An alias to the code, or an empty string
786 * @stable ICU 2.0
787 */
788 const char * getISO3Country() const;
789
790 /**
791 * Returns the Windows LCID value corresponding to this locale.
792 * This value is stored in the resource data for the locale as a one-to-four-digit
793 * hexadecimal number. If the resource is missing, in the wrong format, or
794 * there is no Windows LCID value that corresponds to this locale, returns 0.
795 * @stable ICU 2.0
796 */
797 uint32_t getLCID(void) const;
798
799 /**
800 * Returns whether this locale's script is written right-to-left.
801 * If there is no script subtag, then the likely script is used, see uloc_addLikelySubtags().
802 * If no likely script is known, then FALSE is returned.
803 *
804 * A script is right-to-left according to the CLDR script metadata
805 * which corresponds to whether the script's letters have Bidi_Class=R or AL.
806 *
807 * Returns TRUE for "ar" and "en-Hebr", FALSE for "zh" and "fa-Cyrl".
808 *
809 * @return TRUE if the locale's script is written right-to-left
810 * @stable ICU 54
811 */
812 UBool isRightToLeft() const;
813
814 /**
815 * Fills in "dispLang" with the name of this locale's language in a format suitable for
816 * user display in the default locale. For example, if the locale's language code is
817 * "fr" and the default locale's language code is "en", this function would set
818 * dispLang to "French".
819 * @param dispLang Receives the language's display name.
820 * @return A reference to "dispLang".
821 * @stable ICU 2.0
822 */
823 UnicodeString& getDisplayLanguage(UnicodeString& dispLang) const;
824
825 /**
826 * Fills in "dispLang" with the name of this locale's language in a format suitable for
827 * user display in the locale specified by "displayLocale". For example, if the locale's
828 * language code is "en" and displayLocale's language code is "fr", this function would set
829 * dispLang to "Anglais".
830 * @param displayLocale Specifies the locale to be used to display the name. In other words,
831 * if the locale's language code is "en", passing Locale::getFrench() for
832 * displayLocale would result in "Anglais", while passing Locale::getGerman()
833 * for displayLocale would result in "Englisch".
834 * @param dispLang Receives the language's display name.
835 * @return A reference to "dispLang".
836 * @stable ICU 2.0
837 */
838 UnicodeString& getDisplayLanguage( const Locale& displayLocale,
839 UnicodeString& dispLang) const;
840
841 /**
842 * Fills in "dispScript" with the name of this locale's script in a format suitable
843 * for user display in the default locale. For example, if the locale's script code
844 * is "LATN" and the default locale's language code is "en", this function would set
845 * dispScript to "Latin".
846 * @param dispScript Receives the scripts's display name.
847 * @return A reference to "dispScript".
848 * @stable ICU 2.8
849 */
850 UnicodeString& getDisplayScript( UnicodeString& dispScript) const;
851
852 /**
853 * Fills in "dispScript" with the name of this locale's country in a format suitable
854 * for user display in the locale specified by "displayLocale". For example, if the locale's
855 * script code is "LATN" and displayLocale's language code is "en", this function would set
856 * dispScript to "Latin".
857 * @param displayLocale Specifies the locale to be used to display the name. In other
858 * words, if the locale's script code is "LATN", passing
859 * Locale::getFrench() for displayLocale would result in "", while
860 * passing Locale::getGerman() for displayLocale would result in
861 * "".
862 * @param dispScript Receives the scripts's display name.
863 * @return A reference to "dispScript".
864 * @stable ICU 2.8
865 */
866 UnicodeString& getDisplayScript( const Locale& displayLocale,
867 UnicodeString& dispScript) const;
868
869 /**
870 * Fills in "dispCountry" with the name of this locale's country in a format suitable
871 * for user display in the default locale. For example, if the locale's country code
872 * is "FR" and the default locale's language code is "en", this function would set
873 * dispCountry to "France".
874 * @param dispCountry Receives the country's display name.
875 * @return A reference to "dispCountry".
876 * @stable ICU 2.0
877 */
878 UnicodeString& getDisplayCountry( UnicodeString& dispCountry) const;
879
880 /**
881 * Fills in "dispCountry" with the name of this locale's country in a format suitable
882 * for user display in the locale specified by "displayLocale". For example, if the locale's
883 * country code is "US" and displayLocale's language code is "fr", this function would set
884 * dispCountry to "&Eacute;tats-Unis".
885 * @param displayLocale Specifies the locale to be used to display the name. In other
886 * words, if the locale's country code is "US", passing
887 * Locale::getFrench() for displayLocale would result in "&Eacute;tats-Unis", while
888 * passing Locale::getGerman() for displayLocale would result in
889 * "Vereinigte Staaten".
890 * @param dispCountry Receives the country's display name.
891 * @return A reference to "dispCountry".
892 * @stable ICU 2.0
893 */
894 UnicodeString& getDisplayCountry( const Locale& displayLocale,
895 UnicodeString& dispCountry) const;
896
897 /**
898 * Fills in "dispVar" with the name of this locale's variant code in a format suitable
899 * for user display in the default locale.
900 * @param dispVar Receives the variant's name.
901 * @return A reference to "dispVar".
902 * @stable ICU 2.0
903 */
904 UnicodeString& getDisplayVariant( UnicodeString& dispVar) const;
905
906 /**
907 * Fills in "dispVar" with the name of this locale's variant code in a format
908 * suitable for user display in the locale specified by "displayLocale".
909 * @param displayLocale Specifies the locale to be used to display the name.
910 * @param dispVar Receives the variant's display name.
911 * @return A reference to "dispVar".
912 * @stable ICU 2.0
913 */
914 UnicodeString& getDisplayVariant( const Locale& displayLocale,
915 UnicodeString& dispVar) const;
916
917 /**
918 * Fills in "name" with the name of this locale in a format suitable for user display
919 * in the default locale. This function uses getDisplayLanguage(), getDisplayCountry(),
920 * and getDisplayVariant() to do its work, and outputs the display name in the format
921 * "language (country[,variant])". For example, if the default locale is en_US, then
922 * fr_FR's display name would be "French (France)", and es_MX_Traditional's display name
923 * would be "Spanish (Mexico,Traditional)".
924 * @param name Receives the locale's display name.
925 * @return A reference to "name".
926 * @stable ICU 2.0
927 */
928 UnicodeString& getDisplayName( UnicodeString& name) const;
929
930 /**
931 * Fills in "name" with the name of this locale in a format suitable for user display
932 * in the locale specified by "displayLocale". This function uses getDisplayLanguage(),
933 * getDisplayCountry(), and getDisplayVariant() to do its work, and outputs the display
934 * name in the format "language (country[,variant])". For example, if displayLocale is
935 * fr_FR, then en_US's display name would be "Anglais (&Eacute;tats-Unis)", and no_NO_NY's
936 * display name would be "norv&eacute;gien (Norv&egrave;ge,NY)".
937 * @param displayLocale Specifies the locale to be used to display the name.
938 * @param name Receives the locale's display name.
939 * @return A reference to "name".
940 * @stable ICU 2.0
941 */
942 UnicodeString& getDisplayName( const Locale& displayLocale,
943 UnicodeString& name) const;
944
945 /**
946 * Generates a hash code for the locale.
947 * @stable ICU 2.0
948 */
949 int32_t hashCode(void) const;
950
951 /**
952 * Sets the locale to bogus
953 * A bogus locale represents a non-existing locale associated
954 * with services that can be instantiated from non-locale data
955 * in addition to locale (for example, collation can be
956 * instantiated from a locale and from a rule set).
957 * @stable ICU 2.1
958 */
959 void setToBogus();
960
961 /**
962 * Gets the bogus state. Locale object can be bogus if it doesn't exist
963 * @return FALSE if it is a real locale, TRUE if it is a bogus locale
964 * @stable ICU 2.1
965 */
966 inline UBool isBogus(void) const;
967
968 /**
969 * Returns a list of all installed locales.
970 * @param count Receives the number of locales in the list.
971 * @return A pointer to an array of Locale objects. This array is the list
972 * of all locales with installed resource files. The called does NOT
973 * get ownership of this list, and must NOT delete it.
974 * @stable ICU 2.0
975 */
976 static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
977
978 /**
979 * Gets a list of all available 2-letter country codes defined in ISO 3166. This is a
980 * pointer to an array of pointers to arrays of char. All of these pointers are
981 * owned by ICU-- do not delete them, and do not write through them. The array is
982 * terminated with a null pointer.
983 * @return a list of all available country codes
984 * @stable ICU 2.0
985 */
986 static const char* const* U_EXPORT2 getISOCountries();
987
988 /**
989 * Gets a list of all available language codes defined in ISO 639. This is a pointer
990 * to an array of pointers to arrays of char. All of these pointers are owned
991 * by ICU-- do not delete them, and do not write through them. The array is
992 * terminated with a null pointer.
993 * @return a list of all available language codes
994 * @stable ICU 2.0
995 */
996 static const char* const* U_EXPORT2 getISOLanguages();
997
998 /**
999 * ICU "poor man's RTTI", returns a UClassID for this class.
1000 *
1001 * @stable ICU 2.2
1002 */
1003 static UClassID U_EXPORT2 getStaticClassID();
1004
1005 /**
1006 * ICU "poor man's RTTI", returns a UClassID for the actual class.
1007 *
1008 * @stable ICU 2.2
1009 */
1010 virtual UClassID getDynamicClassID() const;
1011
1012 protected: /* only protected for testing purposes. DO NOT USE. */
1013 #ifndef U_HIDE_INTERNAL_API
1014 /**
1015 * Set this from a single POSIX style locale string.
1016 * @internal
1017 */
1018 void setFromPOSIXID(const char *posixID);
1019 #endif /* U_HIDE_INTERNAL_API */
1020
1021 private:
1022 /**
1023 * Initialize the locale object with a new name.
1024 * Was deprecated - used in implementation - moved internal
1025 *
1026 * @param cLocaleID The new locale name.
1027 * @param canonicalize whether to call uloc_canonicalize on cLocaleID
1028 */
1029 Locale& init(const char* cLocaleID, UBool canonicalize);
1030
1031 /*
1032 * Internal constructor to allow construction of a locale object with
1033 * NO side effects. (Default constructor tries to get
1034 * the default locale.)
1035 */
1036 enum ELocaleType {
1037 eBOGUS
1038 };
1039 Locale(ELocaleType);
1040
1041 /**
1042 * Initialize the locale cache for commonly used locales
1043 */
1044 static Locale *getLocaleCache(void);
1045
1046 char language[ULOC_LANG_CAPACITY];
1047 char script[ULOC_SCRIPT_CAPACITY];
1048 char country[ULOC_COUNTRY_CAPACITY];
1049 int32_t variantBegin;
1050 char* fullName;
1051 char fullNameBuffer[ULOC_FULLNAME_CAPACITY];
1052 // name without keywords
1053 char* baseName;
1054 void initBaseName(UErrorCode& status);
1055
1056 UBool fIsBogus;
1057
1058 static const Locale &getLocale(int locid);
1059
1060 /**
1061 * A friend to allow the default locale to be set by either the C or C++ API.
1062 * @internal (private)
1063 */
1064 friend Locale *locale_set_default_internal(const char *, UErrorCode& status);
1065
1066 /**
1067 * @internal (private)
1068 */
1069 friend void U_CALLCONV locale_available_init();
1070 };
1071
1072 inline UBool
1073 Locale::operator!=(const Locale& other) const
1074 {
1075 return !operator==(other);
1076 }
1077
1078 #ifndef U_HIDE_DRAFT_API
1079 template<typename StringClass> inline StringClass
1080 Locale::toLanguageTag(UErrorCode& status) const
1081 {
1082 StringClass result;
1083 StringByteSink<StringClass> sink(&result);
1084 toLanguageTag(sink, status);
1085 return result;
1086 }
1087 #endif // U_HIDE_DRAFT_API
1088
1089 inline const char *
1090 Locale::getCountry() const
1091 {
1092 return country;
1093 }
1094
1095 inline const char *
1096 Locale::getLanguage() const
1097 {
1098 return language;
1099 }
1100
1101 inline const char *
1102 Locale::getScript() const
1103 {
1104 return script;
1105 }
1106
1107 inline const char *
1108 Locale::getVariant() const
1109 {
1110 return &baseName[variantBegin];
1111 }
1112
1113 inline const char *
1114 Locale::getName() const
1115 {
1116 return fullName;
1117 }
1118
1119 #ifndef U_HIDE_DRAFT_API
1120
1121 template<typename StringClass, typename OutputIterator> inline void
1122 Locale::getKeywords(OutputIterator iterator, UErrorCode& status) const
1123 {
1124 LocalPointer<StringEnumeration> keys(createKeywords(status));
1125 if (U_FAILURE(status)) {
1126 return;
1127 }
1128 for (;;) {
1129 int32_t resultLength;
1130 const char* buffer = keys->next(&resultLength, status);
1131 if (U_FAILURE(status) || buffer == nullptr) {
1132 return;
1133 }
1134 *iterator++ = StringClass(buffer, resultLength);
1135 }
1136 }
1137
1138 template<typename StringClass, typename OutputIterator> inline void
1139 Locale::getUnicodeKeywords(OutputIterator iterator, UErrorCode& status) const
1140 {
1141 LocalPointer<StringEnumeration> keys(createUnicodeKeywords(status));
1142 if (U_FAILURE(status)) {
1143 return;
1144 }
1145 for (;;) {
1146 int32_t resultLength;
1147 const char* buffer = keys->next(&resultLength, status);
1148 if (U_FAILURE(status) || buffer == nullptr) {
1149 return;
1150 }
1151 *iterator++ = StringClass(buffer, resultLength);
1152 }
1153 }
1154
1155 template<typename StringClass> inline StringClass
1156 Locale::getKeywordValue(StringPiece keywordName, UErrorCode& status) const
1157 {
1158 StringClass result;
1159 StringByteSink<StringClass> sink(&result);
1160 getKeywordValue(keywordName, sink, status);
1161 return result;
1162 }
1163
1164 template<typename StringClass> inline StringClass
1165 Locale::getUnicodeKeywordValue(StringPiece keywordName, UErrorCode& status) const
1166 {
1167 StringClass result;
1168 StringByteSink<StringClass> sink(&result);
1169 getUnicodeKeywordValue(keywordName, sink, status);
1170 return result;
1171 }
1172
1173 #endif // U_HIDE_DRAFT_API
1174
1175 inline UBool
1176 Locale::isBogus(void) const {
1177 return fIsBogus;
1178 }
1179
1180 U_NAMESPACE_END
1181 #endif // U_SHOW_CPLUSPLUS_API
1182
1183 #endif