API Documentation¶
-
class
TranslationString
(msgid, domain=None, default=None, mapping=None, context=None)¶ The constructor for a translation string. A translation string is a Unicode-like object that has some extra metadata.
This constructor accepts one required argument named
msgid
.msgid
must be the message identifier for the translation string. It must be aunicode
object or astr
object encoded in the default system encoding.Optional keyword arguments to this object’s constructor include
domain
,default
, andmapping
.domain
represents the translation domain. By default, the translation domain isNone
, indicating that this translation string is associated with the default translation domain (usuallymessages
).default
represents an explicit default text for this translation string. Default text appears when the translation string cannot be translated. Usually, themsgid
of a translation string serves double duty as its default text. However, using this option you can provide a different default text for this translation string. This feature is useful when the default of a translation string is too complicated or too long to be used as a message identifier. Ifdefault
is provided, it must be aunicode
object or astr
object encoded in the default system encoding (usually means ASCII). Ifdefault
isNone
(its default value), themsgid
value used by this translation string will be assumed to be the value ofdefault
.mapping
, if supplied, must be a dictionary-like object which represents the replacement values for any translation string replacement marker instances found within themsgid
(ordefault
) value of this translation string.context
represents the translation context. By default, the translation context isNone
.After a translation string is constructed, it behaves like most other
unicode
objects; itsmsgid
value will be displayed when it is treated like aunicode
object. Only when itsugettext
method is called will it be translated.Its default value is available as the
default
attribute of the object, its translation domain is available as thedomain
attribute, and themapping
is available as themapping
attribute. The object otherwise behaves much like a Unicode string.-
interpolate
(translated=None)¶ Interpolate the value
translated
which is assumed to be a Unicode object containing zero or more replacement markers ($foo
or${bar}
) using themapping
dictionary attached to this instance. If themapping
dictionary is empty orNone
, no interpolation is performed.If
translated
isNone
, interpolation will be performed against thedefault
value.
-
-
TranslationStringFactory
(factory_domain)¶ Create a factory which will generate translation strings without requiring that each call to the factory be passed a
domain
value. A single argument is passed to this class’ constructor:domain
. This value will be used as thedomain
values oftranslationstring.TranslationString
objects generated by the__call__
of this class. Themsgid
,mapping
, anddefault
values provided to the__call__
method of an instance of this class have the meaning as described by the constructor of thetranslationstring.TranslationString
-
ChameleonTranslate
(translator)¶ When necessary, use the result of calling this function as a Chameleon template ‘translate’ function (e.g. the
translate
argument to thechameleon.zpt.template.PageTemplate
constructor) to allow our translation machinery to drive template translation. A single required argumenttranslator
is passsed. Thetranslator
provided should be a callable which accepts a single argumenttranslation_string
( atranslationstring.TranslationString
instance) which returns aunicode
object as a translation.translator
may also optionally beNone
, in which case no translation is performed (themsgid
ordefault
value is returned untranslated).
-
Translator
(translations=None, policy=None)¶ Return a translator object based on the
translations
andpolicy
provided.translations
should be an object supporting at least the Pythongettext.NullTranslations
API but ideally thebabel.support.Translations
API, which has support for domain lookups like dugettext.policy
should be a callable which accepts three arguments:translations
,tstring
anddomain
. It must perform the actual translation lookup. Ifpolicy
isNone
, thetranslationstring.dugettext_policy()
policy will be used.The callable returned accepts three arguments:
tstring
(required),domain
(optional) andmapping
(optional). When called, it will translate thetstring
translation string to aunicode
object using thetranslations
provided. Iftranslations
isNone
, the result of interpolation of the default value is returned. The optionaldomain
argument can be used to specify or override the domain of thetstring
(useful whentstring
is a normal string rather than a translation string). The optionalmapping
argument can specify or override thetstring
interpolation mapping, useful when thetstring
argument is a simple string instead of a translation string.
-
dugettext_policy
(translations, tstring, domain, context)¶ A translator policy function which assumes the use of a
babel.support.Translations
translations object, which supports the dugettext API; fall back to ugettext.
-
ugettext_policy
(translations, tstring, domain, context)¶ A translator policy function which unconditionally uses the
ugettext
API on the translations object.
-
Pluralizer
(translations=None, policy=None)¶ Return a pluralizer object based on the
translations
andpolicy
provided.translations
should be an object supporting at least the Pythongettext.NullTranslations
API but ideally thebabel.support.Translations
API, which has support for domain lookups like dugettext.policy
should be a callable which accepts five arguments:translations
,singular
andplural
,n
anddomain
. It must perform the actual pluralization lookup. Ifpolicy
isNone
, thetranslationstring.dungettext_policy()
policy will be used.The object returned will be a callable which has the following signature:
def pluralizer(singular, plural, n, domain=None, mapping=None): ...
The
singular
andplural
objects passed may be translation strings or unicode strings.n
represents the number of elements.domain
is the translation domain to use to do the pluralization, andmapping
is the interpolation mapping that should be used on the result. Note that if the objects passed are translation strings, their domains and mappings are ignored. The domain and mapping arguments must be used instead. If thedomain
is not supplied, a default domain is used (usuallymessages
).
-
dungettext_policy
(translations, singular, plural, n, domain, context)¶ A pluralizer policy function which assumes the use of the
babel.support.Translations
class, which supports the dungettext API; falls back to ungettext.
-
ungettext_policy
(translations, singular, plural, n, domain, context)¶ A pluralizer policy function which unconditionally uses the
ungettext
API on the translations object.