Back to index

plone3  3.1.7
language.py
Go to the documentation of this file.
00001 from zope.app.schema.vocabulary import IVocabularyFactory
00002 from zope.interface import implements
00003 from zope.schema.vocabulary import SimpleTerm
00004 from zope.schema.vocabulary import SimpleVocabulary
00005 
00006 from Products.CMFCore.utils import getToolByName
00007 
00008 def sort_key(language):
00009     return language[1]
00010 
00011 
00012 class AvailableContentLanguageVocabulary(object):
00013     """Vocabulary factory for available content languages in the portal.
00014 
00015       >>> from zope.component import queryUtility
00016       >>> from plone.app.vocabularies.tests.base import DummyContext
00017       >>> from plone.app.vocabularies.tests.base import DummyTool
00018 
00019       >>> name = 'plone.app.vocabularies.AvailableContentLanguages'
00020       >>> util = queryUtility(IVocabularyFactory, name)
00021       >>> context1 = DummyContext()
00022       >>> context2 = DummyContext()
00023       >>> context1.context = context2
00024 
00025       >>> util(context1) is None
00026       True
00027 
00028       >>> tool = DummyTool('portal_languages')
00029       >>> def getAvailableLanguages():
00030       ...     return dict(en=dict(name='English'),
00031       ...                 de=dict(name='German'))
00032       >>> tool.getAvailableLanguages = getAvailableLanguages
00033       >>> context2.portal_languages = tool
00034 
00035       >>> languages = util(context1)
00036       >>> languages
00037       <zope.schema.vocabulary.SimpleVocabulary object at ...>
00038 
00039       >>> len(languages.by_token)
00040       2
00041 
00042       >>> de = languages.by_token['de']
00043       >>> de.title, de.token, de.value
00044       ('German', 'de', 'de')
00045     """
00046     implements(IVocabularyFactory)
00047 
00048     def __call__(self, context):
00049         context = getattr(context, 'context', context)
00050         ltool = getToolByName(context, 'portal_languages', None)
00051         if ltool is None:
00052             return None
00053         languages = ltool.getAvailableLanguages()
00054         items = [(l, languages[l].get('name', l)) for l in languages]
00055         items.sort(key=sort_key)
00056         items = [SimpleTerm(i[0], i[0], i[1]) for i in items]
00057         return SimpleVocabulary(items)
00058 
00059 AvailableContentLanguageVocabularyFactory = AvailableContentLanguageVocabulary()
00060 
00061 
00062 class SupportedContentLanguageVocabulary(object):
00063     """Vocabulary factory for supported content languages in the portal.
00064 
00065       >>> from zope.component import queryUtility
00066       >>> from plone.app.vocabularies.tests.base import DummyContext
00067       >>> from plone.app.vocabularies.tests.base import DummyTool
00068 
00069       >>> name = 'plone.app.vocabularies.SupportedContentLanguages'
00070       >>> util = queryUtility(IVocabularyFactory, name)
00071       >>> context1 = DummyContext()
00072       >>> context2 = DummyContext()
00073       >>> context1.context = context2
00074 
00075       >>> util(context1) is None
00076       True
00077 
00078       >>> tool = DummyTool('portal_languages')
00079       >>> def listSupportedLanguages():
00080       ...     return [('en', 'English'), ('de', 'German')]
00081       >>> tool.listSupportedLanguages = listSupportedLanguages
00082       >>> context2.portal_languages = tool
00083 
00084       >>> languages = util(context1)
00085       >>> languages
00086       <zope.schema.vocabulary.SimpleVocabulary object at ...>
00087 
00088       >>> len(languages.by_token)
00089       2
00090 
00091       >>> de = languages.by_token['de']
00092       >>> de.title, de.token, de.value
00093       ('German', 'de', 'de')
00094     """
00095     implements(IVocabularyFactory)
00096 
00097     def __call__(self, context):
00098         context = getattr(context, 'context', context)
00099         ltool = getToolByName(context, 'portal_languages', None)
00100         if ltool is None:
00101             return None
00102         items = ltool.listSupportedLanguages()
00103         items.sort(key=sort_key)
00104         items = [SimpleTerm(i[0], i[0], i[1]) for i in items]
00105         return SimpleVocabulary(items)
00106 
00107 SupportedContentLanguageVocabularyFactory = SupportedContentLanguageVocabulary()