Ñò
Ã#xPc @ sã d Z d d k Z d d k Z d d k Z d d k l Z d „ Z d e f d „ ƒ YZ d e f d „ ƒ YZ d e f d
„ ƒ YZ
d e f d „ ƒ YZ d
e f d „ ƒ YZ d e f d „ ƒ YZ
e ƒ Z e
ƒ Z e Z d S( s`
Defers gettext translation till request time.
IPA presents some tricky gettext challenges. On the one hand, most translatable
message are defined as class attributes on the plugins, which means these get
evaluated at module-load time. But on the other hand, each request to the
server can be in a different locale, so the actual translation must not occur
till request time.
The `text` module provides a mechanism for for deferred gettext translation. It
was designed to:
1. Allow translatable strings to be marked with the usual ``_()`` and
``ngettext()`` functions so that standard tools like xgettext can still
be used
2. Allow programmers to mark strings in a natural way without burdening them
with details of the deferred translation mechanism
A typical plugin will use the deferred translation like this:
>>> from ipalib import Command, _, ngettext
>>> class my_plugin(Command):
... my_string = _('Hello, %(name)s.')
... my_plural = ngettext('%(count)d goose', '%(count)d geese', 0)
...
With normal gettext usage, the *my_string* and *my_plural* message would be
translated at module-load-time when your ``my_plugin`` class is defined. This
would mean that all message are translated in the locale of the server rather
than the locale of the request.
However, the ``_()`` function above is actually a `GettextFactory` instance,
which when called returns a `Gettext` instance. A `Gettext` instance stores the
message to be translated, and the gettext domain and localedir, but it doesn't
perform the translation till `Gettext.__unicode__()` is called. For example:
>>> my_plugin.my_string
Gettext('Hello, %(name)s.', domain='ipa', localedir=None)
>>> unicode(my_plugin.my_string)
u'Hello, %(name)s.'
Translation can also be performed via the `Gettext.__mod__()` convenience
method. For example, these two are equivalent:
>>> my_plugin.my_string % dict(name='Joe')
u'Hello, Joe.'
>>> unicode(my_plugin.my_string) % dict(name='Joe') # Long form
u'Hello, Joe.'
Similar to ``_()``, the ``ngettext()`` function above is actually an
`NGettextFactory` instance, which when called returns an `NGettext` instance.
An `NGettext` instance stores the singular and plural messages, and the gettext
domain and localedir, but it doesn't perform the translation till
`NGettext.__call__()` is called. For example:
>>> my_plugin.my_plural
NGettext('%(count)d goose', '%(count)d geese', domain='ipa', localedir=None)
>>> my_plugin.my_plural(1)
u'%(count)d goose'
>>> my_plugin.my_plural(2)
u'%(count)d geese'
Translation can also be performed via the `NGettext.__mod__()` convenience
method. For example, these two are equivalent:
>>> my_plugin.my_plural % dict(count=1)
u'1 goose'
>>> my_plugin.my_plural(1) % dict(count=1) # Long form
u'1 goose'
Lastly, 3rd-party plugins can create factories bound to a different gettext
domain. The default domain is ``'ipa'``, which is also the domain of the
standard ``ipalib._()`` and ``ipalib.ngettext()`` factories. But 3rd-party
plugins can create their own factories like this:
>>> from ipalib import GettextFactory, NGettextFactory
>>> _ = GettextFactory(domain='ipa_foo')
>>> ngettext = NGettextFactory(domain='ipa_foo')
>>> class foo(Command):
... msg1 = _('Foo!')
... msg2 = ngettext('%(count)d bar', '%(count)d bars', 0)
...
Notice that these messages are bound to the ``'ipa_foo'`` domain:
>>> foo.msg1
Gettext('Foo!', domain='ipa_foo', localedir=None)
>>> foo.msg2
NGettext('%(count)d bar', '%(count)d bars', domain='ipa_foo', localedir=None)
For additional details, see `GettextFactory` and `Gettext`, and for plural
forms, see `NGettextFactory` and `NGettext`.
iÿÿÿÿN( t contextc C sa | t i j p t ‚ | \ } } t i | d | d t t d d ƒ d t ƒ} | t i | <| S( Nt localedirt languagest fallback( R t __dict__t AssertionErrort gettextt translationt getattrt Nonet True( t keyt domainR R ( ( s/ /usr/lib/python2.6/site-packages/ipalib/text.pyt create_translationy s
t LazyTextc B s5 e Z d Z d Z d d d „ Z d „ Z d „ Z RS(
s†
Base class for deferred translation.
This class is not used directly. See the `Gettext` and `NGettext`
subclasses.
R R R t argsc C s. | | _ | | _ | | f | _ d | _ d S( s¢
Initialize.
:param domain: The gettext domain in which this message will be
translated, e.g. ``'ipa'`` or ``'ipa_3rd_party'``; default is
``None``
:param localedir: The directory containing the gettext translations,
e.g. ``'/usr/share/locale/'``; default is ``None``, in which case
gettext will use the default system locale directory.
N( R R R R R ( t selfR R ( ( s/ /usr/lib/python2.6/site-packages/ipalib/text.pyt __init__ s c C s+ t | ƒ | i j o t S| i | i j S( sÚ
Return ``True`` if this instances is equal to *other*.
Note that this method cannot be used on the `LazyText` base class itself
as subclasses must define an *args* instance attribute.
( t typet __class__t FalseR ( R t other( ( s/ /usr/lib/python2.6/site-packages/ipalib/text.pyt __eq__Ÿ s c C s | i | ƒ S( sÞ
Return ``True`` if this instances is not equal to *other*.
Note that this method cannot be used on the `LazyText` base class itself
as subclasses must define an *args* instance attribute.
( R ( R R ( ( s/ /usr/lib/python2.6/site-packages/ipalib/text.pyt __ne__ª s ( s domains localedirs keys argsN( t __name__t
__module__t __doc__t __slots__R R R R ( ( ( s/ /usr/lib/python2.6/site-packages/ipalib/text.pyR … s
t Gettextc B sG e Z d Z d Z d d d „ Z d „ Z d „ Z d „ Z d „ Z RS( sí
Deferred translation using ``gettext.ugettext()``.
Normally the `Gettext` class isn't used directly and instead is created via
a `GettextFactory` instance. However, for illustration, we can create one
like this:
>>> msg = Gettext('Hello, %(name)s.')
When you create a `Gettext` instance, the message is stored on the *msg*
attribute:
>>> msg.msg
'Hello, %(name)s.'
No translation is performed till `Gettext.__unicode__()` is called. This
will translate *msg* using ``gettext.ugettext()``, which will return the
translated string as a Python ``unicode`` instance. For example:
>>> unicode(msg)
u'Hello, %(name)s.'
`Gettext.__unicode__()` should be called at request time, which in a
nutshell means it should be called from within your plugin's
``Command.execute()`` method. `Gettext.__unicode__()` will perform the
translation based on the locale of the current request.
`Gettext.__mod__()` is a convenience method for Python "percent" string
formatting. It will translate your message using `Gettext.__unicode__()`
and then perform the string substitution on the translated message. For
example, these two are equivalent:
>>> msg % dict(name='Joe')
u'Hello, Joe.'
>>> unicode(msg) % dict(name='Joe') # Long form
u'Hello, Joe.'
See `GettextFactory` for additional details. If you need to pick between
singular and plural form, use `NGettext` instances via the
`NGettextFactory`.
t msgc C s8 t t | ƒ i | | ƒ | | _ | | | f | _ d S( N( t superR R R R ( R R R R ( ( s/ /usr/lib/python2.6/site-packages/ipalib/text.pyR á s c C s# d | i i | i | i | i f S( Ns %s(%r, domain=%r, localedir=%r)( R R R R R ( R ( ( s/ /usr/lib/python2.6/site-packages/ipalib/text.pyt __repr__æ s c C sI | i t i j o t i | i i } n t | i ƒ i } | | i ƒ S( sN
Translate this message and return as a ``unicode`` instance.
( R R R t ugettextR
R ( R t g( ( s/ /usr/lib/python2.6/site-packages/ipalib/text.pyt __unicode__ê s c C s
| i ƒ S( N( R" ( R ( ( s/ /usr/lib/python2.6/site-packages/ipalib/text.pyt __json__ô s c C s | i ƒ | S( N( R" ( R t kw( ( s/ /usr/lib/python2.6/site-packages/ipalib/text.pyt __mod__÷ s N(
R R R R R R R R" R# R% ( ( ( s/ /usr/lib/python2.6/site-packages/ipalib/text.pyR ´ s )
t FixMec B s) e Z d Z e ƒ Z d „ Z d „ Z RS( sH
Non-translated place-holder for UI labels.
`FixMe` is a subclass of `Gettext` and is used for automatically created
place-holder labels. It generally behaves exactly like `Gettext` except no
translation is ever performed.
`FixMe` allows programmers to get plugins working without first filling in
all the labels that will ultimately be required, while at the same time it
creates conspicuous looking UI labels that remind the programmer to
"fix-me!". For example, the typical usage would be something like this:
>>> class Plugin(object):
... label = None
... def __init__(self):
... self.name = self.__class__.__name__
... if self.label is None:
... self.label = FixMe(self.name + '.label')
... assert isinstance(self.label, Gettext)
...
>>> class user(Plugin):
... pass # Oops, we didn't set user.label yet
...
>>> u = user()
>>> u.label
FixMe('user.label')
Note that as `FixMe` is a subclass of `Gettext`, is passes the above type
check using ``isinstance()``.
Calling `FixMe.__unicode__()` performs no translation, but instead returns
said conspicuous looking label:
>>> unicode(u.label)
u'