From 17dbf189a9cadd33031ad4a17677d8639ec6f56c Mon Sep 17 00:00:00 2001 From: Vladimir Weinstein Date: Thu, 11 Nov 1999 21:25:17 +0000 Subject: [PATCH] ICU-67 Added ResourceBundle::getLocale() and supporting stuff (Locale::getName, rewritten ResourceBundle::constructForLocale...) X-SVN-Rev: 173 --- icu4c/source/common/locid.cpp | 7 ++++++ icu4c/source/common/locid.h | 28 +++++++++++++++------- icu4c/source/common/resbund.cpp | 42 ++++++++++++++++++--------------- icu4c/source/common/resbund.h | 10 +++++++- icu4c/source/common/ures.cpp | 20 ++++++++++++---- icu4c/source/common/ures.h | 9 +++++++ 6 files changed, 83 insertions(+), 33 deletions(-) diff --git a/icu4c/source/common/locid.cpp b/icu4c/source/common/locid.cpp index 397115a4ef..efb1e918b9 100644 --- a/icu4c/source/common/locid.cpp +++ b/icu4c/source/common/locid.cpp @@ -29,6 +29,7 @@ * getLanguagesForCountry() * 03/16/99 bertrand rehaul. * 07/21/99 stephen Added U_CFUNC setDefault +* 11/09/99 weiv Added const char * getName() const; ******************************************************************************* */ @@ -415,6 +416,12 @@ Locale::getName(UnicodeString& name) const return name; } +const char * +Locale::getName() const +{ + return fullName; +} + // deprecated UnicodeString& Locale::getISO3Language(UnicodeString& lang) const diff --git a/icu4c/source/common/locid.h b/icu4c/source/common/locid.h index 3ad6192208..01c8f12af3 100644 --- a/icu4c/source/common/locid.h +++ b/icu4c/source/common/locid.h @@ -22,8 +22,9 @@ * 04/02/97 aliu Made operator!= inline; fixed return value of getName(). * 04/15/97 aliu Cleanup for AIX/Win32. * 04/24/97 aliu Numerous changes per code review. -* 08/18/98 stephen Added tokenizeString(),changed getDisplayName() -* 09/08/98 stephen Moved definition of kEmptyString for Mac Port +* 08/18/98 stephen Added tokenizeString(),changed getDisplayName() +* 09/08/98 stephen Moved definition of kEmptyString for Mac Port +* 11/09/99 weiv Added const char * getName() const; ***************************************************************************************** */ @@ -33,6 +34,13 @@ #include "unistr.h" +typedef struct ULocale ULocale; +typedef struct UHashtable UHashtable; + +#define ULOC_LANG_CAPACITY 3 +#define ULOC_COUNTRY_CAPACITY 3 +#define ULOC_FULLNAME_CAPACITY 50 + /** * * A Locale object represents a specific geographical, political, @@ -170,13 +178,6 @@ * * */ -typedef struct ULocale ULocale; -typedef struct UHashtable UHashtable; - -#define ULOC_LANG_CAPACITY 3 -#define ULOC_COUNTRY_CAPACITY 3 -#define ULOC_FULLNAME_CAPACITY 50 - class U_COMMON_API Locale { public: @@ -324,6 +325,15 @@ public: */ UnicodeString& getName( UnicodeString& name) const; + /** + * Returns the programmatic name of the entire locale, with the language, + * country and variant separated by underbars. If a field is missing, at + * most one underbar will occur. Example: "en", "de_DE", "en_US_WIN", + * "de_POSIX", "fr_MAC" + * @return A pointer to "name". + */ + const char * getName() const; + /** * Fills in "name" with the locale's three-letter language code, as specified * in ISO draft standard ISO-639-2.. diff --git a/icu4c/source/common/resbund.cpp b/icu4c/source/common/resbund.cpp index e748038fc1..8dd9e1bee5 100644 --- a/icu4c/source/common/resbund.cpp +++ b/icu4c/source/common/resbund.cpp @@ -47,6 +47,7 @@ * Cleaned up. * 06/14/99 stephen Removed methods taking a filename suffix. * 06/22/99 stephen Added missing T_FileStream_close in parse() +* 11/09/99 weiv Added getLocale(), rewritten constructForLocale() ******************************************************************************* */ @@ -291,7 +292,7 @@ ResourceBundle::ResourceBundle( const UnicodeString& path, const UnicodeString& localeName, UErrorCode& status) : fPath(path, kDefaultSuffix), - fRealLocaleID(localeName), + fRealLocale(localeName), fIsDataOwned(TRUE), fVersionID(0), fgCache(fgUserCache), @@ -376,35 +377,33 @@ ResourceBundle::constructForLocale(const PathInfo& path, fPath = path; fIsDataOwned = FALSE; fVersionID = 0; - + + // fRealLocale can be inited in three ways, see 1), 2), 3) + UnicodeString returnedLocale; + locale.getName(returnedLocale); + if (returnedLocale.size()!=0) { + // 1) Desired Locale has a name + fRealLocale = Locale(returnedLocale); + } else { + // 2) Desired Locale name is empty, so we use default locale for the system + fRealLocale = Locale(kDefaultLocaleName); + } error = U_ZERO_ERROR; - - locale.getName(fRealLocaleID); - - // if the locale we were passed is Locale("", "", ""), that, by - // convention, refers to the root locale (default.txt), even when - // the system default locale is something else (otherwise there's no - // way to get to it). We can accomplish this by changing the locale - // name to "default" here. I'm not sure this is the best way to do - // this, but it's simple and it works. - if(fRealLocaleID.size() == 0) - fRealLocaleID = kDefaultLocaleName; - for(i = 1; i < kDataCount; ++i) { fData[i] = 0; fDataStatus[i] = U_INTERNAL_PROGRAM_ERROR; fLoaded[i] = FALSE; } - UnicodeString returnedLocale; error = U_ZERO_ERROR; - fData[0] = getHashtableForLocale(fRealLocaleID, returnedLocale, error); + fData[0] = getHashtableForLocale(fRealLocale.getName(), returnedLocale, error); fLoaded[0] = TRUE; fDataStatus[0] = U_ZERO_ERROR; - if(U_SUCCESS(error)) - fRealLocaleID = returnedLocale; + if(U_SUCCESS(error)) + // 3) We're unable to get the desired Locale, so we're using what is provided (fallback occured) + fRealLocale = Locale(returnedLocale); - fLocaleIterator = new LocaleFallbackIterator(fRealLocaleID, + fLocaleIterator = new LocaleFallbackIterator(fRealLocale.getName(), kDefaultLocaleName, FALSE); } @@ -1121,6 +1120,11 @@ ResourceBundle::addToCache(const UnicodeString& localeName, } } +const Locale &ResourceBundle::getLocale(void) const +{ + return fRealLocale; +} + ResourceBundle::PathInfo::PathInfo() : fWPrefix(NULL), fWSuffix(NULL) {} diff --git a/icu4c/source/common/resbund.h b/icu4c/source/common/resbund.h index 4f4668bfab..7c73299b79 100644 --- a/icu4c/source/common/resbund.h +++ b/icu4c/source/common/resbund.h @@ -43,6 +43,7 @@ * Reworked to use new binary format. * Cleaned up. * 06/14/99 stephen Removed methods taking a filename suffix. +* 11/09/99 weiv Added getLocale(), fRealLocale, removed fRealLocaleID ******************************************************************************* */ @@ -423,6 +424,13 @@ public: */ const char* getVersionNumber(void) const; + /** + * Return the Locale associated with this ResourceBundle. + * + * @return a Locale object + */ + const Locale &getLocale(void) const ; + private: class U_COMMON_API PathInfo { public: @@ -621,7 +629,7 @@ private: bool_t fLoaded[kDataCount]; UErrorCode fDataStatus[kDataCount]; // Returns the appropriate error code for each data table. bool_t fIsDataOwned; - UnicodeString fRealLocaleID; + Locale fRealLocale; LocaleFallbackIterator* fLocaleIterator; char* fVersionID; }; diff --git a/icu4c/source/common/ures.cpp b/icu4c/source/common/ures.cpp index b3db31da37..b3b1d76dc4 100644 --- a/icu4c/source/common/ures.cpp +++ b/icu4c/source/common/ures.cpp @@ -18,6 +18,7 @@ * 04/01/97 aliu Creation. * 06/14/99 stephen Removed functions taking a filename suffix. * 07/20/99 stephen Changed for UResourceBundle typedef'd to void* +* 11/09/99 weiv Added ures_getLocale() ******************************************************************************* */ @@ -66,7 +67,7 @@ U_CAPI const UChar* ures_get( const UResourceBundle* resourceBundle, const char* resourceTag, UErrorCode* status) { - if (U_FAILURE(*status)) return NULL; + if (status==NULL || U_FAILURE(*status)) return NULL; if (!resourceBundle || !resourceTag) { *status = U_ILLEGAL_ARGUMENT_ERROR; @@ -84,7 +85,7 @@ U_CAPI const UChar* ures_getArrayItem(const UResourceBundle* resourceBundle, int32_t resourceIndex, UErrorCode* status) { - if (U_FAILURE(*status)) return NULL; + if (status==NULL || U_FAILURE(*status)) return NULL; if (!resourceBundle || !resourceTag || (resourceIndex < 0)) { *status = U_ILLEGAL_ARGUMENT_ERROR; @@ -105,7 +106,7 @@ U_CAPI const UChar* ures_get2dArrayItem(const UResourceBundle* resourceBundle, int32_t columnIndex, UErrorCode* status) { - if (U_FAILURE(*status)) return NULL; + if (status==NULL || U_FAILURE(*status)) return NULL; if (!resourceBundle || !resourceTag || (rowIndex < 0) || (columnIndex < 0)) { *status = U_ILLEGAL_ARGUMENT_ERROR; @@ -120,12 +121,23 @@ U_CAPI const UChar* ures_get2dArrayItem(const UResourceBundle* resourceBundle, else return NULL; } +U_CAPI const char* ures_getLocale(const UResourceBundle* resourceBundle, UErrorCode* status) +{ + if (status==NULL || U_FAILURE(*status)) return NULL; + if (!resourceBundle) + { + *status = U_ILLEGAL_ARGUMENT_ERROR; + return NULL; + } + return ((ResourceBundle*)resourceBundle)->getLocale().getName(); +} + U_CAPI const UChar* ures_getTaggedArrayItem(const UResourceBundle* resourceBundle, const char* resourceTag, const char* itemTag, UErrorCode* status) { - if (U_FAILURE(*status)) return NULL; + if (status==NULL || U_FAILURE(*status)) return NULL; if (!resourceBundle || !resourceTag || !itemTag) { *status = U_ILLEGAL_ARGUMENT_ERROR; diff --git a/icu4c/source/common/ures.h b/icu4c/source/common/ures.h index 7ace7e3e5f..a6522daed9 100644 --- a/icu4c/source/common/ures.h +++ b/icu4c/source/common/ures.h @@ -21,6 +21,7 @@ * 04/15/99 Madhu Updated Javadoc * 06/14/99 stephen Removed functions taking a filename suffix. * 07/20/99 stephen Language-independent ypedef to void* +* 11/09/99 weiv Added ures_getLocale() ******************************************************************************* */ @@ -324,5 +325,13 @@ U_CAPI void U_EXPORT2 ures_close(UResourceBundle* resourceBundle); * string. */ U_CAPI const char* U_EXPORT2 ures_getVersionNumber(const UResourceBundle* resourceBundle); + +/** + * Return the name of the Locale associated with this ResourceBundle. + * @param resourceBundle: resource bundle in question + * @param status: just for catching illegal arguments + * @return A Locale name + */ +U_CAPI const char* ures_getLocale(const UResourceBundle* resourceBundle, UErrorCode* status); #endif /*_URES*/ /*eof*/