2 *******************************************************************************
3 * Copyright (C) 2001-2005, 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"
40 class ICULocaleService
;
43 class LocaleKeyFactory
;
44 class SimpleLocaleKeyFactory
;
45 class ServiceListener
;
48 ******************************************************************
52 * A subclass of Key that implements a locale fallback mechanism.
53 * The first locale to search for is the locale provided by the
54 * client, and the fallback locale to search for is the current
55 * default locale. If a prefix is present, the currentDescriptor
56 * includes it before the locale proper, separated by "/". This
57 * is the default key instantiated by ICULocaleService.</p>
59 * <p>Canonicalization adjusts the locale string so that the
60 * section before the first understore is in lower case, and the rest
61 * is in upper case, with no trailing underscores.</p>
64 class U_COMMON_API LocaleKey
: public ICUServiceKey
{
67 UnicodeString _primaryID
;
68 UnicodeString _fallbackID
;
69 UnicodeString _currentID
;
77 * Create a LocaleKey with canonical primary and fallback IDs.
79 static LocaleKey
* createWithCanonicalFallback(const UnicodeString
* primaryID
,
80 const UnicodeString
* canonicalFallbackID
,
84 * Create a LocaleKey with canonical primary and fallback IDs.
86 static LocaleKey
* createWithCanonicalFallback(const UnicodeString
* primaryID
,
87 const UnicodeString
* canonicalFallbackID
,
93 * PrimaryID is the user's requested locale string,
94 * canonicalPrimaryID is this string in canonical form,
95 * fallbackID is the current default locale's string in
98 LocaleKey(const UnicodeString
& primaryID
,
99 const UnicodeString
& canonicalPrimaryID
,
100 const UnicodeString
* canonicalFallbackID
,
105 * Append the prefix associated with the kind, or nothing if the kind is KIND_ANY.
107 virtual UnicodeString
& prefix(UnicodeString
& result
) const;
110 * Return the kind code associated with this key.
112 virtual int32_t kind() const;
115 * Return the canonicalID.
117 virtual UnicodeString
& canonicalID(UnicodeString
& result
) const;
120 * Return the currentID.
122 virtual UnicodeString
& currentID(UnicodeString
& result
) const;
125 * Return the (canonical) current descriptor, or null if no current id.
127 virtual UnicodeString
& currentDescriptor(UnicodeString
& result
) const;
130 * Convenience method to return the locale corresponding to the (canonical) original ID.
132 virtual Locale
& canonicalLocale(Locale
& result
) const;
135 * Convenience method to return the locale corresponding to the (canonical) current ID.
137 virtual Locale
& currentLocale(Locale
& result
) const;
140 * If the key has a fallback, modify the key and return true,
141 * otherwise return false.</p>
143 * <p>First falls back through the primary ID, then through
144 * the fallbackID. The final fallback is the empty string,
145 * unless the primary id was the empty string, in which case
146 * there is no fallback.
148 virtual UBool
fallback();
151 * Return true if a key created from id matches, or would eventually
152 * fallback to match, the canonical ID of this key.
154 virtual UBool
isFallbackOf(const UnicodeString
& id
) const;
158 * UObject boilerplate.
160 static UClassID U_EXPORT2
getStaticClassID();
162 virtual UClassID
getDynamicClassID() const;
167 virtual ~LocaleKey();
171 virtual UnicodeString
& debug(UnicodeString
& result
) const;
172 virtual UnicodeString
& debugClass(UnicodeString
& result
) const;
178 ******************************************************************
182 * A subclass of ICUServiceFactory that uses LocaleKeys, and is able to
183 * 'cover' more specific locales with more general locales that it
186 * <p>Coverage may be either of the values VISIBLE or INVISIBLE.
188 * <p>'Visible' indicates that the specific locale(s) supported by
189 * the factory are registered in getSupportedIDs, 'Invisible'
190 * indicates that they are not.
192 * <p>Localization of visible ids is handled
193 * by the handling factory, regardless of kind.
195 class U_COMMON_API LocaleKeyFactory
: public ICUServiceFactory
{
197 const UnicodeString _name
;
198 const int32_t _coverage
;
203 * Coverage value indicating that the factory makes
204 * its locales visible, and does not cover more specific
210 * Coverage value indicating that the factory does not make
211 * its locales visible, and does not cover more specific
220 virtual ~LocaleKeyFactory();
224 * Constructor used by subclasses.
226 LocaleKeyFactory(int32_t coverage
);
229 * Constructor used by subclasses.
231 LocaleKeyFactory(int32_t coverage
, const UnicodeString
& name
);
234 * Implement superclass abstract method. This checks the currentID of
235 * the key against the supported IDs, and passes the canonicalLocale and
236 * kind off to handleCreate (which subclasses must implement).
239 virtual UObject
* create(const ICUServiceKey
& key
, const ICUService
* service
, UErrorCode
& status
) const;
242 virtual UBool
handlesKey(const ICUServiceKey
& key
, UErrorCode
& status
) const;
246 * Override of superclass method. This adjusts the result based
247 * on the coverage rule for this factory.
249 virtual void updateVisibleIDs(Hashtable
& result
, UErrorCode
& status
) const;
252 * Return a localized name for the locale represented by id.
254 virtual UnicodeString
& getDisplayName(const UnicodeString
& id
, const Locale
& locale
, UnicodeString
& result
) const;
258 * Utility method used by create(ICUServiceKey, ICUService). Subclasses can implement
259 * this instead of create. The default returns NULL.
261 virtual UObject
* handleCreate(const Locale
& loc
, int32_t kind
, const ICUService
* service
, UErrorCode
& status
) const;
264 * Return true if this id is one the factory supports (visible or
267 // virtual UBool isSupportedID(const UnicodeString& id, UErrorCode& status) const;
270 * Return the set of ids that this factory supports (visible or
271 * otherwise). This can be called often and might need to be
272 * cached if it is expensive to create.
274 virtual const Hashtable
* getSupportedIDs(UErrorCode
& status
) const;
278 * UObject boilerplate.
280 static UClassID U_EXPORT2
getStaticClassID();
282 virtual UClassID
getDynamicClassID() const;
286 virtual UnicodeString
& debug(UnicodeString
& result
) const;
287 virtual UnicodeString
& debugClass(UnicodeString
& result
) const;
293 ******************************************************************
297 * A LocaleKeyFactory that just returns a single object for a kind/locale.
300 class U_COMMON_API SimpleLocaleKeyFactory
: public LocaleKeyFactory
{
307 SimpleLocaleKeyFactory(UObject
* objToAdopt
,
308 const UnicodeString
& locale
,
312 SimpleLocaleKeyFactory(UObject
* objToAdopt
,
313 const Locale
& locale
,
320 virtual ~SimpleLocaleKeyFactory();
323 * Override of superclass method. Returns the service object if kind/locale match. Service is not used.
325 virtual UObject
* create(const ICUServiceKey
& key
, const ICUService
* service
, UErrorCode
& status
) const;
328 * Override of superclass method. This adjusts the result based
329 * on the coverage rule for this factory.
331 virtual void updateVisibleIDs(Hashtable
& result
, UErrorCode
& status
) const;
335 * Return true if this id is equal to the locale name.
337 //virtual UBool isSupportedID(const UnicodeString& id, UErrorCode& status) const;
342 * UObject boilerplate.
344 static UClassID U_EXPORT2
getStaticClassID();
346 virtual UClassID
getDynamicClassID() const;
350 virtual UnicodeString
& debug(UnicodeString
& result
) const;
351 virtual UnicodeString
& debugClass(UnicodeString
& result
) const;
357 ******************************************************************
361 * A LocaleKeyFactory that creates a service based on the ICU locale data.
362 * This is a base class for most ICU factories. Subclasses instantiate it
363 * with a constructor that takes a bundle name, which determines the supported
364 * IDs. Subclasses then override handleCreate to create the actual service
365 * object. The default implementation returns a resource bundle.
367 class U_COMMON_API ICUResourceBundleFactory
: public LocaleKeyFactory
370 UnicodeString _bundleName
;
374 * Convenience constructor that uses the main ICU bundle name.
376 ICUResourceBundleFactory();
379 * A service factory based on ICU resource data in resources with
380 * the given name. This should be a 'path' that can be passed to
381 * ures_openAvailableLocales, such as U_ICUDATA or U_ICUDATA_COLL.
382 * The empty string is equivalent to U_ICUDATA.
384 ICUResourceBundleFactory(const UnicodeString
& bundleName
);
389 virtual ~ICUResourceBundleFactory();
393 * Return the supported IDs. This is the set of all locale names in ICULocaleData.
395 virtual const Hashtable
* getSupportedIDs(UErrorCode
& status
) const;
398 * Create the service. The default implementation returns the resource bundle
399 * for the locale, ignoring kind, and service.
401 virtual UObject
* handleCreate(const Locale
& loc
, int32_t kind
, const ICUService
* service
, UErrorCode
& status
) const;
405 * UObject boilerplate.
407 static UClassID U_EXPORT2
getStaticClassID();
408 virtual UClassID
getDynamicClassID() const;
413 virtual UnicodeString
& debug(UnicodeString
& result
) const;
414 virtual UnicodeString
& debugClass(UnicodeString
& result
) const;
420 ******************************************************************
423 class U_COMMON_API ICULocaleService
: public ICUService
426 Locale fallbackLocale
;
427 UnicodeString fallbackLocaleName
;
432 * Construct an ICULocaleService.
437 * Construct an ICULocaleService with a name (useful for debugging).
439 ICULocaleService(const UnicodeString
& name
);
444 virtual ~ICULocaleService();
447 // redeclare because of overload resolution rules?
448 // no, causes ambiguities since both UnicodeString and Locale have constructors that take a const char*
449 // need some compiler flag to remove warnings
450 UObject
* get(const UnicodeString
& descriptor
, UErrorCode
& status
) const {
451 return ICUService::get(descriptor
, status
);
454 UObject
* get(const UnicodeString
& descriptor
, UnicodeString
* actualReturn
, UErrorCode
& status
) const {
455 return ICUService::get(descriptor
, actualReturn
, status
);
460 * Convenience override for callers using locales. This calls
461 * get(Locale, int, Locale[]) with KIND_ANY for kind and null for
464 UObject
* get(const Locale
& locale
, UErrorCode
& status
) const;
467 * Convenience override for callers using locales. This calls
468 * get(Locale, int, Locale[]) with a null actualReturn.
470 UObject
* get(const Locale
& locale
, int32_t kind
, UErrorCode
& status
) const;
473 * Convenience override for callers using locales. This calls
474 * get(Locale, String, Locale[]) with a null kind.
476 UObject
* get(const Locale
& locale
, Locale
* actualReturn
, UErrorCode
& status
) const;
479 * Convenience override for callers using locales. This uses
480 * createKey(Locale.toString(), kind) to create a key, calls getKey, and then
481 * if actualReturn is not null, returns the actualResult from
482 * getKey (stripping any prefix) into a Locale.
484 UObject
* get(const Locale
& locale
, int32_t kind
, Locale
* actualReturn
, UErrorCode
& status
) const;
487 * Convenience override for callers using locales. This calls
488 * registerObject(Object, Locale, int32_t kind, int coverage)
489 * passing KIND_ANY for the kind, and VISIBLE for the coverage.
491 virtual URegistryKey
registerInstance(UObject
* objToAdopt
, const Locale
& locale
, UErrorCode
& status
);
494 * Convenience function for callers using locales. This calls
495 * registerObject(Object, Locale, int kind, int coverage)
496 * passing VISIBLE for the coverage.
498 virtual URegistryKey
registerInstance(UObject
* objToAdopt
, const Locale
& locale
, int32_t kind
, UErrorCode
& status
);
501 * Convenience function for callers using locales. This instantiates
502 * a SimpleLocaleKeyFactory, and registers the factory.
504 virtual URegistryKey
registerInstance(UObject
* objToAdopt
, const Locale
& locale
, int32_t kind
, int32_t coverage
, UErrorCode
& status
);
508 * (Stop compiler from complaining about hidden overrides.)
509 * Since both UnicodeString and Locale have constructors that take const char*, adding a public
510 * method that takes UnicodeString causes ambiguity at call sites that use const char*.
511 * We really need a flag that is understood by all compilers that will suppress the warning about
514 virtual URegistryKey
registerInstance(UObject
* objToAdopt
, const UnicodeString
& locale
, UBool visible
, UErrorCode
& status
);
517 * Convenience method for callers using locales. This returns the standard
518 * service ID enumeration.
520 virtual StringEnumeration
* getAvailableLocales(void) const;
525 * Return the name of the current fallback locale. If it has changed since this was
526 * last accessed, the service cache is cleared.
528 const UnicodeString
& validateFallbackLocale() const;
531 * Override superclass createKey method.
533 virtual ICUServiceKey
* createKey(const UnicodeString
* id
, UErrorCode
& status
) const;
536 * Additional createKey that takes a kind.
538 virtual ICUServiceKey
* createKey(const UnicodeString
* id
, int32_t kind
, UErrorCode
& status
) const;
540 friend class ServiceEnumeration
;
545 /* UCONFIG_NO_SERVICE */