.. include:: headings.inc .. _Translations: ========================================================================================================================================== |phoenix_title| **Translations** ========================================================================================================================================== This class allows to get translations for strings. In wxWidgets this class manages message catalogs which contain the translations of the strings used to the current language. Unlike :ref:`Locale`, it isn't bound to locale. It can be used either independently of, or in conjunction with :ref:`Locale`. In the latter case, you should initialize :ref:`Locale` (which creates :ref:`Translations` instance) first; in the former, you need to create a :ref:`Translations` object and :meth:`~Translations.Set` it manually. Only one :ref:`Translations` instance is active at a time; it is set with the :meth:`~Translations.Set` method and obtained using :meth:`~Translations.Get`. Unlike :ref:`Locale`, Translations' primary mean of identifying language is by its "canonical name", i.e. ``ISO`` 639 code, possibly combined with ``ISO`` 3166 country code and additional modifiers (examples include "fr", "en_GB" or "ca@valencia"; see :meth:`Locale.GetCanonicalName` for more information). This allows apps using :ref:`Translations` API to use even languages not recognized by the operating system or not listed in Language enum. .. versionadded:: 2.9.1 .. seealso:: :ref:`Locale` | |class_hierarchy| Inheritance Diagram ===================================== Inheritance diagram for class **Translations** .. raw:: html

Inheritance diagram of Translations

| |method_summary| Methods Summary ================================ ================================================================================ ================================================================================ :meth:`~Translations.__init__` Constructor. :meth:`~Translations.AddCatalog` Add a catalog for use with the current locale. :meth:`~Translations.AddStdCatalog` Add standard wxWidgets catalogs ("wxstd" and possible port-specific catalogs). :meth:`~Translations.Get` Returns current translations object, may return ``None``. :meth:`~Translations.GetAvailableTranslations` Returns list of all translations of `domain` that were found. :meth:`~Translations.GetHeaderValue` Returns the header value for header `header`. :meth:`~Translations.GetString` Retrieves the translation for a string in all loaded domains unless the `domain` parameter is specified (and then only this catalog/domain is searched). :meth:`~Translations.IsLoaded` Check if the given catalog is loaded, and returns ``True`` if it is. :meth:`~Translations.Set` Sets current translations object. :meth:`~Translations.SetLanguage` Sets translations language to use. :meth:`~Translations.SetLoader` Changes loader use to read catalogs to a non-default one. ================================================================================ ================================================================================ | |api| Class API =============== .. class:: Translations(object) This class allows to get translations for strings. **Possible constructors**:: Translations() .. method:: __init__(self) Constructor. .. method:: AddCatalog(self, *args, **kw) |overload| **Overloaded Implementations**: **~~~** **AddCatalog** `(self, domain)` Add a catalog for use with the current locale. By default, it is searched for in standard places (see :ref:`FileTranslationsLoader`), but you may also prepend additional directories to the search path with :meth:`FileTranslationsLoader.AddCatalogLookupPathPrefix` . All loaded catalogs will be used for message lookup by :meth:`GetString` for the current locale. In this overload, ``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 :ref:`Writing Non-English Applications `. :param `domain`: :type `domain`: string :rtype: `bool` :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). **~~~** **AddCatalog** `(self, domain, msgIdLanguage)` Same as :meth:`AddCatalog` , but takes an additional argument, `msgIdLanguage`. :param `domain`: The catalog domain to add. :type `domain`: string :param `msgIdLanguage`: Specifies the language of "msgid" strings in source code (i.e. arguments to :meth:`GetString` , :func:`GetTranslation` and the :func:`_` macro). It is used if :meth:`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. :type `msgIdLanguage`: Language :rtype: `bool` :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). **~~~** .. method:: AddStdCatalog(self) Add standard wxWidgets catalogs ("wxstd" and possible port-specific catalogs). :rtype: `bool` :returns: ``True`` if a suitable catalog was found, ``False`` otherwise .. seealso:: :meth:`AddCatalog` .. staticmethod:: Get() Returns current translations object, may return ``None``. You must either call this early in app initialization code, or let :ref:`Locale` do it for you. :rtype: :ref:`Translations` .. method:: GetAvailableTranslations(self, domain) Returns list of all translations of `domain` that were found. This method can be used e.g. to populate list of application's translations offered to the user. To do this, pass the app's main catalog as `domain`. :param `domain`: :type `domain`: string :rtype: `list of strings` .. method:: GetHeaderValue(self, header, domain='') Returns the header value for header `header`. The search for `header` is case sensitive. If an `domain` 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. :param `header`: :type `header`: string :param `domain`: :type `domain`: string :rtype: `string` .. method:: GetString(self, *args, **kw) |overload| **Overloaded Implementations**: **~~~** **GetString** `(self, origString, domain='')` Retrieves the translation for a string in all loaded domains unless the `domain` 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 :ref:`LogNull` to suppress it). :param `origString`: :type `origString`: string :param `domain`: :type `domain`: string :rtype: `string` .. note:: Domains are searched in the last to first order, i.e. catalogs added later override those added before. **~~~** **GetString** `(self, origString, origString2, n, domain='')` Retrieves the translation for a string in all loaded domains unless the `domain` 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 :ref:`LogNull` to suppress it). This 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: `origString` 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 `origString2` parameter is the plural form (in English). The parameter `n` is used to determine the plural form. If no message catalog is found `origString` is returned if 'n == 1', otherwise `origString2`. See ``GNU`` gettext manual for additional information on plural forms handling. This method is called by the :func:`GetTranslation` function and :func:`_` macro. :param `origString`: :type `origString`: string :param `origString2`: :type `origString2`: string :param `n`: :param `domain`: :type `domain`: string :rtype: `string` .. note:: Domains are searched in the last to first order, i.e. catalogs added later override those added before. **~~~** .. method:: IsLoaded(self, domain) 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. :param `domain`: :type `domain`: string :rtype: `bool` .. seealso:: :meth:`AddCatalog` .. staticmethod:: Set(t) Sets current translations object. Deletes previous translation object and takes ownership of `t`. :param `t`: :type `t`: Translations .. method:: SetLanguage(self, *args, **kw) |overload| **Overloaded Implementations**: **~~~** **SetLanguage** `(self, lang)` Sets translations language to use. ``LANGUAGE_DEFAULT`` has special meaning: best suitable translation, given user's preference and available translations, will be used. :param `lang`: :type `lang`: Language **~~~** **SetLanguage** `(self, lang)` Sets translations language to use. Empty `lang` string has the same meaning as ``LANGUAGE_DEFAULT`` in :meth:`SetLanguage` : best suitable translation, given user's preference and available translations, will be used. :param `lang`: :type `lang`: string **~~~** .. method:: SetLoader(self, loader) Changes loader use to read catalogs to a non-default one. Deletes previous loader and takes ownership of `loader`. :param `loader`: :type `loader`: TranslationsLoader .. seealso:: :ref:`TranslationsLoader`, :ref:`FileTranslationsLoader`, `ResourceTranslationsLoader`