locid.h 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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();
  190. /** Useful constant for this language. @stable ICU 2.0 */
  191. static const Locale& U_EXPORT2 getEnglish();
  192. /** Useful constant for this language. @stable ICU 2.0 */
  193. static const Locale& U_EXPORT2 getFrench();
  194. /** Useful constant for this language. @stable ICU 2.0 */
  195. static const Locale& U_EXPORT2 getGerman();
  196. /** Useful constant for this language. @stable ICU 2.0 */
  197. static const Locale& U_EXPORT2 getItalian();
  198. /** Useful constant for this language. @stable ICU 2.0 */
  199. static const Locale& U_EXPORT2 getJapanese();
  200. /** Useful constant for this language. @stable ICU 2.0 */
  201. static const Locale& U_EXPORT2 getKorean();
  202. /** Useful constant for this language. @stable ICU 2.0 */
  203. static const Locale& U_EXPORT2 getChinese();
  204. /** Useful constant for this language. @stable ICU 2.0 */
  205. static const Locale& U_EXPORT2 getSimplifiedChinese();
  206. /** Useful constant for this language. @stable ICU 2.0 */
  207. static const Locale& U_EXPORT2 getTraditionalChinese();
  208. /** Useful constant for this country/region. @stable ICU 2.0 */
  209. static const Locale& U_EXPORT2 getFrance();
  210. /** Useful constant for this country/region. @stable ICU 2.0 */
  211. static const Locale& U_EXPORT2 getGermany();
  212. /** Useful constant for this country/region. @stable ICU 2.0 */
  213. static const Locale& U_EXPORT2 getItaly();
  214. /** Useful constant for this country/region. @stable ICU 2.0 */
  215. static const Locale& U_EXPORT2 getJapan();
  216. /** Useful constant for this country/region. @stable ICU 2.0 */
  217. static const Locale& U_EXPORT2 getKorea();
  218. /** Useful constant for this country/region. @stable ICU 2.0 */
  219. static const Locale& U_EXPORT2 getChina();
  220. /** Useful constant for this country/region. @stable ICU 2.0 */
  221. static const Locale& U_EXPORT2 getPRC();
  222. /** Useful constant for this country/region. @stable ICU 2.0 */
  223. static const Locale& U_EXPORT2 getTaiwan();
  224. /** Useful constant for this country/region. @stable ICU 2.0 */
  225. static const Locale& U_EXPORT2 getUK();
  226. /** Useful constant for this country/region. @stable ICU 2.0 */
  227. static const Locale& U_EXPORT2 getUS();
  228. /** Useful constant for this country/region. @stable ICU 2.0 */
  229. static const Locale& U_EXPORT2 getCanada();
  230. /** Useful constant for this country/region. @stable ICU 2.0 */
  231. static const Locale& U_EXPORT2 getCanadaFrench();
  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 nullptr; 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 = nullptr,
  266. const char* variant = nullptr,
  267. const char* keywordsAndValues = nullptr);
  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) 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) 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. bool 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 bool 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 nullptr 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();
  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 nullptr, 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 BCP 47, and so
  373. * supports legacy language tags (marked as “Type: grandfathered” in BCP 47)
  374. * (regular and irregular) as well as private use language tags.
  375. *
  376. * Private use tags are represented as 'x-whatever',
  377. * and legacy tags are converted to their canonical replacements where they exist.
  378. *
  379. * Note that a few legacy tags have no modern replacement;
  380. * these will be converted using the fallback described in
  381. * the first paragraph, so some information might be lost.
  382. *
  383. * @param tag the input BCP47 language tag.
  384. * @param status error information if creating the Locale failed.
  385. * @return the Locale for the specified BCP47 language tag.
  386. * @stable ICU 63
  387. */
  388. static Locale U_EXPORT2 forLanguageTag(StringPiece tag, UErrorCode& status);
  389. /**
  390. * Returns a well-formed language tag for this Locale.
  391. * <p>
  392. * <b>Note</b>: Any locale fields which do not satisfy the BCP47 syntax
  393. * requirement will be silently omitted from the result.
  394. *
  395. * If this function fails, partial output may have been written to the sink.
  396. *
  397. * @param sink the output sink receiving the BCP47 language
  398. * tag for this Locale.
  399. * @param status error information if creating the language tag failed.
  400. * @stable ICU 63
  401. */
  402. void toLanguageTag(ByteSink& sink, UErrorCode& status) const;
  403. /**
  404. * Returns a well-formed language tag for this Locale.
  405. * <p>
  406. * <b>Note</b>: Any locale fields which do not satisfy the BCP47 syntax
  407. * requirement will be silently omitted from the result.
  408. *
  409. * @param status error information if creating the language tag failed.
  410. * @return the BCP47 language tag for this Locale.
  411. * @stable ICU 63
  412. */
  413. template<typename StringClass>
  414. inline StringClass toLanguageTag(UErrorCode& status) const;
  415. /**
  416. * Creates a locale which has had minimal canonicalization
  417. * as per uloc_getName().
  418. * @param name The name to create from. If name is null,
  419. * the default Locale is used.
  420. * @return new locale object
  421. * @stable ICU 2.0
  422. * @see uloc_getName
  423. */
  424. static Locale U_EXPORT2 createFromName(const char *name);
  425. /**
  426. * Creates a locale from the given string after canonicalizing
  427. * the string according to CLDR by calling uloc_canonicalize().
  428. * @param name the locale ID to create from. Must not be nullptr.
  429. * @return a new locale object corresponding to the given name
  430. * @stable ICU 3.0
  431. * @see uloc_canonicalize
  432. */
  433. static Locale U_EXPORT2 createCanonical(const char* name);
  434. /**
  435. * Returns the locale's ISO-639 language code.
  436. * @return An alias to the code
  437. * @stable ICU 2.0
  438. */
  439. inline const char * getLanguage( ) const;
  440. /**
  441. * Returns the locale's ISO-15924 abbreviation script code.
  442. * @return An alias to the code
  443. * @see uscript_getShortName
  444. * @see uscript_getCode
  445. * @stable ICU 2.8
  446. */
  447. inline const char * getScript( ) const;
  448. /**
  449. * Returns the locale's ISO-3166 country code.
  450. * @return An alias to the code
  451. * @stable ICU 2.0
  452. */
  453. inline const char * getCountry( ) const;
  454. /**
  455. * Returns the locale's variant code.
  456. * @return An alias to the code
  457. * @stable ICU 2.0
  458. */
  459. inline const char * getVariant( ) const;
  460. /**
  461. * Returns the programmatic name of the entire locale, with the language,
  462. * country and variant separated by underbars. If a field is missing, up
  463. * to two leading underbars will occur. Example: "en", "de_DE", "en_US_WIN",
  464. * "de__POSIX", "fr__MAC", "__MAC", "_MT", "_FR_EURO"
  465. * @return A pointer to "name".
  466. * @stable ICU 2.0
  467. */
  468. inline const char * getName() const;
  469. /**
  470. * Returns the programmatic name of the entire locale as getName() would return,
  471. * but without keywords.
  472. * @return A pointer to "name".
  473. * @see getName
  474. * @stable ICU 2.8
  475. */
  476. const char * getBaseName() const;
  477. /**
  478. * Add the likely subtags for this Locale, per the algorithm described
  479. * in the following CLDR technical report:
  480. *
  481. * http://www.unicode.org/reports/tr35/#Likely_Subtags
  482. *
  483. * If this Locale is already in the maximal form, or not valid, or there is
  484. * no data available for maximization, the Locale will be unchanged.
  485. *
  486. * For example, "sh" cannot be maximized, since there is no
  487. * reasonable maximization.
  488. *
  489. * Examples:
  490. *
  491. * "und_Zzzz" maximizes to "en_Latn_US"
  492. *
  493. * "en" maximizes to "en_Latn_US"
  494. *
  495. * "de" maximizes to "de_Latn_DE"
  496. *
  497. * "sr" maximizes to "sr_Cyrl_RS"
  498. *
  499. * "zh_Hani" maximizes to "zh_Hani_CN"
  500. *
  501. * @param status error information if maximizing this Locale failed.
  502. * If this Locale is not well-formed, the error code is
  503. * U_ILLEGAL_ARGUMENT_ERROR.
  504. * @stable ICU 63
  505. */
  506. void addLikelySubtags(UErrorCode& status);
  507. /**
  508. * Minimize the subtags for this Locale, per the algorithm described
  509. * in the following CLDR technical report:
  510. *
  511. * http://www.unicode.org/reports/tr35/#Likely_Subtags
  512. *
  513. * If this Locale is already in the minimal form, or not valid, or there is
  514. * no data available for minimization, the Locale will be unchanged.
  515. *
  516. * Since the minimization algorithm relies on proper maximization, see the
  517. * comments for addLikelySubtags for reasons why there might not be any
  518. * data.
  519. *
  520. * Examples:
  521. *
  522. * "en_Latn_US" minimizes to "en"
  523. *
  524. * "de_Latn_US" minimizes to "de"
  525. *
  526. * "sr_Cyrl_RS" minimizes to "sr"
  527. *
  528. * "zh_Hant_TW" minimizes to "zh_TW" (The region is preferred to the
  529. * script, and minimizing to "zh" would imply "zh_Hans_CN".)
  530. *
  531. * @param status error information if maximizing this Locale failed.
  532. * If this Locale is not well-formed, the error code is
  533. * U_ILLEGAL_ARGUMENT_ERROR.
  534. * @stable ICU 63
  535. */
  536. void minimizeSubtags(UErrorCode& status);
  537. /**
  538. * Canonicalize the locale ID of this object according to CLDR.
  539. * @param status the status code
  540. * @stable ICU 67
  541. * @see createCanonical
  542. */
  543. void canonicalize(UErrorCode& status);
  544. /**
  545. * Gets the list of keywords for the specified locale.
  546. *
  547. * @param status the status code
  548. * @return pointer to StringEnumeration class, or nullptr if there are no keywords.
  549. * Client must dispose of it by calling delete.
  550. * @see getKeywords
  551. * @stable ICU 2.8
  552. */
  553. StringEnumeration * createKeywords(UErrorCode &status) const;
  554. /**
  555. * Gets the list of Unicode keywords for the specified locale.
  556. *
  557. * @param status the status code
  558. * @return pointer to StringEnumeration class, or nullptr if there are no keywords.
  559. * Client must dispose of it by calling delete.
  560. * @see getUnicodeKeywords
  561. * @stable ICU 63
  562. */
  563. StringEnumeration * createUnicodeKeywords(UErrorCode &status) const;
  564. /**
  565. * Gets the set of keywords for this Locale.
  566. *
  567. * A wrapper to call createKeywords() and write the resulting
  568. * keywords as standard strings (or compatible objects) into any kind of
  569. * container that can be written to by an STL style output iterator.
  570. *
  571. * @param iterator an STL style output iterator to write the keywords to.
  572. * @param status error information if creating set of keywords failed.
  573. * @stable ICU 63
  574. */
  575. template<typename StringClass, typename OutputIterator>
  576. inline void getKeywords(OutputIterator iterator, UErrorCode& status) const;
  577. /**
  578. * Gets the set of Unicode keywords for this Locale.
  579. *
  580. * A wrapper to call createUnicodeKeywords() and write the resulting
  581. * keywords as standard strings (or compatible objects) into any kind of
  582. * container that can be written to by an STL style output iterator.
  583. *
  584. * @param iterator an STL style output iterator to write the keywords to.
  585. * @param status error information if creating set of keywords failed.
  586. * @stable ICU 63
  587. */
  588. template<typename StringClass, typename OutputIterator>
  589. inline void getUnicodeKeywords(OutputIterator iterator, UErrorCode& status) const;
  590. /**
  591. * Gets the value for a keyword.
  592. *
  593. * This uses legacy keyword=value pairs, like "collation=phonebook".
  594. *
  595. * ICU4C doesn't do automatic conversion between legacy and Unicode
  596. * keywords and values in getters and setters (as opposed to ICU4J).
  597. *
  598. * @param keywordName name of the keyword for which we want the value. Case insensitive.
  599. * @param buffer The buffer to receive the keyword value.
  600. * @param bufferCapacity The capacity of receiving buffer
  601. * @param status Returns any error information while performing this operation.
  602. * @return the length of the keyword value
  603. *
  604. * @stable ICU 2.8
  605. */
  606. int32_t getKeywordValue(const char* keywordName, char *buffer, int32_t bufferCapacity, UErrorCode &status) const;
  607. /**
  608. * Gets the value for a keyword.
  609. *
  610. * This uses legacy keyword=value pairs, like "collation=phonebook".
  611. *
  612. * ICU4C doesn't do automatic conversion between legacy and Unicode
  613. * keywords and values in getters and setters (as opposed to ICU4J).
  614. *
  615. * @param keywordName name of the keyword for which we want the value.
  616. * @param sink the sink to receive the keyword value.
  617. * @param status error information if getting the value failed.
  618. * @stable ICU 63
  619. */
  620. void getKeywordValue(StringPiece keywordName, ByteSink& sink, UErrorCode& status) const;
  621. /**
  622. * Gets the value for a keyword.
  623. *
  624. * This uses legacy keyword=value pairs, like "collation=phonebook".
  625. *
  626. * ICU4C doesn't do automatic conversion between legacy and Unicode
  627. * keywords and values in getters and setters (as opposed to ICU4J).
  628. *
  629. * @param keywordName name of the keyword for which we want the value.
  630. * @param status error information if getting the value failed.
  631. * @return the keyword value.
  632. * @stable ICU 63
  633. */
  634. template<typename StringClass>
  635. inline StringClass getKeywordValue(StringPiece keywordName, UErrorCode& status) const;
  636. /**
  637. * Gets the Unicode value for a Unicode keyword.
  638. *
  639. * This uses Unicode key-value pairs, like "co-phonebk".
  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.
  645. * @param sink the sink to receive the keyword value.
  646. * @param status error information if getting the value failed.
  647. * @stable ICU 63
  648. */
  649. void getUnicodeKeywordValue(StringPiece keywordName, ByteSink& sink, UErrorCode& status) const;
  650. /**
  651. * Gets the Unicode value for a Unicode keyword.
  652. *
  653. * This uses Unicode key-value pairs, like "co-phonebk".
  654. *
  655. * ICU4C doesn't do automatic conversion between legacy and Unicode
  656. * keywords and values in getters and setters (as opposed to ICU4J).
  657. *
  658. * @param keywordName name of the keyword for which we want the value.
  659. * @param status error information if getting the value failed.
  660. * @return the keyword value.
  661. * @stable ICU 63
  662. */
  663. template<typename StringClass>
  664. inline StringClass getUnicodeKeywordValue(StringPiece keywordName, UErrorCode& status) const;
  665. /**
  666. * Sets or removes the value for a keyword.
  667. *
  668. * For removing all keywords, use getBaseName(),
  669. * and construct a new Locale if it differs from getName().
  670. *
  671. * This uses legacy keyword=value pairs, like "collation=phonebook".
  672. *
  673. * ICU4C doesn't do automatic conversion between legacy and Unicode
  674. * keywords and values in getters and setters (as opposed to ICU4J).
  675. *
  676. * @param keywordName name of the keyword to be set. Case insensitive.
  677. * @param keywordValue value of the keyword to be set. If 0-length or
  678. * nullptr, will result in the keyword being removed. No error is given if
  679. * that keyword does not exist.
  680. * @param status Returns any error information while performing this operation.
  681. *
  682. * @stable ICU 49
  683. */
  684. void setKeywordValue(const char* keywordName, const char* keywordValue, UErrorCode &status) {
  685. setKeywordValue(StringPiece{keywordName}, StringPiece{keywordValue}, status);
  686. }
  687. /**
  688. * Sets or removes the value for a keyword.
  689. *
  690. * For removing all keywords, use getBaseName(),
  691. * and construct a new Locale if it differs from getName().
  692. *
  693. * This uses legacy keyword=value pairs, like "collation=phonebook".
  694. *
  695. * ICU4C doesn't do automatic conversion between legacy and Unicode
  696. * keywords and values in getters and setters (as opposed to ICU4J).
  697. *
  698. * @param keywordName name of the keyword to be set.
  699. * @param keywordValue value of the keyword to be set. If 0-length or
  700. * nullptr, will result in the keyword being removed. No error is given if
  701. * that keyword does not exist.
  702. * @param status Returns any error information while performing this operation.
  703. * @stable ICU 63
  704. */
  705. void setKeywordValue(StringPiece keywordName, StringPiece keywordValue, UErrorCode& status);
  706. /**
  707. * Sets or removes the Unicode value for a Unicode keyword.
  708. *
  709. * For removing all keywords, use getBaseName(),
  710. * and construct a new Locale if it differs from getName().
  711. *
  712. * This uses Unicode key-value pairs, like "co-phonebk".
  713. *
  714. * ICU4C doesn't do automatic conversion between legacy and Unicode
  715. * keywords and values in getters and setters (as opposed to ICU4J).
  716. *
  717. * @param keywordName name of the keyword to be set.
  718. * @param keywordValue value of the keyword to be set. If 0-length or
  719. * nullptr, will result in the keyword being removed. No error is given if
  720. * that keyword does not exist.
  721. * @param status Returns any error information while performing this operation.
  722. * @stable ICU 63
  723. */
  724. void setUnicodeKeywordValue(StringPiece keywordName, StringPiece keywordValue, UErrorCode& status);
  725. /**
  726. * returns the locale's three-letter language code, as specified
  727. * in ISO draft standard ISO-639-2.
  728. * @return An alias to the code, or an empty string
  729. * @stable ICU 2.0
  730. */
  731. const char * getISO3Language() const;
  732. /**
  733. * Fills in "name" with the locale's three-letter ISO-3166 country code.
  734. * @return An alias to the code, or an empty string
  735. * @stable ICU 2.0
  736. */
  737. const char * getISO3Country() const;
  738. /**
  739. * Returns the Windows LCID value corresponding to this locale.
  740. * This value is stored in the resource data for the locale as a one-to-four-digit
  741. * hexadecimal number. If the resource is missing, in the wrong format, or
  742. * there is no Windows LCID value that corresponds to this locale, returns 0.
  743. * @stable ICU 2.0
  744. */
  745. uint32_t getLCID() const;
  746. /**
  747. * Returns whether this locale's script is written right-to-left.
  748. * If there is no script subtag, then the likely script is used, see uloc_addLikelySubtags().
  749. * If no likely script is known, then false is returned.
  750. *
  751. * A script is right-to-left according to the CLDR script metadata
  752. * which corresponds to whether the script's letters have Bidi_Class=R or AL.
  753. *
  754. * Returns true for "ar" and "en-Hebr", false for "zh" and "fa-Cyrl".
  755. *
  756. * @return true if the locale's script is written right-to-left
  757. * @stable ICU 54
  758. */
  759. UBool isRightToLeft() const;
  760. /**
  761. * Fills in "dispLang" with the name of this locale's language in a format suitable for
  762. * user display in the default locale. For example, if the locale's language code is
  763. * "fr" and the default locale's language code is "en", this function would set
  764. * dispLang to "French".
  765. * @param dispLang Receives the language's display name.
  766. * @return A reference to "dispLang".
  767. * @stable ICU 2.0
  768. */
  769. UnicodeString& getDisplayLanguage(UnicodeString& dispLang) const;
  770. /**
  771. * Fills in "dispLang" with the name of this locale's language in a format suitable for
  772. * user display in the locale specified by "displayLocale". For example, if the locale's
  773. * language code is "en" and displayLocale's language code is "fr", this function would set
  774. * dispLang to "Anglais".
  775. * @param displayLocale Specifies the locale to be used to display the name. In other words,
  776. * if the locale's language code is "en", passing Locale::getFrench() for
  777. * displayLocale would result in "Anglais", while passing Locale::getGerman()
  778. * for displayLocale would result in "Englisch".
  779. * @param dispLang Receives the language's display name.
  780. * @return A reference to "dispLang".
  781. * @stable ICU 2.0
  782. */
  783. UnicodeString& getDisplayLanguage( const Locale& displayLocale,
  784. UnicodeString& dispLang) const;
  785. /**
  786. * Fills in "dispScript" with the name of this locale's script in a format suitable
  787. * for user display in the default locale. For example, if the locale's script code
  788. * is "LATN" and the default locale's language code is "en", this function would set
  789. * dispScript to "Latin".
  790. * @param dispScript Receives the scripts's display name.
  791. * @return A reference to "dispScript".
  792. * @stable ICU 2.8
  793. */
  794. UnicodeString& getDisplayScript( UnicodeString& dispScript) const;
  795. /**
  796. * Fills in "dispScript" with the name of this locale's country in a format suitable
  797. * for user display in the locale specified by "displayLocale". For example, if the locale's
  798. * script code is "LATN" and displayLocale's language code is "en", this function would set
  799. * dispScript to "Latin".
  800. * @param displayLocale Specifies the locale to be used to display the name. In other
  801. * words, if the locale's script code is "LATN", passing
  802. * Locale::getFrench() for displayLocale would result in "", while
  803. * passing Locale::getGerman() for displayLocale would result in
  804. * "".
  805. * @param dispScript Receives the scripts's display name.
  806. * @return A reference to "dispScript".
  807. * @stable ICU 2.8
  808. */
  809. UnicodeString& getDisplayScript( const Locale& displayLocale,
  810. UnicodeString& dispScript) const;
  811. /**
  812. * Fills in "dispCountry" with the name of this locale's country in a format suitable
  813. * for user display in the default locale. For example, if the locale's country code
  814. * is "FR" and the default locale's language code is "en", this function would set
  815. * dispCountry to "France".
  816. * @param dispCountry Receives the country's display name.
  817. * @return A reference to "dispCountry".
  818. * @stable ICU 2.0
  819. */
  820. UnicodeString& getDisplayCountry( UnicodeString& dispCountry) const;
  821. /**
  822. * Fills in "dispCountry" with the name of this locale's country in a format suitable
  823. * for user display in the locale specified by "displayLocale". For example, if the locale's
  824. * country code is "US" and displayLocale's language code is "fr", this function would set
  825. * dispCountry to "&Eacute;tats-Unis".
  826. * @param displayLocale Specifies the locale to be used to display the name. In other
  827. * words, if the locale's country code is "US", passing
  828. * Locale::getFrench() for displayLocale would result in "&Eacute;tats-Unis", while
  829. * passing Locale::getGerman() for displayLocale would result in
  830. * "Vereinigte Staaten".
  831. * @param dispCountry Receives the country's display name.
  832. * @return A reference to "dispCountry".
  833. * @stable ICU 2.0
  834. */
  835. UnicodeString& getDisplayCountry( const Locale& displayLocale,
  836. UnicodeString& dispCountry) const;
  837. /**
  838. * Fills in "dispVar" with the name of this locale's variant code in a format suitable
  839. * for user display in the default locale.
  840. * @param dispVar Receives the variant's name.
  841. * @return A reference to "dispVar".
  842. * @stable ICU 2.0
  843. */
  844. UnicodeString& getDisplayVariant( UnicodeString& dispVar) const;
  845. /**
  846. * Fills in "dispVar" with the name of this locale's variant code in a format
  847. * suitable for user display in the locale specified by "displayLocale".
  848. * @param displayLocale Specifies the locale to be used to display the name.
  849. * @param dispVar Receives the variant's display name.
  850. * @return A reference to "dispVar".
  851. * @stable ICU 2.0
  852. */
  853. UnicodeString& getDisplayVariant( const Locale& displayLocale,
  854. UnicodeString& dispVar) const;
  855. /**
  856. * Fills in "name" with the name of this locale in a format suitable for user display
  857. * in the default locale. This function uses getDisplayLanguage(), getDisplayCountry(),
  858. * and getDisplayVariant() to do its work, and outputs the display name in the format
  859. * "language (country[,variant])". For example, if the default locale is en_US, then
  860. * fr_FR's display name would be "French (France)", and es_MX_Traditional's display name
  861. * would be "Spanish (Mexico,Traditional)".
  862. * @param name Receives the locale's display name.
  863. * @return A reference to "name".
  864. * @stable ICU 2.0
  865. */
  866. UnicodeString& getDisplayName( UnicodeString& name) const;
  867. /**
  868. * Fills in "name" with the name of this locale in a format suitable for user display
  869. * in the locale specified by "displayLocale". This function uses getDisplayLanguage(),
  870. * getDisplayCountry(), and getDisplayVariant() to do its work, and outputs the display
  871. * name in the format "language (country[,variant])". For example, if displayLocale is
  872. * fr_FR, then en_US's display name would be "Anglais (&Eacute;tats-Unis)", and no_NO_NY's
  873. * display name would be "norv&eacute;gien (Norv&egrave;ge,NY)".
  874. * @param displayLocale Specifies the locale to be used to display the name.
  875. * @param name Receives the locale's display name.
  876. * @return A reference to "name".
  877. * @stable ICU 2.0
  878. */
  879. UnicodeString& getDisplayName( const Locale& displayLocale,
  880. UnicodeString& name) const;
  881. /**
  882. * Generates a hash code for the locale.
  883. * @stable ICU 2.0
  884. */
  885. int32_t hashCode() const;
  886. /**
  887. * Sets the locale to bogus
  888. * A bogus locale represents a non-existing locale associated
  889. * with services that can be instantiated from non-locale data
  890. * in addition to locale (for example, collation can be
  891. * instantiated from a locale and from a rule set).
  892. * @stable ICU 2.1
  893. */
  894. void setToBogus();
  895. /**
  896. * Gets the bogus state. Locale object can be bogus if it doesn't exist
  897. * @return false if it is a real locale, true if it is a bogus locale
  898. * @stable ICU 2.1
  899. */
  900. inline UBool isBogus() const;
  901. /**
  902. * Returns a list of all installed locales.
  903. * @param count Receives the number of locales in the list.
  904. * @return A pointer to an array of Locale objects. This array is the list
  905. * of all locales with installed resource files. The called does NOT
  906. * get ownership of this list, and must NOT delete it.
  907. * @stable ICU 2.0
  908. */
  909. static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
  910. /**
  911. * Gets a list of all available 2-letter country codes defined in ISO 3166. This is a
  912. * pointer to an array of pointers to arrays of char. All of these pointers are
  913. * owned by ICU-- do not delete them, and do not write through them. The array is
  914. * terminated with a null pointer.
  915. * @return a list of all available country codes
  916. * @stable ICU 2.0
  917. */
  918. static const char* const* U_EXPORT2 getISOCountries();
  919. /**
  920. * Returns a list of all unique language codes defined in ISO 639.
  921. * They can be 2 or 3 letter codes, as defined by
  922. * <a href="https://www.ietf.org/rfc/bcp/bcp47.html#section-2.2.1">
  923. * BCP 47, section 2.2.1</a>. This is a pointer
  924. * to an array of pointers to arrays of char. All of these pointers are owned
  925. * by ICU-- do not delete them, and do not write through them. The array is
  926. * terminated with a null pointer.
  927. * @return a list of all available language codes
  928. * @stable ICU 2.0
  929. */
  930. static const char* const* U_EXPORT2 getISOLanguages();
  931. /**
  932. * ICU "poor man's RTTI", returns a UClassID for this class.
  933. *
  934. * @stable ICU 2.2
  935. */
  936. static UClassID U_EXPORT2 getStaticClassID();
  937. /**
  938. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  939. *
  940. * @stable ICU 2.2
  941. */
  942. virtual UClassID getDynamicClassID() const override;
  943. /**
  944. * A Locale iterator interface similar to a Java Iterator<Locale>.
  945. * @stable ICU 65
  946. */
  947. class U_COMMON_API Iterator /* not : public UObject because this is an interface/mixin class */ {
  948. public:
  949. /** @stable ICU 65 */
  950. virtual ~Iterator();
  951. /**
  952. * @return true if next() can be called again.
  953. * @stable ICU 65
  954. */
  955. virtual UBool hasNext() const = 0;
  956. /**
  957. * @return the next locale.
  958. * @stable ICU 65
  959. */
  960. virtual const Locale &next() = 0;
  961. };
  962. /**
  963. * A generic Locale iterator implementation over Locale input iterators.
  964. * @stable ICU 65
  965. */
  966. template<typename Iter>
  967. class RangeIterator : public Iterator, public UMemory {
  968. public:
  969. /**
  970. * Constructs an iterator from a begin/end range.
  971. * Each of the iterator parameter values must be an
  972. * input iterator whose value is convertible to const Locale &.
  973. *
  974. * @param begin Start of range.
  975. * @param end Exclusive end of range.
  976. * @stable ICU 65
  977. */
  978. RangeIterator(Iter begin, Iter end) : it_(begin), end_(end) {}
  979. /**
  980. * @return true if next() can be called again.
  981. * @stable ICU 65
  982. */
  983. UBool hasNext() const override { return it_ != end_; }
  984. /**
  985. * @return the next locale.
  986. * @stable ICU 65
  987. */
  988. const Locale &next() override { return *it_++; }
  989. private:
  990. Iter it_;
  991. const Iter end_;
  992. };
  993. /**
  994. * A generic Locale iterator implementation over Locale input iterators.
  995. * Calls the converter to convert each *begin to a const Locale &.
  996. * @stable ICU 65
  997. */
  998. template<typename Iter, typename Conv>
  999. class ConvertingIterator : public Iterator, public UMemory {
  1000. public:
  1001. /**
  1002. * Constructs an iterator from a begin/end range.
  1003. * Each of the iterator parameter values must be an
  1004. * input iterator whose value the converter converts to const Locale &.
  1005. *
  1006. * @param begin Start of range.
  1007. * @param end Exclusive end of range.
  1008. * @param converter Converter from *begin to const Locale & or compatible.
  1009. * @stable ICU 65
  1010. */
  1011. ConvertingIterator(Iter begin, Iter end, Conv converter) :
  1012. it_(begin), end_(end), converter_(converter) {}
  1013. /**
  1014. * @return true if next() can be called again.
  1015. * @stable ICU 65
  1016. */
  1017. UBool hasNext() const override { return it_ != end_; }
  1018. /**
  1019. * @return the next locale.
  1020. * @stable ICU 65
  1021. */
  1022. const Locale &next() override { return converter_(*it_++); }
  1023. private:
  1024. Iter it_;
  1025. const Iter end_;
  1026. Conv converter_;
  1027. };
  1028. protected: /* only protected for testing purposes. DO NOT USE. */
  1029. #ifndef U_HIDE_INTERNAL_API
  1030. /**
  1031. * Set this from a single POSIX style locale string.
  1032. * @internal
  1033. */
  1034. void setFromPOSIXID(const char *posixID);
  1035. /**
  1036. * Minimize the subtags for this Locale, per the algorithm described
  1037. * @param favorScript favor to keep script if true, to keep region if false.
  1038. * @param status error information if maximizing this Locale failed.
  1039. * If this Locale is not well-formed, the error code is
  1040. * U_ILLEGAL_ARGUMENT_ERROR.
  1041. * @internal
  1042. */
  1043. void minimizeSubtags(bool favorScript, UErrorCode& status);
  1044. #endif /* U_HIDE_INTERNAL_API */
  1045. private:
  1046. /**
  1047. * Initialize the locale object with a new name.
  1048. * Was deprecated - used in implementation - moved internal
  1049. *
  1050. * @param cLocaleID The new locale name.
  1051. * @param canonicalize whether to call uloc_canonicalize on cLocaleID
  1052. */
  1053. Locale& init(const char* cLocaleID, UBool canonicalize);
  1054. /*
  1055. * Internal constructor to allow construction of a locale object with
  1056. * NO side effects. (Default constructor tries to get
  1057. * the default locale.)
  1058. */
  1059. enum ELocaleType {
  1060. eBOGUS
  1061. };
  1062. Locale(ELocaleType);
  1063. /**
  1064. * Initialize the locale cache for commonly used locales
  1065. */
  1066. static Locale* getLocaleCache();
  1067. char language[ULOC_LANG_CAPACITY];
  1068. char script[ULOC_SCRIPT_CAPACITY];
  1069. char country[ULOC_COUNTRY_CAPACITY];
  1070. int32_t variantBegin;
  1071. char* fullName;
  1072. char fullNameBuffer[ULOC_FULLNAME_CAPACITY];
  1073. // name without keywords
  1074. char* baseName;
  1075. void initBaseName(UErrorCode& status);
  1076. UBool fIsBogus;
  1077. static const Locale &getLocale(int locid);
  1078. /**
  1079. * A friend to allow the default locale to be set by either the C or C++ API.
  1080. * @internal (private)
  1081. */
  1082. friend Locale *locale_set_default_internal(const char *, UErrorCode& status);
  1083. /**
  1084. * @internal (private)
  1085. */
  1086. friend void U_CALLCONV locale_available_init();
  1087. };
  1088. inline bool
  1089. Locale::operator!=(const Locale& other) const
  1090. {
  1091. return !operator==(other);
  1092. }
  1093. template<typename StringClass> inline StringClass
  1094. Locale::toLanguageTag(UErrorCode& status) const
  1095. {
  1096. if (U_FAILURE(status)) { return {}; }
  1097. StringClass result;
  1098. StringByteSink<StringClass> sink(&result);
  1099. toLanguageTag(sink, status);
  1100. return result;
  1101. }
  1102. inline const char *
  1103. Locale::getCountry() const
  1104. {
  1105. return country;
  1106. }
  1107. inline const char *
  1108. Locale::getLanguage() const
  1109. {
  1110. return language;
  1111. }
  1112. inline const char *
  1113. Locale::getScript() const
  1114. {
  1115. return script;
  1116. }
  1117. inline const char *
  1118. Locale::getVariant() const
  1119. {
  1120. return fIsBogus ? "" : &baseName[variantBegin];
  1121. }
  1122. inline const char *
  1123. Locale::getName() const
  1124. {
  1125. return fullName;
  1126. }
  1127. template<typename StringClass, typename OutputIterator> inline void
  1128. Locale::getKeywords(OutputIterator iterator, UErrorCode& status) const
  1129. {
  1130. if (U_FAILURE(status)) { return; }
  1131. LocalPointer<StringEnumeration> keys(createKeywords(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, typename OutputIterator> inline void
  1145. Locale::getUnicodeKeywords(OutputIterator iterator, UErrorCode& status) const
  1146. {
  1147. if (U_FAILURE(status)) { return; }
  1148. LocalPointer<StringEnumeration> keys(createUnicodeKeywords(status));
  1149. if (U_FAILURE(status) || keys.isNull()) {
  1150. return;
  1151. }
  1152. for (;;) {
  1153. int32_t resultLength;
  1154. const char* buffer = keys->next(&resultLength, status);
  1155. if (U_FAILURE(status) || buffer == nullptr) {
  1156. return;
  1157. }
  1158. *iterator++ = StringClass(buffer, resultLength);
  1159. }
  1160. }
  1161. template<typename StringClass> inline StringClass
  1162. Locale::getKeywordValue(StringPiece keywordName, UErrorCode& status) const
  1163. {
  1164. if (U_FAILURE(status)) { return {}; }
  1165. StringClass result;
  1166. StringByteSink<StringClass> sink(&result);
  1167. getKeywordValue(keywordName, sink, status);
  1168. return result;
  1169. }
  1170. template<typename StringClass> inline StringClass
  1171. Locale::getUnicodeKeywordValue(StringPiece keywordName, UErrorCode& status) const
  1172. {
  1173. if (U_FAILURE(status)) { return {}; }
  1174. StringClass result;
  1175. StringByteSink<StringClass> sink(&result);
  1176. getUnicodeKeywordValue(keywordName, sink, status);
  1177. return result;
  1178. }
  1179. inline UBool
  1180. Locale::isBogus() const {
  1181. return fIsBogus;
  1182. }
  1183. U_NAMESPACE_END
  1184. #endif /* U_SHOW_CPLUSPLUS_API */
  1185. #endif