Back to index

plone3  3.1.7
adapters.py
Go to the documentation of this file.
00001 from plone.i18n.normalizer.interfaces import IFileNameNormalizer
00002 from plone.i18n.normalizer.interfaces import IURLNormalizer
00003 from plone.i18n.normalizer.interfaces import IUserPreferredFileNameNormalizer
00004 from plone.i18n.normalizer.interfaces import IUserPreferredURLNormalizer
00005 
00006 from zope.component import queryUtility
00007 from zope.interface import implements
00008 from zope.i18n.interfaces import IUserPreferredLanguages
00009 
00010 
00011 class UserPreferredFileNameNormalizer(object):
00012     """
00013     An adapter for the HTTPRequest to provide user preferred language
00014     dependent normalization.
00015     
00016     Let's make sure that this implementation actually fulfills the API.
00017 
00018       >>> from zope.interface.verify import verifyClass
00019       >>> verifyClass(IUserPreferredFileNameNormalizer, UserPreferredFileNameNormalizer)
00020       True
00021     """
00022     implements(IUserPreferredFileNameNormalizer)
00023 
00024     def __init__(self, context):
00025         self.context = context # the context must be the request
00026 
00027     def normalize(self, text):
00028         """Returns a normalized Unicode string."""
00029         locale = None
00030         langs = IUserPreferredLanguages(self.context).getPreferredLanguages()
00031         if langs:
00032             locale = langs[0]
00033 
00034         util = queryUtility(IFileNameNormalizer)
00035         return util.normalize(text, locale=locale)
00036 
00037 
00038 class UserPreferredURLNormalizer(object):
00039     """
00040     An adapter for the HTTPRequest to provide user preferred language
00041     dependent normalization.
00042     
00043     Let's make sure that this implementation actually fulfills the API.
00044 
00045       >>> from zope.interface.verify import verifyClass
00046       >>> verifyClass(IUserPreferredURLNormalizer, UserPreferredURLNormalizer)
00047       True
00048     """
00049     implements(IUserPreferredURLNormalizer)
00050 
00051     def __init__(self, context):
00052         self.context = context # the context must be the request
00053 
00054     def normalize(self, text):
00055         """Returns a normalized Unicode string."""
00056         locale = None
00057         langs = IUserPreferredLanguages(self.context).getPreferredLanguages()
00058         if langs:
00059             locale = langs[0]
00060 
00061         util = queryUtility(IURLNormalizer)
00062         return util.normalize(text, locale=locale)