Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Internationalization Module Reference Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

RWUResourceBundle

Module:  Internationalization Module   Group:  Localization


Does Not Inherit

Local Index

Members

Header File

#include <rw/i18n/RWUResourceBundle.h> 

Description

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 RWUResourceBundles, 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:

  1. The library first looks for the exact named RWUResourceBundle.

  2. Failing that, the library looks for a parent locale along the named locale's branch.

  3. If no parent locale is found, the library looks for the locale named by RWULocale::getDefault().

  4. Failing that, the library looks for a parent locale along the default locale's branch.

  5. Failing that, the library attempts to open the RWUResourceBundle named root.

  6. Finally, if it can find no information for any of the above mentioned locales, the library throws an RWUException.

For example, suppose there are RWUResourceBundles 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:

  1. ja_JP -- the exact match

  2. ja -- the parent

  3. es_ES -- the default locale

  4. es -- the default parent: Success!

  5. if es been missing, root would have been tried next

In the second stage, you access a resource named at the top level:

  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.

  4. Failing that, the library throws an RWUException.

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:

  1. The library looks only in the already open top-level resource.

  2. If the requested datum is not there, an RWUException is thrown.

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 (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.

Related Classes

RWULocale

Public Enums

enum ResourceType { None,
                    String,
                    Int,
                    IntVec,
                    Binary,
                    Table, 
                    Array,
                    Reserved
};

Public Constructors

RWUResourceBundle(const RWUString& path,
           const RWULocale& locale = RWULocale::getDefault());
RWUResourceBundle(const char* path,
           const RWULocale& locale = RWULocale::getDefault());
RWUResourceBundle(const RWUResourceBundle& other);

Public Destructor

~RWUResourceBundle();

Public Member Operators

RWUResourceBundle&
operator=(const RWUResourceBundle& other);

Public Member Functions

RWUResourceBundle
get(int32_t index) const;
RWUResourceBundle
get(const RWCString& key) const;
const uint8_t*
getBinary() const;
int32_t
getInt() const;
const int32_t*
getIntVector() const;
RWCString
getKey() const;
RWULocale
getLocale() const;

NOTE -- When a top-level resource bundle is constructed, fallback may be used to find resource data, if data for the requested locale is not available. Method getLocale() returns the actual locale where the top-level resource bundle was found, which may not be the requested locale.
RWCString
getLocaleName() const;
int32_t
getSize() const;
RWUStatusCode
getStatus() const;
RWUString
getString() const; 
RWUString
getString(int32_t index) const;
RWUString
getString(const RWCString& key) const;
ResourceType
getType() const;
uint32_t
getUInt() const;


Previous fileTop of DocumentContentsIndex pageNext file

© 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.