SourcePro C++ 12.0 |
SourcePro® C++ API Reference Guide |
SourcePro C++ Documentation Home |
Provides a way to store and access locale-dependent data. More...
#include <rw/i18n/RWUResourceBundle.h>
Public Types | |
enum | ResourceType { None, String, Int, IntVec, Binary, Table, Array, Reserved } |
Public Member Functions | |
RWUResourceBundle (const RWUString &path, const RWULocale &locale=RWULocale::getDefault()) | |
RWUResourceBundle (const char *path, const RWULocale &locale=RWULocale::getDefault()) | |
RWUResourceBundle (const RWUResourceBundle &other) | |
RWUResourceBundle & | operator= (const RWUResourceBundle &other) |
~RWUResourceBundle () | |
ResourceType | getType () const |
int32_t | getSize () const |
RWCString | getKey () const |
RWUString | getString () const |
RWUString | getString (int32_t index) const |
RWUString | getString (const RWCString &key) const |
const uint8_t * | getBinary () const |
const int32_t * | getIntVector () const |
uint32_t | getUInt () const |
int32_t | getInt () const |
RWULocale | getLocale () const |
RWCString | getLocaleName () const |
RWUResourceBundle | get (int32_t index) const |
RWUResourceBundle | get (const RWCString &key) const |
RWUStatusCode | getStatus () const |
RWUResourceBundle is a mechanism for storing and accessing locale-dependent data.
Every resource bundle contains one or more resources. An individual resource can be of type string, integer, integer vector, or binary. A collection of resources can be arranged as a table or as an array. Each element of a table has a const
char*
key; each element of an array has an integer index. Any element of a table or array may be either an individual resource or another table or array. Tables and arrays are themselves treated as RWUResourceBundle instances, so RWUResourceBundle instances nest recursively.
Each resource bundle contains data for one locale. The bundles for an application are organized in a logical hierarchy based upon the locales associated with the bundles. At the base of the hierarchy is a "root" resource bundle that contains a resource value for every aspect of the program that needs to be localized; typically, these resource values are expressed in the locale of the application developer. Below the "root" resource bundle are language-specific bundles (for example, en
, de
, and ja
bundles). Below the language-specific bundles are region-specific bundles (for example, de_DE
and de_CH
), and below the region-specific bundles are variant-specific bundles (for example, de_DE_PREEURO
). Typically, only the "root" bundle contains a resource value for every localizable aspect of the application. A fallback mechanism ensures that a child resource bundle need only contain the resources whose values differ from its ancestor bundles.
When an application needs localized data, it constructs an RWUResourceBundle, then queries the bundle for the needed resources. The constructors for RWUResourceBundle accept the name of a directory containing .res
files, and the name of a locale. The RWUResourceBundle instances created by these constructors are known as "top-level" resource bundles. These top-level resource bundles are guaranteed to be of type table, containing one or more "top-level" resources associated with string keys.
When creating a top-level RWUResourceBundle, the constructor may succeed, succeed through fallback, or fail, depending on whether an exact match to the requested locale is found, a fallback match is found, or no bundle at all is found. Constructors throw an RWUException if no bundle is found, but otherwise succeed and cache an RWUStatusCode to indicate the level of success. The getStatus() method returns the status of an RWUResourceBundle.
Similarly, when a top-level RWUResourceBundle is queried for a particular top-level resource, the query may succeed, succeed through fallback, or fail. The query throws RWUException if it fails, but otherwise returns an RWUResourceBundle holding a cached RWUStatusCode. Again, the getStatus() method returns the status code.
The fallback mechanism works in three stages. In the first stage, you create an RWUResourceBundle from a locale name and path:
root
.For example, suppose there are RWUResourceBundle instances available for root
, en
, en_US
, en_US_POSIX
, and es
, and that the current default RWULocale is es_ES
. If you attempt to open the RWUResourceBundle named ja_JP
, the library tries these bundle names in order:
ja_JP
-- the exact matchja
-- the parentes_ES
-- the default localees
-- the default parent: Success!es
been missing, root
would have been tried nextIn the second stage, you access a resource named at the top level:
root
resource bundle.For example, suppose that in the es
resource bundle there is a table named menu
containing entries for file
, edit
, and help
. Suppose also that the root
resource bundle has, in addition to the menu
table, a string resource named currentVersion
. If you attempt to access the menu
table resource, the library returns the one in the es
RWUResourceBundle. If you attempt to access the currentVersion
string resource, the library returns the one in root
.
In the third stage, you open a resource held within a top-level array or table:
For instance, continuing from the Stage 2 example, suppose you have opened the menu
resource, and now are looking for the tools
string within the menu
table. Suppose this entry is available in the menu
resource in the root
resource bundle, but not in the menu
resource in the es
resource bundle. Your attempt does not fall back--it fails.
Data in a resource bundle are compiled into a compact form. Note that binary data, as well as keyed data, are compiled without regard to platform-specific details. As a result, table data may not be found on a platform where the character encoding (US-ASCII or EBCDIC, for instance) used for the keys is different than that where compiled. For similar reasons, binary data may be wrong if compiled on a big-endian machine and read on a little-endian machine, or vice versa.
The ResourceType enum represents the type of the resource contained by a resource bundle.
RWUResourceBundle::RWUResourceBundle | ( | const RWUString & | path, | |
const RWULocale & | locale = RWULocale::getDefault() | |||
) |
Constructs an RWUResourceBundle from the data specific to locale held within the directory path.
The newly-constructed RWUResourceBundle is a top-level bundle containing table data. Fallback may occur during the construction of the bundle or during the retrieval of its top-level resources. See getStatus() and getLocale().
RWUException | Thrown if any error occurs during the construction. |
RWUResourceBundle::RWUResourceBundle | ( | const char * | path, | |
const RWULocale & | locale = RWULocale::getDefault() | |||
) |
Constructs an RWUResourceBundle from the data specific to locale held within the directory path.
The newly-constructed RWUResourceBundle is a top-level bundle containing table data. Fallback may occur during the construction of the bundle or during the retrieval of its top-level resources. See getStatus() and getLocale().
RWUException | Thrown if any error occurs during the construction. |
RWUResourceBundle::RWUResourceBundle | ( | const RWUResourceBundle & | other | ) | [inline] |
Copy constructor.
RWUResourceBundle::~RWUResourceBundle | ( | ) | [inline] |
Destructor.
RWUResourceBundle RWUResourceBundle::get | ( | const RWCString & | key | ) | const [inline] |
Returns the resource associated with the given key.
RWUException | Thrown if self is not of type RWUResourceBundle::table, or if there is no such key. |
RWUResourceBundle RWUResourceBundle::get | ( | int32_t | index | ) | const [inline] |
Returns the resource located at the given offset index within an array.
RWUException | Thrown if self is not of type RWUResourceBundle::Array, or if the index is out of bounds. |
const uint8_t * RWUResourceBundle::getBinary | ( | ) | const [inline] |
Returns a binary resource as a pointer to an array of unsigned bytes. Call getSize() to find the number of bytes.
RWUException | Thrown if self is not of type RWUResourceBundle::Binary. |
int32_t RWUResourceBundle::getInt | ( | ) | const [inline] |
Returns a signed int32_t
.
RWUException | Thrown if self is not of type RWUResourceBundle::Int. |
const int32_t * RWUResourceBundle::getIntVector | ( | ) | const [inline] |
Returns an integer vector as a pointer to int32_t
. Call getSize() to find the number of integers in the vector.
RWUException | Thrown if self is not of type RWUResourceBundle::IntVec. |
RWCString RWUResourceBundle::getKey | ( | ) | const [inline] |
Returns the key of the resource held by self if the resource is an element of a table; otherwise, an empty string.
RWULocale RWUResourceBundle::getLocale | ( | void | ) | const [inline] |
Returns the locale associated with self.
RWCString RWUResourceBundle::getLocaleName | ( | ) | const [inline] |
Returns the name of the locale associated with self. Convenience synonym for getLocale().getName().
int32_t RWUResourceBundle::getSize | ( | ) | const |
Returns the size of the resource held by self. Returns 1
for strings or integers, a count of bytes for binary resources, and a count of elements for integer vectors, arrays, and tables.
RWUStatusCode RWUResourceBundle::getStatus | ( | ) | const [inline] |
Returns an RWUStatusCode indicating whether fallback occurred during the creation or retrieval of this resource bundle:
Returns the string value associated with the given key. Convenience synonym for get(key).getString().
RWUException | Thrown if self is not of type RWUResourceBundle::Table, if there is no such key, or if the resource associated with the key is not of type RWUResourceBundle::String. |
RWUString RWUResourceBundle::getString | ( | int32_t | index | ) | const [inline] |
Returns the string value at the given index. Convenience synonym for get(index).getString().
RWUException | Thrown if self is not of type RWUResourceBundle::Array, if the resource at this index is not of type RWUResourceBundle::String, or if the index is out of bounds. |
RWUString RWUResourceBundle::getString | ( | void | ) | const [inline] |
Returns the string value of a string resource.
RWUException | Thrown if self is not of type RWUResourceBundle::String. |
RWUResourceBundle::ResourceType RWUResourceBundle::getType | ( | ) | const [inline] |
Returns the ResourceType of the resource held by self.
If the resource is of type string, integer, integer vector, or binary, then its value may be extracted directly. If the resource is of type table or array, then its elements may be extracted in the form of nested RWUResourceBundle instances. Any attempt to extract a value directly from a table or array results in an exception of type RWUException.
uint32_t RWUResourceBundle::getUInt | ( | ) | const [inline] |
Returns an unsigned int32_t
.
RWUException | Thrown if self is not of type RWUResourceBundle::Int. |
RWUResourceBundle & RWUResourceBundle::operator= | ( | const RWUResourceBundle & | other | ) | [inline] |
Assignment operator. Replaces self with a copy of the resource bundle other.
© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.