2 **********************************************************************
3 * Copyright (C) 1997-2012, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
9 * Created by: Richard Gillam
11 * Modification History:
13 * Date Name Description
14 * 02/11/97 aliu Changed gLocPath to fgDataDirectory and added
15 * methods to get and set it.
16 * 04/02/97 aliu Made operator!= inline; fixed return value
18 * 04/15/97 aliu Cleanup for AIX/Win32.
19 * 04/24/97 aliu Numerous changes per code review.
20 * 08/18/98 stephen Changed getDisplayName()
21 * Added SIMPLIFIED_CHINESE, TRADITIONAL_CHINESE
22 * Added getISOCountries(), getISOLanguages(),
23 * getLanguagesForCountry()
24 * 03/16/99 bertrand rehaul.
25 * 07/21/99 stephen Added U_CFUNC setDefault
26 * 11/09/99 weiv Added const char * getName() const;
27 * 04/12/00 srl removing unicodestring api's and cached hash code
28 * 08/10/01 grhoten Change the static Locales to accessor functions
29 ******************************************************************************
33 #include "unicode/locid.h"
34 #include "unicode/uloc.h"
45 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
48 static UBool U_CALLCONV locale_cleanup(void);
53 static Locale *gLocaleCache = NULL;
55 // gDefaultLocaleMutex protects all access to gDefaultLocalesHashT and gDefaultLocale.
56 static UMutex gDefaultLocaleMutex = U_MUTEX_INITIALIZER;
57 static UHashtable *gDefaultLocalesHashT = NULL;
58 static Locale *gDefaultLocale = NULL;
62 typedef enum ELocalePos {
76 eCHINA, /* Alias for PRC */
89 U_CFUNC int32_t locale_getKeywords(const char *localeID,
91 char *keywords, int32_t keywordCapacity,
92 char *values, int32_t valuesCapacity, int32_t *valLen,
98 // Deleter function for Locales owned by the default Locale hash table/
100 static void U_CALLCONV
101 deleteLocale(void *obj) {
102 delete (icu::Locale *) obj;
105 static UBool U_CALLCONV locale_cleanup(void)
110 delete [] gLocaleCache;
114 if (gDefaultLocalesHashT) {
115 uhash_close(gDefaultLocalesHashT); // Automatically deletes all elements, using deleter func.
116 gDefaultLocalesHashT = NULL;
117 gDefaultLocale = NULL;
126 Locale *locale_set_default_internal(const char *id, UErrorCode& status) {
127 // Synchronize this entire function.
128 Mutex lock(&gDefaultLocaleMutex);
130 UBool canonicalize = FALSE;
132 // If given a NULL string for the locale id, grab the default
133 // name from the system.
134 // (Different from most other locale APIs, where a null name means use
135 // the current ICU default locale.)
137 id = uprv_getDefaultLocaleID(); // This function not thread safe? TODO: verify.
138 canonicalize = TRUE; // always canonicalize host ID
141 char localeNameBuf[512];
144 uloc_canonicalize(id, localeNameBuf, sizeof(localeNameBuf)-1, &status);
146 uloc_getName(id, localeNameBuf, sizeof(localeNameBuf)-1, &status);
148 localeNameBuf[sizeof(localeNameBuf)-1] = 0; // Force null termination in event of
149 // a long name filling the buffer.
150 // (long names are truncated.)
152 if (U_FAILURE(status)) {
153 return gDefaultLocale;
156 if (gDefaultLocalesHashT == NULL) {
157 gDefaultLocalesHashT = uhash_open(uhash_hashChars, uhash_compareChars, NULL, &status);
158 if (U_FAILURE(status)) {
159 return gDefaultLocale;
161 uhash_setValueDeleter(gDefaultLocalesHashT, deleteLocale);
162 ucln_common_registerCleanup(UCLN_COMMON_LOCALE, locale_cleanup);
165 Locale *newDefault = (Locale *)uhash_get(gDefaultLocalesHashT, localeNameBuf);
166 if (newDefault == NULL) {
167 newDefault = new Locale(Locale::eBOGUS);
168 if (newDefault == NULL) {
169 status = U_MEMORY_ALLOCATION_ERROR;
170 return gDefaultLocale;
172 newDefault->init(localeNameBuf, FALSE);
173 uhash_put(gDefaultLocalesHashT, (char*) newDefault->getName(), newDefault, &status);
174 if (U_FAILURE(status)) {
175 return gDefaultLocale;
178 gDefaultLocale = newDefault;
179 return gDefaultLocale;
186 locale_set_default(const char *id)
189 UErrorCode status = U_ZERO_ERROR;
190 locale_set_default_internal(id, status);
195 locale_get_default(void)
198 return Locale::getDefault().getName();
204 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Locale)
206 /*Character separating the posix id fields*/
208 // In the platform codepage.
213 /*if fullName is on the heap, we free it*/
214 if (fullName != fullNameBuffer)
219 if (baseName && baseName != baseNameBuffer) {
226 : UObject(), fullName(fullNameBuffer), baseName(NULL)
232 * Internal constructor to allow construction of a locale object with
233 * NO side effects. (Default constructor tries to get
234 * the default locale.)
236 Locale::Locale(Locale::ELocaleType)
237 : UObject(), fullName(fullNameBuffer), baseName(NULL)
243 Locale::Locale( const char * newLanguage,
244 const char * newCountry,
245 const char * newVariant,
246 const char * newKeywords)
247 : UObject(), fullName(fullNameBuffer), baseName(NULL)
249 if( (newLanguage==NULL) && (newCountry == NULL) && (newVariant == NULL) )
251 init(NULL, FALSE); /* shortcut */
255 MaybeStackArray<char, ULOC_FULLNAME_CAPACITY> togo;
263 // Calculate the size of the resulting string.
266 if ( newLanguage != NULL )
268 lsize = (int32_t)uprv_strlen(newLanguage);
273 if ( newCountry != NULL )
275 csize = (int32_t)uprv_strlen(newCountry);
280 if ( newVariant != NULL )
282 // remove leading _'s
283 while(newVariant[0] == SEP_CHAR)
288 // remove trailing _'s
289 vsize = (int32_t)uprv_strlen(newVariant);
290 while( (vsize>1) && (newVariant[vsize-1] == SEP_CHAR) )
304 size += 2; // at least: __v
306 else if ( csize > 0 )
308 size += 1; // at least: _v
311 if ( newKeywords != NULL)
313 ksize = (int32_t)uprv_strlen(newKeywords);
318 // NOW we have the full locale string..
320 /*if the whole string is longer than our internal limit, we need
321 to go to the heap for temporary buffers*/
322 if (size >= togo.getCapacity())
324 // If togo_heap could not be created, initialize with default settings.
325 if (togo.resize(size+1) == NULL) {
332 // Now, copy it back.
336 uprv_strcpy(p, newLanguage);
340 if ( ( vsize != 0 ) || (csize != 0) ) // at least: __v
347 uprv_strcpy(p, newCountry);
353 *p++ = SEP_CHAR; // at least: __v
355 uprv_strncpy(p, newVariant, vsize); // Must use strncpy because
356 p += vsize; // of trimming (above).
362 if (uprv_strchr(newKeywords, '=')) {
363 *p++ = '@'; /* keyword parsing */
366 *p++ = '_'; /* Variant parsing with a script */
368 *p++ = '_'; /* No country found */
371 uprv_strcpy(p, newKeywords);
375 // Parse it, because for example 'language' might really be a complete
377 init(togo.getAlias(), FALSE);
381 Locale::Locale(const Locale &other)
382 : UObject(other), fullName(fullNameBuffer), baseName(NULL)
387 Locale &Locale::operator=(const Locale &other)
389 if (this == &other) {
393 if (&other == NULL) {
398 /* Free our current storage */
399 if(fullName != fullNameBuffer) {
401 fullName = fullNameBuffer;
404 /* Allocate the full name if necessary */
405 if(other.fullName != other.fullNameBuffer) {
406 fullName = (char *)uprv_malloc(sizeof(char)*(uprv_strlen(other.fullName)+1));
407 if (fullName == NULL) {
411 /* Copy the full name */
412 uprv_strcpy(fullName, other.fullName);
414 /* baseName is the cached result of getBaseName. if 'other' has a
415 baseName and it fits in baseNameBuffer, then copy it. otherwise set
416 it to NULL, and let the user lazy-create it (in getBaseName) if they
418 if(baseName && baseName != baseNameBuffer) {
423 if(other.baseName == other.baseNameBuffer) {
424 uprv_strcpy(baseNameBuffer, other.baseNameBuffer);
425 baseName = baseNameBuffer;
428 /* Copy the language and country fields */
429 uprv_strcpy(language, other.language);
430 uprv_strcpy(script, other.script);
431 uprv_strcpy(country, other.country);
433 /* The variantBegin is an offset, just copy it */
434 variantBegin = other.variantBegin;
435 fIsBogus = other.fIsBogus;
440 Locale::clone() const {
441 return new Locale(*this);
445 Locale::operator==( const Locale& other) const
447 return (uprv_strcmp(other.fullName, fullName) == 0);
450 #define ISASCIIALPHA(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
452 /*This function initializes a Locale from a C locale ID*/
453 Locale& Locale::init(const char* localeID, UBool canonicalize)
456 /* Free our current storage */
457 if(fullName != fullNameBuffer) {
459 fullName = fullNameBuffer;
462 if(baseName && baseName != baseNameBuffer) {
468 // just an easy way to have a common error-exit
469 // without goto and without another function
472 char *field[5] = {0};
473 int32_t fieldLen[5] = {0};
475 int32_t variantField;
479 if(localeID == NULL) {
480 // not an error, just set the default locale
481 return *this = getDefault();
484 /* preset all fields to empty */
485 language[0] = script[0] = country[0] = 0;
487 // "canonicalize" the locale ID to ICU/Java format
489 length = canonicalize ?
490 uloc_canonicalize(localeID, fullName, sizeof(fullNameBuffer), &err) :
491 uloc_getName(localeID, fullName, sizeof(fullNameBuffer), &err);
493 if(err == U_BUFFER_OVERFLOW_ERROR || length >= (int32_t)sizeof(fullNameBuffer)) {
494 /*Go to heap for the fullName if necessary*/
495 fullName = (char *)uprv_malloc(sizeof(char)*(length + 1));
497 fullName = fullNameBuffer;
498 break; // error: out of memory
501 length = canonicalize ?
502 uloc_canonicalize(localeID, fullName, length+1, &err) :
503 uloc_getName(localeID, fullName, length+1, &err);
505 if(U_FAILURE(err) || err == U_STRING_NOT_TERMINATED_WARNING) {
506 /* should never occur */
510 variantBegin = length;
512 /* after uloc_getName/canonicalize() we know that only '_' are separators */
513 separator = field[0] = fullName;
515 while ((separator = uprv_strchr(field[fieldIdx-1], SEP_CHAR)) && fieldIdx < (int32_t)(sizeof(field)/sizeof(field[0]))-1) {
516 field[fieldIdx] = separator + 1;
517 fieldLen[fieldIdx-1] = (int32_t)(separator - field[fieldIdx-1]);
520 // variant may contain @foo or .foo POSIX cruft; remove it
521 separator = uprv_strchr(field[fieldIdx-1], '@');
522 char* sep2 = uprv_strchr(field[fieldIdx-1], '.');
523 if (separator!=NULL || sep2!=NULL) {
524 if (separator==NULL || (sep2!=NULL && separator > sep2)) {
527 fieldLen[fieldIdx-1] = (int32_t)(separator - field[fieldIdx-1]);
529 fieldLen[fieldIdx-1] = length - (int32_t)(field[fieldIdx-1] - fullName);
532 if (fieldLen[0] >= (int32_t)(sizeof(language)))
534 break; // error: the language field is too long
537 variantField = 1; /* Usually the 2nd one, except when a script or country is also used. */
538 if (fieldLen[0] > 0) {
539 /* We have a language */
540 uprv_memcpy(language, fullName, fieldLen[0]);
541 language[fieldLen[0]] = 0;
543 if (fieldLen[1] == 4 && ISASCIIALPHA(field[1][0]) &&
544 ISASCIIALPHA(field[1][1]) && ISASCIIALPHA(field[1][2]) &&
545 ISASCIIALPHA(field[1][3])) {
546 /* We have at least a script */
547 uprv_memcpy(script, field[1], fieldLen[1]);
548 script[fieldLen[1]] = 0;
552 if (fieldLen[variantField] == 2 || fieldLen[variantField] == 3) {
553 /* We have a country */
554 uprv_memcpy(country, field[variantField], fieldLen[variantField]);
555 country[fieldLen[variantField]] = 0;
557 } else if (fieldLen[variantField] == 0) {
558 variantField++; /* script or country empty but variant in next field (i.e. en__POSIX) */
561 if (fieldLen[variantField] > 0) {
562 /* We have a variant */
563 variantBegin = (int32_t)(field[variantField] - fullName);
566 // successful end of init()
568 } while(0); /*loop doesn't iterate*/
570 // when an error occurs, then set this object to "bogus" (there is no UErrorCode here)
577 Locale::hashCode() const
579 return ustr_hashCharsN(fullName, uprv_strlen(fullName));
583 Locale::setToBogus() {
584 /* Free our current storage */
585 if(fullName != fullNameBuffer) {
587 fullName = fullNameBuffer;
589 if(baseName && baseName != baseNameBuffer) {
600 const Locale& U_EXPORT2
604 Mutex lock(&gDefaultLocaleMutex);
605 if (gDefaultLocale != NULL) {
606 return *gDefaultLocale;
609 UErrorCode status = U_ZERO_ERROR;
610 return *locale_set_default_internal(NULL, status);
616 Locale::setDefault( const Locale& newLocale,
619 if (U_FAILURE(status)) {
623 /* Set the default from the full name string of the supplied locale.
624 * This is a convenient way to access the default locale caching mechanisms.
626 const char *localeID = newLocale.getName();
627 locale_set_default_internal(localeID, status);
631 Locale::createFromName (const char *name)
644 Locale::createCanonical(const char* name) {
646 loc.init(name, TRUE);
651 Locale::getISO3Language() const
653 return uloc_getISO3Language(fullName);
658 Locale::getISO3Country() const
660 return uloc_getISO3Country(fullName);
664 * Return the LCID value as specified in the "LocaleID" resource for this
665 * locale. The LocaleID must be expressed as a hexadecimal number, from
666 * one to four digits. If the LocaleID resource is not present, or is
667 * in an incorrect format, 0 is returned. The LocaleID is for use in
668 * Windows (it is an LCID), but is available on all platforms.
671 Locale::getLCID() const
673 return uloc_getLCID(fullName);
676 const char* const* U_EXPORT2 Locale::getISOCountries()
678 return uloc_getISOCountries();
681 const char* const* U_EXPORT2 Locale::getISOLanguages()
683 return uloc_getISOLanguages();
686 // Set the locale's data based on a posix id.
687 void Locale::setFromPOSIXID(const char *posixID)
692 const Locale & U_EXPORT2
693 Locale::getRoot(void)
695 return getLocale(eROOT);
698 const Locale & U_EXPORT2
699 Locale::getEnglish(void)
701 return getLocale(eENGLISH);
704 const Locale & U_EXPORT2
705 Locale::getFrench(void)
707 return getLocale(eFRENCH);
710 const Locale & U_EXPORT2
711 Locale::getGerman(void)
713 return getLocale(eGERMAN);
716 const Locale & U_EXPORT2
717 Locale::getItalian(void)
719 return getLocale(eITALIAN);
722 const Locale & U_EXPORT2
723 Locale::getJapanese(void)
725 return getLocale(eJAPANESE);
728 const Locale & U_EXPORT2
729 Locale::getKorean(void)
731 return getLocale(eKOREAN);
734 const Locale & U_EXPORT2
735 Locale::getChinese(void)
737 return getLocale(eCHINESE);
740 const Locale & U_EXPORT2
741 Locale::getSimplifiedChinese(void)
743 return getLocale(eCHINA);
746 const Locale & U_EXPORT2
747 Locale::getTraditionalChinese(void)
749 return getLocale(eTAIWAN);
753 const Locale & U_EXPORT2
754 Locale::getFrance(void)
756 return getLocale(eFRANCE);
759 const Locale & U_EXPORT2
760 Locale::getGermany(void)
762 return getLocale(eGERMANY);
765 const Locale & U_EXPORT2
766 Locale::getItaly(void)
768 return getLocale(eITALY);
771 const Locale & U_EXPORT2
772 Locale::getJapan(void)
774 return getLocale(eJAPAN);
777 const Locale & U_EXPORT2
778 Locale::getKorea(void)
780 return getLocale(eKOREA);
783 const Locale & U_EXPORT2
784 Locale::getChina(void)
786 return getLocale(eCHINA);
789 const Locale & U_EXPORT2
792 return getLocale(eCHINA);
795 const Locale & U_EXPORT2
796 Locale::getTaiwan(void)
798 return getLocale(eTAIWAN);
801 const Locale & U_EXPORT2
804 return getLocale(eUK);
807 const Locale & U_EXPORT2
810 return getLocale(eUS);
813 const Locale & U_EXPORT2
814 Locale::getCanada(void)
816 return getLocale(eCANADA);
819 const Locale & U_EXPORT2
820 Locale::getCanadaFrench(void)
822 return getLocale(eCANADA_FRENCH);
826 Locale::getLocale(int locid)
828 Locale *localeCache = getLocaleCache();
829 U_ASSERT((locid < eMAX_LOCALES)&&(locid>=0));
830 if (localeCache == NULL) {
831 // Failure allocating the locale cache.
832 // The best we can do is return a NULL reference.
835 return localeCache[locid]; /*operating on NULL*/
839 This function is defined this way in order to get around static
840 initialization and static destruction.
843 Locale::getLocaleCache(void)
846 UBool needInit = (gLocaleCache == NULL);
850 Locale *tLocaleCache = new Locale[(int)eMAX_LOCALES];
851 if (tLocaleCache == NULL) {
854 tLocaleCache[eROOT] = Locale("");
855 tLocaleCache[eENGLISH] = Locale("en");
856 tLocaleCache[eFRENCH] = Locale("fr");
857 tLocaleCache[eGERMAN] = Locale("de");
858 tLocaleCache[eITALIAN] = Locale("it");
859 tLocaleCache[eJAPANESE] = Locale("ja");
860 tLocaleCache[eKOREAN] = Locale("ko");
861 tLocaleCache[eCHINESE] = Locale("zh");
862 tLocaleCache[eFRANCE] = Locale("fr", "FR");
863 tLocaleCache[eGERMANY] = Locale("de", "DE");
864 tLocaleCache[eITALY] = Locale("it", "IT");
865 tLocaleCache[eJAPAN] = Locale("ja", "JP");
866 tLocaleCache[eKOREA] = Locale("ko", "KR");
867 tLocaleCache[eCHINA] = Locale("zh", "CN");
868 tLocaleCache[eTAIWAN] = Locale("zh", "TW");
869 tLocaleCache[eUK] = Locale("en", "GB");
870 tLocaleCache[eUS] = Locale("en", "US");
871 tLocaleCache[eCANADA] = Locale("en", "CA");
872 tLocaleCache[eCANADA_FRENCH] = Locale("fr", "CA");
875 if (gLocaleCache == NULL) {
876 gLocaleCache = tLocaleCache;
878 ucln_common_registerCleanup(UCLN_COMMON_LOCALE, locale_cleanup);
882 delete [] tLocaleCache; // Fancy array delete will destruct each member.
888 class KeywordEnumeration : public StringEnumeration {
893 UnicodeString currUSKey;
894 static const char fgClassID;/* Warning this is used beyond the typical RTTI usage. */
897 static UClassID U_EXPORT2 getStaticClassID(void) { return (UClassID)&fgClassID; }
898 virtual UClassID getDynamicClassID(void) const { return getStaticClassID(); }
900 KeywordEnumeration(const char *keys, int32_t keywordLen, int32_t currentIndex, UErrorCode &status)
901 : keywords((char *)&fgClassID), current((char *)&fgClassID), length(0) {
902 if(U_SUCCESS(status) && keywordLen != 0) {
903 if(keys == NULL || keywordLen < 0) {
904 status = U_ILLEGAL_ARGUMENT_ERROR;
906 keywords = (char *)uprv_malloc(keywordLen+1);
907 if (keywords == NULL) {
908 status = U_MEMORY_ALLOCATION_ERROR;
911 uprv_memcpy(keywords, keys, keywordLen);
912 keywords[keywordLen] = 0;
913 current = keywords + currentIndex;
920 virtual ~KeywordEnumeration();
922 virtual StringEnumeration * clone() const
924 UErrorCode status = U_ZERO_ERROR;
925 return new KeywordEnumeration(keywords, length, (int32_t)(current - keywords), status);
928 virtual int32_t count(UErrorCode &/*status*/) const {
933 kw += uprv_strlen(kw)+1;
938 virtual const char* next(int32_t* resultLength, UErrorCode& status) {
941 if(U_SUCCESS(status) && *current != 0) {
943 len = (int32_t)uprv_strlen(current);
945 if(resultLength != NULL) {
949 if(resultLength != NULL) {
957 virtual const UnicodeString* snext(UErrorCode& status) {
958 int32_t resultLength = 0;
959 const char *s = next(&resultLength, status);
960 return setChars(s, resultLength, status);
963 virtual void reset(UErrorCode& /*status*/) {
968 const char KeywordEnumeration::fgClassID = '\0';
970 KeywordEnumeration::~KeywordEnumeration() {
975 Locale::createKeywords(UErrorCode &status) const
978 int32_t keywordCapacity = 256;
979 StringEnumeration *result = NULL;
981 const char* variantStart = uprv_strchr(fullName, '@');
982 const char* assignment = uprv_strchr(fullName, '=');
984 if(assignment > variantStart) {
985 int32_t keyLen = locale_getKeywords(variantStart+1, '@', keywords, keywordCapacity, NULL, 0, NULL, FALSE, &status);
987 result = new KeywordEnumeration(keywords, keyLen, 0, status);
990 status = U_INVALID_FORMAT_ERROR;
997 Locale::getKeywordValue(const char* keywordName, char *buffer, int32_t bufLen, UErrorCode &status) const
999 return uloc_getKeywordValue(fullName, keywordName, buffer, bufLen, &status);
1003 Locale::setKeywordValue(const char* keywordName, const char* keywordValue, UErrorCode &status)
1005 uloc_setKeywordValue(keywordName, keywordValue, fullName, ULOC_FULLNAME_CAPACITY, &status);
1009 Locale::getBaseName() const
1012 UErrorCode status = U_ZERO_ERROR;
1013 // semantically const
1015 ((Locale *)this)->baseName = ((Locale *)this)->baseNameBuffer;
1016 int32_t baseNameSize = uloc_getBaseName(fullName, baseName, ULOC_FULLNAME_CAPACITY, &status);
1017 if(baseNameSize >= ULOC_FULLNAME_CAPACITY) {
1018 ((Locale *)this)->baseName = (char *)uprv_malloc(sizeof(char) * baseNameSize + 1);
1019 if (baseName == NULL) {
1022 uloc_getBaseName(fullName, baseName, baseNameSize+1, &status);
1024 baseName[baseNameSize] = 0;
1026 // the computation of variantBegin leaves it equal to the length
1027 // of fullName if there is no variant. It should instead be
1028 // the length of the baseName. Patch around this for now.
1029 if (variantBegin == (int32_t)uprv_strlen(fullName)) {
1030 ((Locale*)this)->variantBegin = baseNameSize;