Back to index

plone3  3.1.7
subdomain.py
Go to the documentation of this file.
00001 from zope.component import queryUtility
00002 from zope.i18n.interfaces import ILanguageAvailability
00003 from zope.i18n.interfaces import IUserPreferredLanguages
00004 from zope.interface import implements
00005 
00006 from plone.i18n.locales.interfaces import ICcTLDInformation
00007 
00008 
00009 class SubdomainLanguage(object):
00010 
00011     implements(IUserPreferredLanguages)
00012 
00013     def __init__(self, request):
00014         self.request = request
00015 
00016     def getPreferredLanguages(self):
00017         """Return a sequence of user preferred languages."""
00018         langs = []
00019 
00020         host = self.request.get('HTTP_HOST', None)
00021         tlds = queryUtility(ICcTLDInformation)
00022         if host is None or tlds is None:
00023             return None
00024 
00025         subdomain = host.split(":")[0].lower().split(".")[0]
00026         wanted = tlds.getTLDs().get(subdomain, [])
00027 
00028         available = queryUtility(ILanguageAvailability)
00029         if available is not None:
00030             langs = [str(lang) for lang in available.getAvailableLanguages()]
00031 
00032         langs = [lang for lang in wanted if lang in langs]
00033         return langs and langs or None