Back to index

plone3  3.1.7
utility.py
Go to the documentation of this file.
00001 from zope.interface import implements
00002 from zope.i18n import interpolate
00003 from zope.publisher.interfaces.browser import IBrowserRequest
00004 
00005 from Products.PlacelessTranslationService import getTranslationService
00006 from Products.PlacelessTranslationService.interfaces import \
00007     IPTSTranslationDomain
00008 
00009 
00010 class PTSTranslationDomain(object):
00011     """Makes translation domains that are still kept in PTS available as
00012     ITranslationDomain utilities. That way they are usable from Zope 3 code
00013     such as Zope 3 PageTemplates."""
00014 
00015     implements(IPTSTranslationDomain)
00016 
00017     def __init__(self, domain):
00018         self.domain = domain
00019 
00020     def translate(self, msgid, mapping=None, context=None,
00021                   target_language=None, default=None):
00022 
00023         pts  = getTranslationService()
00024         if pts is None or context is None:
00025             # If we don't have enough context use interpolate
00026             return interpolate(default, mapping)
00027 
00028         # Don't accept anything which isn't a real request
00029         if not IBrowserRequest.providedBy(context) or 'PARENTS' not in context:
00030             raise ValueError, "You didn't pass in a request as the context."
00031 
00032         parent = context['PARENTS'][-1]
00033         return pts.translate(self.domain, msgid, mapping, parent,
00034                              target_language, default)