ICU 65.1  65.1
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(void);
200  static const Locale &U_EXPORT2 getEnglish(void);
202  static const Locale &U_EXPORT2 getFrench(void);
204  static const Locale &U_EXPORT2 getGerman(void);
206  static const Locale &U_EXPORT2 getItalian(void);
208  static const Locale &U_EXPORT2 getJapanese(void);
210  static const Locale &U_EXPORT2 getKorean(void);
212  static const Locale &U_EXPORT2 getChinese(void);
214  static const Locale &U_EXPORT2 getSimplifiedChinese(void);
216  static const Locale &U_EXPORT2 getTraditionalChinese(void);
217 
219  static const Locale &U_EXPORT2 getFrance(void);
221  static const Locale &U_EXPORT2 getGermany(void);
223  static const Locale &U_EXPORT2 getItaly(void);
225  static const Locale &U_EXPORT2 getJapan(void);
227  static const Locale &U_EXPORT2 getKorea(void);
229  static const Locale &U_EXPORT2 getChina(void);
231  static const Locale &U_EXPORT2 getPRC(void);
233  static const Locale &U_EXPORT2 getTaiwan(void);
235  static const Locale &U_EXPORT2 getUK(void);
237  static const Locale &U_EXPORT2 getUS(void);
239  static const Locale &U_EXPORT2 getCanada(void);
241  static const Locale &U_EXPORT2 getCanadaFrench(void);
242 
243 
251  Locale();
252 
277  Locale( const char * language,
278  const char * country = 0,
279  const char * variant = 0,
280  const char * keywordsAndValues = 0);
281 
288  Locale(const Locale& other);
289 
297  Locale(Locale&& other) U_NOEXCEPT;
298 
303  virtual ~Locale() ;
304 
312  Locale& operator=(const Locale& other);
313 
323  Locale& operator=(Locale&& other) U_NOEXCEPT;
324 
332  UBool operator==(const Locale& other) const;
333 
342  inline UBool operator!=(const Locale& other) const;
343 
355  Locale *clone() const;
356 
357 #ifndef U_HIDE_SYSTEM_API
358 
373  static const Locale& U_EXPORT2 getDefault(void);
374 
387  static void U_EXPORT2 setDefault(const Locale& newLocale,
388  UErrorCode& success);
389 #endif /* U_HIDE_SYSTEM_API */
390 
408  static Locale U_EXPORT2 forLanguageTag(StringPiece tag, UErrorCode& status);
409 
423  void toLanguageTag(ByteSink& sink, UErrorCode& status) const;
424 
435  template<typename StringClass>
436  inline StringClass toLanguageTag(UErrorCode& status) const;
437 
447  static Locale U_EXPORT2 createFromName(const char *name);
448 
457  static Locale U_EXPORT2 createCanonical(const char* name);
458 
464  inline const char * getLanguage( ) const;
465 
473  inline const char * getScript( ) const;
474 
480  inline const char * getCountry( ) const;
481 
487  inline const char * getVariant( ) const;
488 
497  inline const char * getName() const;
498 
506  const char * getBaseName() const;
507 
537  void addLikelySubtags(UErrorCode& status);
538 
568  void minimizeSubtags(UErrorCode& status);
569 
579  StringEnumeration * createKeywords(UErrorCode &status) const;
580 
590  StringEnumeration * createUnicodeKeywords(UErrorCode &status) const;
591 
603  template<typename StringClass, typename OutputIterator>
604  inline void getKeywords(OutputIterator iterator, UErrorCode& status) const;
605 
617  template<typename StringClass, typename OutputIterator>
618  inline void getUnicodeKeywords(OutputIterator iterator, UErrorCode& status) const;
619 
636  int32_t getKeywordValue(const char* keywordName, char *buffer, int32_t bufferCapacity, UErrorCode &status) const;
637 
651  void getKeywordValue(StringPiece keywordName, ByteSink& sink, UErrorCode& status) const;
652 
666  template<typename StringClass>
667  inline StringClass getKeywordValue(StringPiece keywordName, UErrorCode& status) const;
668 
682  void getUnicodeKeywordValue(StringPiece keywordName, ByteSink& sink, UErrorCode& status) const;
683 
697  template<typename StringClass>
698  inline StringClass getUnicodeKeywordValue(StringPiece keywordName, UErrorCode& status) const;
699 
719  void setKeywordValue(const char* keywordName, const char* keywordValue, UErrorCode &status);
720 
739  void setKeywordValue(StringPiece keywordName, StringPiece keywordValue, UErrorCode& status);
740 
759  void setUnicodeKeywordValue(StringPiece keywordName, StringPiece keywordValue, UErrorCode& status);
760 
767  const char * getISO3Language() const;
768 
774  const char * getISO3Country() const;
775 
783  uint32_t getLCID(void) const;
784 
798  UBool isRightToLeft() const;
799 
809  UnicodeString& getDisplayLanguage(UnicodeString& dispLang) const;
810 
824  UnicodeString& getDisplayLanguage( const Locale& displayLocale,
825  UnicodeString& dispLang) const;
826 
836  UnicodeString& getDisplayScript( UnicodeString& dispScript) const;
837 
852  UnicodeString& getDisplayScript( const Locale& displayLocale,
853  UnicodeString& dispScript) const;
854 
864  UnicodeString& getDisplayCountry( UnicodeString& dispCountry) const;
865 
880  UnicodeString& getDisplayCountry( const Locale& displayLocale,
881  UnicodeString& dispCountry) const;
882 
890  UnicodeString& getDisplayVariant( UnicodeString& dispVar) const;
891 
900  UnicodeString& getDisplayVariant( const Locale& displayLocale,
901  UnicodeString& dispVar) const;
902 
914  UnicodeString& getDisplayName( UnicodeString& name) const;
915 
928  UnicodeString& getDisplayName( const Locale& displayLocale,
929  UnicodeString& name) const;
930 
935  int32_t hashCode(void) const;
936 
945  void setToBogus();
946 
952  inline UBool isBogus(void) const;
953 
962  static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
963 
972  static const char* const* U_EXPORT2 getISOCountries();
973 
982  static const char* const* U_EXPORT2 getISOLanguages();
983 
989  static UClassID U_EXPORT2 getStaticClassID();
990 
996  virtual UClassID getDynamicClassID() const;
997 
998 #ifndef U_HIDE_DRAFT_API
999 
1003  class U_COMMON_API Iterator /* not : public UObject because this is an interface/mixin class */ {
1004  public:
1006  virtual ~Iterator();
1007 
1012  virtual UBool hasNext() const = 0;
1013 
1018  virtual const Locale &next() = 0;
1019  };
1020 
1025  template<typename Iter>
1026  class RangeIterator : public Iterator, public UMemory {
1027  public:
1037  RangeIterator(Iter begin, Iter end) : it_(begin), end_(end) {}
1038 
1043  UBool hasNext() const override { return it_ != end_; }
1044 
1049  const Locale &next() override { return *it_++; }
1050 
1051  private:
1052  Iter it_;
1053  const Iter end_;
1054  };
1055 
1061  template<typename Iter, typename Conv>
1062  class ConvertingIterator : public Iterator, public UMemory {
1063  public:
1074  ConvertingIterator(Iter begin, Iter end, Conv converter) :
1075  it_(begin), end_(end), converter_(converter) {}
1076 
1081  UBool hasNext() const override { return it_ != end_; }
1082 
1087  const Locale &next() override { return converter_(*it_++); }
1088 
1089  private:
1090  Iter it_;
1091  const Iter end_;
1092  Conv converter_;
1093  };
1094 #endif // U_HIDE_DRAFT_API
1095 
1096 protected: /* only protected for testing purposes. DO NOT USE. */
1097 #ifndef U_HIDE_INTERNAL_API
1098 
1102  void setFromPOSIXID(const char *posixID);
1103 #endif /* U_HIDE_INTERNAL_API */
1104 
1105 private:
1113  Locale& init(const char* cLocaleID, UBool canonicalize);
1114 
1115  /*
1116  * Internal constructor to allow construction of a locale object with
1117  * NO side effects. (Default constructor tries to get
1118  * the default locale.)
1119  */
1120  enum ELocaleType {
1121  eBOGUS
1122  };
1123  Locale(ELocaleType);
1124 
1128  static Locale *getLocaleCache(void);
1129 
1130  char language[ULOC_LANG_CAPACITY];
1131  char script[ULOC_SCRIPT_CAPACITY];
1132  char country[ULOC_COUNTRY_CAPACITY];
1133  int32_t variantBegin;
1134  char* fullName;
1135  char fullNameBuffer[ULOC_FULLNAME_CAPACITY];
1136  // name without keywords
1137  char* baseName;
1138  void initBaseName(UErrorCode& status);
1139 
1140  UBool fIsBogus;
1141 
1142  static const Locale &getLocale(int locid);
1143 
1148  friend Locale *locale_set_default_internal(const char *, UErrorCode& status);
1149 
1153  friend void U_CALLCONV locale_available_init();
1154 };
1155 
1156 inline UBool
1157 Locale::operator!=(const Locale& other) const
1158 {
1159  return !operator==(other);
1160 }
1161 
1162 template<typename StringClass> inline StringClass
1163 Locale::toLanguageTag(UErrorCode& status) const
1164 {
1165  StringClass result;
1166  StringByteSink<StringClass> sink(&result);
1167  toLanguageTag(sink, status);
1168  return result;
1169 }
1170 
1171 inline const char *
1172 Locale::getCountry() const
1173 {
1174  return country;
1175 }
1176 
1177 inline const char *
1178 Locale::getLanguage() const
1179 {
1180  return language;
1181 }
1182 
1183 inline const char *
1184 Locale::getScript() const
1185 {
1186  return script;
1187 }
1188 
1189 inline const char *
1190 Locale::getVariant() const
1191 {
1192  return &baseName[variantBegin];
1193 }
1194 
1195 inline const char *
1196 Locale::getName() const
1197 {
1198  return fullName;
1199 }
1200 
1201 template<typename StringClass, typename OutputIterator> inline void
1202 Locale::getKeywords(OutputIterator iterator, UErrorCode& status) const
1203 {
1204  LocalPointer<StringEnumeration> keys(createKeywords(status));
1205  if (U_FAILURE(status) || keys.isNull()) {
1206  return;
1207  }
1208  for (;;) {
1209  int32_t resultLength;
1210  const char* buffer = keys->next(&resultLength, status);
1211  if (U_FAILURE(status) || buffer == nullptr) {
1212  return;
1213  }
1214  *iterator++ = StringClass(buffer, resultLength);
1215  }
1216 }
1217 
1218 template<typename StringClass, typename OutputIterator> inline void
1219 Locale::getUnicodeKeywords(OutputIterator iterator, UErrorCode& status) const
1220 {
1221  LocalPointer<StringEnumeration> keys(createUnicodeKeywords(status));
1222  if (U_FAILURE(status) || keys.isNull()) {
1223  return;
1224  }
1225  for (;;) {
1226  int32_t resultLength;
1227  const char* buffer = keys->next(&resultLength, status);
1228  if (U_FAILURE(status) || buffer == nullptr) {
1229  return;
1230  }
1231  *iterator++ = StringClass(buffer, resultLength);
1232  }
1233 }
1234 
1235 template<typename StringClass> inline StringClass
1236 Locale::getKeywordValue(StringPiece keywordName, UErrorCode& status) const
1237 {
1238  StringClass result;
1239  StringByteSink<StringClass> sink(&result);
1240  getKeywordValue(keywordName, sink, status);
1241  return result;
1242 }
1243 
1244 template<typename StringClass> inline StringClass
1245 Locale::getUnicodeKeywordValue(StringPiece keywordName, UErrorCode& status) const
1246 {
1247  StringClass result;
1248  StringByteSink<StringClass> sink(&result);
1249  getUnicodeKeywordValue(keywordName, sink, status);
1250  return result;
1251 }
1252 
1253 inline UBool
1254 Locale::isBogus(void) const {
1255  return fIsBogus;
1256 }
1257 
1258 U_NAMESPACE_END
1259 
1260 #endif /* U_SHOW_CPLUSPLUS_API */
1261 
1262 #endif
const Locale & next() override
Definition: locid.h:1087
#define U_FAILURE(x)
Does the error code indicate a failure?
Definition: utypes.h:709
#define ULOC_LANG_CAPACITY
Useful constant for the maximum size of the language part of a locale ID.
Definition: uloc.h:251
"Smart pointer" 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:870
UBool hasNext() const override
Definition: locid.h:1043
void * UClassID
UClassID is used to identify classes without using the compiler&#39;s RTTI.
Definition: uobject.h:96
#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
A Locale iterator interface similar to a Java Iterator<Locale>.
Definition: locid.h:1003
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:1026
UBool isNull() const
NULL check.
Definition: localpointer.h:94
C++ API: "Smart pointers" for use with and in ICU4C C++ code.
UBool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:251
virtual UClassID getDynamicClassID() const
ICU4C "poor man&#39;s RTTI", returns a UClassID for the actual ICU class.
C API: Platform Utilities.
RangeIterator(Iter begin, Iter end)
Constructs an iterator from a begin/end range.
Definition: locid.h:1037
C++ API: Common ICU base class UObject.
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:529
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
C++ API: String Enumeration.
Basic definitions for ICU, for both C and C++ APIs.
Implementation of ByteSink that writes to a "string".
Definition: bytestream.h:235
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside...
Definition: utypes.h:300
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:294
A generic Locale iterator implementation over Locale input iterators.
Definition: locid.h:1062
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:60
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
UMemory is the common ICU base class.
Definition: uobject.h:115
const Locale & next() override
Definition: locid.h:1049
ConvertingIterator(Iter begin, Iter end, Conv converter)
Constructs an iterator from a begin/end range.
Definition: locid.h:1074
UBool hasNext() const override
Definition: locid.h:1081
int8_t UBool
The ICU boolean type.
Definition: umachine.h:261
C API: Locale.
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195