Back to index

plone3  3.1.7
selectionwidget.py
Go to the documentation of this file.
00001 from zope.app.form.browser.itemswidgets import DropdownWidget
00002 from zope.component import queryMultiAdapter
00003 from zope.schema.interfaces import ITitledTokenizedTerm
00004 
00005 
00006 class LanguageDropdownChoiceWidget(DropdownWidget):
00007     """ A DropdownWidget which renders a localized language selection.
00008     """
00009 
00010     def __init__(self, field, request):
00011         """Initialize the widget."""
00012         super(LanguageDropdownChoiceWidget, self).__init__(field,
00013             field.vocabulary, request)
00014         portal_state = queryMultiAdapter((self.context, request),
00015                                          name=u'plone_portal_state')
00016         self.languages = portal_state.locale().displayNames.languages
00017 
00018     def textForValue(self, term):
00019         """Extract a string from the `term`.
00020 
00021         The `term` must be a vocabulary tokenized term.
00022         """
00023         if ITitledTokenizedTerm.providedBy(term):
00024             title = self.languages.get(term.value, term.title)
00025             if title == term.value:
00026                 title = term.title
00027             return title
00028         return term.token