00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef LOCID_H
00032 #define LOCID_H
00033
00034 #include "unicode/utypes.h"
00035
00036 #if U_SHOW_CPLUSPLUS_API
00037
00038 #include "unicode/bytestream.h"
00039 #include "unicode/localpointer.h"
00040 #include "unicode/strenum.h"
00041 #include "unicode/stringpiece.h"
00042 #include "unicode/uobject.h"
00043 #include "unicode/putil.h"
00044 #include "unicode/uloc.h"
00045
00051 U_NAMESPACE_BEGIN
00052
00053
00054 void U_CALLCONV locale_available_init();
00056 class StringEnumeration;
00057 class UnicodeString;
00058
00195 class U_COMMON_API Locale : public UObject {
00196 public:
00198 static const Locale &U_EXPORT2 getRoot(void);
00200 static const Locale &U_EXPORT2 getEnglish(void);
00202 static const Locale &U_EXPORT2 getFrench(void);
00204 static const Locale &U_EXPORT2 getGerman(void);
00206 static const Locale &U_EXPORT2 getItalian(void);
00208 static const Locale &U_EXPORT2 getJapanese(void);
00210 static const Locale &U_EXPORT2 getKorean(void);
00212 static const Locale &U_EXPORT2 getChinese(void);
00214 static const Locale &U_EXPORT2 getSimplifiedChinese(void);
00216 static const Locale &U_EXPORT2 getTraditionalChinese(void);
00217
00219 static const Locale &U_EXPORT2 getFrance(void);
00221 static const Locale &U_EXPORT2 getGermany(void);
00223 static const Locale &U_EXPORT2 getItaly(void);
00225 static const Locale &U_EXPORT2 getJapan(void);
00227 static const Locale &U_EXPORT2 getKorea(void);
00229 static const Locale &U_EXPORT2 getChina(void);
00231 static const Locale &U_EXPORT2 getPRC(void);
00233 static const Locale &U_EXPORT2 getTaiwan(void);
00235 static const Locale &U_EXPORT2 getUK(void);
00237 static const Locale &U_EXPORT2 getUS(void);
00239 static const Locale &U_EXPORT2 getCanada(void);
00241 static const Locale &U_EXPORT2 getCanadaFrench(void);
00242
00243
00251 Locale();
00252
00277 Locale( const char * language,
00278 const char * country = 0,
00279 const char * variant = 0,
00280 const char * keywordsAndValues = 0);
00281
00288 Locale(const Locale& other);
00289
00297 Locale(Locale&& other) U_NOEXCEPT;
00298
00303 virtual ~Locale() ;
00304
00312 Locale& operator=(const Locale& other);
00313
00323 Locale& operator=(Locale&& other) U_NOEXCEPT;
00324
00332 UBool operator==(const Locale& other) const;
00333
00342 inline UBool operator!=(const Locale& other) const;
00343
00355 Locale *clone() const;
00356
00357 #ifndef U_HIDE_SYSTEM_API
00358
00373 static const Locale& U_EXPORT2 getDefault(void);
00374
00387 static void U_EXPORT2 setDefault(const Locale& newLocale,
00388 UErrorCode& success);
00389 #endif
00390
00408 static Locale U_EXPORT2 forLanguageTag(StringPiece tag, UErrorCode& status);
00409
00423 void toLanguageTag(ByteSink& sink, UErrorCode& status) const;
00424
00435 template<typename StringClass>
00436 inline StringClass toLanguageTag(UErrorCode& status) const;
00437
00447 static Locale U_EXPORT2 createFromName(const char *name);
00448
00457 static Locale U_EXPORT2 createCanonical(const char* name);
00458
00464 inline const char * getLanguage( ) const;
00465
00473 inline const char * getScript( ) const;
00474
00480 inline const char * getCountry( ) const;
00481
00487 inline const char * getVariant( ) const;
00488
00497 inline const char * getName() const;
00498
00506 const char * getBaseName() const;
00507
00537 void addLikelySubtags(UErrorCode& status);
00538
00568 void minimizeSubtags(UErrorCode& status);
00569
00570 #ifndef U_HIDE_DRAFT_API
00571
00577 void canonicalize(UErrorCode& status);
00578 #endif // U_HIDE_DRAFT_API
00579
00589 StringEnumeration * createKeywords(UErrorCode &status) const;
00590
00600 StringEnumeration * createUnicodeKeywords(UErrorCode &status) const;
00601
00613 template<typename StringClass, typename OutputIterator>
00614 inline void getKeywords(OutputIterator iterator, UErrorCode& status) const;
00615
00627 template<typename StringClass, typename OutputIterator>
00628 inline void getUnicodeKeywords(OutputIterator iterator, UErrorCode& status) const;
00629
00646 int32_t getKeywordValue(const char* keywordName, char *buffer, int32_t bufferCapacity, UErrorCode &status) const;
00647
00661 void getKeywordValue(StringPiece keywordName, ByteSink& sink, UErrorCode& status) const;
00662
00676 template<typename StringClass>
00677 inline StringClass getKeywordValue(StringPiece keywordName, UErrorCode& status) const;
00678
00692 void getUnicodeKeywordValue(StringPiece keywordName, ByteSink& sink, UErrorCode& status) const;
00693
00707 template<typename StringClass>
00708 inline StringClass getUnicodeKeywordValue(StringPiece keywordName, UErrorCode& status) const;
00709
00729 void setKeywordValue(const char* keywordName, const char* keywordValue, UErrorCode &status);
00730
00749 void setKeywordValue(StringPiece keywordName, StringPiece keywordValue, UErrorCode& status);
00750
00769 void setUnicodeKeywordValue(StringPiece keywordName, StringPiece keywordValue, UErrorCode& status);
00770
00777 const char * getISO3Language() const;
00778
00784 const char * getISO3Country() const;
00785
00793 uint32_t getLCID(void) const;
00794
00808 UBool isRightToLeft() const;
00809
00819 UnicodeString& getDisplayLanguage(UnicodeString& dispLang) const;
00820
00834 UnicodeString& getDisplayLanguage( const Locale& displayLocale,
00835 UnicodeString& dispLang) const;
00836
00846 UnicodeString& getDisplayScript( UnicodeString& dispScript) const;
00847
00862 UnicodeString& getDisplayScript( const Locale& displayLocale,
00863 UnicodeString& dispScript) const;
00864
00874 UnicodeString& getDisplayCountry( UnicodeString& dispCountry) const;
00875
00890 UnicodeString& getDisplayCountry( const Locale& displayLocale,
00891 UnicodeString& dispCountry) const;
00892
00900 UnicodeString& getDisplayVariant( UnicodeString& dispVar) const;
00901
00910 UnicodeString& getDisplayVariant( const Locale& displayLocale,
00911 UnicodeString& dispVar) const;
00912
00924 UnicodeString& getDisplayName( UnicodeString& name) const;
00925
00938 UnicodeString& getDisplayName( const Locale& displayLocale,
00939 UnicodeString& name) const;
00940
00945 int32_t hashCode(void) const;
00946
00955 void setToBogus();
00956
00962 inline UBool isBogus(void) const;
00963
00972 static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
00973
00982 static const char* const* U_EXPORT2 getISOCountries();
00983
00992 static const char* const* U_EXPORT2 getISOLanguages();
00993
00999 static UClassID U_EXPORT2 getStaticClassID();
01000
01006 virtual UClassID getDynamicClassID() const;
01007
01008 #ifndef U_HIDE_DRAFT_API
01009
01013 class U_COMMON_API Iterator {
01014 public:
01016 virtual ~Iterator();
01017
01022 virtual UBool hasNext() const = 0;
01023
01028 virtual const Locale &next() = 0;
01029 };
01030
01035 template<typename Iter>
01036 class RangeIterator : public Iterator, public UMemory {
01037 public:
01047 RangeIterator(Iter begin, Iter end) : it_(begin), end_(end) {}
01048
01053 UBool hasNext() const override { return it_ != end_; }
01054
01059 const Locale &next() override { return *it_++; }
01060
01061 private:
01062 Iter it_;
01063 const Iter end_;
01064 };
01065
01071 template<typename Iter, typename Conv>
01072 class ConvertingIterator : public Iterator, public UMemory {
01073 public:
01084 ConvertingIterator(Iter begin, Iter end, Conv converter) :
01085 it_(begin), end_(end), converter_(converter) {}
01086
01091 UBool hasNext() const override { return it_ != end_; }
01092
01097 const Locale &next() override { return converter_(*it_++); }
01098
01099 private:
01100 Iter it_;
01101 const Iter end_;
01102 Conv converter_;
01103 };
01104 #endif // U_HIDE_DRAFT_API
01105
01106 protected:
01107 #ifndef U_HIDE_INTERNAL_API
01108
01112 void setFromPOSIXID(const char *posixID);
01113 #endif
01114
01115 private:
01123 Locale& init(const char* cLocaleID, UBool canonicalize);
01124
01125
01126
01127
01128
01129
01130 enum ELocaleType {
01131 eBOGUS
01132 };
01133 Locale(ELocaleType);
01134
01138 static Locale *getLocaleCache(void);
01139
01140 char language[ULOC_LANG_CAPACITY];
01141 char script[ULOC_SCRIPT_CAPACITY];
01142 char country[ULOC_COUNTRY_CAPACITY];
01143 int32_t variantBegin;
01144 char* fullName;
01145 char fullNameBuffer[ULOC_FULLNAME_CAPACITY];
01146
01147 char* baseName;
01148 void initBaseName(UErrorCode& status);
01149
01150 UBool fIsBogus;
01151
01152 static const Locale &getLocale(int locid);
01153
01158 friend Locale *locale_set_default_internal(const char *, UErrorCode& status);
01159
01163 friend void U_CALLCONV locale_available_init();
01164 };
01165
01166 inline UBool
01167 Locale::operator!=(const Locale& other) const
01168 {
01169 return !operator==(other);
01170 }
01171
01172 template<typename StringClass> inline StringClass
01173 Locale::toLanguageTag(UErrorCode& status) const
01174 {
01175 StringClass result;
01176 StringByteSink<StringClass> sink(&result);
01177 toLanguageTag(sink, status);
01178 return result;
01179 }
01180
01181 inline const char *
01182 Locale::getCountry() const
01183 {
01184 return country;
01185 }
01186
01187 inline const char *
01188 Locale::getLanguage() const
01189 {
01190 return language;
01191 }
01192
01193 inline const char *
01194 Locale::getScript() const
01195 {
01196 return script;
01197 }
01198
01199 inline const char *
01200 Locale::getVariant() const
01201 {
01202 return &baseName[variantBegin];
01203 }
01204
01205 inline const char *
01206 Locale::getName() const
01207 {
01208 return fullName;
01209 }
01210
01211 template<typename StringClass, typename OutputIterator> inline void
01212 Locale::getKeywords(OutputIterator iterator, UErrorCode& status) const
01213 {
01214 LocalPointer<StringEnumeration> keys(createKeywords(status));
01215 if (U_FAILURE(status) || keys.isNull()) {
01216 return;
01217 }
01218 for (;;) {
01219 int32_t resultLength;
01220 const char* buffer = keys->next(&resultLength, status);
01221 if (U_FAILURE(status) || buffer == nullptr) {
01222 return;
01223 }
01224 *iterator++ = StringClass(buffer, resultLength);
01225 }
01226 }
01227
01228 template<typename StringClass, typename OutputIterator> inline void
01229 Locale::getUnicodeKeywords(OutputIterator iterator, UErrorCode& status) const
01230 {
01231 LocalPointer<StringEnumeration> keys(createUnicodeKeywords(status));
01232 if (U_FAILURE(status) || keys.isNull()) {
01233 return;
01234 }
01235 for (;;) {
01236 int32_t resultLength;
01237 const char* buffer = keys->next(&resultLength, status);
01238 if (U_FAILURE(status) || buffer == nullptr) {
01239 return;
01240 }
01241 *iterator++ = StringClass(buffer, resultLength);
01242 }
01243 }
01244
01245 template<typename StringClass> inline StringClass
01246 Locale::getKeywordValue(StringPiece keywordName, UErrorCode& status) const
01247 {
01248 StringClass result;
01249 StringByteSink<StringClass> sink(&result);
01250 getKeywordValue(keywordName, sink, status);
01251 return result;
01252 }
01253
01254 template<typename StringClass> inline StringClass
01255 Locale::getUnicodeKeywordValue(StringPiece keywordName, UErrorCode& status) const
01256 {
01257 StringClass result;
01258 StringByteSink<StringClass> sink(&result);
01259 getUnicodeKeywordValue(keywordName, sink, status);
01260 return result;
01261 }
01262
01263 inline UBool
01264 Locale::isBogus(void) const {
01265 return fIsBogus;
01266 }
01267
01268 U_NAMESPACE_END
01269
01270 #endif
01271
01272 #endif