ICU 77.1  77.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
locid.h
Go to the documentation of this file.
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 ******************************************************************************
5 *
6 * Copyright (C) 1996-2015, International Business Machines
7 * Corporation and others. All Rights Reserved.
8 *
9 ******************************************************************************
10 *
11 * File locid.h
12 *
13 * Created by: Helena Shih
14 *
15 * Modification History:
16 *
17 * Date Name Description
18 * 02/11/97 aliu Changed gLocPath to fgLocPath and added methods to
19 * get and set it.
20 * 04/02/97 aliu Made operator!= inline; fixed return value of getName().
21 * 04/15/97 aliu Cleanup for AIX/Win32.
22 * 04/24/97 aliu Numerous changes per code review.
23 * 08/18/98 stephen Added tokenizeString(),changed getDisplayName()
24 * 09/08/98 stephen Moved definition of kEmptyString for Mac Port
25 * 11/09/99 weiv Added const char * getName() const;
26 * 04/12/00 srl removing unicodestring api's and cached hash code
27 * 08/10/01 grhoten Change the static Locales to accessor functions
28 ******************************************************************************
29 */
30 
31 #ifndef LOCID_H
32 #define LOCID_H
33 
34 #include "unicode/utypes.h"
35 
36 #if U_SHOW_CPLUSPLUS_API
37 
38 #include "unicode/bytestream.h"
39 #include "unicode/localpointer.h"
40 #include "unicode/strenum.h"
41 #include "unicode/stringpiece.h"
42 #include "unicode/uobject.h"
43 #include "unicode/putil.h"
44 #include "unicode/uloc.h"
45 
51 U_NAMESPACE_BEGIN
52 
53 // Forward Declarations
56 class StringEnumeration;
57 class UnicodeString;
58 
195 class U_COMMON_API Locale : public UObject {
196 public:
198  static const Locale& U_EXPORT2 getRoot();
200  static const Locale& U_EXPORT2 getEnglish();
202  static const Locale& U_EXPORT2 getFrench();
204  static const Locale& U_EXPORT2 getGerman();
206  static const Locale& U_EXPORT2 getItalian();
208  static const Locale& U_EXPORT2 getJapanese();
210  static const Locale& U_EXPORT2 getKorean();
212  static const Locale& U_EXPORT2 getChinese();
214  static const Locale& U_EXPORT2 getSimplifiedChinese();
216  static const Locale& U_EXPORT2 getTraditionalChinese();
217 
219  static const Locale& U_EXPORT2 getFrance();
221  static const Locale& U_EXPORT2 getGermany();
223  static const Locale& U_EXPORT2 getItaly();
225  static const Locale& U_EXPORT2 getJapan();
227  static const Locale& U_EXPORT2 getKorea();
229  static const Locale& U_EXPORT2 getChina();
231  static const Locale& U_EXPORT2 getPRC();
233  static const Locale& U_EXPORT2 getTaiwan();
235  static const Locale& U_EXPORT2 getUK();
237  static const Locale& U_EXPORT2 getUS();
239  static const Locale& U_EXPORT2 getCanada();
241  static const Locale& U_EXPORT2 getCanadaFrench();
242 
250  Locale();
251 
276  Locale(const char* language,
277  const char* country = nullptr,
278  const char* variant = nullptr,
279  const char* keywordsAndValues = nullptr);
280 
287  Locale(const Locale& other);
288 
296  Locale(Locale&& other) noexcept;
297 
302  virtual ~Locale() ;
303 
311  Locale& operator=(const Locale& other);
312 
322  Locale& operator=(Locale&& other) noexcept;
323 
331  bool operator==(const Locale& other) const;
332 
341  inline bool operator!=(const Locale& other) const;
342 
354  Locale *clone() const;
355 
356 #ifndef U_HIDE_SYSTEM_API
357 
372  static const Locale& U_EXPORT2 getDefault();
373 
386  static void U_EXPORT2 setDefault(const Locale& newLocale,
387  UErrorCode& success);
388 #endif /* U_HIDE_SYSTEM_API */
389 
411  static Locale U_EXPORT2 forLanguageTag(StringPiece tag, UErrorCode& status);
412 
426  void toLanguageTag(ByteSink& sink, UErrorCode& status) const;
427 
438  template<typename StringClass>
439  inline StringClass toLanguageTag(UErrorCode& status) const;
440 
450  static Locale U_EXPORT2 createFromName(const char *name);
451 
452 #ifndef U_HIDE_INTERNAL_API
453 
454  static Locale U_EXPORT2 createFromName(StringPiece name);
455 #endif /* U_HIDE_INTERNAL_API */
456 
465  static Locale U_EXPORT2 createCanonical(const char* name);
466 
472  inline const char * getLanguage( ) const;
473 
481  inline const char * getScript( ) const;
482 
488  inline const char * getCountry( ) const;
489 
495  inline const char * getVariant( ) const;
496 
505  inline const char * getName() const;
506 
514  const char * getBaseName() const;
515 
545  void addLikelySubtags(UErrorCode& status);
546 
576  void minimizeSubtags(UErrorCode& status);
577 
584  void canonicalize(UErrorCode& status);
585 
595  StringEnumeration * createKeywords(UErrorCode &status) const;
596 
606  StringEnumeration * createUnicodeKeywords(UErrorCode &status) const;
607 
619  template<typename StringClass, typename OutputIterator>
620  inline void getKeywords(OutputIterator iterator, UErrorCode& status) const;
621 
633  template<typename StringClass, typename OutputIterator>
634  inline void getUnicodeKeywords(OutputIterator iterator, UErrorCode& status) const;
635 
652  int32_t getKeywordValue(const char* keywordName, char *buffer, int32_t bufferCapacity, UErrorCode &status) const;
653 
667  void getKeywordValue(StringPiece keywordName, ByteSink& sink, UErrorCode& status) const;
668 
682  template<typename StringClass>
683  inline StringClass getKeywordValue(StringPiece keywordName, UErrorCode& status) const;
684 
698  void getUnicodeKeywordValue(StringPiece keywordName, ByteSink& sink, UErrorCode& status) const;
699 
713  template<typename StringClass>
714  inline StringClass getUnicodeKeywordValue(StringPiece keywordName, UErrorCode& status) const;
715 
735  void setKeywordValue(const char* keywordName, const char* keywordValue, UErrorCode &status) {
736  setKeywordValue(StringPiece{keywordName}, StringPiece{keywordValue}, status);
737  }
738 
757  void setKeywordValue(StringPiece keywordName, StringPiece keywordValue, UErrorCode& status);
758 
777  void setUnicodeKeywordValue(StringPiece keywordName, StringPiece keywordValue, UErrorCode& status);
778 
785  const char * getISO3Language() const;
786 
792  const char * getISO3Country() const;
793 
801  uint32_t getLCID() const;
802 
816  UBool isRightToLeft() const;
817 
827  UnicodeString& getDisplayLanguage(UnicodeString& dispLang) const;
828 
842  UnicodeString& getDisplayLanguage( const Locale& displayLocale,
843  UnicodeString& dispLang) const;
844 
854  UnicodeString& getDisplayScript( UnicodeString& dispScript) const;
855 
870  UnicodeString& getDisplayScript( const Locale& displayLocale,
871  UnicodeString& dispScript) const;
872 
882  UnicodeString& getDisplayCountry( UnicodeString& dispCountry) const;
883 
898  UnicodeString& getDisplayCountry( const Locale& displayLocale,
899  UnicodeString& dispCountry) const;
900 
908  UnicodeString& getDisplayVariant( UnicodeString& dispVar) const;
909 
918  UnicodeString& getDisplayVariant( const Locale& displayLocale,
919  UnicodeString& dispVar) const;
920 
932  UnicodeString& getDisplayName( UnicodeString& name) const;
933 
946  UnicodeString& getDisplayName( const Locale& displayLocale,
947  UnicodeString& name) const;
948 
953  int32_t hashCode() const;
954 
963  void setToBogus();
964 
970  inline UBool isBogus() const;
971 
980  static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
981 
990  static const char* const* U_EXPORT2 getISOCountries();
991 
1003  static const char* const* U_EXPORT2 getISOLanguages();
1004 
1010  static UClassID U_EXPORT2 getStaticClassID();
1011 
1017  virtual UClassID getDynamicClassID() const override;
1018 
1023  class U_COMMON_API Iterator /* not : public UObject because this is an interface/mixin class */ {
1024  public:
1026  virtual ~Iterator();
1027 
1032  virtual UBool hasNext() const = 0;
1033 
1038  virtual const Locale &next() = 0;
1039  };
1040 
1045  template<typename Iter>
1046  class RangeIterator : public Iterator, public UMemory {
1047  public:
1057  RangeIterator(Iter begin, Iter end) : it_(begin), end_(end) {}
1058 
1063  UBool hasNext() const override { return it_ != end_; }
1064 
1069  const Locale &next() override { return *it_++; }
1070 
1071  private:
1072  Iter it_;
1073  const Iter end_;
1074  };
1075 
1081  template<typename Iter, typename Conv>
1082  class ConvertingIterator : public Iterator, public UMemory {
1083  public:
1094  ConvertingIterator(Iter begin, Iter end, Conv converter) :
1095  it_(begin), end_(end), converter_(converter) {}
1096 
1101  UBool hasNext() const override { return it_ != end_; }
1102 
1107  const Locale &next() override { return converter_(*it_++); }
1108 
1109  private:
1110  Iter it_;
1111  const Iter end_;
1112  Conv converter_;
1113  };
1114 
1115 protected: /* only protected for testing purposes. DO NOT USE. */
1116 #ifndef U_HIDE_INTERNAL_API
1117 
1121  void setFromPOSIXID(const char *posixID);
1130  void minimizeSubtags(bool favorScript, UErrorCode& status);
1131 #endif /* U_HIDE_INTERNAL_API */
1132 
1133 private:
1141  Locale& init(const char* localeID, UBool canonicalize);
1143  Locale& init(StringPiece localeID, UBool canonicalize);
1144 
1145  /*
1146  * Internal constructor to allow construction of a locale object with
1147  * NO side effects. (Default constructor tries to get
1148  * the default locale.)
1149  */
1150  enum ELocaleType {
1151  eBOGUS
1152  };
1153  Locale(ELocaleType);
1154 
1158  static Locale* getLocaleCache();
1159 
1160  char language[ULOC_LANG_CAPACITY];
1161  char script[ULOC_SCRIPT_CAPACITY];
1162  char country[ULOC_COUNTRY_CAPACITY];
1163  int32_t variantBegin;
1164  char* fullName;
1165  char fullNameBuffer[ULOC_FULLNAME_CAPACITY];
1166  // name without keywords
1167  char* baseName;
1168  void initBaseName(UErrorCode& status);
1169 
1170  UBool fIsBogus;
1171 
1172  static const Locale &getLocale(int locid);
1173 
1178  friend Locale *locale_set_default_internal(const char *, UErrorCode& status);
1179 
1183  friend void U_CALLCONV locale_available_init();
1184 };
1185 
1186 inline bool
1187 Locale::operator!=(const Locale& other) const
1188 {
1189  return !operator==(other);
1190 }
1191 
1192 template<typename StringClass> inline StringClass
1193 Locale::toLanguageTag(UErrorCode& status) const
1194 {
1195  if (U_FAILURE(status)) { return {}; }
1196  StringClass result;
1197  StringByteSink<StringClass> sink(&result);
1198  toLanguageTag(sink, status);
1199  return result;
1200 }
1201 
1202 inline const char *
1203 Locale::getCountry() const
1204 {
1205  return country;
1206 }
1207 
1208 inline const char *
1209 Locale::getLanguage() const
1210 {
1211  return language;
1212 }
1213 
1214 inline const char *
1215 Locale::getScript() const
1216 {
1217  return script;
1218 }
1219 
1220 inline const char *
1221 Locale::getVariant() const
1222 {
1223  return fIsBogus ? "" : &baseName[variantBegin];
1224 }
1225 
1226 inline const char *
1227 Locale::getName() const
1228 {
1229  return fullName;
1230 }
1231 
1232 template<typename StringClass, typename OutputIterator> inline void
1233 Locale::getKeywords(OutputIterator iterator, UErrorCode& status) const
1234 {
1235  if (U_FAILURE(status)) { return; }
1236  LocalPointer<StringEnumeration> keys(createKeywords(status));
1237  if (U_FAILURE(status) || keys.isNull()) {
1238  return;
1239  }
1240  for (;;) {
1241  int32_t resultLength;
1242  const char* buffer = keys->next(&resultLength, status);
1243  if (U_FAILURE(status) || buffer == nullptr) {
1244  return;
1245  }
1246  *iterator++ = StringClass(buffer, resultLength);
1247  }
1248 }
1249 
1250 template<typename StringClass, typename OutputIterator> inline void
1251 Locale::getUnicodeKeywords(OutputIterator iterator, UErrorCode& status) const
1252 {
1253  if (U_FAILURE(status)) { return; }
1254  LocalPointer<StringEnumeration> keys(createUnicodeKeywords(status));
1255  if (U_FAILURE(status) || keys.isNull()) {
1256  return;
1257  }
1258  for (;;) {
1259  int32_t resultLength;
1260  const char* buffer = keys->next(&resultLength, status);
1261  if (U_FAILURE(status) || buffer == nullptr) {
1262  return;
1263  }
1264  *iterator++ = StringClass(buffer, resultLength);
1265  }
1266 }
1267 
1268 template<typename StringClass> inline StringClass
1269 Locale::getKeywordValue(StringPiece keywordName, UErrorCode& status) const
1270 {
1271  if (U_FAILURE(status)) { return {}; }
1272  StringClass result;
1273  StringByteSink<StringClass> sink(&result);
1274  getKeywordValue(keywordName, sink, status);
1275  return result;
1276 }
1277 
1278 template<typename StringClass> inline StringClass
1279 Locale::getUnicodeKeywordValue(StringPiece keywordName, UErrorCode& status) const
1280 {
1281  if (U_FAILURE(status)) { return {}; }
1282  StringClass result;
1283  StringByteSink<StringClass> sink(&result);
1284  getUnicodeKeywordValue(keywordName, sink, status);
1285  return result;
1286 }
1287 
1288 inline UBool
1289 Locale::isBogus() const {
1290  return fIsBogus;
1291 }
1292 
1293 U_NAMESPACE_END
1294 
1295 #endif /* U_SHOW_CPLUSPLUS_API */
1296 
1297 #endif
bool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:346
const Locale & next() override
Definition: locid.h:1107
virtual UClassID getDynamicClassID() const
ICU4C &quot;poor man&#39;s RTTI&quot;, returns a UClassID for the actual ICU class.
#define U_FAILURE(x)
Does the error code indicate a failure?
Definition: utypes.h:748
#define ULOC_LANG_CAPACITY
Useful constant for the maximum size of the language part of a locale ID.
Definition: uloc.h:251
&quot;Smart pointer&quot; class, deletes objects via the standard C++ delete operator.
Definition: localpointer.h:191
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
#define ULOC_COUNTRY_CAPACITY
Useful constant for the maximum size of the country part of a locale ID (including the terminating NU...
Definition: uloc.h:258
#define U_CALLCONV
Similar to U_CDECL_BEGIN/U_CDECL_END, this qualifier is necessary in callback function typedefs to ma...
Definition: platform.h:846
UBool hasNext() const override
Definition: locid.h:1063
void * UClassID
UClassID is used to identify classes without using the compiler&#39;s RTTI.
Definition: uobject.h:96
void setKeywordValue(const char *keywordName, const char *keywordValue, UErrorCode &status)
Sets or removes the value for a keyword.
Definition: locid.h:735
#define ULOC_FULLNAME_CAPACITY
Useful constant for the maximum size of the whole locale ID (including the terminating NULL and all k...
Definition: uloc.h:264
Base class for &#39;pure&#39; C++ implementations of uenum api.
Definition: strenum.h:61
#define ULOC_SCRIPT_CAPACITY
Useful constant for the maximum size of the script part of a locale ID (including the terminating NUL...
Definition: uloc.h:271
A ByteSink can be filled with bytes.
Definition: bytestream.h:53
UBool isNull() const
nullptr check.
Definition: localpointer.h:94
A Locale iterator interface similar to a Java Iterator&lt;Locale&gt;.
Definition: locid.h:1023
void locale_available_init()
C++ API: StringPiece: Read-only byte string wrapper class.
C++ API: Interface for writing bytes, and implementation classes.
A generic Locale iterator implementation over Locale input iterators.
Definition: locid.h:1046
C++ API: &quot;Smart pointers&quot; for use with and in ICU4C C++ code.
C API: Platform Utilities.
RangeIterator(Iter begin, Iter end)
Constructs an iterator from a begin/end range.
Definition: locid.h:1057
C++ API: Common ICU base class UObject.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:430
C++ API: String Enumeration.
Basic definitions for ICU, for both C and C++ APIs.
Implementation of ByteSink that writes to a &quot;string&quot;.
Definition: bytestream.h:267
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside...
Definition: utypes.h:315
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:295
A generic Locale iterator implementation over Locale input iterators.
Definition: locid.h:1082
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:61
UObject is the common ICU &quot;boilerplate&quot; class.
Definition: uobject.h:223
UMemory is the common ICU base class.
Definition: uobject.h:115
const Locale & next() override
Definition: locid.h:1069
ConvertingIterator(Iter begin, Iter end, Conv converter)
Constructs an iterator from a begin/end range.
Definition: locid.h:1094
UBool hasNext() const override
Definition: locid.h:1101
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:247
C API: Locale ID functionality similar to C++ class Locale.
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195