contents up previous next

wxlocale

wxlocale class encapsulates all language-dependent settings and is a generalization of the c locale concept.

in wxwidgets this class manages message catalogs which contain the translations of the strings used to the current language.

wxperl note: in wxperl you can't use the '_' function name, so the wx::locale module can export the gettext and gettext_noop under any given name.

  # this imports gettext ( equivalent to wx::gettranslation
  # and gettext_noop ( a noop )
  # into your module
  use wx::locale qw(:default);

  # ....

  # use the functions
  print gettext( ``panic!'' ); 

  button = wx::button->new( window, -1, gettext( ``label'' ) );
if you need to translate a lot of strings, then adding gettext( ) around each one is a long task ( that is why _( ) was introduced ), so just choose a shorter name for gettext:

  #
  use wx::locale 'gettext' => 't',
                 'gettext_noop' => 'gettext_noop';

  # ...

  # use the functions
  print t( ``panic!!'' );

  # ...
derived from

no base class

see also

internationalization overview,
internat sample

include files

<wx/intl.h>

members

supported languages
wxlocale::wxlocale
wxlocale::~wxlocale
wxlocale::addcatalog
wxlocale::addcataloglookuppathprefix
wxlocale::addlanguage
wxlocale::findlanguageinfo
wxlocale::getcanonicalname
wxlocale::getlanguage
wxlocale::getlanguageinfo
wxlocale::getlanguagename
wxlocale::getlocale
wxlocale::getname
wxlocale::getstring
wxlocale::getheadervalue
wxlocale::getsysname
wxlocale::getsystemencoding
wxlocale::getsystemencodingname
wxlocale::getsystemlanguage
wxlocale::init
wxlocale::isavailable
wxlocale::isloaded
wxlocale::isok


supported languages

see list of recognized language constants. these constants may be used to specify the language in init and are returned by getsystemlanguage:


wxlocale::wxlocale

wxlocale()

this is the default constructor and it does nothing to initialize the object: init() must be used to do that.

wxlocale(int language, int flags = wxlocale_load_default | wxlocale_conv_encoding)

see init() for parameters description.

wxlocale(const char *szname, const char *szshort = null, const char *szlocale = null, bool bloaddefault = true, bool bconvertencoding = false)

see init() for parameters description.

the call of this function has several global side effects which you should understand: first of all, the application locale is changed - note that this will affect many of standard c library functions such as printf() or strftime(). second, this wxlocale object becomes the new current global locale for the application and so all subsequent calls to wxgettranslation() will try to translate the messages using the message catalogs for this locale.


wxlocale::~wxlocale

~wxlocale()

the destructor, like the constructor, also has global side effects: the previously set locale is restored and so the changes described in init documentation are rolled back.


wxlocale::addcatalog

bool addcatalog(const char *szdomain)

bool addcatalog(const char *szdomain, wxlanguagemsgidlanguage, const char *msgidcharset)

add a catalog for use with the current locale: it is searched for in standard places (current directory first, then the system one), but you may also prepend additional directories to the search path with addcataloglookuppathprefix().

all loaded catalogs will be used for message lookup by getstring() for the current locale.

returns true if catalog was successfully loaded, false otherwise (which might mean that the catalog is not found or that it isn't in the correct format).

the second form of this method takes two additional arguments, msgidlanguage and msgidcharset.

msgidlanguage specifies the language of "msgid" strings in source code (i.e. arguments to getstring, wxgettranslation and the _() macro). it is used if addcatalog cannot find any catalog for current language: if the language is same as source code language, then strings from source code are used instead.

msgidcharset lets you specify the charset used for msgids in sources in case they use 8-bit characters (e.g. german or french strings). this argument has no effect in unicode build, because literals in sources are unicode strings; you have to use compiler-specific method of setting the right charset when compiling with unicode.

by default (i.e. when you use the first form), msgid strings are assumed to be in english and written only using 7-bit ascii characters.

if you have to deal with non-english strings or 8-bit characters in the source code, see the instructions in writing non-english applications.


wxlocale::addcataloglookuppathprefix

void addcataloglookuppathprefix(const wxstring& prefix)

add a prefix to the catalog lookup path: the message catalog files will be looked up under prefix/<lang>/lc_messages, prefix/<lang> and prefix (in this order).

this only applies to subsequent invocations of addcatalog().


wxlocale::addlanguage

static void addlanguage(const wxlanguageinfo& info)

adds custom, user-defined language to the database of known languages. this database is used in conjunction with the first form of init.

wxlanguageinfo is defined as follows:

struct wxdllexport wxlanguageinfo
{
    int language;                   // wxlanguage id
    wxstring canonicalname;         // canonical name, e.g. fr_fr
#ifdef __win32__
    wxuint32 winlang, winsublang;   // win32 language identifiers
                                    // (lang_xxxx, sublang_xxxx)
#endif
    wxstring description;           // human-readable name of the language
};
language should be greater than wxlanguage_user_defined.

wxperl note: in wxperl wx::languageinfo has only one method:

wx::languageinfo->new( language, canonicalname, winlang, winsublang, description )


wxlocale::findlanguageinfo

static wxlanguageinfo * findlanguageinfo(const wxstring& locale)

this function may be used to find the language description structure for the given locale, specified either as a two letter iso language code (for example, "pt"), a language code followed by the country code ("pt_br") or a full, human readable, language description ("portuguese-brazil").

returns the information for the given language or null if this language is unknown. note that even if the returned pointer is valid, the caller should not delete it.

see also

getlanguageinfo


wxlocale::getcanonicalname

wxstring getcanonicalname() const

returns the canonical form of current locale name. canonical form is the one that is used on unix systems: it is a two- or five-letter string in xx or xx_yy format, where xx is iso 639 code of language and yy is iso 3166 code of the country. examples are "en", "en_gb", "en_us" or "fr_fr".

this form is internally used when looking up message catalogs.

compare getsysname.


wxlocale::getlanguage

int getlanguage() const

returns wxlanguage constant of current language. note that you can call this function only if you used the form of init that takes wxlanguage argument.


wxlocale::getlanguageinfo

static wxlanguageinfo * getlanguageinfo(int lang) const

returns a pointer to wxlanguageinfo structure containing information about the given language or null if this language is unknown. note that even if the returned pointer is valid, the caller should not delete it.

see addlanguage for the wxlanguageinfo description.

as with init, wxlanguage_default has the special meaning if passed as an argument to this function and in this case the result of getsystemlanguage() is used.


wxlocale::getlanguagename

static wxstring getlanguagename(int lang) const

returns english name of the given language or empty string if this language is unknown.

see getlanguageinfo for a remark about special meaning of wxlanguage_default.


wxlocale::getlocale

const char* getlocale() const

returns the locale name as passed to the constructor or init(). this is full, human-readable name, e.g. "english" or "french".


wxlocale::getname

const wxstring& getname() const

returns the current short name for the locale (as given to the constructor or the init() function).


wxlocale::getstring

const char* getstring(const char *szorigstring, const char *szdomain = null) const

const char* getstring(const char *szorigstring, const char *szorigstring2, size_t n, const char *szdomain = null) const

retrieves the translation for a string in all loaded domains unless the szdomain parameter is specified (and then only this catalog/domain is searched).

returns original string if translation is not available (in this case an error message is generated the first time a string is not found; use wxlognull to suppress it).

the second form is used when retrieving translation of string that has different singular and plural form in english or different plural forms in some other language. it takes two extra arguments: szorigstring parameter must contain the singular form of the string to be converted. it is also used as the key for the search in the catalog. the szorigstring2 parameter is the plural form (in english). the parameter n is used to determine the plural form. if no message catalog is found szorigstring is returned if 'n == 1', otherwise szorigstring2. see gnu gettext manual for additional information on plural forms handling.

this method is called by the wxgettranslation function and _() macro.

remarks

domains are searched in the last to first order, i.e. catalogs added later override those added before.


wxlocale::getheadervalue

wxstring getheadervalue(const char *szheader, const char *szdomain = null) const

returns the header value for header szheader. the search for szheader is case sensitive. if an szdomain is passed, this domain is searched. else all domains will be searched until a header has been found. the return value is the value of the header if found. else this will be empty.


wxlocale::getsysname

wxstring getsysname() const

returns current platform-specific locale name as passed to setlocale().

compare getcanonicalname.


wxlocale::getsystemencoding

static wxfontencoding getsystemencoding() const

tries to detect the user's default font encoding. returns wxfontencoding value or wxfontencoding_system if it couldn't be determined.


wxlocale::getsystemencodingname

static wxstring getsystemencodingname() const

tries to detect the name of the user's default font encoding. this string isn't particularly useful for the application as its form is platform-dependent and so you should probably use getsystemencoding instead.

returns a user-readable string value or an empty string if it couldn't be determined.


wxlocale::getsystemlanguage

static int getsystemlanguage() const

tries to detect the user's default language setting. returns wxlanguage value or wxlanguage_unknown if the language-guessing algorithm failed.


wxlocale::init

bool init(int language = wxlanguage_default, int flags = wxlocale_load_default | wxlocale_conv_encoding)

bool init(const char *szname, const char *szshort = null, const char *szlocale = null, bool bloaddefault = true, bool bconvertencoding = false)

the second form is deprecated, use the first one unless you know what you are doing.

parameters

language

flags

szname

szshort

szlocale

bloaddefault

bconvertencoding

the call of this function has several global side effects which you should understand: first of all, the application locale is changed - note that this will affect many of standard c library functions such as printf() or strftime(). second, this wxlocale object becomes the new current global locale for the application and so all subsequent calls to wxgettranslation() will try to translate the messages using the message catalogs for this locale.

returns true on success or false if the given locale couldn't be set.


wxlocale::isavailable

static bool isavailable(int lang)

check whether the operating system and/or c run time environment supports this locale. for example in windows 2000 and windows xp, support for many locales is not installed by default. returns true if the locale is supported.

the argument lang is the wxlanguage identifier. to obtain this for a given a two letter iso language code, use findlanguageinfo to obtain its wxlanguageinfo structure. see addlanguage for the wxlanguageinfo description.

this function is new since wxwidgets version 2.7.1.


wxlocale::isloaded

bool isloaded(const char* domain) const

check if the given catalog is loaded, and returns true if it is.

according to gnu gettext tradition, each catalog normally corresponds to 'domain' which is more or less the application name.

see also: addcatalog


wxlocale::isok

bool isok() const

returns true if the locale could be set successfully.