resbund.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ******************************************************************************
  5. *
  6. * Copyright (C) 1996-2013, International Business Machines Corporation
  7. * and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. *
  11. * File resbund.h
  12. *
  13. * CREATED BY
  14. * Richard Gillam
  15. *
  16. * Modification History:
  17. *
  18. * Date Name Description
  19. * 2/5/97 aliu Added scanForLocaleInFile. Added
  20. * constructor which attempts to read resource bundle
  21. * from a specific file, without searching other files.
  22. * 2/11/97 aliu Added UErrorCode return values to constructors. Fixed
  23. * infinite loops in scanForFile and scanForLocale.
  24. * Modified getRawResourceData to not delete storage
  25. * in localeData and resourceData which it doesn't own.
  26. * Added Mac compatibility #ifdefs for tellp() and
  27. * ios::nocreate.
  28. * 2/18/97 helena Updated with 100% documentation coverage.
  29. * 3/13/97 aliu Rewrote to load in entire resource bundle and store
  30. * it as a Hashtable of ResourceBundleData objects.
  31. * Added state table to govern parsing of files.
  32. * Modified to load locale index out of new file
  33. * distinct from default.txt.
  34. * 3/25/97 aliu Modified to support 2-d arrays, needed for timezone
  35. * data. Added support for custom file suffixes. Again,
  36. * needed to support timezone data.
  37. * 4/7/97 aliu Cleaned up.
  38. * 03/02/99 stephen Removed dependency on FILE*.
  39. * 03/29/99 helena Merged Bertrand and Stephen's changes.
  40. * 06/11/99 stephen Removed parsing of .txt files.
  41. * Reworked to use new binary format.
  42. * Cleaned up.
  43. * 06/14/99 stephen Removed methods taking a filename suffix.
  44. * 11/09/99 weiv Added getLocale(), fRealLocale, removed fRealLocaleID
  45. ******************************************************************************
  46. */
  47. #ifndef RESBUND_H
  48. #define RESBUND_H
  49. #include "unicode/utypes.h"
  50. #if U_SHOW_CPLUSPLUS_API
  51. #include "unicode/uobject.h"
  52. #include "unicode/ures.h"
  53. #include "unicode/unistr.h"
  54. #include "unicode/locid.h"
  55. /**
  56. * \file
  57. * \brief C++ API: Resource Bundle
  58. */
  59. U_NAMESPACE_BEGIN
  60. /**
  61. * A class representing a collection of resource information pertaining to a given
  62. * locale. A resource bundle provides a way of accessing locale- specific information in
  63. * a data file. You create a resource bundle that manages the resources for a given
  64. * locale and then ask it for individual resources.
  65. * <P>
  66. * Resource bundles in ICU4C are currently defined using text files which conform to the following
  67. * <a href="https://github.com/unicode-org/icu-docs/blob/main/design/bnf_rb.txt">BNF definition</a>.
  68. * More on resource bundle concepts and syntax can be found in the
  69. * <a href="https://unicode-org.github.io/icu/userguide/locale/resources">Users Guide</a>.
  70. * <P>
  71. *
  72. * The ResourceBundle class is not suitable for subclassing.
  73. *
  74. * @stable ICU 2.0
  75. */
  76. class U_COMMON_API ResourceBundle : public UObject {
  77. public:
  78. /**
  79. * Constructor
  80. *
  81. * @param packageName The packageName and locale together point to an ICU udata object,
  82. * as defined by <code> udata_open( packageName, "res", locale, err) </code>
  83. * or equivalent. Typically, packageName will refer to a (.dat) file, or to
  84. * a package registered with udata_setAppData(). Using a full file or directory
  85. * pathname for packageName is deprecated.
  86. * @param locale This is the locale this resource bundle is for. To get resources
  87. * for the French locale, for example, you would create a
  88. * ResourceBundle passing Locale::FRENCH for the "locale" parameter,
  89. * and all subsequent calls to that resource bundle will return
  90. * resources that pertain to the French locale. If the caller doesn't
  91. * pass a locale parameter, the default locale for the system (as
  92. * returned by Locale::getDefault()) will be used.
  93. * @param err The Error Code.
  94. * The UErrorCode& err parameter is used to return status information to the user. To
  95. * check whether the construction succeeded or not, you should check the value of
  96. * U_SUCCESS(err). If you wish more detailed information, you can check for
  97. * informational error results which still indicate success. U_USING_FALLBACK_WARNING
  98. * indicates that a fall back locale was used. For example, 'de_CH' was requested,
  99. * but nothing was found there, so 'de' was used. U_USING_DEFAULT_WARNING indicates that
  100. * the default locale data was used; neither the requested locale nor any of its
  101. * fall back locales could be found.
  102. * @stable ICU 2.0
  103. */
  104. ResourceBundle(const UnicodeString& packageName,
  105. const Locale& locale,
  106. UErrorCode& err);
  107. /**
  108. * Construct a resource bundle for the default bundle in the specified package.
  109. *
  110. * @param packageName The packageName and locale together point to an ICU udata object,
  111. * as defined by <code> udata_open( packageName, "res", locale, err) </code>
  112. * or equivalent. Typically, packageName will refer to a (.dat) file, or to
  113. * a package registered with udata_setAppData(). Using a full file or directory
  114. * pathname for packageName is deprecated.
  115. * @param err A UErrorCode value
  116. * @stable ICU 2.0
  117. */
  118. ResourceBundle(const UnicodeString& packageName,
  119. UErrorCode& err);
  120. /**
  121. * Construct a resource bundle for the ICU default bundle.
  122. *
  123. * @param err A UErrorCode value
  124. * @stable ICU 2.0
  125. */
  126. ResourceBundle(UErrorCode &err);
  127. /**
  128. * Standard constructor, constructs a resource bundle for the locale-specific
  129. * bundle in the specified package.
  130. *
  131. * @param packageName The packageName and locale together point to an ICU udata object,
  132. * as defined by <code> udata_open( packageName, "res", locale, err) </code>
  133. * or equivalent. Typically, packageName will refer to a (.dat) file, or to
  134. * a package registered with udata_setAppData(). Using a full file or directory
  135. * pathname for packageName is deprecated.
  136. * nullptr is used to refer to ICU data.
  137. * @param locale The locale for which to open a resource bundle.
  138. * @param err A UErrorCode value
  139. * @stable ICU 2.0
  140. */
  141. ResourceBundle(const char* packageName,
  142. const Locale& locale,
  143. UErrorCode& err);
  144. /**
  145. * Copy constructor.
  146. *
  147. * @param original The resource bundle to copy.
  148. * @stable ICU 2.0
  149. */
  150. ResourceBundle(const ResourceBundle &original);
  151. /**
  152. * Constructor from a C UResourceBundle. The resource bundle is
  153. * copied and not adopted. ures_close will still need to be used on the
  154. * original resource bundle.
  155. *
  156. * @param res A pointer to the C resource bundle.
  157. * @param status A UErrorCode value.
  158. * @stable ICU 2.0
  159. */
  160. ResourceBundle(UResourceBundle *res,
  161. UErrorCode &status);
  162. /**
  163. * Assignment operator.
  164. *
  165. * @param other The resource bundle to copy.
  166. * @stable ICU 2.0
  167. */
  168. ResourceBundle&
  169. operator=(const ResourceBundle& other);
  170. /** Destructor.
  171. * @stable ICU 2.0
  172. */
  173. virtual ~ResourceBundle();
  174. /**
  175. * Clone this object.
  176. * Clones can be used concurrently in multiple threads.
  177. * If an error occurs, then nullptr is returned.
  178. * The caller must delete the clone.
  179. *
  180. * @return a clone of this object
  181. *
  182. * @see getDynamicClassID
  183. * @stable ICU 2.8
  184. */
  185. ResourceBundle *clone() const;
  186. /**
  187. * Returns the size of a resource. Size for scalar types is always 1, and for vector/table types is
  188. * the number of child resources.
  189. * @warning Integer array is treated as a scalar type. There are no
  190. * APIs to access individual members of an integer array. It
  191. * is always returned as a whole.
  192. *
  193. * @return number of resources in a given resource.
  194. * @stable ICU 2.0
  195. */
  196. int32_t getSize() const;
  197. /**
  198. * returns a string from a string resource type
  199. *
  200. * @param status fills in the outgoing error code
  201. * could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
  202. * could be a warning
  203. * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
  204. * @return a pointer to a zero-terminated char16_t array which lives in a memory mapped/DLL file.
  205. * @stable ICU 2.0
  206. */
  207. UnicodeString
  208. getString(UErrorCode& status) const;
  209. /**
  210. * returns a binary data from a resource. Can be used at most primitive resource types (binaries,
  211. * strings, ints)
  212. *
  213. * @param len fills in the length of resulting byte chunk
  214. * @param status fills in the outgoing error code
  215. * could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
  216. * could be a warning
  217. * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
  218. * @return a pointer to a chunk of unsigned bytes which live in a memory mapped/DLL file.
  219. * @stable ICU 2.0
  220. */
  221. const uint8_t*
  222. getBinary(int32_t& len, UErrorCode& status) const;
  223. /**
  224. * returns an integer vector from a resource.
  225. *
  226. * @param len fills in the length of resulting integer vector
  227. * @param status fills in the outgoing error code
  228. * could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
  229. * could be a warning
  230. * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
  231. * @return a pointer to a vector of integers that lives in a memory mapped/DLL file.
  232. * @stable ICU 2.0
  233. */
  234. const int32_t*
  235. getIntVector(int32_t& len, UErrorCode& status) const;
  236. /**
  237. * returns an unsigned integer from a resource.
  238. * This integer is originally 28 bits.
  239. *
  240. * @param status fills in the outgoing error code
  241. * could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
  242. * could be a warning
  243. * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
  244. * @return an unsigned integer value
  245. * @stable ICU 2.0
  246. */
  247. uint32_t
  248. getUInt(UErrorCode& status) const;
  249. /**
  250. * returns a signed integer from a resource.
  251. * This integer is originally 28 bit and the sign gets propagated.
  252. *
  253. * @param status fills in the outgoing error code
  254. * could be <TT>U_MISSING_RESOURCE_ERROR</TT> if the key is not found
  255. * could be a warning
  256. * e.g.: <TT>U_USING_FALLBACK_WARNING</TT>,<TT>U_USING_DEFAULT_WARNING </TT>
  257. * @return a signed integer value
  258. * @stable ICU 2.0
  259. */
  260. int32_t
  261. getInt(UErrorCode& status) const;
  262. /**
  263. * Checks whether the resource has another element to iterate over.
  264. *
  265. * @return true if there are more elements, false if there is no more elements
  266. * @stable ICU 2.0
  267. */
  268. UBool hasNext() const;
  269. /**
  270. * Resets the internal context of a resource so that iteration starts from the first element.
  271. *
  272. * @stable ICU 2.0
  273. */
  274. void resetIterator();
  275. /**
  276. * Returns the key associated with this resource. Not all the resources have a key - only
  277. * those that are members of a table.
  278. *
  279. * @return a key associated to this resource, or nullptr if it doesn't have a key
  280. * @stable ICU 2.0
  281. */
  282. const char* getKey() const;
  283. /**
  284. * Gets the locale ID of the resource bundle as a string.
  285. * Same as getLocale().getName() .
  286. *
  287. * @return the locale ID of the resource bundle as a string
  288. * @stable ICU 2.0
  289. */
  290. const char* getName() const;
  291. /**
  292. * Returns the type of a resource. Available types are defined in enum UResType
  293. *
  294. * @return type of the given resource.
  295. * @stable ICU 2.0
  296. */
  297. UResType getType() const;
  298. /**
  299. * Returns the next resource in a given resource or nullptr if there are no more resources
  300. *
  301. * @param status fills in the outgoing error code
  302. * @return ResourceBundle object.
  303. * @stable ICU 2.0
  304. */
  305. ResourceBundle
  306. getNext(UErrorCode& status);
  307. /**
  308. * Returns the next string in a resource or nullptr if there are no more resources
  309. * to iterate over.
  310. *
  311. * @param status fills in the outgoing error code
  312. * @return an UnicodeString object.
  313. * @stable ICU 2.0
  314. */
  315. UnicodeString
  316. getNextString(UErrorCode& status);
  317. /**
  318. * Returns the next string in a resource or nullptr if there are no more resources
  319. * to iterate over.
  320. *
  321. * @param key fill in for key associated with this string
  322. * @param status fills in the outgoing error code
  323. * @return an UnicodeString object.
  324. * @stable ICU 2.0
  325. */
  326. UnicodeString
  327. getNextString(const char ** key,
  328. UErrorCode& status);
  329. /**
  330. * Returns the resource in a resource at the specified index.
  331. *
  332. * @param index an index to the wanted resource.
  333. * @param status fills in the outgoing error code
  334. * @return ResourceBundle object. If there is an error, resource is invalid.
  335. * @stable ICU 2.0
  336. */
  337. ResourceBundle
  338. get(int32_t index,
  339. UErrorCode& status) const;
  340. /**
  341. * Returns the string in a given resource at the specified index.
  342. *
  343. * @param index an index to the wanted string.
  344. * @param status fills in the outgoing error code
  345. * @return an UnicodeString object. If there is an error, string is bogus
  346. * @stable ICU 2.0
  347. */
  348. UnicodeString
  349. getStringEx(int32_t index,
  350. UErrorCode& status) const;
  351. /**
  352. * Returns a resource in a resource that has a given key. This procedure works only with table
  353. * resources.
  354. *
  355. * @param key a key associated with the wanted resource
  356. * @param status fills in the outgoing error code.
  357. * @return ResourceBundle object. If there is an error, resource is invalid.
  358. * @stable ICU 2.0
  359. */
  360. ResourceBundle
  361. get(const char* key,
  362. UErrorCode& status) const;
  363. /**
  364. * Returns a string in a resource that has a given key. This procedure works only with table
  365. * resources.
  366. *
  367. * @param key a key associated with the wanted string
  368. * @param status fills in the outgoing error code
  369. * @return an UnicodeString object. If there is an error, string is bogus
  370. * @stable ICU 2.0
  371. */
  372. UnicodeString
  373. getStringEx(const char* key,
  374. UErrorCode& status) const;
  375. #ifndef U_HIDE_DEPRECATED_API
  376. /**
  377. * Return the version number associated with this ResourceBundle as a string. Please
  378. * use getVersion, as this method is going to be deprecated.
  379. *
  380. * @return A version number string as specified in the resource bundle or its parent.
  381. * The caller does not own this string.
  382. * @see getVersion
  383. * @deprecated ICU 2.8 Use getVersion instead.
  384. */
  385. const char* getVersionNumber() const;
  386. #endif /* U_HIDE_DEPRECATED_API */
  387. /**
  388. * Return the version number associated with this ResourceBundle as a UVersionInfo array.
  389. *
  390. * @param versionInfo A UVersionInfo array that is filled with the version number
  391. * as specified in the resource bundle or its parent.
  392. * @stable ICU 2.0
  393. */
  394. void
  395. getVersion(UVersionInfo versionInfo) const;
  396. #ifndef U_HIDE_DEPRECATED_API
  397. /**
  398. * Return the Locale associated with this ResourceBundle.
  399. *
  400. * @return a Locale object
  401. * @deprecated ICU 2.8 Use getLocale(ULocDataLocaleType type, UErrorCode &status) overload instead.
  402. */
  403. const Locale& getLocale() const;
  404. #endif /* U_HIDE_DEPRECATED_API */
  405. /**
  406. * Return the Locale associated with this ResourceBundle.
  407. * @param type You can choose between requested, valid and actual
  408. * locale. For description see the definition of
  409. * ULocDataLocaleType in uloc.h
  410. * @param status just for catching illegal arguments
  411. *
  412. * @return a Locale object
  413. * @stable ICU 2.8
  414. */
  415. const Locale
  416. getLocale(ULocDataLocaleType type, UErrorCode &status) const;
  417. #ifndef U_HIDE_INTERNAL_API
  418. /**
  419. * This API implements multilevel fallback
  420. * @internal
  421. */
  422. ResourceBundle
  423. getWithFallback(const char* key, UErrorCode& status);
  424. #endif /* U_HIDE_INTERNAL_API */
  425. /**
  426. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  427. *
  428. * @stable ICU 2.2
  429. */
  430. virtual UClassID getDynamicClassID() const override;
  431. /**
  432. * ICU "poor man's RTTI", returns a UClassID for this class.
  433. *
  434. * @stable ICU 2.2
  435. */
  436. static UClassID U_EXPORT2 getStaticClassID();
  437. private:
  438. ResourceBundle() = delete; // default constructor not implemented
  439. UResourceBundle *fResource;
  440. void constructForLocale(const UnicodeString& path, const Locale& locale, UErrorCode& error);
  441. Locale *fLocale;
  442. };
  443. U_NAMESPACE_END
  444. #endif /* U_SHOW_CPLUSPLUS_API */
  445. #endif