Cross-platform resource loader.
#include <UTResource.h>
Public Member Functions | |
Resource_t (stringliteral *library_name=NULL) | |
~Resource_t () | |
const String_t & | ModuleName () const |
int | Load (int resource_id, byte *contents, int max_size) const |
void | Load (int resource_id, Buffer_t *contents) const |
void | LoadText (int resource_id, String_t *string) const |
void | SetLocale (stringliteral *locale, bool global) |
void | GetLocalizedString (int string_id, String_t *string) const |
bool | IsSystemDefaultLocaleSupported (bool support_language_only=true) const |
int | CountSupportedLocales () const |
void | GetSupportedLocale (int index, String_t *string) const |
Definition at line 49 of file UTResource.h.
Resource_t::Resource_t | ( | stringliteral * | library_name = NULL |
) |
If library_name is NULL, initializes a resource pool for the main application. If library_name is not NULL, initializes a resource pool for a library of the specified name. The ".dll" or ".so" extension should not be included. Resources cannot be guaranteed to be available before main has been called or after main has returned, so no resource objects may exist at those times or an assertion failure will occir. When a module is unloaded, there can be no stale Resource_t objects referencing it. If any remain, an assertion failure will occur.
Resource_t::~Resource_t | ( | ) |
Destructor.
const String_t& Resource_t::ModuleName | ( | ) | const [inline] |
Returns the name of the module represented by the resource. The module name will be an empty string if the resource object was constructed with a library name of NULL, representing the main application.
int Resource_t::Load | ( | int | resource_id, | |
byte * | contents, | |||
int | max_size | |||
) | const |
Loads an arbitrary glob of data into the buffer provided by the caller. Specific loader functions for dialog, png, and jpeg contents are included in the UTgfx and UTgui libraries. The resource content must fit in the provided buffer as specified by max_size, otherwise an assertion failure will occur. The actual size of the resource content is returned.
void Resource_t::Load | ( | int | resource_id, | |
Buffer_t * | contents | |||
) | const |
Loads an arbitrary glob of data into the Buffer_t provided by the caller. Specific loader functions for dialog, png, and jpeg contents are included in the UTgfx and UTgui libraries.
void Resource_t::LoadText | ( | int | resource_id, | |
String_t * | string | |||
) | const |
Loads an arbitrary glob of text classified as being a string into the String_t provided by the caller. This mechanism should NOT be used for localization. For that purpose, use GetLocalizedString instead.
void Resource_t::SetLocale | ( | stringliteral * | locale, | |
bool | global | |||
) |
Selects the locale used by this instance of Resource_t. If global is true, new resource instances will default to that locale.
The locale-specific string mappings are specified with an ISO 639-3 language code followed by an underscore, followed by * to specify any territory or an ISO 3166-1 alpha-2 country code, for example "eng_US" for English (U.S.), "eng_GB" English (Great Britain), "deu_DE" for German (Germany), "spa_ES" for Spanish (Spain), "fra_CA" for French (Canada), or "eng_*" to match and provide translations for any english language locale including "eng_GB", "eng_US", or other locales beginning with "eng_".
Some locales have mutiple 3-character identifiers corresponding to a single 2-character identifier, due either to common but incorrect usage, or deprecation. Linux version 2.6.22 returns 2-character language identifiers, so those identifiers are mapped to a single 3-character identifier for locale strings lookup purposes.
Preferred Not preferred 2-char Name Why sqi alb sq Albanian alb not in ISO 639-3 hye arm hy Armenian arm not in ISO 639-3 eus baq eu Basque baq not in ISO 639-3 mya bur my Burmese bur not in ISO 639-3 zho chi zh Chinese chi not in ISO 639-3 hrv scr hr Croatian scr not in ISO 639-3 ces cze cs Czech cze not in ISO 639-3 nld dut nl Dutch dut not in ISO 639-3 fra fre fr French fre not in ISO 639-3 kat geo ka Georgian geo not in ISO 639-3 deu ger de German ger not in ISO 639-3 ell gre el Greek gre not in ISO 639-3 isl ice is Icelandic ice not in ISO 639-3 mkd mac mk Macedonian mac not in ISO 639-3 mkd mac mk Macedonian mac not in ISO 639-3 msa may ms Malaysian may not in ISO 639-3 mri mao mi Maori mao not in ISO 639-3 fas per fa Persian per not in ISO 639-3 ron rum ro Romanian rum not in ISO 639-3 srp scc sr Serbian scc not in ISO 639-3 slk slo sk Slovak slo not in ISO 639-3 bod tib bo Tibetan tib not in ISO 639-3 cym wel cy Welsh wel not in ISO 639-3
Because the specified locale cannot be guaranteed to be supported by the application and all of the libraries that it makes use of, locale selection is fairly complicated. In general terms, the selection logic is as follows:
A language set by Resource_t::SetLocale (not global) is preferred if it is supported.
A language set by Resource_t::SetLocale (global) is preferred if it is supported.
The system default language is preferred if neither of the above are supported.
The first language supported by the application is preferred if none of the above are supported.
Within the language chosen above, if the specified country is supported, it is used.
Within the language chosen above, if the specified country is not supported, the first country supported by that language is used.
void Resource_t::GetLocalizedString | ( | int | string_id, | |
String_t * | string | |||
) | const |
Retrieves a localized string based on the supported locales included in the application's resources and the current locale. See SetLocale for information on locale selection.
bool Resource_t::IsSystemDefaultLocaleSupported | ( | bool | support_language_only = true |
) | const |
Returns true if the native locale of the system is supported by the module. This would typically be called once at startup for the application, to determine whether a locale selector dialog should be displayed. If support_language_only is true, then even if the default locale is not supported, true will be returned if the language (not country) portion of the system default locale is supported.
int Resource_t::CountSupportedLocales | ( | ) | const |
Returns the number of locales supported by the application or library which this resource instance refers to.
void Resource_t::GetSupportedLocale | ( | int | index, | |
String_t * | string | |||
) | const |
Returns the name of a locale supported by the application or library, at the specified index.