ICU 64.2  64.2
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/bytestream.h"
35 #include "unicode/localpointer.h"
36 #include "unicode/strenum.h"
37 #include "unicode/stringpiece.h"
38 #include "unicode/utypes.h"
39 #include "unicode/uobject.h"
40 #include "unicode/putil.h"
41 #include "unicode/uloc.h"
42 
49 
50 // Forward Declarations
53 class StringEnumeration;
54 class UnicodeString;
55 
192 class U_COMMON_API Locale : public UObject {
193 public:
195  static const Locale &U_EXPORT2 getRoot(void);
197  static const Locale &U_EXPORT2 getEnglish(void);
199  static const Locale &U_EXPORT2 getFrench(void);
201  static const Locale &U_EXPORT2 getGerman(void);
203  static const Locale &U_EXPORT2 getItalian(void);
205  static const Locale &U_EXPORT2 getJapanese(void);
207  static const Locale &U_EXPORT2 getKorean(void);
209  static const Locale &U_EXPORT2 getChinese(void);
211  static const Locale &U_EXPORT2 getSimplifiedChinese(void);
213  static const Locale &U_EXPORT2 getTraditionalChinese(void);
214 
216  static const Locale &U_EXPORT2 getFrance(void);
218  static const Locale &U_EXPORT2 getGermany(void);
220  static const Locale &U_EXPORT2 getItaly(void);
222  static const Locale &U_EXPORT2 getJapan(void);
224  static const Locale &U_EXPORT2 getKorea(void);
226  static const Locale &U_EXPORT2 getChina(void);
228  static const Locale &U_EXPORT2 getPRC(void);
230  static const Locale &U_EXPORT2 getTaiwan(void);
232  static const Locale &U_EXPORT2 getUK(void);
234  static const Locale &U_EXPORT2 getUS(void);
236  static const Locale &U_EXPORT2 getCanada(void);
238  static const Locale &U_EXPORT2 getCanadaFrench(void);
239 
240 
248  Locale();
249 
274  Locale( const char * language,
275  const char * country = 0,
276  const char * variant = 0,
277  const char * keywordsAndValues = 0);
278 
285  Locale(const Locale& other);
286 
287 #ifndef U_HIDE_DRAFT_API
288 
295  Locale(Locale&& other) U_NOEXCEPT;
296 #endif // U_HIDE_DRAFT_API
297 
302  virtual ~Locale() ;
303 
311  Locale& operator=(const Locale& other);
312 
313 #ifndef U_HIDE_DRAFT_API
314 
323  Locale& operator=(Locale&& other) U_NOEXCEPT;
324 #endif // U_HIDE_DRAFT_API
325 
333  UBool operator==(const Locale& other) const;
334 
343  inline UBool operator!=(const Locale& other) const;
344 
356  Locale *clone() const;
357 
358 #ifndef U_HIDE_SYSTEM_API
359 
374  static const Locale& U_EXPORT2 getDefault(void);
375 
388  static void U_EXPORT2 setDefault(const Locale& newLocale,
389  UErrorCode& success);
390 #endif /* U_HIDE_SYSTEM_API */
391 
392 #ifndef U_HIDE_DRAFT_API
393 
410  static Locale U_EXPORT2 forLanguageTag(StringPiece tag, UErrorCode& status);
411 
425  void toLanguageTag(ByteSink& sink, UErrorCode& status) const;
426 
437  template<typename StringClass>
438  inline StringClass toLanguageTag(UErrorCode& status) const;
439 #endif // U_HIDE_DRAFT_API
440 
450  static Locale U_EXPORT2 createFromName(const char *name);
451 
460  static Locale U_EXPORT2 createCanonical(const char* name);
461 
467  inline const char * getLanguage( ) const;
468 
476  inline const char * getScript( ) const;
477 
483  inline const char * getCountry( ) const;
484 
490  inline const char * getVariant( ) const;
491 
500  inline const char * getName() const;
501 
509  const char * getBaseName() const;
510 
511 #ifndef U_HIDE_DRAFT_API
512 
541  void addLikelySubtags(UErrorCode& status);
542 
572  void minimizeSubtags(UErrorCode& status);
573 #endif // U_HIDE_DRAFT_API
574 
584  StringEnumeration * createKeywords(UErrorCode &status) const;
585 
586 #ifndef U_HIDE_DRAFT_API
587 
597  StringEnumeration * createUnicodeKeywords(UErrorCode &status) const;
598 
610  template<typename StringClass, typename OutputIterator>
611  inline void getKeywords(OutputIterator iterator, UErrorCode& status) const;
612 
624  template<typename StringClass, typename OutputIterator>
625  inline void getUnicodeKeywords(OutputIterator iterator, UErrorCode& status) const;
626 
627 #endif // U_HIDE_DRAFT_API
628 
645  int32_t getKeywordValue(const char* keywordName, char *buffer, int32_t bufferCapacity, UErrorCode &status) const;
646 
647 #ifndef U_HIDE_DRAFT_API
648 
661  void getKeywordValue(StringPiece keywordName, ByteSink& sink, UErrorCode& status) const;
662 
676  template<typename StringClass>
677  inline StringClass getKeywordValue(StringPiece keywordName, UErrorCode& status) const;
678 
692  void getUnicodeKeywordValue(StringPiece keywordName, ByteSink& sink, UErrorCode& status) const;
693 
707  template<typename StringClass>
708  inline StringClass getUnicodeKeywordValue(StringPiece keywordName, UErrorCode& status) const;
709 #endif // U_HIDE_DRAFT_API
710 
730  void setKeywordValue(const char* keywordName, const char* keywordValue, UErrorCode &status);
731 
732 #ifndef U_HIDE_DRAFT_API
733 
751  void setKeywordValue(StringPiece keywordName, StringPiece keywordValue, UErrorCode& status);
752 
771  void setUnicodeKeywordValue(StringPiece keywordName, StringPiece keywordValue, UErrorCode& status);
772 #endif // U_HIDE_DRAFT_API
773 
780  const char * getISO3Language() const;
781 
787  const char * getISO3Country() const;
788 
796  uint32_t getLCID(void) const;
797 
811  UBool isRightToLeft() const;
812 
822  UnicodeString& getDisplayLanguage(UnicodeString& dispLang) const;
823 
837  UnicodeString& getDisplayLanguage( const Locale& displayLocale,
838  UnicodeString& dispLang) const;
839 
849  UnicodeString& getDisplayScript( UnicodeString& dispScript) const;
850 
865  UnicodeString& getDisplayScript( const Locale& displayLocale,
866  UnicodeString& dispScript) const;
867 
877  UnicodeString& getDisplayCountry( UnicodeString& dispCountry) const;
878 
893  UnicodeString& getDisplayCountry( const Locale& displayLocale,
894  UnicodeString& dispCountry) const;
895 
903  UnicodeString& getDisplayVariant( UnicodeString& dispVar) const;
904 
913  UnicodeString& getDisplayVariant( const Locale& displayLocale,
914  UnicodeString& dispVar) const;
915 
927  UnicodeString& getDisplayName( UnicodeString& name) const;
928 
941  UnicodeString& getDisplayName( const Locale& displayLocale,
942  UnicodeString& name) const;
943 
948  int32_t hashCode(void) const;
949 
958  void setToBogus();
959 
965  inline UBool isBogus(void) const;
966 
975  static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
976 
985  static const char* const* U_EXPORT2 getISOCountries();
986 
995  static const char* const* U_EXPORT2 getISOLanguages();
996 
1002  static UClassID U_EXPORT2 getStaticClassID();
1003 
1009  virtual UClassID getDynamicClassID() const;
1010 
1011 protected: /* only protected for testing purposes. DO NOT USE. */
1012 #ifndef U_HIDE_INTERNAL_API
1013 
1017  void setFromPOSIXID(const char *posixID);
1018 #endif /* U_HIDE_INTERNAL_API */
1019 
1020 private:
1028  Locale& init(const char* cLocaleID, UBool canonicalize);
1029 
1030  /*
1031  * Internal constructor to allow construction of a locale object with
1032  * NO side effects. (Default constructor tries to get
1033  * the default locale.)
1034  */
1035  enum ELocaleType {
1036  eBOGUS
1037  };
1038  Locale(ELocaleType);
1039 
1043  static Locale *getLocaleCache(void);
1044 
1045  char language[ULOC_LANG_CAPACITY];
1046  char script[ULOC_SCRIPT_CAPACITY];
1047  char country[ULOC_COUNTRY_CAPACITY];
1048  int32_t variantBegin;
1049  char* fullName;
1050  char fullNameBuffer[ULOC_FULLNAME_CAPACITY];
1051  // name without keywords
1052  char* baseName;
1053  void initBaseName(UErrorCode& status);
1054 
1055  UBool fIsBogus;
1056 
1057  static const Locale &getLocale(int locid);
1058 
1063  friend Locale *locale_set_default_internal(const char *, UErrorCode& status);
1064 
1068  friend void U_CALLCONV locale_available_init();
1069 };
1070 
1071 inline UBool
1072 Locale::operator!=(const Locale& other) const
1073 {
1074  return !operator==(other);
1075 }
1076 
1077 #ifndef U_HIDE_DRAFT_API
1078 template<typename StringClass> inline StringClass
1080 {
1081  StringClass result;
1082  StringByteSink<StringClass> sink(&result);
1083  toLanguageTag(sink, status);
1084  return result;
1085 }
1086 #endif // U_HIDE_DRAFT_API
1087 
1088 inline const char *
1090 {
1091  return country;
1092 }
1093 
1094 inline const char *
1096 {
1097  return language;
1098 }
1099 
1100 inline const char *
1102 {
1103  return script;
1104 }
1105 
1106 inline const char *
1108 {
1109  return &baseName[variantBegin];
1110 }
1111 
1112 inline const char *
1114 {
1115  return fullName;
1116 }
1117 
1118 #ifndef U_HIDE_DRAFT_API
1119 
1120 template<typename StringClass, typename OutputIterator> inline void
1121 Locale::getKeywords(OutputIterator iterator, UErrorCode& status) const
1122 {
1123  LocalPointer<StringEnumeration> keys(createKeywords(status));
1124  if (U_FAILURE(status)) {
1125  return;
1126  }
1127  for (;;) {
1128  int32_t resultLength;
1129  const char* buffer = keys->next(&resultLength, status);
1130  if (U_FAILURE(status) || buffer == nullptr) {
1131  return;
1132  }
1133  *iterator++ = StringClass(buffer, resultLength);
1134  }
1135 }
1136 
1137 template<typename StringClass, typename OutputIterator> inline void
1138 Locale::getUnicodeKeywords(OutputIterator iterator, UErrorCode& status) const
1139 {
1140  LocalPointer<StringEnumeration> keys(createUnicodeKeywords(status));
1141  if (U_FAILURE(status)) {
1142  return;
1143  }
1144  for (;;) {
1145  int32_t resultLength;
1146  const char* buffer = keys->next(&resultLength, status);
1147  if (U_FAILURE(status) || buffer == nullptr) {
1148  return;
1149  }
1150  *iterator++ = StringClass(buffer, resultLength);
1151  }
1152 }
1153 
1154 template<typename StringClass> inline StringClass
1156 {
1157  StringClass result;
1158  StringByteSink<StringClass> sink(&result);
1159  getKeywordValue(keywordName, sink, status);
1160  return result;
1161 }
1162 
1163 template<typename StringClass> inline StringClass
1165 {
1166  StringClass result;
1167  StringByteSink<StringClass> sink(&result);
1168  getUnicodeKeywordValue(keywordName, sink, status);
1169  return result;
1170 }
1171 
1172 #endif // U_HIDE_DRAFT_API
1173 
1174 inline UBool
1175 Locale::isBogus(void) const {
1176  return fIsBogus;
1177 }
1178 
1180 
1181 #endif
const char * getLanguage() const
Returns the locale&#39;s ISO-639 language code.
Definition: locid.h:1095
#define U_FAILURE(x)
Does the error code indicate a failure?
Definition: utypes.h:695
#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
const char * getScript() const
Returns the locale&#39;s ISO-15924 abbreviation script code.
Definition: locid.h:1101
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:840
void * UClassID
UClassID is used to identify classes without using the compiler&#39;s RTTI.
Definition: uobject.h:93
const char * getName() const
Returns the programmatic name of the entire locale, with the language, country and variant separated ...
Definition: locid.h:1113
#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:57
#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:50
const char * getCountry() const
Returns the locale&#39;s ISO-3166 country code.
Definition: locid.h:1089
void locale_available_init()
const char * getVariant() const
Returns the locale&#39;s variant code.
Definition: locid.h:1107
C++ API: StringPiece: Read-only byte string wrapper class.
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
void getUnicodeKeywords(OutputIterator iterator, UErrorCode &status) const
Gets the set of Unicode keywords for this Locale.
Definition: locid.h:1138
C++ API: Interface for writing bytes, and implementation classes.
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:218
UBool isBogus(void) const
Gets the bogus state.
Definition: locid.h:1175
virtual UClassID getDynamicClassID() const
ICU4C "poor man&#39;s RTTI", returns a UClassID for the actual ICU class.
C API: Platform Utilities.
void getUnicodeKeywordValue(StringPiece keywordName, ByteSink &sink, UErrorCode &status) const
Gets the Unicode value for a Unicode keyword.
UBool operator!=(const Locale &other) const
Checks if two locale keys are not the same.
Definition: locid.h:1072
C++ API: Common ICU base class UObject.
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:503
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:138
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:401
C++ API: String Enumeration.
Basic definitions for ICU, for both C and C++ APIs.
void getKeywords(OutputIterator iterator, UErrorCode &status) const
Gets the set of keywords for this Locale.
Definition: locid.h:1121
Implementation of ByteSink that writes to a "string".
Definition: bytestream.h:232
#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:289
void toLanguageTag(ByteSink &sink, UErrorCode &status) const
Returns a well-formed language tag for this Locale.
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:54
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
int8_t UBool
The ICU boolean type.
Definition: umachine.h:225
int32_t getKeywordValue(const char *keywordName, char *buffer, int32_t bufferCapacity, UErrorCode &status) const
Gets the value for a keyword.
C API: Locale.
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:192