1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTranslation class
4 // Author: wxWidgets team
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
10 This class allows to get translations for strings.
12 In wxWidgets this class manages message catalogs which contain the
13 translations of the strings used to the current language. Unlike wxLocale,
14 it isn't bound to locale. It can be used either independently of, or in
15 conjunction with wxLocale. In the latter case, you should initialize
16 wxLocale (which creates wxTranslations instance) first; in the former, you
17 need to create a wxTranslations object and Set() it manually.
19 Only one wxTranslations instance is active at a time; it is set with the
20 Set() method and obtained using Get().
22 Unlike wxLocale, wxTranslations' primary mean of identifying language
23 is by its "canonical name", i.e. ISO 639 code, possibly combined with
24 ISO 3166 country code and additional modifiers (examples include
25 "fr", "en_GB" or "ca@valencia"; see wxLocale::GetCanonicalName() for
26 more information). This allows apps using wxTranslations API to use even
27 languages not recognized by the operating system or not listed in
32 @see wxLocale, wxTranslationsLoader, wxFileTranslationsLoader
41 Returns current translations object, may return NULL.
43 You must either call this early in app initialization code, or let
44 wxLocale do it for you.
46 static wxTranslations
*Get();
49 Sets current translations object.
51 Deletes previous translation object and takes ownership of @a t.
53 static void Set(wxTranslations
*t
);
56 Changes loader use to read catalogs to a non-default one.
58 Deletes previous loader and takes ownership of @a loader.
60 @see wxTranslationsLoader, wxFileTranslationsLoader, wxResourceTranslationsLoader
62 void SetLoader(wxTranslationsLoader
*loader
);
65 Sets translations language to use.
67 wxLANGUAGE_DEFAULT has special meaning: best suitable translation,
68 given user's preference and available translations, will be used.
70 void SetLanguage(wxLanguage lang
);
73 Sets translations language to use.
75 Empty @a lang string has the same meaning as wxLANGUAGE_DEFAULT in
76 SetLanguage(wxLanguage): best suitable translation, given user's
77 preference and available translations, will be used.
79 void SetLanguage(const wxString
& lang
);
82 Returns list of all translations of @a domain that were found.
84 This method can be used e.g. to populate list of application's
85 translations offered to the user. To do this, pass the app's main
88 @see GetBestTranslation()
90 wxArrayString
GetAvailableTranslations(const wxString
& domain
) const;
93 Returns the best UI language for the @a domain.
95 The language is determined from the preferred UI language or languages
96 list the user configured in the OS. Notice that this may or may not
97 correspond to the default @em locale as obtained from
98 wxLocale::GetSystemLanguage(); modern operation systems (Windows
99 Vista+, OS X) have separate language and regional (= locale) settings.
102 The catalog domain to look for.
105 Specifies the language of "msgid" strings in source code
106 (i.e. arguments to GetString(), wxGetTranslation() and the _() macro).
108 @return Language code if a suitable match was found, empty string
113 wxString
GetBestTranslation(const wxString
& domain
, wxLanguage msgIdLanguage
);
116 Returns the best UI language for the @a domain.
118 The language is determined from the preferred UI language or languages
119 list the user configured in the OS. Notice that this may or may not
120 correspond to the default @em locale as obtained from
121 wxLocale::GetSystemLanguage(); modern operation systems (Windows
122 Vista+, OS X) have separate language and regional (= locale) settings.
125 The catalog domain to look for.
128 Specifies the language of "msgid" strings in source code
129 (i.e. arguments to GetString(), wxGetTranslation() and the _() macro).
131 @return Language code if a suitable match was found, empty string
136 wxString
GetBestTranslation(const wxString
& domain
,
137 const wxString
& msgIdLanguage
= "en");
140 Add standard wxWidgets catalogs ("wxstd" and possible port-specific
143 @return @true if a suitable catalog was found, @false otherwise
147 bool AddStdCatalog();
150 Add a catalog for use with the current locale.
152 By default, it is searched for in standard places (see
153 wxFileTranslationsLoader), but you may also prepend additional
154 directories to the search path with
155 wxFileTranslationsLoader::AddCatalogLookupPathPrefix().
157 All loaded catalogs will be used for message lookup by GetString() for
160 In this overload, @c msgid strings are assumed
161 to be in English and written only using 7-bit ASCII characters.
162 If you have to deal with non-English strings or 8-bit characters in the
163 source code, see the instructions in @ref overview_nonenglish.
166 @true if catalog was successfully loaded, @false otherwise (which might
167 mean that the catalog is not found or that it isn't in the correct format).
169 bool AddCatalog(const wxString
& domain
);
172 Same as AddCatalog(const wxString&), but takes an additional argument,
176 The catalog domain to add.
179 Specifies the language of "msgid" strings in source code
180 (i.e. arguments to GetString(), wxGetTranslation() and the _() macro).
181 It is used if AddCatalog() cannot find any catalog for current language:
182 if the language is same as source code language, then strings from source
183 code are used instead.
186 @true if catalog was successfully loaded, @false otherwise (which might
187 mean that the catalog is not found or that it isn't in the correct format).
189 bool AddCatalog(const wxString
& domain
, wxLanguage msgIdLanguage
);
192 Same as AddCatalog(const wxString&, wxLanguage), but takes two
193 additional arguments, @a msgIdLanguage and @a msgIdCharset.
195 This overload is only available in non-Unicode build.
198 The catalog domain to add.
201 Specifies the language of "msgid" strings in source code
202 (i.e. arguments to GetString(), wxGetTranslation() and the _() macro).
203 It is used if AddCatalog() cannot find any catalog for current language:
204 if the language is same as source code language, then strings from source
205 code are used instead.
208 Lets you specify the charset used for msgids in sources
209 in case they use 8-bit characters (e.g. German or French strings).
212 @true if catalog was successfully loaded, @false otherwise (which might
213 mean that the catalog is not found or that it isn't in the correct format).
215 bool AddCatalog(const wxString
& domain
,
216 wxLanguage msgIdLanguage
,
217 const wxString
& msgIdCharset
);
220 Check if the given catalog is loaded, and returns @true if it is.
222 According to GNU gettext tradition, each catalog normally corresponds to
223 'domain' which is more or less the application name.
227 bool IsLoaded(const wxString
& domain
) const;
230 Retrieves the translation for a string in all loaded domains unless the @a domain
231 parameter is specified (and then only this catalog/domain is searched).
233 Returns original string if translation is not available (in this case an
234 error message is generated the first time a string is not found; use
235 wxLogNull to suppress it).
237 @remarks Domains are searched in the last to first order, i.e. catalogs
238 added later override those added before.
240 const wxString
& GetString(const wxString
& origString
,
241 const wxString
& domain
= wxEmptyString
) const;
244 Retrieves the translation for a string in all loaded domains unless the @a domain
245 parameter is specified (and then only this catalog/domain is searched).
247 Returns original string if translation is not available (in this case an
248 error message is generated the first time a string is not found; use
249 wxLogNull to suppress it).
251 This form is used when retrieving translation of string that has different
252 singular and plural form in English or different plural forms in some
254 It takes two extra arguments: @a origString parameter must contain the
255 singular form of the string to be converted.
257 It is also used as the key for the search in the catalog.
258 The @a origString2 parameter is the plural form (in English).
260 The parameter @a n is used to determine the plural form.
261 If no message catalog is found @a origString is returned if 'n == 1',
262 otherwise @a origString2.
264 See GNU gettext manual for additional information on plural forms handling.
265 This method is called by the wxGetTranslation() function and _() macro.
267 @remarks Domains are searched in the last to first order, i.e. catalogs
268 added later override those added before.
270 const wxString
& GetString(const wxString
& origString
,
271 const wxString
& origString2
,
273 const wxString
& domain
= wxEmptyString
) const;
276 Returns the header value for header @a header.
277 The search for @a header is case sensitive. If an @a domain is passed,
278 this domain is searched. Else all domains will be searched until a
279 header has been found.
281 The return value is the value of the header if found. Else this will be empty.
283 wxString
GetHeaderValue(const wxString
& header
,
284 const wxString
& domain
= wxEmptyString
) const;
289 Abstraction of translations discovery and loading.
291 This interface makes it possible to override wxWidgets' default catalogs
292 loading mechanism and load MO files from locations other than the
293 filesystem (e.g. embed them in executable).
295 Implementations must implement the LoadCatalog() method.
297 @see wxFileTranslationsLoader, wxResourceTranslationsLoader
301 class wxTranslationsLoader
304 /// Trivial default constructor.
305 wxTranslationsLoader();
308 Called to load requested catalog.
310 If the catalog is found, LoadCatalog() should create wxMsgCatalog
311 instance with its data and return it. The caller will take ownership
314 @param domain Domain to load.
315 @param lang Language to look for. This is "canonical name"
316 (see wxLocale::GetCanonicalName()), i.e. ISO 639
317 code, possibly combined with country code or
318 additional modifiers (e.g. "fr", "en_GB" or
321 @return Loaded catalog or NULL on failure.
323 virtual wxMsgCatalog
*LoadCatalog(const wxString
& domain
,
324 const wxString
& lang
) = 0;
327 Implements wxTranslations::GetAvailableTranslations().
329 virtual wxArrayString
GetAvailableTranslations(const wxString
& domain
) const = 0;
333 Standard wxTranslationsLoader implementation.
335 This finds catalogs in the filesystem, using the standard Unix layout.
336 This is the default unless you change the loader with
337 wxTranslations::SetLoader().
339 Catalogs are searched for in standard places (system locales directory,
340 `LC_PATH` on Unix systems, Resources subdirectory of the application bundle
341 on OS X, executable's directory on Windows), but you may also prepend
342 additional directories to the search path with
343 AddCatalogLookupPathPrefix().
347 class wxFileTranslationsLoader
: public wxTranslationsLoader
351 Add a prefix to the catalog lookup path: the message catalog files will
352 be looked up under prefix/lang/LC_MESSAGES and prefix/lang directories
355 This only applies to subsequent invocations of
356 wxTranslations::AddCatalog().
358 static void AddCatalogLookupPathPrefix(const wxString
& prefix
);
362 This loader makes it possible to load translations from Windows
365 If you wish to store translation MO files in resources, you have to
366 enable this loader before calling wxTranslations::AddCatalog() or
367 wxLocale::AddCatalog():
370 wxTranslations::Get()->SetLoader(new wxResourceTranslationsLoader);
373 Translations are stored in resources as compiled MO files, with type
374 set to "MOFILE" (unless you override GetResourceType()) and name
375 consisting of the domain, followed by underscore, followed by language
376 identification. For example, the relevant part of .rc file would look
380 myapp_de MOFILE "catalogs/de/myapp.mo"
381 myapp_fr MOFILE "catalogs/fr/myapp.mo"
382 myapp_en_GB MOFILE "catalogs/en_GB/myapp.mo"
385 This class is only available on Windows.
389 class wxResourceTranslationsLoader
: public wxTranslationsLoader
393 Returns resource type to use for translations.
395 Default type is "MOFILE".
397 virtual wxString
GetResourceType() const;
400 Returns handle of the module to load resources from.
402 By default, the main executable is used.
404 virtual WXHINSTANCE
GetModule() const;
409 Represents a loaded translations message catalog.
411 This class should only be used directly by wxTranslationsLoader
420 Creates catalog loaded from a MO file.
422 @param filename Path to the MO file to load.
423 @param domain Catalog's domain. This typically matches
426 @return Successfully loaded catalog or NULL on failure.
428 static wxMsgCatalog
*CreateFromFile(const wxString
& filename
,
429 const wxString
& domain
);
432 Creates catalog from MO file data in memory buffer.
434 @param data Data in MO file format.
435 @param domain Catalog's domain. This typically matches
438 @return Successfully loaded catalog or NULL on failure.
440 static wxMsgCatalog
*CreateFromData(const wxScopedCharBuffer
& data
,
441 const wxString
& domain
);
445 // ============================================================================
446 // Global functions/macros
447 // ============================================================================
449 /** @addtogroup group_funcmacro_string */
453 This macro is identical to _() but for the plural variant of
456 @return A const wxString.
460 #define wxPLURAL(string, plural, n)
463 This macro doesn't do anything in the program code -- it simply expands to
464 the value of its argument.
466 However it does have a purpose which is to mark the literal strings for the
467 extraction into the message catalog created by @c xgettext program. Usually
468 this is achieved using _() but that macro not only marks the string for
469 extraction but also expands into a wxGetTranslation() call which means that
470 it cannot be used in some situations, notably for static array
473 Here is an example which should make it more clear: suppose that you have a
474 static array of strings containing the weekday names and which have to be
475 translated (note that it is a bad example, really, as wxDateTime already
476 can be used to get the localized week day names already). If you write:
479 static const char * const weekdays[] = { _("Mon"), ..., _("Sun") };
481 // use weekdays[n] as usual
484 The code wouldn't compile because the function calls are forbidden in the
485 array initializer. So instead you should do this:
488 static const char * const weekdays[] = { wxTRANSLATE("Mon"), ...,
489 wxTRANSLATE("Sun") };
491 // use wxGetTranslation(weekdays[n])
494 Note that although the code @b would compile if you simply omit
495 wxTRANSLATE() in the above, it wouldn't work as expected because there
496 would be no translations for the weekday names in the program message
497 catalog and wxGetTranslation() wouldn't find them.
499 @return A const wxChar*.
503 #define wxTRANSLATE(string)
506 This function returns the translation of @a string in the current
507 @c locale(). If the string is not found in any of the loaded message
508 catalogs (see @ref overview_i18n), the original string is returned. In
509 debug build, an error message is logged -- this should help to find the
510 strings which were not yet translated. If @a domain is specified then only
511 that domain/catalog is searched for a matching string. As this function is
512 used very often, an alternative (and also common in Unix world) syntax is
513 provided: the _() macro is defined to do the same thing as
516 This function calls wxTranslations::GetString().
518 @note This function is not suitable for literal strings in Unicode builds
519 since the literal strings must be enclosed in wxT() macro which makes
520 them unrecognised by @c xgettext, and so they are not extracted to
521 the message catalog. Instead, use the _() and wxPLURAL() macro for
524 @see wxGetTranslation(const wxString&, const wxString&, unsigned, const wxString&)
528 const wxString
& wxGetTranslation(const wxString
& string
,
529 const wxString
& domain
= wxEmptyString
);
532 This is an overloaded version of
533 wxGetTranslation(const wxString&, const wxString&), please see its
534 documentation for general information.
536 This version is used when retrieving translation of string that has
537 different singular and plural forms in English or different plural forms in
538 some other language. Like wxGetTranslation(const wxString&,const wxString&),
539 the @a string parameter must contain the singular form of the string to be
540 converted and is used as the key for the search in the catalog. The
541 @a plural parameter is the plural form (in English). The parameter @a n is
542 used to determine the plural form. If no message catalog is found,
543 @a string is returned if "n == 1", otherwise @a plural is returned.
545 See GNU gettext Manual for additional information on plural forms handling:
546 <http://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms>
547 For a shorter alternative see the wxPLURAL() macro.
549 This function calls wxLocale::GetString().
553 const wxString
& wxGetTranslation(const wxString
& string
,
554 const wxString
& plural
, unsigned n
,
555 const wxString
& domain
= wxEmptyString
);
558 Macro to be used around all literal strings that should be translated.
560 This macro expands into a call to wxGetTranslation(), so it marks the
561 message for the extraction by @c xgettext just as wxTRANSLATE() does, but
562 also returns the translation of the string for the current locale during
567 const wxString
& _(const wxString
& string
);