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.msgidmust be the message identifier for the translation string. It must be aunicodeobject or astrobject encoded in the default system encoding.Optional keyword arguments to this object’s constructor include
domain,default, andmapping.domainrepresents the translation domain. By default, the translation domain isNone, indicating that this translation string is associated with the default translation domain (usuallymessages).defaultrepresents an explicit default text for this translation string. Default text appears when the translation string cannot be translated. Usually, themsgidof 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. Ifdefaultis provided, it must be aunicodeobject or astrobject encoded in the default system encoding (usually means ASCII). IfdefaultisNone(its default value), themsgidvalue 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.contextrepresents the translation context. By default, the translation context isNone.After a translation string is constructed, it behaves like most other
unicodeobjects; itsmsgidvalue will be displayed when it is treated like aunicodeobject. Only when itsugettextmethod is called will it be translated.Its default value is available as the
defaultattribute of the object, its translation domain is available as thedomainattribute, and themappingis available as themappingattribute. The object otherwise behaves much like a Unicode string.- interpolate(translated=None)¶
Interpolate the value
translatedwhich is assumed to be a Unicode object containing zero or more replacement markers ($fooor${bar}) using themappingdictionary attached to this instance. If themappingdictionary is empty orNone, no interpolation is performed.If
translatedisNone, interpolation will be performed against thedefaultvalue.
- TranslationStringFactory(factory_domain)¶
Create a factory which will generate translation strings without requiring that each call to the factory be passed a
domainvalue. A single argument is passed to this class’ constructor:domain. This value will be used as thedomainvalues oftranslationstring.TranslationStringobjects generated by the__call__of this class. Themsgid,mapping, anddefaultvalues 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
translateargument to thechameleon.zpt.template.PageTemplateconstructor) to allow our translation machinery to drive template translation. A single required argumenttranslatoris passsed. Thetranslatorprovided should be a callable which accepts a single argumenttranslation_string( atranslationstring.TranslationStringinstance) which returns aunicodeobject as a translation.translatormay also optionally beNone, in which case no translation is performed (themsgidordefaultvalue is returned untranslated).
- Translator(translations=None, policy=None)¶
Return a translator object based on the
translationsandpolicyprovided.translationsshould be an object supporting at least the Pythongettext.NullTranslationsAPI but ideally thebabel.support.TranslationsAPI, which has support for domain lookups like dugettext.policyshould be a callable which accepts three arguments:translations,tstringanddomain. It must perform the actual translation lookup. IfpolicyisNone, thetranslationstring.dugettext_policy()policy will be used.The callable returned accepts three arguments:
tstring(required),domain(optional) andmapping(optional). When called, it will translate thetstringtranslation string to aunicodeobject using thetranslationsprovided. IftranslationsisNone, the result of interpolation of the default value is returned. The optionaldomainargument can be used to specify or override the domain of thetstring(useful whentstringis a normal string rather than a translation string). The optionalmappingargument can specify or override thetstringinterpolation mapping, useful when thetstringargument 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.Translationstranslations object, which supports the dugettext API; fall back to ugettext.
- ugettext_policy(translations, tstring, domain, context)¶
A translator policy function which unconditionally uses the
ugettextAPI on the translations object.
- Pluralizer(translations=None, policy=None)¶
Return a pluralizer object based on the
translationsandpolicyprovided.translationsshould be an object supporting at least the Pythongettext.NullTranslationsAPI but ideally thebabel.support.TranslationsAPI, which has support for domain lookups like dugettext.policyshould be a callable which accepts five arguments:translations,singularandplural,nanddomain. It must perform the actual pluralization lookup. IfpolicyisNone, 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
singularandpluralobjects passed may be translation strings or unicode strings.nrepresents the number of elements.domainis the translation domain to use to do the pluralization, andmappingis 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 thedomainis 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.Translationsclass, 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
ungettextAPI on the translations object.