locid.h 48 KB

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