Debian Codegraph @

intl/icu/source/common/unicode/locid.h

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