2 *******************************************************************************
3 * Copyright (C) 2001-2004, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 *******************************************************************************
12 #include "unicode/utypes.h"
14 #if UCONFIG_NO_SERVICE
19 * Allow the declaration of APIs with pointers to ICUService
20 * even when service is removed from the build.
22 class ICULocaleService
;
28 #include "unicode/unistr.h"
29 #include "unicode/locid.h"
30 #include "unicode/strenum.h"
39 class ICULocaleService
;
42 class LocaleKeyFactory
;
43 class SimpleLocaleKeyFactory
;
44 class ServiceListener
;
47 ******************************************************************
51 * A subclass of Key that implements a locale fallback mechanism.
52 * The first locale to search for is the locale provided by the
53 * client, and the fallback locale to search for is the current
54 * default locale. If a prefix is present, the currentDescriptor
55 * includes it before the locale proper, separated by "/". This
56 * is the default key instantiated by ICULocaleService.</p>
58 * <p>Canonicalization adjusts the locale string so that the
59 * section before the first understore is in lower case, and the rest
60 * is in upper case, with no trailing underscores.</p>
63 class U_COMMON_API LocaleKey
: public ICUServiceKey
{
66 UnicodeString _primaryID
;
67 UnicodeString _fallbackID
;
68 UnicodeString _currentID
;
71 static const int32_t KIND_ANY
; // = -1;
74 * Create a LocaleKey with canonical primary and fallback IDs.
76 static LocaleKey
* createWithCanonicalFallback(const UnicodeString
* primaryID
,
77 const UnicodeString
* canonicalFallbackID
,
81 * Create a LocaleKey with canonical primary and fallback IDs.
83 static LocaleKey
* createWithCanonicalFallback(const UnicodeString
* primaryID
,
84 const UnicodeString
* canonicalFallbackID
,
90 * PrimaryID is the user's requested locale string,
91 * canonicalPrimaryID is this string in canonical form,
92 * fallbackID is the current default locale's string in
95 LocaleKey(const UnicodeString
& primaryID
,
96 const UnicodeString
& canonicalPrimaryID
,
97 const UnicodeString
* canonicalFallbackID
,
102 * Append the prefix associated with the kind, or nothing if the kind is KIND_ANY.
104 virtual UnicodeString
& prefix(UnicodeString
& result
) const;
107 * Return the kind code associated with this key.
109 virtual int32_t kind() const;
112 * Return the canonicalID.
114 virtual UnicodeString
& canonicalID(UnicodeString
& result
) const;
117 * Return the currentID.
119 virtual UnicodeString
& currentID(UnicodeString
& result
) const;
122 * Return the (canonical) current descriptor, or null if no current id.
124 virtual UnicodeString
& currentDescriptor(UnicodeString
& result
) const;
127 * Convenience method to return the locale corresponding to the (canonical) original ID.
129 virtual Locale
& canonicalLocale(Locale
& result
) const;
132 * Convenience method to return the locale corresponding to the (canonical) current ID.
134 virtual Locale
& currentLocale(Locale
& result
) const;
137 * If the key has a fallback, modify the key and return true,
138 * otherwise return false.</p>
140 * <p>First falls back through the primary ID, then through
141 * the fallbackID. The final fallback is the empty string,
142 * unless the primary id was the empty string, in which case
143 * there is no fallback.
145 virtual UBool
fallback();
148 * Return true if a key created from id matches, or would eventually
149 * fallback to match, the canonical ID of this key.
151 virtual UBool
isFallbackOf(const UnicodeString
& id
) const;
155 * UObject boilerplate.
157 static UClassID U_EXPORT2
getStaticClassID();
159 virtual UClassID
getDynamicClassID() const;
164 virtual ~LocaleKey();
168 virtual UnicodeString
& debug(UnicodeString
& result
) const;
169 virtual UnicodeString
& debugClass(UnicodeString
& result
) const;
175 ******************************************************************
179 * A subclass of ICUServiceFactory that uses LocaleKeys, and is able to
180 * 'cover' more specific locales with more general locales that it
183 * <p>Coverage may be either of the values VISIBLE or INVISIBLE.
185 * <p>'Visible' indicates that the specific locale(s) supported by
186 * the factory are registered in getSupportedIDs, 'Invisible'
187 * indicates that they are not.
189 * <p>Localization of visible ids is handled
190 * by the handling factory, regardless of kind.
192 class U_COMMON_API LocaleKeyFactory
: public ICUServiceFactory
{
194 const UnicodeString _name
;
195 const int32_t _coverage
;
200 * Coverage value indicating that the factory makes
201 * its locales visible, and does not cover more specific
207 * Coverage value indicating that the factory does not make
208 * its locales visible, and does not cover more specific
217 virtual ~LocaleKeyFactory();
221 * Constructor used by subclasses.
223 LocaleKeyFactory(int32_t coverage
);
226 * Constructor used by subclasses.
228 LocaleKeyFactory(int32_t coverage
, const UnicodeString
& name
);
231 * Implement superclass abstract method. This checks the currentID of
232 * the key against the supported IDs, and passes the canonicalLocale and
233 * kind off to handleCreate (which subclasses must implement).
236 virtual UObject
* create(const ICUServiceKey
& key
, const ICUService
* service
, UErrorCode
& status
) const;
239 virtual UBool
handlesKey(const ICUServiceKey
& key
, UErrorCode
& status
) const;
243 * Override of superclass method. This adjusts the result based
244 * on the coverage rule for this factory.
246 void updateVisibleIDs(Hashtable
& result
, UErrorCode
& status
) const;
249 * Return a localized name for the locale represented by id.
251 UnicodeString
& getDisplayName(const UnicodeString
& id
, const Locale
& locale
, UnicodeString
& result
) const;
255 * Utility method used by create(ICUServiceKey, ICUService). Subclasses can implement
256 * this instead of create. The default returns NULL.
258 virtual UObject
* handleCreate(const Locale
& loc
, int32_t kind
, const ICUService
* service
, UErrorCode
& status
) const;
261 * Return true if this id is one the factory supports (visible or
264 virtual UBool
isSupportedID(const UnicodeString
& id
, UErrorCode
& status
) const;
267 * Return the set of ids that this factory supports (visible or
268 * otherwise). This can be called often and might need to be
269 * cached if it is expensive to create.
271 virtual const Hashtable
* getSupportedIDs(UErrorCode
& status
) const;
275 * UObject boilerplate.
277 static UClassID U_EXPORT2
getStaticClassID();
279 virtual UClassID
getDynamicClassID() const;
283 virtual UnicodeString
& debug(UnicodeString
& result
) const;
284 virtual UnicodeString
& debugClass(UnicodeString
& result
) const;
290 ******************************************************************
294 * A LocaleKeyFactory that just returns a single object for a kind/locale.
297 class U_COMMON_API SimpleLocaleKeyFactory
: public LocaleKeyFactory
{
304 SimpleLocaleKeyFactory(UObject
* objToAdopt
,
305 const UnicodeString
& locale
,
309 SimpleLocaleKeyFactory(UObject
* objToAdopt
,
310 const Locale
& locale
,
317 virtual ~SimpleLocaleKeyFactory();
320 * Override of superclass method. Returns the service object if kind/locale match. Service is not used.
322 UObject
* create(const ICUServiceKey
& key
, const ICUService
* service
, UErrorCode
& status
) const;
325 * Override of superclass method. This adjusts the result based
326 * on the coverage rule for this factory.
328 void updateVisibleIDs(Hashtable
& result
, UErrorCode
& status
) const;
332 * Return true if this id is equal to the locale name.
334 virtual UBool
isSupportedID(const UnicodeString
& id
, UErrorCode
& status
) const;
339 * UObject boilerplate.
341 static UClassID U_EXPORT2
getStaticClassID();
343 virtual UClassID
getDynamicClassID() const;
347 virtual UnicodeString
& debug(UnicodeString
& result
) const;
348 virtual UnicodeString
& debugClass(UnicodeString
& result
) const;
354 ******************************************************************
358 * A LocaleKeyFactory that creates a service based on the ICU locale data.
359 * This is a base class for most ICU factories. Subclasses instantiate it
360 * with a constructor that takes a bundle name, which determines the supported
361 * IDs. Subclasses then override handleCreate to create the actual service
362 * object. The default implementation returns a resource bundle.
364 class U_COMMON_API ICUResourceBundleFactory
: public LocaleKeyFactory
367 UnicodeString _bundleName
;
371 * Convenience constructor that uses the main ICU bundle name.
373 ICUResourceBundleFactory();
376 * A service factory based on ICU resource data in resources with
377 * the given name. This should be a 'path' that can be passed to
378 * ures_openAvailableLocales, such as U_ICUDATA or U_ICUDATA_COLL.
379 * The empty string is equivalent to U_ICUDATA.
381 ICUResourceBundleFactory(const UnicodeString
& bundleName
);
386 virtual ~ICUResourceBundleFactory();
390 * Return the supported IDs. This is the set of all locale names in ICULocaleData.
392 virtual const Hashtable
* getSupportedIDs(UErrorCode
& status
) const;
395 * Create the service. The default implementation returns the resource bundle
396 * for the locale, ignoring kind, and service.
398 virtual UObject
* handleCreate(const Locale
& loc
, int32_t kind
, const ICUService
* service
, UErrorCode
& status
) const;
402 * UObject boilerplate.
404 static UClassID U_EXPORT2
getStaticClassID();
405 virtual UClassID
getDynamicClassID() const;
410 virtual UnicodeString
& debug(UnicodeString
& result
) const;
411 virtual UnicodeString
& debugClass(UnicodeString
& result
) const;
417 ******************************************************************
420 class U_COMMON_API ICULocaleService
: public ICUService
423 Locale fallbackLocale
;
424 UnicodeString fallbackLocaleName
;
429 * Construct an ICULocaleService.
434 * Construct an ICULocaleService with a name (useful for debugging).
436 ICULocaleService(const UnicodeString
& name
);
441 virtual ~ICULocaleService();
444 // redeclare because of overload resolution rules?
445 // no, causes ambiguities since both UnicodeString and Locale have constructors that take a const char*
446 // need some compiler flag to remove warnings
447 UObject
* get(const UnicodeString
& descriptor
, UErrorCode
& status
) const {
448 return ICUService::get(descriptor
, status
);
451 UObject
* get(const UnicodeString
& descriptor
, UnicodeString
* actualReturn
, UErrorCode
& status
) const {
452 return ICUService::get(descriptor
, actualReturn
, status
);
457 * Convenience override for callers using locales. This calls
458 * get(Locale, int, Locale[]) with KIND_ANY for kind and null for
461 UObject
* get(const Locale
& locale
, UErrorCode
& status
) const;
464 * Convenience override for callers using locales. This calls
465 * get(Locale, int, Locale[]) with a null actualReturn.
467 UObject
* get(const Locale
& locale
, int32_t kind
, UErrorCode
& status
) const;
470 * Convenience override for callers using locales. This calls
471 * get(Locale, String, Locale[]) with a null kind.
473 UObject
* get(const Locale
& locale
, Locale
* actualReturn
, UErrorCode
& status
) const;
476 * Convenience override for callers using locales. This uses
477 * createKey(Locale.toString(), kind) to create a key, calls getKey, and then
478 * if actualReturn is not null, returns the actualResult from
479 * getKey (stripping any prefix) into a Locale.
481 UObject
* get(const Locale
& locale
, int32_t kind
, Locale
* actualReturn
, UErrorCode
& status
) const;
484 * Convenience override for callers using locales. This calls
485 * registerObject(Object, Locale, int32_t kind, int coverage)
486 * passing KIND_ANY for the kind, and VISIBLE for the coverage.
488 virtual URegistryKey
registerInstance(UObject
* objToAdopt
, const Locale
& locale
, UErrorCode
& status
);
491 * Convenience function for callers using locales. This calls
492 * registerObject(Object, Locale, int kind, int coverage)
493 * passing VISIBLE for the coverage.
495 virtual URegistryKey
registerInstance(UObject
* objToAdopt
, const Locale
& locale
, int32_t kind
, UErrorCode
& status
);
498 * Convenience function for callers using locales. This instantiates
499 * a SimpleLocaleKeyFactory, and registers the factory.
501 virtual URegistryKey
registerInstance(UObject
* objToAdopt
, const Locale
& locale
, int32_t kind
, int32_t coverage
, UErrorCode
& status
);
505 * (Stop compiler from complaining about hidden overrides.)
506 * Since both UnicodeString and Locale have constructors that take const char*, adding a public
507 * method that takes UnicodeString causes ambiguity at call sites that use const char*.
508 * We really need a flag that is understood by all compilers that will suppress the warning about
511 virtual URegistryKey
registerInstance(UObject
* objToAdopt
, const UnicodeString
& locale
, UBool visible
, UErrorCode
& status
);
514 * Convenience method for callers using locales. This returns the standard
515 * service ID enumeration.
517 virtual StringEnumeration
* getAvailableLocales(void) const;
522 * Return the name of the current fallback locale. If it has changed since this was
523 * last accessed, the service cache is cleared.
525 const UnicodeString
& validateFallbackLocale() const;
528 * Override superclass createKey method.
530 virtual ICUServiceKey
* createKey(const UnicodeString
* id
, UErrorCode
& status
) const;
533 * Additional createKey that takes a kind.
535 virtual ICUServiceKey
* createKey(const UnicodeString
* id
, int32_t kind
, UErrorCode
& status
) const;
537 friend class ServiceEnumeration
;
540 // temporary utility functions, till I know where to find them
541 // in header so tests can also access them
543 class U_COMMON_API LocaleUtility
{
545 static UnicodeString
& canonicalLocaleString(const UnicodeString
* id
, UnicodeString
& result
);
546 static Locale
& initLocaleFromName(const UnicodeString
& id
, Locale
& result
);
547 static UnicodeString
& initNameFromLocale(const Locale
& locale
, UnicodeString
& result
);
548 static const Hashtable
* getAvailableLocaleNames(const UnicodeString
& bundleID
);
549 static UBool
isFallbackOf(const UnicodeString
& root
, const UnicodeString
& child
);
554 /* UCONFIG_NO_SERVICE */