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
00579 StringEnumeration * createKeywords(UErrorCode &status) const;
00580
00590 StringEnumeration * createUnicodeKeywords(UErrorCode &status) const;
00591
00603 template<typename StringClass, typename OutputIterator>
00604 inline void getKeywords(OutputIterator iterator, UErrorCode& status) const;
00605
00617 template<typename StringClass, typename OutputIterator>
00618 inline void getUnicodeKeywords(OutputIterator iterator, UErrorCode& status) const;
00619
00636 int32_t getKeywordValue(const char* keywordName, char *buffer, int32_t bufferCapacity, UErrorCode &status) const;
00637
00651 void getKeywordValue(StringPiece keywordName, ByteSink& sink, UErrorCode& status) const;
00652
00666 template<typename StringClass>
00667 inline StringClass getKeywordValue(StringPiece keywordName, UErrorCode& status) const;
00668
00682 void getUnicodeKeywordValue(StringPiece keywordName, ByteSink& sink, UErrorCode& status) const;
00683
00697 template<typename StringClass>
00698 inline StringClass getUnicodeKeywordValue(StringPiece keywordName, UErrorCode& status) const;
00699
00719 void setKeywordValue(const char* keywordName, const char* keywordValue, UErrorCode &status);
00720
00739 void setKeywordValue(StringPiece keywordName, StringPiece keywordValue, UErrorCode& status);
00740
00759 void setUnicodeKeywordValue(StringPiece keywordName, StringPiece keywordValue, UErrorCode& status);
00760
00767 const char * getISO3Language() const;
00768
00774 const char * getISO3Country() const;
00775
00783 uint32_t getLCID(void) const;
00784
00798 UBool isRightToLeft() const;
00799
00809 UnicodeString& getDisplayLanguage(UnicodeString& dispLang) const;
00810
00824 UnicodeString& getDisplayLanguage( const Locale& displayLocale,
00825 UnicodeString& dispLang) const;
00826
00836 UnicodeString& getDisplayScript( UnicodeString& dispScript) const;
00837
00852 UnicodeString& getDisplayScript( const Locale& displayLocale,
00853 UnicodeString& dispScript) const;
00854
00864 UnicodeString& getDisplayCountry( UnicodeString& dispCountry) const;
00865
00880 UnicodeString& getDisplayCountry( const Locale& displayLocale,
00881 UnicodeString& dispCountry) const;
00882
00890 UnicodeString& getDisplayVariant( UnicodeString& dispVar) const;
00891
00900 UnicodeString& getDisplayVariant( const Locale& displayLocale,
00901 UnicodeString& dispVar) const;
00902
00914 UnicodeString& getDisplayName( UnicodeString& name) const;
00915
00928 UnicodeString& getDisplayName( const Locale& displayLocale,
00929 UnicodeString& name) const;
00930
00935 int32_t hashCode(void) const;
00936
00945 void setToBogus();
00946
00952 inline UBool isBogus(void) const;
00953
00962 static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
00963
00972 static const char* const* U_EXPORT2 getISOCountries();
00973
00982 static const char* const* U_EXPORT2 getISOLanguages();
00983
00989 static UClassID U_EXPORT2 getStaticClassID();
00990
00996 virtual UClassID getDynamicClassID() const;
00997
00998 #ifndef U_HIDE_DRAFT_API
00999
01003 class U_COMMON_API Iterator {
01004 public:
01006 virtual ~Iterator();
01007
01012 virtual UBool hasNext() const = 0;
01013
01018 virtual const Locale &next() = 0;
01019 };
01020
01025 template<typename Iter>
01026 class RangeIterator : public Iterator, public UMemory {
01027 public:
01037 RangeIterator(Iter begin, Iter end) : it_(begin), end_(end) {}
01038
01043 UBool hasNext() const override { return it_ != end_; }
01044
01049 const Locale &next() override { return *it_++; }
01050
01051 private:
01052 Iter it_;
01053 const Iter end_;
01054 };
01055
01061 template<typename Iter, typename Conv>
01062 class ConvertingIterator : public Iterator, public UMemory {
01063 public:
01074 ConvertingIterator(Iter begin, Iter end, Conv converter) :
01075 it_(begin), end_(end), converter_(converter) {}
01076
01081 UBool hasNext() const override { return it_ != end_; }
01082
01087 const Locale &next() override { return converter_(*it_++); }
01088
01089 private:
01090 Iter it_;
01091 const Iter end_;
01092 Conv converter_;
01093 };
01094 #endif // U_HIDE_DRAFT_API
01095
01096 protected:
01097 #ifndef U_HIDE_INTERNAL_API
01098
01102 void setFromPOSIXID(const char *posixID);
01103 #endif
01104
01105 private:
01113 Locale& init(const char* cLocaleID, UBool canonicalize);
01114
01115
01116
01117
01118
01119
01120 enum ELocaleType {
01121 eBOGUS
01122 };
01123 Locale(ELocaleType);
01124
01128 static Locale *getLocaleCache(void);
01129
01130 char language[ULOC_LANG_CAPACITY];
01131 char script[ULOC_SCRIPT_CAPACITY];
01132 char country[ULOC_COUNTRY_CAPACITY];
01133 int32_t variantBegin;
01134 char* fullName;
01135 char fullNameBuffer[ULOC_FULLNAME_CAPACITY];
01136
01137 char* baseName;
01138 void initBaseName(UErrorCode& status);
01139
01140 UBool fIsBogus;
01141
01142 static const Locale &getLocale(int locid);
01143
01148 friend Locale *locale_set_default_internal(const char *, UErrorCode& status);
01149
01153 friend void U_CALLCONV locale_available_init();
01154 };
01155
01156 inline UBool
01157 Locale::operator!=(const Locale& other) const
01158 {
01159 return !operator==(other);
01160 }
01161
01162 template<typename StringClass> inline StringClass
01163 Locale::toLanguageTag(UErrorCode& status) const
01164 {
01165 StringClass result;
01166 StringByteSink<StringClass> sink(&result);
01167 toLanguageTag(sink, status);
01168 return result;
01169 }
01170
01171 inline const char *
01172 Locale::getCountry() const
01173 {
01174 return country;
01175 }
01176
01177 inline const char *
01178 Locale::getLanguage() const
01179 {
01180 return language;
01181 }
01182
01183 inline const char *
01184 Locale::getScript() const
01185 {
01186 return script;
01187 }
01188
01189 inline const char *
01190 Locale::getVariant() const
01191 {
01192 return &baseName[variantBegin];
01193 }
01194
01195 inline const char *
01196 Locale::getName() const
01197 {
01198 return fullName;
01199 }
01200
01201 template<typename StringClass, typename OutputIterator> inline void
01202 Locale::getKeywords(OutputIterator iterator, UErrorCode& status) const
01203 {
01204 LocalPointer<StringEnumeration> keys(createKeywords(status));
01205 if (U_FAILURE(status) || keys.isNull()) {
01206 return;
01207 }
01208 for (;;) {
01209 int32_t resultLength;
01210 const char* buffer = keys->next(&resultLength, status);
01211 if (U_FAILURE(status) || buffer == nullptr) {
01212 return;
01213 }
01214 *iterator++ = StringClass(buffer, resultLength);
01215 }
01216 }
01217
01218 template<typename StringClass, typename OutputIterator> inline void
01219 Locale::getUnicodeKeywords(OutputIterator iterator, UErrorCode& status) const
01220 {
01221 LocalPointer<StringEnumeration> keys(createUnicodeKeywords(status));
01222 if (U_FAILURE(status) || keys.isNull()) {
01223 return;
01224 }
01225 for (;;) {
01226 int32_t resultLength;
01227 const char* buffer = keys->next(&resultLength, status);
01228 if (U_FAILURE(status) || buffer == nullptr) {
01229 return;
01230 }
01231 *iterator++ = StringClass(buffer, resultLength);
01232 }
01233 }
01234
01235 template<typename StringClass> inline StringClass
01236 Locale::getKeywordValue(StringPiece keywordName, UErrorCode& status) const
01237 {
01238 StringClass result;
01239 StringByteSink<StringClass> sink(&result);
01240 getKeywordValue(keywordName, sink, status);
01241 return result;
01242 }
01243
01244 template<typename StringClass> inline StringClass
01245 Locale::getUnicodeKeywordValue(StringPiece keywordName, UErrorCode& status) const
01246 {
01247 StringClass result;
01248 StringByteSink<StringClass> sink(&result);
01249 getUnicodeKeywordValue(keywordName, sink, status);
01250 return result;
01251 }
01252
01253 inline UBool
01254 Locale::isBogus(void) const {
01255 return fIsBogus;
01256 }
01257
01258 U_NAMESPACE_END
01259
01260 #endif
01261
01262 #endif