Support for localized strings using XML files. More...
#include <Wt/WMessageResourceBundle>
Inherits Wt::WLocalizedStrings.
Public Member Functions | |
WMessageResourceBundle () | |
Creates a message resource bundle. | |
void | use (const std::string &path, bool loadInMemory=true) |
Adds a (series) of message resource files to be used. | |
virtual void | refresh () |
Rereads the message resources. | |
virtual void | hibernate () |
Purges memory resources, if possible. | |
virtual bool | resolveKey (const std::string &key, std::string &result) |
Resolves a key in the current locale. |
Support for localized strings using XML files.
The resource bundle manages a number of resource files, which allow the developer to conceptually manage its messages in a number of libraries.
For example, a WApplication may have a generic message library, that is shared with many other libraries, with re-occurring messages (such as 'welcome', 'add to shopping cart', and 'pay'), and a specific message library for specific messages.
Usage example:
XML file "general.xml":
<?xml version="1.0" encoding="UTF-8"?> <messages> <message id='welcome-text'> Welcome dear visiter, {1} of the WFooBar magic website ! </message> <message id='company-policy'> The company policy is to <b>please our share-holders</b>. </message> </messages>
Use this resource bundle in your program:
// load the message resource bundle Wt::WApplication *app = Wt::WApplication::instance(); app->messageResourceBundle().use("general"); // resolve a string using the resource bundle Wt::WText *welcome = new Wt::WText(tr("welcome-visitor").arg("Bart"));
void Wt::WMessageResourceBundle::hibernate | ( | ) | [virtual] |
Purges memory resources, if possible.
This is called afer event handling, and is an opportunity to conserve memory inbetween events, by freeing memory used for cached key/value bindings, if applicable.
The default implementation does nothing.
Reimplemented from Wt::WLocalizedStrings.
void Wt::WMessageResourceBundle::refresh | ( | ) | [virtual] |
Rereads the message resources.
Purge any cached key/values, if applicable.
The default implementation does nothing.
Reimplemented from Wt::WLocalizedStrings.
bool Wt::WMessageResourceBundle::resolveKey | ( | const std::string & | key, | |
std::string & | result | |||
) | [virtual] |
Resolves a key in the current locale.
This method is used by WString to obtain the UTF8 value corresponding to a key in the current locale.
Returns true
if the key could be resolved. The value is written in result
, encoded using UTF8.
Implements Wt::WLocalizedStrings.
void Wt::WMessageResourceBundle::use | ( | const std::string & | path, | |
bool | loadInMemory = true | |||
) |
Adds a (series) of message resource files to be used.
The path
is not a URI, and relative paths will be resolved with respect to the working directory of the server. The XML files do not need to be deployed in the web server's docroot.
When you give as path:
/path/to/name, then the following message resource files will be used:
The message file that is used depends on the application's locale.
A Message resource file must be formatted as follows:
<?xml version="1.0" encoding="UTF-8"?> <messages> <message id='welcome-text'> Welcome dear visiter, {1} of the WFooBar magic website ! </message> <message id='company-policy'> The company policy is to <b>please our share-holders</b>. </message> </messages>
The encodings supported are ASCII, UTF-8 (recommended) or UTF-16.
To refer the two messages defined in this resource file, use WString::tr("welcome-text").arg(userName) or WWidget::tr("company-policy").