2
******************************************************************************
4
* Copyright (C) 1996-2007, International Business Machines Corporation
5
* and others. All Rights Reserved.
7
******************************************************************************
14
* Modification History:
16
* Date Name Description
17
* 2/5/97 aliu Added scanForLocaleInFile. Added
18
* constructor which attempts to read resource bundle
19
* from a specific file, without searching other files.
20
* 2/11/97 aliu Added UErrorCode return values to constructors. Fixed
21
* infinite loops in scanForFile and scanForLocale.
22
* Modified getRawResourceData to not delete storage
23
* in localeData and resourceData which it doesn't own.
24
* Added Mac compatibility #ifdefs for tellp() and
26
* 2/18/97 helena Updated with 100% documentation coverage.
27
* 3/13/97 aliu Rewrote to load in entire resource bundle and store
28
* it as a Hashtable of ResourceBundleData objects.
29
* Added state table to govern parsing of files.
30
* Modified to load locale index out of new file
31
* distinct from default.txt.
32
* 3/25/97 aliu Modified to support 2-d arrays, needed for timezone
33
* data. Added support for custom file suffixes. Again,
34
* needed to support timezone data.
35
* 4/7/97 aliu Cleaned up.
36
* 03/02/99 stephen Removed dependency on FILE*.
37
* 03/29/99 helena Merged Bertrand and Stephen's changes.
38
* 06/11/99 stephen Removed parsing of .txt files.
39
* Reworked to use new binary format.
41
* 06/14/99 stephen Removed methods taking a filename suffix.
42
* 11/09/99 weiv Added getLocale(), fRealLocale, removed fRealLocaleID
43
******************************************************************************
49
#include "unicode/utypes.h"
50
#include "unicode/uobject.h"
51
#include "unicode/ures.h"
52
#include "unicode/unistr.h"
53
#include "unicode/locid.h"
57
* \brief C++ API: Resource Bundle
63
* A class representing a collection of resource information pertaining to a given
64
* locale. A resource bundle provides a way of accessing locale- specfic information in
65
* a data file. You create a resource bundle that manages the resources for a given
66
* locale and then ask it for individual resources.
68
* Resource bundles in ICU4C are currently defined using text files which conform to the following
69
* <a href="http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt">BNF definition</a>.
70
* More on resource bundle concepts and syntax can be found in the
71
* <a href="http://icu-project.org/userguide/ResourceManagement.html">Users Guide</a>.
74
* The ResourceBundle class is not suitable for subclassing.
78
class U_COMMON_API ResourceBundle : public UObject {
83
* @param packageName The packageName and locale together point to an ICU udata object,
84
* as defined by <code> udata_open( packageName, "res", locale, err) </code>
85
* or equivalent. Typically, packageName will refer to a (.dat) file, or to
86
* a package registered with udata_setAppData(). Using a full file or directory
87
* pathname for packageName is deprecated.
88
* @param locale This is the locale this resource bundle is for. To get resources
89
* for the French locale, for example, you would create a
90
* ResourceBundle passing Locale::FRENCH for the "locale" parameter,
91
* and all subsequent calls to that resource bundle will return
92
* resources that pertain to the French locale. If the caller doesn't
93
* pass a locale parameter, the default locale for the system (as
94
* returned by Locale::getDefault()) will be used.
95
* @param err The Error Code.
96
* The UErrorCode& err parameter is used to return status information to the user. To
97
* check whether the construction succeeded or not, you should check the value of
98
* U_SUCCESS(err). If you wish more detailed information, you can check for
99
* informational error results which still indicate success. U_USING_FALLBACK_WARNING
100
* indicates that a fall back locale was used. For example, 'de_CH' was requested,
101
* but nothing was found there, so 'de' was used. U_USING_DEFAULT_WARNING indicates that
102
* the default locale data was used; neither the requested locale nor any of its
103
* fall back locales could be found.
106
ResourceBundle(const UnicodeString& packageName,
107
const Locale& locale,
111
* Construct a resource bundle for the default bundle in the specified package.
113
* @param packageName The packageName and locale together point to an ICU udata object,
114
* as defined by <code> udata_open( packageName, "res", locale, err) </code>
115
* or equivalent. Typically, packageName will refer to a (.dat) file, or to
116
* a package registered with udata_setAppData(). Using a full file or directory
117
* pathname for packageName is deprecated.
118
* @param err A UErrorCode value
121
ResourceBundle(const UnicodeString& packageName,
125
* Construct a resource bundle for the ICU default bundle.
127
* @param err A UErrorCode value
130
ResourceBundle(UErrorCode &err);
133
* Standard constructor, onstructs a resource bundle for the locale-specific
134
* bundle in the specified package.
136
* @param packageName The packageName and locale together point to an ICU udata object,
137
* as defined by <code> udata_open( packageName, "res", locale, err) </code>
138
* or equivalent. Typically, packageName will refer to a (.dat) file, or to
139
* a package registered with udata_setAppData(). Using a full file or directory
140
* pathname for packageName is deprecated.
141
* NULL is used to refer to ICU data.
142
* @param locale The locale for which to open a resource bundle.
143
* @param err A UErrorCode value
146
ResourceBundle(const char* packageName,
147
const Locale& locale,
153
* @param original The resource bundle to copy.
156
ResourceBundle(const ResourceBundle &original);
159
* Constructor from a C UResourceBundle. The resource bundle is
160
* copied and not adopted. ures_close will still need to be used on the
161
* original resource bundle.
163
* @param res A pointer to the C resource bundle.
164
* @param status A UErrorCode value.
167
ResourceBundle(UResourceBundle *res,
171
* Assignment operator.
173
* @param other The resource bundle to copy.
177
operator=(const ResourceBundle& other);
182
virtual ~ResourceBundle();
186
* Clones can be used concurrently in multiple threads.
187
* If an error occurs, then NULL is returned.
188
* The caller must delete the clone.
190
* @return a clone of this object
192
* @see getDynamicClassID
195
ResourceBundle *clone() const;
198
* Returns the size of a resource. Size for scalar types is always 1, and for vector/table types is
199
* the number of child resources.
200
* @warning Integer array is treated as a scalar type. There are no
201
* APIs to access individual members of an integer array. It
202
* is always returned as a whole.
204
* @return number of resources in a given resource.
211
* returns a string from a string resource type
213
* @param status fills in the outgoing error code
214
* could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
216
* e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
217
* @return a pointer to a zero-terminated UChar array which lives in a memory mapped/DLL file.
221
getString(UErrorCode& status) const;
224
* returns a binary data from a resource. Can be used at most primitive resource types (binaries,
227
* @param len fills in the length of resulting byte chunk
228
* @param status fills in the outgoing error code
229
* could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
231
* e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
232
* @return a pointer to a chunk of unsigned bytes which live in a memory mapped/DLL file.
236
getBinary(int32_t& len, UErrorCode& status) const;
240
* returns an integer vector from a resource.
242
* @param len fills in the length of resulting integer vector
243
* @param status fills in the outgoing error code
244
* could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
246
* e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
247
* @return a pointer to a vector of integers that lives in a memory mapped/DLL file.
251
getIntVector(int32_t& len, UErrorCode& status) const;
254
* returns an unsigned integer from a resource.
255
* This integer is originally 28 bits.
257
* @param status fills in the outgoing error code
258
* could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
260
* e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
261
* @return an unsigned integer value
265
getUInt(UErrorCode& status) const;
268
* returns a signed integer from a resource.
269
* This integer is originally 28 bit and the sign gets propagated.
271
* @param status fills in the outgoing error code
272
* could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
274
* e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
275
* @return a signed integer value
279
getInt(UErrorCode& status) const;
282
* Checks whether the resource has another element to iterate over.
284
* @return TRUE if there are more elements, FALSE if there is no more elements
291
* Resets the internal context of a resource so that iteration starts from the first element.
299
* Returns the key associated with this resource. Not all the resources have a key - only
300
* those that are members of a table.
302
* @return a key associated to this resource, or NULL if it doesn't have a key
309
* Gets the locale ID of the resource bundle as a string.
310
* Same as getLocale().getName() .
312
* @return the locale ID of the resource bundle as a string
320
* Returns the type of a resource. Available types are defined in enum UResType
322
* @return type of the given resource.
329
* Returns the next resource in a given resource or NULL if there are no more resources
331
* @param status fills in the outgoing error code
332
* @return ResourceBundle object.
336
getNext(UErrorCode& status);
339
* Returns the next string in a resource or NULL if there are no more resources
342
* @param status fills in the outgoing error code
343
* @return an UnicodeString object.
347
getNextString(UErrorCode& status);
350
* Returns the next string in a resource or NULL if there are no more resources
353
* @param key fill in for key associated with this string
354
* @param status fills in the outgoing error code
355
* @return an UnicodeString object.
359
getNextString(const char ** key,
363
* Returns the resource in a resource at the specified index.
365
* @param index an index to the wanted resource.
366
* @param status fills in the outgoing error code
367
* @return ResourceBundle object. If there is an error, resource is invalid.
372
UErrorCode& status) const;
375
* Returns the string in a given resource at the specified index.
377
* @param index an index to the wanted string.
378
* @param status fills in the outgoing error code
379
* @return an UnicodeString object. If there is an error, string is bogus
383
getStringEx(int32_t index,
384
UErrorCode& status) const;
387
* Returns a resource in a resource that has a given key. This procedure works only with table
390
* @param key a key associated with the wanted resource
391
* @param status fills in the outgoing error code.
392
* @return ResourceBundle object. If there is an error, resource is invalid.
397
UErrorCode& status) const;
400
* Returns a string in a resource that has a given key. This procedure works only with table
403
* @param key a key associated with the wanted string
404
* @param status fills in the outgoing error code
405
* @return an UnicodeString object. If there is an error, string is bogus
409
getStringEx(const char* key,
410
UErrorCode& status) const;
413
* Return the version number associated with this ResourceBundle as a string. Please
414
* use getVersion, as this method is going to be deprecated.
416
* @return A version number string as specified in the resource bundle or its parent.
417
* The caller does not own this string.
419
* @deprecated ICU 2.8 Use getVersion instead.
422
getVersionNumber(void) const;
425
* Return the version number associated with this ResourceBundle as a UVersionInfo array.
427
* @param versionInfo A UVersionInfo array that is filled with the version number
428
* as specified in the resource bundle or its parent.
432
getVersion(UVersionInfo versionInfo) const;
435
* Return the Locale associated with this ResourceBundle.
437
* @return a Locale object
438
* @deprecated ICU 2.8 Use getLocale(ULocDataLocaleType type, UErrorCode &status) overload instead.
441
getLocale(void) const;
444
* Return the Locale associated with this ResourceBundle.
445
* @param type You can choose between requested, valid and actual
446
* locale. For description see the definition of
447
* ULocDataLocaleType in uloc.h
448
* @param status just for catching illegal arguments
450
* @return a Locale object
454
getLocale(ULocDataLocaleType type, UErrorCode &status) const;
456
* This API implements multilevel fallback
460
getWithFallback(const char* key, UErrorCode& status);
462
* ICU "poor man's RTTI", returns a UClassID for the actual class.
466
virtual UClassID getDynamicClassID() const;
469
* ICU "poor man's RTTI", returns a UClassID for this class.
473
static UClassID U_EXPORT2 getStaticClassID();
476
ResourceBundle(); // default constructor not implemented
478
UResourceBundle *fResource;
479
void constructForLocale(const UnicodeString& path, const Locale& locale, UErrorCode& error);