ICU 70.1  70.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 
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 
298 
303  virtual ~Locale() ;
304 
312  Locale& operator=(const Locale& other);
313 
324 
332  bool operator==(const Locale& other) const;
333 
342  inline bool operator!=(const Locale& other) const;
343 
355  Locale *clone() const;
356 
357 #ifndef U_HIDE_SYSTEM_API
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 
412  static Locale U_EXPORT2 forLanguageTag(StringPiece tag, UErrorCode& status);
413 
427  void toLanguageTag(ByteSink& sink, UErrorCode& status) const;
428 
439  template<typename StringClass>
440  inline StringClass toLanguageTag(UErrorCode& status) const;
441 
451  static Locale U_EXPORT2 createFromName(const char *name);
452 
461  static Locale U_EXPORT2 createCanonical(const char* name);
462 
468  inline const char * getLanguage( ) const;
469 
477  inline const char * getScript( ) const;
478 
484  inline const char * getCountry( ) const;
485 
491  inline const char * getVariant( ) const;
492 
501  inline const char * getName() const;
502 
510  const char * getBaseName() const;
511 
542 
573 
580  void canonicalize(UErrorCode& status);
581 
592 
603 
615  template<typename StringClass, typename OutputIterator>
616  inline void getKeywords(OutputIterator iterator, UErrorCode& status) const;
617 
629  template<typename StringClass, typename OutputIterator>
630  inline void getUnicodeKeywords(OutputIterator iterator, UErrorCode& status) const;
631 
648  int32_t getKeywordValue(const char* keywordName, char *buffer, int32_t bufferCapacity, UErrorCode &status) const;
649 
663  void getKeywordValue(StringPiece keywordName, ByteSink& sink, UErrorCode& status) const;
664 
678  template<typename StringClass>
679  inline StringClass getKeywordValue(StringPiece keywordName, UErrorCode& status) const;
680 
694  void getUnicodeKeywordValue(StringPiece keywordName, ByteSink& sink, UErrorCode& status) const;
695 
709  template<typename StringClass>
710  inline StringClass getUnicodeKeywordValue(StringPiece keywordName, UErrorCode& status) const;
711 
731  void setKeywordValue(const char* keywordName, const char* keywordValue, UErrorCode &status);
732 
751  void setKeywordValue(StringPiece keywordName, StringPiece keywordValue, UErrorCode& status);
752 
771  void setUnicodeKeywordValue(StringPiece keywordName, StringPiece keywordValue, UErrorCode& status);
772 
779  const char * getISO3Language() const;
780 
786  const char * getISO3Country() const;
787 
795  uint32_t getLCID(void) const;
796 
811 
822 
836  UnicodeString& getDisplayLanguage( const Locale& displayLocale,
837  UnicodeString& dispLang) const;
838 
849 
864  UnicodeString& getDisplayScript( const Locale& displayLocale,
865  UnicodeString& dispScript) const;
866 
877 
892  UnicodeString& getDisplayCountry( const Locale& displayLocale,
893  UnicodeString& dispCountry) const;
894 
903 
912  UnicodeString& getDisplayVariant( const Locale& displayLocale,
913  UnicodeString& dispVar) const;
914 
927 
940  UnicodeString& getDisplayName( const Locale& displayLocale,
941  UnicodeString& name) const;
942 
947  int32_t hashCode(void) const;
948 
957  void setToBogus();
958 
964  inline UBool isBogus(void) const;
965 
974  static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
975 
984  static const char* const* U_EXPORT2 getISOCountries();
985 
994  static const char* const* U_EXPORT2 getISOLanguages();
995 
1001  static UClassID U_EXPORT2 getStaticClassID();
1002 
1008  virtual UClassID getDynamicClassID() const override;
1009 
1014  class U_COMMON_API Iterator /* not : public UObject because this is an interface/mixin class */ {
1015  public:
1017  virtual ~Iterator();
1018 
1023  virtual UBool hasNext() const = 0;
1024 
1029  virtual const Locale &next() = 0;
1030  };
1031 
1036  template<typename Iter>
1037  class RangeIterator : public Iterator, public UMemory {
1038  public:
1048  RangeIterator(Iter begin, Iter end) : it_(begin), end_(end) {}
1049 
1054  UBool hasNext() const override { return it_ != end_; }
1055 
1060  const Locale &next() override { return *it_++; }
1061 
1062  private:
1063  Iter it_;
1064  const Iter end_;
1065  };
1066 
1072  template<typename Iter, typename Conv>
1073  class ConvertingIterator : public Iterator, public UMemory {
1074  public:
1085  ConvertingIterator(Iter begin, Iter end, Conv converter) :
1086  it_(begin), end_(end), converter_(converter) {}
1087 
1092  UBool hasNext() const override { return it_ != end_; }
1093 
1098  const Locale &next() override { return converter_(*it_++); }
1099 
1100  private:
1101  Iter it_;
1102  const Iter end_;
1103  Conv converter_;
1104  };
1105 
1106 protected: /* only protected for testing purposes. DO NOT USE. */
1107 #ifndef U_HIDE_INTERNAL_API
1112  void setFromPOSIXID(const char *posixID);
1113 #endif /* U_HIDE_INTERNAL_API */
1114 
1115 private:
1123  Locale& init(const char* cLocaleID, UBool canonicalize);
1124 
1125  /*
1126  * Internal constructor to allow construction of a locale object with
1127  * NO side effects. (Default constructor tries to get
1128  * the default locale.)
1129  */
1130  enum ELocaleType {
1131  eBOGUS
1132  };
1133  Locale(ELocaleType);
1134 
1138  static Locale *getLocaleCache(void);
1139 
1140  char language[ULOC_LANG_CAPACITY];
1141  char script[ULOC_SCRIPT_CAPACITY];
1142  char country[ULOC_COUNTRY_CAPACITY];
1143  int32_t variantBegin;
1144  char* fullName;
1145  char fullNameBuffer[ULOC_FULLNAME_CAPACITY];
1146  // name without keywords
1147  char* baseName;
1148  void initBaseName(UErrorCode& status);
1149 
1150  UBool fIsBogus;
1151 
1152  static const Locale &getLocale(int locid);
1153 
1158  friend Locale *locale_set_default_internal(const char *, UErrorCode& status);
1159 
1164 };
1165 
1166 inline bool
1167 Locale::operator!=(const Locale& other) const
1168 {
1169  return !operator==(other);
1170 }
1171 
1172 template<typename StringClass> inline StringClass
1173 Locale::toLanguageTag(UErrorCode& status) const
1174 {
1175  StringClass result;
1176  StringByteSink<StringClass> sink(&result);
1177  toLanguageTag(sink, status);
1178  return result;
1179 }
1180 
1181 inline const char *
1182 Locale::getCountry() const
1183 {
1184  return country;
1185 }
1186 
1187 inline const char *
1188 Locale::getLanguage() const
1189 {
1190  return language;
1191 }
1192 
1193 inline const char *
1194 Locale::getScript() const
1195 {
1196  return script;
1197 }
1198 
1199 inline const char *
1200 Locale::getVariant() const
1201 {
1202  return &baseName[variantBegin];
1203 }
1204 
1205 inline const char *
1206 Locale::getName() const
1207 {
1208  return fullName;
1209 }
1210 
1211 template<typename StringClass, typename OutputIterator> inline void
1212 Locale::getKeywords(OutputIterator iterator, UErrorCode& status) const
1213 {
1214  LocalPointer<StringEnumeration> keys(createKeywords(status));
1215  if (U_FAILURE(status) || keys.isNull()) {
1216  return;
1217  }
1218  for (;;) {
1219  int32_t resultLength;
1220  const char* buffer = keys->next(&resultLength, status);
1221  if (U_FAILURE(status) || buffer == nullptr) {
1222  return;
1223  }
1224  *iterator++ = StringClass(buffer, resultLength);
1225  }
1226 }
1227 
1228 template<typename StringClass, typename OutputIterator> inline void
1229 Locale::getUnicodeKeywords(OutputIterator iterator, UErrorCode& status) const
1230 {
1231  LocalPointer<StringEnumeration> keys(createUnicodeKeywords(status));
1232  if (U_FAILURE(status) || keys.isNull()) {
1233  return;
1234  }
1235  for (;;) {
1236  int32_t resultLength;
1237  const char* buffer = keys->next(&resultLength, status);
1238  if (U_FAILURE(status) || buffer == nullptr) {
1239  return;
1240  }
1241  *iterator++ = StringClass(buffer, resultLength);
1242  }
1243 }
1244 
1245 template<typename StringClass> inline StringClass
1246 Locale::getKeywordValue(StringPiece keywordName, UErrorCode& status) const
1247 {
1248  StringClass result;
1249  StringByteSink<StringClass> sink(&result);
1250  getKeywordValue(keywordName, sink, status);
1251  return result;
1252 }
1253 
1254 template<typename StringClass> inline StringClass
1255 Locale::getUnicodeKeywordValue(StringPiece keywordName, UErrorCode& status) const
1256 {
1257  StringClass result;
1258  StringByteSink<StringClass> sink(&result);
1259  getUnicodeKeywordValue(keywordName, sink, status);
1260  return result;
1261 }
1262 
1263 inline UBool
1264 Locale::isBogus(void) const {
1265  return fIsBogus;
1266 }
1267 
1268 U_NAMESPACE_END
1269 
1270 #endif /* U_SHOW_CPLUSPLUS_API */
1271 
1272 #endif
C++ API: Interface for writing bytes, and implementation classes.
A ByteSink can be filled with bytes.
Definition: bytestream.h:53
UBool isNull() const
NULL check.
Definition: localpointer.h:94
"Smart pointer" class, deletes objects via the standard C++ delete operator.
Definition: localpointer.h:191
A generic Locale iterator implementation over Locale input iterators.
Definition: locid.h:1073
UBool hasNext() const override
Definition: locid.h:1092
ConvertingIterator(Iter begin, Iter end, Conv converter)
Constructs an iterator from a begin/end range.
Definition: locid.h:1085
const Locale & next() override
Definition: locid.h:1098
A Locale iterator interface similar to a Java Iterator<Locale>.
Definition: locid.h:1014
virtual UBool hasNext() const =0
virtual const Locale & next()=0
A generic Locale iterator implementation over Locale input iterators.
Definition: locid.h:1037
RangeIterator(Iter begin, Iter end)
Constructs an iterator from a begin/end range.
Definition: locid.h:1048
UBool hasNext() const override
Definition: locid.h:1054
const Locale & next() override
Definition: locid.h:1060
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195
static const Locale & getDefault(void)
Common methods of getting the current default Locale.
static const Locale & getJapanese(void)
Useful constant for this language.
void minimizeSubtags(UErrorCode &status)
Minimize the subtags for this Locale, per the algorithm described in the following CLDR technical rep...
static const Locale * getAvailableLocales(int32_t &count)
Returns a list of all installed locales.
static const Locale & getGerman(void)
Useful constant for this language.
static const char *const * getISOCountries()
Gets a list of all available 2-letter country codes defined in ISO 3166.
static const Locale & getChina(void)
Useful constant for this country/region.
static Locale createFromName(const char *name)
Creates a locale which has had minimal canonicalization as per uloc_getName().
Locale & operator=(Locale &&other) U_NOEXCEPT
Move assignment operator; might leave source in bogus state.
friend void locale_available_init()
friend Locale * locale_set_default_internal(const char *, UErrorCode &status)
A friend to allow the default locale to be set by either the C or C++ API.
static const Locale & getRoot(void)
Useful constant for the Root locale.
static Locale createCanonical(const char *name)
Creates a locale from the given string after canonicalizing the string according to CLDR by calling u...
UnicodeString & getDisplayCountry(const Locale &displayLocale, UnicodeString &dispCountry) const
Fills in "dispCountry" with the name of this locale's country in a format suitable for user display i...
Locale(Locale &&other) U_NOEXCEPT
Move constructor; might leave source in bogus state.
static UClassID getStaticClassID()
ICU "poor man's RTTI", returns a UClassID for this class.
Locale()
Construct a default locale object, a Locale for the default locale ID.
Locale * clone() const
Clone this object.
void setKeywordValue(const char *keywordName, const char *keywordValue, UErrorCode &status)
Sets or removes the value for a keyword.
uint32_t getLCID(void) const
Returns the Windows LCID value corresponding to this locale.
UnicodeString & getDisplayCountry(UnicodeString &dispCountry) const
Fills in "dispCountry" with the name of this locale's country in a format suitable for user display i...
static const Locale & getChinese(void)
Useful constant for this language.
Locale(const char *language, const char *country=0, const char *variant=0, const char *keywordsAndValues=0)
Construct a locale from language, country, variant.
UnicodeString & getDisplayLanguage(const Locale &displayLocale, UnicodeString &dispLang) const
Fills in "dispLang" with the name of this locale's language in a format suitable for user display in ...
static const Locale & getPRC(void)
Useful constant for this country/region.
static const Locale & getCanada(void)
Useful constant for this country/region.
static const char *const * getISOLanguages()
Gets a list of all available language codes defined in ISO 639.
const char * getISO3Country() const
Fills in "name" with the locale's three-letter ISO-3166 country code.
UnicodeString & getDisplayName(UnicodeString &name) const
Fills in "name" with the name of this locale in a format suitable for user display in the default loc...
virtual ~Locale()
Destructor.
static const Locale & getGermany(void)
Useful constant for this country/region.
int32_t getKeywordValue(const char *keywordName, char *buffer, int32_t bufferCapacity, UErrorCode &status) const
Gets the value for a keyword.
static void setDefault(const Locale &newLocale, UErrorCode &success)
Sets the default.
static const Locale & getUS(void)
Useful constant for this country/region.
bool operator==(const Locale &other) const
Checks if two locale keys are the same.
StringEnumeration * createKeywords(UErrorCode &status) const
Gets the list of keywords for the specified locale.
UnicodeString & getDisplayLanguage(UnicodeString &dispLang) const
Fills in "dispLang" with the name of this locale's language in a format suitable for user display in ...
void setKeywordValue(StringPiece keywordName, StringPiece keywordValue, UErrorCode &status)
Sets or removes the value for a keyword.
void setUnicodeKeywordValue(StringPiece keywordName, StringPiece keywordValue, UErrorCode &status)
Sets or removes the Unicode value for a Unicode keyword.
StringEnumeration * createUnicodeKeywords(UErrorCode &status) const
Gets the list of Unicode keywords for the specified locale.
void toLanguageTag(ByteSink &sink, UErrorCode &status) const
Returns a well-formed language tag for this Locale.
const char * getBaseName() const
Returns the programmatic name of the entire locale as getName() would return, but without keywords.
UnicodeString & getDisplayName(const Locale &displayLocale, UnicodeString &name) const
Fills in "name" with the name of this locale in a format suitable for user display in the locale spec...
void setToBogus()
Sets the locale to bogus A bogus locale represents a non-existing locale associated with services tha...
void addLikelySubtags(UErrorCode &status)
Add the likely subtags for this Locale, per the algorithm described in the following CLDR technical r...
Locale & operator=(const Locale &other)
Replaces the entire contents of *this with the specified value.
static const Locale & getTraditionalChinese(void)
Useful constant for this language.
static const Locale & getEnglish(void)
Useful constant for this language.
static const Locale & getSimplifiedChinese(void)
Useful constant for this language.
void canonicalize(UErrorCode &status)
Canonicalize the locale ID of this object according to CLDR.
static const Locale & getKorean(void)
Useful constant for this language.
static const Locale & getFrench(void)
Useful constant for this language.
int32_t hashCode(void) const
Generates a hash code for the locale.
static const Locale & getCanadaFrench(void)
Useful constant for this country/region.
Locale(const Locale &other)
Initializes a Locale object from another Locale object.
static const Locale & getItaly(void)
Useful constant for this country/region.
static const Locale & getUK(void)
Useful constant for this country/region.
UnicodeString & getDisplayVariant(UnicodeString &dispVar) const
Fills in "dispVar" with the name of this locale's variant code in a format suitable for user display ...
const char * getISO3Language() const
returns the locale's three-letter language code, as specified in ISO draft standard ISO-639-2.
void getUnicodeKeywordValue(StringPiece keywordName, ByteSink &sink, UErrorCode &status) const
Gets the Unicode value for a Unicode keyword.
UnicodeString & getDisplayScript(const Locale &displayLocale, UnicodeString &dispScript) const
Fills in "dispScript" with the name of this locale's country in a format suitable for user display in...
static const Locale & getJapan(void)
Useful constant for this country/region.
UnicodeString & getDisplayScript(UnicodeString &dispScript) const
Fills in "dispScript" with the name of this locale's script in a format suitable for user display in ...
static const Locale & getKorea(void)
Useful constant for this country/region.
static const Locale & getFrance(void)
Useful constant for this country/region.
UBool isRightToLeft() const
Returns whether this locale's script is written right-to-left.
void setFromPOSIXID(const char *posixID)
Set this from a single POSIX style locale string.
static const Locale & getItalian(void)
Useful constant for this language.
void getKeywordValue(StringPiece keywordName, ByteSink &sink, UErrorCode &status) const
Gets the value for a keyword.
UnicodeString & getDisplayVariant(const Locale &displayLocale, UnicodeString &dispVar) const
Fills in "dispVar" with the name of this locale's variant code in a format suitable for user display ...
static const Locale & getTaiwan(void)
Useful constant for this country/region.
virtual UClassID getDynamicClassID() const override
ICU "poor man's RTTI", returns a UClassID for the actual class.
static Locale forLanguageTag(StringPiece tag, UErrorCode &status)
Returns a Locale for the specified BCP47 language tag string.
Implementation of ByteSink that writes to a "string".
Definition: bytestream.h:267
Base class for 'pure' C++ implementations of uenum api.
Definition: strenum.h:61
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:60
UMemory is the common ICU base class.
Definition: uobject.h:115
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:296
C++ API: "Smart pointers" for use with and in ICU4C C++ code.
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
void locale_available_init()
bool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:335
#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
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:529
C API: Platform Utilities.
C++ API: String Enumeration.
C++ API: StringPiece: Read-only byte string wrapper class.
C API: Locale.
#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
#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 ULOC_LANG_CAPACITY
Useful constant for the maximum size of the language part of a locale ID.
Definition: uloc.h:251
#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
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
C++ API: Common ICU base class UObject.
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:96
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
#define U_FAILURE(x)
Does the error code indicate a failure?
Definition: utypes.h:717
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside.
Definition: utypes.h:300