Back to index

plone3  3.1.7
selector.py
Go to the documentation of this file.
00001 from zope.interface import implements
00002 from zope.viewlet.interfaces import IViewlet
00003 
00004 from Acquisition import aq_base
00005 from Products.CMFCore.utils import getToolByName
00006 from Products.Five.browser import BrowserView
00007 from Products.Five.browser.pagetemplatefile import ZopeTwoPageTemplateFile
00008 
00009 
00010 class LanguageSelector(BrowserView):
00011     """Language selector.
00012 
00013       >>> ls = LanguageSelector(None, dict(), None, None)
00014       >>> ls
00015       <plone.app.i18n.locales.browser.selector.LanguageSelector object at ...>
00016 
00017       >>> ls.update()
00018       >>> ls.available()
00019       False
00020       >>> ls.languages()
00021       []
00022       >>> ls.showFlags()
00023       False
00024 
00025       >>> class Tool(object):
00026       ...     use_cookie_negotiation = False
00027       ...
00028       ...     def __init__(self, **kw):
00029       ...         self.__dict__.update(kw)
00030       ...
00031       ...     def showFlags(self):
00032       ...         return True
00033       ...
00034       ...     def getAvailableLanguageInformation(self):
00035       ...         return dict(en={'selected' : True}, de={'selected' : False},
00036       ...                     nl={'selected' : True})
00037       ...
00038       ...     def getLanguageBindings(self):
00039       ...         # en = selected by user, nl = default, [] = other options
00040       ...         return ('en', 'nl', [])
00041 
00042       >>> ls.tool = Tool()
00043       >>> ls.available()
00044       False
00045 
00046       >>> ls.tool = Tool(use_cookie_negotiation=True)
00047       >>> ls.available()
00048       True
00049       >>> ls.languages()
00050       [{'code': 'nl', 'selected': False}, {'code': 'en', 'selected': True}]
00051       >>> ls.showFlags()
00052       True
00053 
00054       >>> class NewTool(Tool):
00055       ...     always_show_selector = False
00056       ...
00057       ...     def showSelector(self):
00058       ...         return bool(self.use_cookie_negotiation or self.always_show_selector)
00059 
00060       >>> ls.tool = NewTool()
00061       >>> ls.available()
00062       False
00063 
00064       >>> ls.tool = NewTool(use_cookie_negotiation=True)
00065       >>> ls.available()
00066       True
00067 
00068       >>> ls.tool = NewTool(always_show_selector=True)
00069       >>> ls.available()
00070       True
00071 
00072       >>> class Dummy(object):
00073       ...     def getPortalObject(self):
00074       ...         return self
00075       ...
00076       ...     def absolute_url(self):
00077       ...         return 'absolute url'
00078 
00079       >>> context = Dummy()
00080       >>> context.portal_url = Dummy()
00081       >>> ls = LanguageSelector(context, dict(), None, None)
00082       >>> ls.portal_url
00083       'absolute url'
00084     """
00085     implements(IViewlet)
00086 
00087     render = ZopeTwoPageTemplateFile('languageselector.pt')
00088 
00089     def __init__(self, context, request, view, manager):
00090         super(LanguageSelector, self).__init__(context, request)
00091         self.__parent__ = view
00092         self.context = context
00093         self.request = request
00094         self.view = view
00095         self.manager = manager
00096         self.tool = getToolByName(context, 'portal_languages', None)
00097         portal_tool = getToolByName(context, 'portal_url', None)
00098         self.portal_url = None
00099         if portal_tool is not None:
00100             self.portal_url = portal_tool.getPortalObject().absolute_url()
00101 
00102     def update(self):
00103         pass
00104 
00105     def available(self):
00106         if self.tool is not None:
00107             # Ask the language tool. Using getattr here for BBB with older
00108             # versions of the tool.
00109             showSelector = getattr(aq_base(self.tool), 'showSelector', None)
00110             if showSelector is not None:
00111                 return self.tool.showSelector() # Call with aq context
00112             # BBB
00113             return bool(self.tool.use_cookie_negotiation)
00114         return False
00115 
00116     def languages(self):
00117         """Returns list of languages."""
00118         if self.tool is None:
00119             return []
00120 
00121         bound = self.tool.getLanguageBindings()
00122         current = bound[0]
00123 
00124         def merge(lang, info):
00125             info["code"]=lang
00126             if lang == current:
00127                 info['selected'] = True
00128             else:
00129                 info['selected'] = False
00130             return info
00131 
00132         return [merge(lang, info) for (lang,info) in
00133                     self.tool.getAvailableLanguageInformation().items()
00134                     if info["selected"]]
00135 
00136     def showFlags(self):
00137         """Do we use flags?."""
00138         if self.tool is not None:
00139             return self.tool.showFlags()
00140         return False