ICU 73.2  73.2
locid.h
Go to the documentation of this file.
00001 // © 2016 and later: Unicode, Inc. and others.
00002 // License & terms of use: http://www.unicode.org/copyright.html
00003 /*
00004 ******************************************************************************
00005 *
00006 *   Copyright (C) 1996-2015, International Business Machines
00007 *   Corporation and others.  All Rights Reserved.
00008 *
00009 ******************************************************************************
00010 *
00011 * File locid.h
00012 *
00013 * Created by: Helena Shih
00014 *
00015 * Modification History:
00016 *
00017 *   Date        Name        Description
00018 *   02/11/97    aliu        Changed gLocPath to fgLocPath and added methods to
00019 *                           get and set it.
00020 *   04/02/97    aliu        Made operator!= inline; fixed return value of getName().
00021 *   04/15/97    aliu        Cleanup for AIX/Win32.
00022 *   04/24/97    aliu        Numerous changes per code review.
00023 *   08/18/98    stephen     Added tokenizeString(),changed getDisplayName()
00024 *   09/08/98    stephen     Moved definition of kEmptyString for Mac Port
00025 *   11/09/99    weiv        Added const char * getName() const;
00026 *   04/12/00    srl         removing unicodestring api's and cached hash code
00027 *   08/10/01    grhoten     Change the static Locales to accessor functions
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 // Forward Declarations
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) noexcept;
00298 
00303     virtual ~Locale() ;
00304 
00312     Locale& operator=(const Locale& other);
00313 
00323     Locale& operator=(Locale&& other) noexcept;
00324 
00332     bool    operator==(const    Locale&     other) const;
00333 
00342     inline bool    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  /* U_HIDE_SYSTEM_API */
00390 
00412     static Locale U_EXPORT2 forLanguageTag(StringPiece tag, UErrorCode& status);
00413 
00427     void toLanguageTag(ByteSink& sink, UErrorCode& status) const;
00428 
00439     template<typename StringClass>
00440     inline StringClass toLanguageTag(UErrorCode& status) const;
00441 
00451     static Locale U_EXPORT2 createFromName(const char *name);
00452 
00461     static Locale U_EXPORT2 createCanonical(const char* name);
00462 
00468     inline const char *  getLanguage( ) const;
00469 
00477     inline const char *  getScript( ) const;
00478 
00484     inline const char *  getCountry( ) const;
00485 
00491     inline const char *  getVariant( ) const;
00492 
00501     inline const char * getName() const;
00502 
00510     const char * getBaseName() const;
00511 
00541     void addLikelySubtags(UErrorCode& status);
00542 
00572     void minimizeSubtags(UErrorCode& status);
00573 
00580     void canonicalize(UErrorCode& status);
00581 
00591     StringEnumeration * createKeywords(UErrorCode &status) const;
00592 
00602     StringEnumeration * createUnicodeKeywords(UErrorCode &status) const;
00603 
00615     template<typename StringClass, typename OutputIterator>
00616     inline void getKeywords(OutputIterator iterator, UErrorCode& status) const;
00617 
00629     template<typename StringClass, typename OutputIterator>
00630     inline void getUnicodeKeywords(OutputIterator iterator, UErrorCode& status) const;
00631 
00648     int32_t getKeywordValue(const char* keywordName, char *buffer, int32_t bufferCapacity, UErrorCode &status) const;
00649 
00663     void getKeywordValue(StringPiece keywordName, ByteSink& sink, UErrorCode& status) const;
00664 
00678     template<typename StringClass>
00679     inline StringClass getKeywordValue(StringPiece keywordName, UErrorCode& status) const;
00680 
00694     void getUnicodeKeywordValue(StringPiece keywordName, ByteSink& sink, UErrorCode& status) const;
00695 
00709     template<typename StringClass>
00710     inline StringClass getUnicodeKeywordValue(StringPiece keywordName, UErrorCode& status) const;
00711 
00731     void setKeywordValue(const char* keywordName, const char* keywordValue, UErrorCode &status);
00732 
00751     void setKeywordValue(StringPiece keywordName, StringPiece keywordValue, UErrorCode& status);
00752 
00771     void setUnicodeKeywordValue(StringPiece keywordName, StringPiece keywordValue, UErrorCode& status);
00772 
00779     const char * getISO3Language() const;
00780 
00786     const char * getISO3Country() const;
00787 
00795     uint32_t        getLCID(void) const;
00796 
00810     UBool isRightToLeft() const;
00811 
00821     UnicodeString&  getDisplayLanguage(UnicodeString&   dispLang) const;
00822 
00836     UnicodeString&  getDisplayLanguage( const   Locale&         displayLocale,
00837                                                 UnicodeString&  dispLang) const;
00838 
00848     UnicodeString&  getDisplayScript(          UnicodeString& dispScript) const;
00849 
00864     UnicodeString&  getDisplayScript(  const   Locale&         displayLocale,
00865                                                UnicodeString&  dispScript) const;
00866 
00876     UnicodeString&  getDisplayCountry(          UnicodeString& dispCountry) const;
00877 
00892     UnicodeString&  getDisplayCountry(  const   Locale&         displayLocale,
00893                                                 UnicodeString&  dispCountry) const;
00894 
00902     UnicodeString&  getDisplayVariant(      UnicodeString& dispVar) const;
00903 
00912     UnicodeString&  getDisplayVariant(  const   Locale&         displayLocale,
00913                                                 UnicodeString&  dispVar) const;
00914 
00926     UnicodeString&  getDisplayName(         UnicodeString&  name) const;
00927 
00940     UnicodeString&  getDisplayName( const   Locale&         displayLocale,
00941                                             UnicodeString&  name) const;
00942 
00947     int32_t         hashCode(void) const;
00948 
00957     void setToBogus();
00958 
00964     inline UBool isBogus(void) const;
00965 
00974     static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
00975 
00984     static const char* const* U_EXPORT2 getISOCountries();
00985 
00994     static const char* const* U_EXPORT2 getISOLanguages();
00995 
01001     static UClassID U_EXPORT2 getStaticClassID();
01002 
01008     virtual UClassID getDynamicClassID() const override;
01009 
01014     class U_COMMON_API Iterator /* not : public UObject because this is an interface/mixin class */ {
01015     public:
01017         virtual ~Iterator();
01018 
01023         virtual UBool hasNext() const = 0;
01024 
01029         virtual const Locale &next() = 0;
01030     };
01031 
01036     template<typename Iter>
01037     class RangeIterator : public Iterator, public UMemory {
01038     public:
01048         RangeIterator(Iter begin, Iter end) : it_(begin), end_(end) {}
01049 
01054         UBool hasNext() const override { return it_ != end_; }
01055 
01060         const Locale &next() override { return *it_++; }
01061 
01062     private:
01063         Iter it_;
01064         const Iter end_;
01065     };
01066 
01072     template<typename Iter, typename Conv>
01073     class ConvertingIterator : public Iterator, public UMemory {
01074     public:
01085         ConvertingIterator(Iter begin, Iter end, Conv converter) :
01086                 it_(begin), end_(end), converter_(converter) {}
01087 
01092         UBool hasNext() const override { return it_ != end_; }
01093 
01098         const Locale &next() override { return converter_(*it_++); }
01099 
01100     private:
01101         Iter it_;
01102         const Iter end_;
01103         Conv converter_;
01104     };
01105 
01106 protected: /* only protected for testing purposes. DO NOT USE. */
01107 #ifndef U_HIDE_INTERNAL_API
01108 
01112     void setFromPOSIXID(const char *posixID);
01113 #endif  /* U_HIDE_INTERNAL_API */
01114 
01115 private:
01123     Locale& init(const char* cLocaleID, UBool canonicalize);
01124 
01125     /*
01126      * Internal constructor to allow construction of a locale object with
01127      *   NO side effects.   (Default constructor tries to get
01128      *   the default locale.)
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     // name without keywords
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 bool
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 /* U_SHOW_CPLUSPLUS_API */
01271 
01272 #endif