Function
zope.i18n.translate

Signature

translate(msgid, domain=None, mapping=None, context=None, target_language=None, default=None)

Documentation String

Translate text.

First setup some test components:

>>> from zope import component, interface >>> import zope.i18n.interfaces

>>> class TestDomain: ... interface.implements(zope.i18n.interfaces.ITranslationDomain) ... ... def __init__(self, *catalog): ... self.catalog = catalog ... ... def translate(self, text, _, **__): ... return self.catalog[text]

Normally, the translation system will use a domain utility:

>>> component.provideUtility(TestDomain(eek=u'ook'), name=my.domain) >>> translate(u'eek', my.domain) u'ook'

Normally, if no domain is given, or if there is no domain utility for the given domain, then the text isn't translated:

>>> translate(u'eek') u'eek'

Moreover the text will be converted to unicode:

>>> translate(eek, your.domain) u'eek'

A fallback domain factory can be provided. This is normally used for testing:

>>> def fallback(domain=u''): ... return TestDomain(eek=u'test-from-' + domain) >>> interface.directlyProvides( ... fallback, ... zope.i18n.interfaces.IFallbackTranslationDomainFactory, ... )

>>> component.provideUtility(fallback)

>>> translate(u'eek') u'test-from-'

>>> translate(u'eek', your.domain) u'test-from-your.domain'