]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/intl.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxLocale
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
12 wxLocale class encapsulates all language-dependent settings and is a
13 generalization of the C locale concept.
15 In wxWidgets this class manages message catalogs which contain the translations
16 of the strings used to the current language.
18 @b wxPerl note: In wxPerl you can't use the '_' function name, so
19 the @c Wx::Locale module can export the @c gettext and
20 @c gettext_noop under any given name.
23 # this imports gettext ( equivalent to Wx::GetTranslation
24 # and gettext_noop ( a noop )
26 use Wx::Locale qw(:default);
31 print gettext( "Panic!" );
33 button = Wx::Button-new( window, -1, gettext( "Label" ) );
36 If you need to translate a lot of strings, then adding gettext( ) around
37 each one is a long task ( that is why _( ) was introduced ), so just choose
38 a shorter name for gettext:
42 use Wx::Locale 'gettext' = 't',
43 'gettext_noop' = 'gettext_noop';
56 @see @ref overview_internationalization, @ref overview_sampleinternat "Internat
64 See Init() for parameters description.
65 The call of this function has several global side effects which you should
66 understand: first of all, the application locale is changed - note that this
67 will affect many of standard C library functions such as printf() or strftime().
68 Second, this wxLocale object becomes the new current global locale for the
69 application and so all subsequent calls to wxGetTranslation() will try to
70 translate the messages using the message catalogs for this locale.
73 wxLocale(int language
,
75 wxLOCALE_LOAD_DEFAULT
| wxLOCALE_CONV_ENCODING
);
76 wxLocale(const wxString
& name
,
77 const wxString
& short = wxEmptyString
,
78 const wxString
& locale
= wxEmptyString
,
79 bool bLoadDefault
= true,
80 bool bConvertEncoding
= false);
84 The destructor, like the constructor, also has global side effects: the
86 set locale is restored and so the changes described in
87 Init() documentation are rolled back.
93 Add a catalog for use with the current locale: it is searched for in standard
94 places (current directory first, then the system one), but you may also prepend
95 additional directories to the search path with
96 AddCatalogLookupPathPrefix().
97 All loaded catalogs will be used for message lookup by
98 GetString() for the current locale.
99 Returns @true if catalog was successfully loaded, @false otherwise (which might
100 mean that the catalog is not found or that it isn't in the correct format).
101 The second form of this method takes two additional arguments,
102 @a msgIdLanguage and @e msgIdCharset.
103 @a msgIdLanguage specifies the language of "msgid" strings in source code
104 (i.e. arguments to GetString(),
105 wxGetTranslation() and the
106 _() macro). It is used if AddCatalog cannot find any
107 catalog for current language: if the language is same as source code language,
108 then strings from source code are used instead.
109 @a msgIdCharset lets you specify the charset used for msgids in sources
110 in case they use 8-bit characters (e.g. German or French strings). This
111 argument has no effect in Unicode build, because literals in sources are
112 Unicode strings; you have to use compiler-specific method of setting the right
113 charset when compiling with Unicode.
114 By default (i.e. when you use the first form), msgid strings are assumed
115 to be in English and written only using 7-bit ASCII characters.
116 If you have to deal with non-English strings or 8-bit characters in the source
117 code, see the instructions in
118 @ref overview_nonenglishoverview "Writing non-English applications".
120 bool AddCatalog(const wxString
& domain
);
121 bool AddCatalog(const wxString
& domain
,
122 wxLanguage msgIdLanguage
,
123 const wxString
& msgIdCharset
);
127 Add a prefix to the catalog lookup path: the message catalog files will be
128 looked up under prefix/lang/LC_MESSAGES, prefix/lang and prefix
130 This only applies to subsequent invocations of AddCatalog().
132 void AddCatalogLookupPathPrefix(const wxString
& prefix
);
135 Adds custom, user-defined language to the database of known languages. This
136 database is used in conjunction with the first form of
138 wxLanguageInfo is defined as follows:
139 @e Language should be greater than wxLANGUAGE_USER_DEFINED.
140 Wx::LanguageInfo-new( language, canonicalName, WinLang, WinSubLang, Description
143 static void AddLanguage(const wxLanguageInfo
& info
);
146 This function may be used to find the language description structure for the
147 given locale, specified either as a two letter ISO language code (for example,
148 "pt"), a language code followed by the country code ("pt_BR") or a full, human
149 readable, language description ("Portuguese-Brazil").
150 Returns the information for the given language or @NULL if this language
151 is unknown. Note that even if the returned pointer is valid, the caller should
154 @see GetLanguageInfo()
156 static wxLanguageInfo
* FindLanguageInfo(const wxString
& locale
);
159 Returns the canonical form of current locale name. Canonical form is the
160 one that is used on UNIX systems: it is a two- or five-letter string in xx or
161 xx_YY format, where xx is ISO 639 code of language and YY is ISO 3166 code of
162 the country. Examples are "en", "en_GB", "en_US" or "fr_FR".
163 This form is internally used when looking up message catalogs.
164 Compare GetSysName().
166 wxString
GetCanonicalName() const;
169 Returns the header value for header @e header. The search for @a header is case
170 sensitive. If an @e domain
171 is passed, this domain is searched. Else all domains will be searched until a
172 header has been found.
173 The return value is the value of the header if found. Else this will be empty.
175 wxString
GetHeaderValue(const wxString
& header
,
176 const wxString
& domain
= wxEmptyString
) const;
179 Returns wxLanguage() constant of current language.
180 Note that you can call this function only if you used the form of
181 Init() that takes wxLanguage argument.
183 int GetLanguage() const;
186 Returns a pointer to wxLanguageInfo structure containing information about the
187 given language or @NULL if this language is unknown. Note that even if the
188 returned pointer is valid, the caller should @e not delete it.
189 See AddLanguage() for the wxLanguageInfo
191 As with Init(), @c wxLANGUAGE_DEFAULT has the
192 special meaning if passed as an argument to this function and in this case the
193 result of GetSystemLanguage() is used.
195 static wxLanguageInfo
* GetLanguageInfo(int lang
) const;
198 Returns English name of the given language or empty string if this
200 See GetLanguageInfo() for a remark about
201 special meaning of @c wxLANGUAGE_DEFAULT.
203 static wxString
GetLanguageName(int lang
) const;
206 Returns the locale name as passed to the constructor or
207 Init(). This is full, human-readable name,
208 e.g. "English" or "French".
210 const wxString
GetLocale() const;
213 Returns the current short name for the locale (as given to the constructor or
214 the Init() function).
216 const wxString
GetName() const;
220 Retrieves the translation for a string in all loaded domains unless the szDomain
221 parameter is specified (and then only this catalog/domain is searched).
222 Returns original string if translation is not available
223 (in this case an error message is generated the first time
224 a string is not found; use wxLogNull to suppress it).
225 The second form is used when retrieving translation of string that has
226 different singular and plural form in English or different plural forms in some
227 other language. It takes two extra arguments: @e origString
228 parameter must contain the singular form of the string to be converted.
229 It is also used as the key for the search in the catalog.
230 The @a origString2 parameter is the plural form (in English).
231 The parameter @a n is used to determine the plural form. If no
232 message catalog is found @a origString is returned if 'n == 1',
233 otherwise @e origString2.
234 See GNU gettext manual for additional information on plural forms handling.
235 This method is called by the wxGetTranslation()
236 function and _() macro.
238 @remarks Domains are searched in the last to first order, i.e. catalogs
239 added later override those added before.
241 const wxString
GetString(const wxString
& origString
,
242 const wxString
& domain
= wxEmptyString
) const;
243 const const wxString
& GetString(const wxString
& origString
,
244 const wxString
& origString2
,
246 const wxString
& domain
= NULL
) const;
250 Returns current platform-specific locale name as passed to setlocale().
251 Compare GetCanonicalName().
253 wxString
GetSysName() const;
256 Tries to detect the user's default font encoding.
257 Returns wxFontEncoding() value or
258 @b wxFONTENCODING_SYSTEM if it couldn't be determined.
260 static wxFontEncoding
GetSystemEncoding() const;
263 Tries to detect the name of the user's default font encoding. This string isn't
264 particularly useful for the application as its form is platform-dependent and
265 so you should probably use
266 GetSystemEncoding() instead.
267 Returns a user-readable string value or an empty string if it couldn't be
270 static wxString
GetSystemEncodingName() const;
273 Tries to detect the user's default language setting.
274 Returns wxLanguage() value or
275 @b wxLANGUAGE_UNKNOWN if the language-guessing algorithm failed.
277 static int GetSystemLanguage() const;
281 The second form is deprecated, use the first one unless you know what you are
285 wxLanguage identifier of the locale.
286 wxLANGUAGE_DEFAULT has special meaning -- wxLocale will use system's
288 language (see GetSystemLanguage).
290 Combination of the following:
298 wxLOCALE_LOAD_DEFAULT
303 Load the message catalog
304 for the given locale containing the translations of standard wxWidgets
312 wxLOCALE_CONV_ENCODING
317 Automatically convert message
318 catalogs to platform's default encoding. Note that it will do only basic
319 conversion between well-known pair like iso8859-1 and windows-1252 or
320 iso8859-2 and windows-1250. See Writing non-English applications for
322 description of this behaviour. Note that this flag is meaningless in
325 The name of the locale. Only used in diagnostic messages.
327 The standard 2 letter locale abbreviation; it is used as the
328 directory prefix when looking for the message catalog files.
330 The parameter for the call to setlocale(). Note that it is
333 May be set to @false to prevent loading of the message catalog
334 for the given locale containing the translations of standard wxWidgets
336 This parameter would be rarely used in normal circumstances.
337 @param bConvertEncoding
338 May be set to @true to do automatic conversion of message
339 catalogs to platform's native encoding. Note that it will do only basic
340 conversion between well-known pair like iso8859-1 and windows-1252 or
341 iso8859-2 and windows-1250.
342 See Writing non-English applications for detailed
343 description of this behaviour.
345 bool Init(int language
= wxLANGUAGE_DEFAULT
,
347 wxLOCALE_LOAD_DEFAULT
| wxLOCALE_CONV_ENCODING
);
348 bool Init(const wxString
& name
,
349 const wxString
& short = wxEmptyString
,
350 const wxString
& locale
= wxEmptyString
,
351 bool bLoadDefault
= true,
352 bool bConvertEncoding
= false);
356 Check whether the operating system and/or C run time environment supports
357 this locale. For example in Windows 2000 and Windows XP, support for many
358 locales is not installed by default. Returns @true if the locale is
360 The argument @a lang is the wxLanguage identifier. To obtain this for a
361 given a two letter ISO language code, use
362 FindLanguageInfo() to obtain its
363 wxLanguageInfo structure. See AddLanguage() for
364 the wxLanguageInfo description.
368 static bool IsAvailable(int lang
);
371 Check if the given catalog is loaded, and returns @true if it is.
372 According to GNU gettext tradition, each catalog
373 normally corresponds to 'domain' which is more or less the application name.
374 See also: AddCatalog()
376 bool IsLoaded(const char* domain
) const;
379 Returns @true if the locale could be set successfully.
384 See @ref overview_languagecodes "list of recognized language constants".
385 These constants may be used to specify the language
386 in Init() and are returned by
398 wxXLocale::GetCLocale
404 This class represents a locale object used by so-called xlocale API. Unlike
405 wxLocale it doesn't provide any non-trivial operations but
406 simply provides a portable wrapper for POSIX @c locale_t type. It exists
407 solely to be provided as an argument to various @c wxFoo_l() functions
408 which are the extensions of the standard locale-dependent functions (hence the
409 name xlocale). These functions do exactly the same thing as the corresponding
410 standard @c foo() except that instead of using the global program locale
411 they use the provided wxXLocale object. For example, if the user runs the
412 program in French locale, the standard @c printf() function will output
413 floating point numbers using decimal comma instead of decimal period. If the
414 program needs to format a floating-point number in a standard format it can
415 use @c wxPrintf_l(wxXLocale::GetCLocale(), "%g", number) to do it.
416 Conversely, if a program wanted to output the number in French locale, even if
417 the current locale is different, it could use wxXLocale(wxLANGUAGE_FRENCH).
422 This class is fully implemented only under the platforms where xlocale POSIX
423 API or equivalent is available. Currently the xlocale API is available under
424 most of the recent Unix systems (including Linux, various BSD and Mac OS X) and
425 Microsoft Visual C++ standard library provides a similar API starting from
426 version 8 (Visual Studio 2005).
428 If neither POSIX API nor Microsoft proprietary equivalent are available, this
429 class is still available but works in degraded mode: the only supported locale
430 is the C one and attempts to create wxXLocale object for any other locale will
431 fail. You can use the preprocessor macro @c wxHAS_XLOCALE_SUPPORT to
432 test if full xlocale API is available or only skeleton C locale support is
435 Notice that wxXLocale is new in wxWidgets 2.9.0 and is not compiled in if
436 @c wxUSE_XLOCALE was set to 0 during the library compilation.
439 Locale-dependent functions
441 Currently the following @c _l-functions are available:
443 Character classification functions: @c wxIsxxx_l(), e.g.
444 @c wxIsalpha_l(), @c wxIslower_l() and all the others.
445 Character transformation functions: @c wxTolower_l() and
448 We hope to provide many more functions (covering numbers, time and formatted
449 IO) in the near future.
461 Creates the locale object corresponding to the specified locale string. The
462 locale string is system-dependent, use constructor taking wxLanguage for better
466 wxLocale(wxLanguage lang
);
467 wxLocale(const char* loc
);
471 This class is fully implemented only under the platforms where xlocale POSIX
472 API or equivalent is available. Currently the xlocale API is available under
473 most of the recent Unix systems (including Linux, various BSD and Mac OS X) and
474 Microsoft Visual C++ standard library provides a similar API starting from
475 version 8 (Visual Studio 2005).
476 If neither POSIX API nor Microsoft proprietary equivalent are available, this
477 class is still available but works in degraded mode: the only supported locale
478 is the C one and attempts to create wxXLocale object for any other locale will
479 fail. You can use the preprocessor macro @c wxHAS_XLOCALE_SUPPORT to
480 test if full xlocale API is available or only skeleton C locale support is
482 Notice that wxXLocale is new in wxWidgets 2.9.0 and is not compiled in if
483 @c wxUSE_XLOCALE was set to 0 during the library compilation.
488 Returns the global object representing the "C" locale. For an even shorter
489 access to this object a global @c wxCLocale variable (implemented as a
490 macro) is provided and can be used instead of calling this method.
492 static wxXLocale
GetCLocale();
495 This class represents a locale object used by so-called xlocale API. Unlike
496 wxLocale it doesn't provide any non-trivial operations but
497 simply provides a portable wrapper for POSIX @c locale_t type. It exists
498 solely to be provided as an argument to various @c wxFoo_l() functions
499 which are the extensions of the standard locale-dependent functions (hence the
500 name xlocale). These functions do exactly the same thing as the corresponding
501 standard @c foo() except that instead of using the global program locale
502 they use the provided wxXLocale object. For example, if the user runs the
503 program in French locale, the standard @c printf() function will output
504 floating point numbers using decimal comma instead of decimal period. If the
505 program needs to format a floating-point number in a standard format it can
506 use @c wxPrintf_l(wxXLocale::GetCLocale(), "%g", number) to do it.
507 Conversely, if a program wanted to output the number in French locale, even if
508 the current locale is different, it could use wxXLocale(wxLANGUAGE_FRENCH).
513 Returns @true if this object is initialized, i.e. represents a valid locale
520 Currently the following @c _l-functions are available:
521 Character classification functions: @c wxIsxxx_l(), e.g.
522 @c wxIsalpha_l(), @c wxIslower_l() and all the others.
523 Character transformation functions: @c wxTolower_l() and
525 We hope to provide many more functions (covering numbers, time and formatted
526 IO) in the near future.
534 // ============================================================================
535 // Global functions/macros
536 // ============================================================================
538 /** @ingroup group_funcmacro_string */
542 This macro is identical to _() but for the plural variant of
545 @return A const wxString.
549 #define wxPLURAL(string, plural, n)
552 This macro doesn't do anything in the program code -- it simply expands to
553 the value of its argument.
555 However it does have a purpose which is to mark the literal strings for the
556 extraction into the message catalog created by @c xgettext program. Usually
557 this is achieved using _() but that macro not only marks the string for
558 extraction but also expands into a wxGetTranslation() call which means that
559 it cannot be used in some situations, notably for static array
562 Here is an example which should make it more clear: suppose that you have a
563 static array of strings containing the weekday names and which have to be
564 translated (note that it is a bad example, really, as wxDateTime already
565 can be used to get the localized week day names already). If you write:
568 static const char * const weekdays[] = { _("Mon"), ..., _("Sun") };
570 // use weekdays[n] as usual
573 The code wouldn't compile because the function calls are forbidden in the
574 array initializer. So instead you should do this:
577 static const char * const weekdays[] = { wxTRANSLATE("Mon"), ...,
578 wxTRANSLATE("Sun") };
580 // use wxGetTranslation(weekdays[n])
583 Note that although the code @b would compile if you simply omit
584 wxTRANSLATE() in the above, it wouldn't work as expected because there
585 would be no translations for the weekday names in the program message
586 catalog and wxGetTranslation() wouldn't find them.
588 @return A const wxChar*.
592 #define wxTRANSLATE(string)
595 This function returns the translation of @a string in the current
596 @c locale(). If the string is not found in any of the loaded message
597 catalogs (see @ref overview_i18n), the original string is returned. In
598 debug build, an error message is logged -- this should help to find the
599 strings which were not yet translated. If @a domain is specified then only
600 that domain/catalog is searched for a matching string. As this function is
601 used very often, an alternative (and also common in Unix world) syntax is
602 provided: the _() macro is defined to do the same thing as
605 This function calls wxLocale::GetString().
607 @note This function is not suitable for literal strings in Unicode builds
608 since the literal strings must be enclosed into _T() or wxT() macro
609 which makes them unrecognised by @c xgettext, and so they are not
610 extracted to the message catalog. Instead, use the _() and wxPLURAL()
611 macro for all literal strings.
613 @see wxGetTranslation(const wxString&, const wxString&, size_t, const wxString&)
617 const wxString
wxGetTranslation(const wxString
& string
,
618 const wxString
& domain
= wxEmptyString
);
621 This is an overloaded version of
622 wxGetTranslation(const wxString&, const wxString&), please see its
623 documentation for general information.
625 This version is used when retrieving translation of string that has
626 different singular and plural forms in English or different plural forms in
627 some other language. Like wxGetTranslation(const wxString&,const wxString&),
628 the @a string parameter must contain the singular form of the string to be
629 converted and is used as the key for the search in the catalog. The
630 @a plural parameter is the plural form (in English). The parameter @a n is
631 used to determine the plural form. If no message catalog is found,
632 @a string is returned if "n == 1", otherwise @a plural is returned.
634 See GNU gettext Manual for additional information on plural forms handling:
635 <http://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms>
636 For a shorter alternative see the wxPLURAL() macro.
638 This function calls wxLocale::GetString().
642 const wxString
wxGetTranslation(const wxString
& string
,
643 const wxString
& plural
, size_t n
,
644 const wxString
& domain
= wxEmptyString
);
647 This macro expands into a call to wxGetTranslation(), so it marks the
648 message for the extraction by @c xgettext just as wxTRANSLATE() does, but
649 also returns the translation of the string for the current locale during
652 Don't confuse this with _T()!
656 const wxString
_(const wxString
& string
);