From b1f70f3cd2f76fb567e2b52bc2a2cabfc816cce3 Mon Sep 17 00:00:00 2001 From: Alan Liu Date: Fri, 23 Jan 2004 18:06:22 +0000 Subject: [PATCH] ICU-2202 add Ugly Hack to make collator getLocale() work properly with service registration and unregistration -- to be removed in 3.0 X-SVN-Rev: 14391 --- icu4c/source/i18n/coll.cpp | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/icu4c/source/i18n/coll.cpp b/icu4c/source/i18n/coll.cpp index f7b86cf7cd..fea283aec1 100644 --- a/icu4c/source/i18n/coll.cpp +++ b/icu4c/source/i18n/coll.cpp @@ -111,7 +111,10 @@ public: virtual UObject* handleDefault(const ICUServiceKey& key, UnicodeString* actualID, UErrorCode& status) const { LocaleKey& lkey = (LocaleKey&)key; if (actualID) { - lkey.canonicalID(*actualID); + // Ugly Hack Alert! We return an empty actualID to signal + // to callers that this is a default object, not a "real" + // service-created object. (TODO remove in 3.0) [aliu] + actualID->truncate(0); } Locale loc(""); lkey.canonicalLocale(loc); @@ -124,7 +127,13 @@ public: actualReturn = &ar; } Collator* result = (Collator*)ICULocaleService::getKey(key, actualReturn, status); - if (result) { + // Ugly Hack Alert! If the actualReturn length is zero, this + // means we got a default object, not a "real" service-created + // object. We don't call setLocales() on a default object, + // because that will overwrite its correct built-in locale + // metadata (valid & actual) with our incorrect data (all we + // have is the requested locale). (TODO remove in 3.0) [aliu] + if (result && actualReturn->length() > 0) { const LocaleKey& lkey = (const LocaleKey&)key; Locale canonicalLocale(""); Locale currentLocale(""); @@ -221,7 +230,19 @@ Collator* Collator::createInstance(const Locale& desiredLocale, return 0; if (hasService()) { - return (Collator*)gService->get(desiredLocale, status); + Locale actualLoc; + Collator *result = + (Collator*)gService->get(desiredLocale, &actualLoc, status); + // Ugly Hack Alert! If the returned locale is empty (not root, + // but empty -- getName() == "") then that means the service + // returned a default object, not a "real" service object. In + // that case, the locale metadata (valid & actual) is setup + // correctly already, and we don't want to overwrite it. (TODO + // remove in 3.0) [aliu] + if (*actualLoc.getName() != 0) { + result->setLocales(desiredLocale, actualLoc); + } + return result; } return makeInstance(desiredLocale, status); }