ICU 74.2  74.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/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) noexcept;
298 
303  virtual ~Locale() ;
304 
312  Locale& operator=(const Locale& other);
313 
323  Locale& operator=(Locale&& other) noexcept;
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
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 
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 
541  void addLikelySubtags(UErrorCode& status);
542 
572  void minimizeSubtags(UErrorCode& status);
573 
580  void canonicalize(UErrorCode& status);
581 
591  StringEnumeration * createKeywords(UErrorCode &status) const;
592 
602  StringEnumeration * createUnicodeKeywords(UErrorCode &status) const;
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 
810  UBool isRightToLeft() const;
811 
821  UnicodeString& getDisplayLanguage(UnicodeString& dispLang) const;
822 
836  UnicodeString& getDisplayLanguage( const Locale& displayLocale,
837  UnicodeString& dispLang) const;
838 
848  UnicodeString& getDisplayScript( UnicodeString& dispScript) const;
849 
864  UnicodeString& getDisplayScript( const Locale& displayLocale,
865  UnicodeString& dispScript) const;
866 
876  UnicodeString& getDisplayCountry( UnicodeString& dispCountry) const;
877 
892  UnicodeString& getDisplayCountry( const Locale& displayLocale,
893  UnicodeString& dispCountry) const;
894 
902  UnicodeString& getDisplayVariant( UnicodeString& dispVar) const;
903 
912  UnicodeString& getDisplayVariant( const Locale& displayLocale,
913  UnicodeString& dispVar) const;
914 
926  UnicodeString& getDisplayName( UnicodeString& name) const;
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 
997  static const char* const* U_EXPORT2 getISOLanguages();
998 
1004  static UClassID U_EXPORT2 getStaticClassID();
1005 
1011  virtual UClassID getDynamicClassID() const override;
1012 
1017  class U_COMMON_API Iterator /* not : public UObject because this is an interface/mixin class */ {
1018  public:
1020  virtual ~Iterator();
1021 
1026  virtual UBool hasNext() const = 0;
1027 
1032  virtual const Locale &next() = 0;
1033  };
1034 
1039  template<typename Iter>
1040  class RangeIterator : public Iterator, public UMemory {
1041  public:
1051  RangeIterator(Iter begin, Iter end) : it_(begin), end_(end) {}
1052 
1057  UBool hasNext() const override { return it_ != end_; }
1058 
1063  const Locale &next() override { return *it_++; }
1064 
1065  private:
1066  Iter it_;
1067  const Iter end_;
1068  };
1069 
1075  template<typename Iter, typename Conv>
1076  class ConvertingIterator : public Iterator, public UMemory {
1077  public:
1088  ConvertingIterator(Iter begin, Iter end, Conv converter) :
1089  it_(begin), end_(end), converter_(converter) {}
1090 
1095  UBool hasNext() const override { return it_ != end_; }
1096 
1101  const Locale &next() override { return converter_(*it_++); }
1102 
1103  private:
1104  Iter it_;
1105  const Iter end_;
1106  Conv converter_;
1107  };
1108 
1109 protected: /* only protected for testing purposes. DO NOT USE. */
1110 #ifndef U_HIDE_INTERNAL_API
1111 
1115  void setFromPOSIXID(const char *posixID);
1124  void minimizeSubtags(bool favorScript, UErrorCode& status);
1125 #endif /* U_HIDE_INTERNAL_API */
1126 
1127 private:
1135  Locale& init(const char* cLocaleID, UBool canonicalize);
1136 
1137  /*
1138  * Internal constructor to allow construction of a locale object with
1139  * NO side effects. (Default constructor tries to get
1140  * the default locale.)
1141  */
1142  enum ELocaleType {
1143  eBOGUS
1144  };
1145  Locale(ELocaleType);
1146 
1150  static Locale *getLocaleCache(void);
1151 
1152  char language[ULOC_LANG_CAPACITY];
1153  char script[ULOC_SCRIPT_CAPACITY];
1154  char country[ULOC_COUNTRY_CAPACITY];
1155  int32_t variantBegin;
1156  char* fullName;
1157  char fullNameBuffer[ULOC_FULLNAME_CAPACITY];
1158  // name without keywords
1159  char* baseName;
1160  void initBaseName(UErrorCode& status);
1161 
1162  UBool fIsBogus;
1163 
1164  static const Locale &getLocale(int locid);
1165 
1170  friend Locale *locale_set_default_internal(const char *, UErrorCode& status);
1171 
1175  friend void U_CALLCONV locale_available_init();
1176 };
1177 
1178 inline bool
1179 Locale::operator!=(const Locale& other) const
1180 {
1181  return !operator==(other);
1182 }
1183 
1184 template<typename StringClass> inline StringClass
1185 Locale::toLanguageTag(UErrorCode& status) const
1186 {
1187  StringClass result;
1188  StringByteSink<StringClass> sink(&result);
1189  toLanguageTag(sink, status);
1190  return result;
1191 }
1192 
1193 inline const char *
1194 Locale::getCountry() const
1195 {
1196  return country;
1197 }
1198 
1199 inline const char *
1200 Locale::getLanguage() const
1201 {
1202  return language;
1203 }
1204 
1205 inline const char *
1206 Locale::getScript() const
1207 {
1208  return script;
1209 }
1210 
1211 inline const char *
1212 Locale::getVariant() const
1213 {
1214  return &baseName[variantBegin];
1215 }
1216 
1217 inline const char *
1218 Locale::getName() const
1219 {
1220  return fullName;
1221 }
1222 
1223 template<typename StringClass, typename OutputIterator> inline void
1224 Locale::getKeywords(OutputIterator iterator, UErrorCode& status) const
1225 {
1226  LocalPointer<StringEnumeration> keys(createKeywords(status));
1227  if (U_FAILURE(status) || keys.isNull()) {
1228  return;
1229  }
1230  for (;;) {
1231  int32_t resultLength;
1232  const char* buffer = keys->next(&resultLength, status);
1233  if (U_FAILURE(status) || buffer == nullptr) {
1234  return;
1235  }
1236  *iterator++ = StringClass(buffer, resultLength);
1237  }
1238 }
1239 
1240 template<typename StringClass, typename OutputIterator> inline void
1241 Locale::getUnicodeKeywords(OutputIterator iterator, UErrorCode& status) const
1242 {
1243  LocalPointer<StringEnumeration> keys(createUnicodeKeywords(status));
1244  if (U_FAILURE(status) || keys.isNull()) {
1245  return;
1246  }
1247  for (;;) {
1248  int32_t resultLength;
1249  const char* buffer = keys->next(&resultLength, status);
1250  if (U_FAILURE(status) || buffer == nullptr) {
1251  return;
1252  }
1253  *iterator++ = StringClass(buffer, resultLength);
1254  }
1255 }
1256 
1257 template<typename StringClass> inline StringClass
1258 Locale::getKeywordValue(StringPiece keywordName, UErrorCode& status) const
1259 {
1260  StringClass result;
1261  StringByteSink<StringClass> sink(&result);
1262  getKeywordValue(keywordName, sink, status);
1263  return result;
1264 }
1265 
1266 template<typename StringClass> inline StringClass
1267 Locale::getUnicodeKeywordValue(StringPiece keywordName, UErrorCode& status) const
1268 {
1269  StringClass result;
1270  StringByteSink<StringClass> sink(&result);
1271  getUnicodeKeywordValue(keywordName, sink, status);
1272  return result;
1273 }
1274 
1275 inline UBool
1276 Locale::isBogus(void) const {
1277  return fIsBogus;
1278 }
1279 
1280 U_NAMESPACE_END
1281 
1282 #endif /* U_SHOW_CPLUSPLUS_API */
1283 
1284 #endif
bool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:335
const Locale & next() override
Definition: locid.h:1101
#define U_FAILURE(x)
Does the error code indicate a failure?
Definition: utypes.h:717
#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:866
UBool hasNext() const override
Definition: locid.h:1057
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:1017
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:1040
UBool isNull() const
nullptr check.
Definition: localpointer.h:94
C++ API: "Smart pointers" for use with and in ICU4C C++ code.
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:1051
C++ API: Common ICU base class UObject.
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:267
#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:295
A generic Locale iterator implementation over Locale input iterators.
Definition: locid.h:1076
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:1063
ConvertingIterator(Iter begin, Iter end, Conv converter)
Constructs an iterator from a begin/end range.
Definition: locid.h:1088
UBool hasNext() const override
Definition: locid.h:1095
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