Accessing a Resource
In the Internationalization Module, resources within a resource bundle are themselves represented as
RWUResourceBundles, so
RWUResourceBundle instances nest recursively. The
RWUResourceBundle instances created by the constructors are called “top-level” resource bundles. Top-level resource bundles are guaranteed to be of type
table, containing one or more top-level resources associated with
const char* keys.
You can access a top-level resource using the
get() method. This method returns the requested resource as an
RWUResourceBundle. For example:
RWUResourceBundle rb = RWUResourceBundle("C:\\data", "root");
RWUResourceBundle myResource = rb.get("salutations");
When the top-level
RWUResourceBundle is queried for a particular top-level resource, the query may succeed, succeed through fallback, or fail.
The fallback mechanism works as follows:
1. The library first looks for the named resource in the currently open
RWUResourceBundle.
2. Failing that, the library looks for the named resource in a parent of the currently open
RWUResourceBundle.
3. Failing that, the library looks in the 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.
Note that fallback occurs only for top-level resources. When you open a resource held within a top level array or table, the library looks only in the already open top-level
RWUResourceBundle. If the requested datum is not there, an
RWUException is thrown. For example, continuing from the example above, suppose you have opened the
menu resource, and now are looking for the
tools string within the
menu table. Suppose furthermore 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.
The lookup query throws
RWUException if it fails, but otherwise returns an
RWUResourceBundle holding a cached RWUStatusCode. The
getStatus() method returns the status code:
RWUNoError The queried bundle held the resource directly.
RWUUsingFallbackWarning RWUUsingDefaultWarning The requested bundle was at the top-level of a locale’s
RWUResourceBundle, but was not found in that
RWUResourceBundle directly; instead, the resource was found in the
root bundle via fallback.
The
get() method returns the requested resource as an
RWUResourceBundle. If you know the type of the resource, you can then access it using the appropriate method:
Strings The
getString() method returns the resource as an
RWUString. For example:
RWUString s = rb.get("Version").getString();
Because string resources are so common, a getString(const RWCString&) method is also provided as a convenience for get(const RWCString&).getString():
RWUString s = rb.getString("Version");
Integers The getInt() method returns the resource as a signed int32_t; the getUInt() method returns it as an unsigned int32_t. Thus:
int32_t i = rb.get("numStatesOrProvinces").getInt();
Integer Vectors The getIntVector() method returns the resource as a pointer to int32_t. Call getSize() to find the number of integers in the vector.
Binaries
The getBinary() method returns the resource as a pointer to an array of unsigned bytes. Call getSize() to find the number of bytes.
Tables As described above for top-level resources, which are tables, the get(const RWCString&) method returns the resource in a table associated with the given string key. If the resource is an element of a table, the getKey() method returns the key of the resource.
Arrays The get(int32_t) method returns the resource located at the given offset index within an array. For example:
RWUString s = rb.get("AmPmMarkers").get(1).getString();
Because string resources are so common, a getString(int32_t) method is also provided as a convenience for get(int32_t).getString():
RWUString s = rb.get("AmPmMarkers").getString(1);
All
getX() methods throw
RWUException if the resource is not of the specified type. If you don’t know the type of a resource held by an
RWUResourceBundle, you can query it using the
getType() method, which returns a value from the
ResourceType enumeration:
None,
String,
Int,
IntVec,
Binary,
Table, or
Array. For example:
if (rb.getType() == RWUResourceBundle::Int) {
int i = rb.getInt();
}