File: translation.rst

package info (click to toggle)
python-translationstring 1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 248 kB
  • sloc: python: 797; makefile: 70
file content (72 lines) | stat: -rw-r--r-- 2,653 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
.. _translation_chapter:

Translation
===========

:mod:`translationstring` provides a function named
:func:`translationstring.Translator` which is used to create a
:term:`translator` object.

It is called like so:

.. code-block:: python
   :linenos:

   import gettext
   from translationstring import Translator
   translations = gettext.translations(.. the right arguments ...)
   translator = Translator(translations)

The ``translations`` argument is required; it should be an object
supporting *at least* the Python :class:`gettext.NullTranslations` API
but ideally the :class:`babel.support.Translations` API, which has
support for domain lookups like dugettext.

The callable returned accepts three arguments: a translation string
``tstring`` (required), ``domain`` (optional), and ``mapping``
(optional).  When called, it will translate the ``tstring``
translation string to a ``unicode`` object using the ``translations``
object provided and interpolate the result.

.. code-block:: python
   :linenos:

   from gettext import translations
   from translationstring import Translator
   from translationstring import TranslationString

   t = translations(.. the right arguments ...)
   translator = Translator(t)
   ts = TranslationString('Add ${number}', domain='foo', mapping={'number':1})
   translator(ts)

If ``translations`` is ``None``, the result of interpolation of the
msgid or default value of the translation string is returned.

The translation function can also deal with plain Unicode objects.
The optional ``domain`` argument can be used to specify or override
the domain of the ``tstring`` argument (useful when ``tstring`` is a
normal string rather than a translation string).  The optional
``mapping`` argument can specify the interpolation mapping, useful
when the ``tstring`` argument is not a translation string. If 
``tstring`` is a translation string its mapping data, if present, is
combined with the data from the ``mapping`` argument.

.. code-block:: python
   :linenos:

   from gettext import translations
   from translationstring import Translator
   from translationstring import TranslationString

   t = translations(.. the right arguments ...)
   translator = Translator(t)
   translator('Add ${number}', domain='foo', mapping={'number':1})

The :func:`translationstring.Translator` function accepts an
additional optional argument named ``policy``.  ``policy`` should be a
callable which accepts three arguments: ``translations``, ``tstring``
and ``domain``.  It must perform the actual translation lookup.  If
``policy`` is ``None``, the :func:`translationstring.dugettext_policy`
policy will be used.