products/CPSonFive.old

changeset 43:68965a419966

- CPSonFive now uses it's own IUserPreferredLanguages adapter. - CPSonFive now implements an ILanguageAvailability adapter, that will return the languages that are available for the current site.
author lregebro
date Mon, 12 Jun 2006 17:48:53 +0000
parents 777767a9bb61
children 0659b76afdf7
files CHANGES __init__.py overrides.zcml
diffstat 3 files changed, 74 insertions(+), 3 deletions(-) [+]
line diff
     1.1 --- a/CHANGES
     1.2 +++ b/CHANGES
     1.3 @@ -3,7 +3,9 @@
     1.4  -
     1.5  New features:
     1.6  ~~~~~~~~~~~~~
     1.7 --
     1.8 +- CPSonFive now uses it's own IUserPreferredLanguages adapter.
     1.9 +- CPSonFive now implements an ILanguageAvailability adapter, that will
    1.10 +  return the languages that are available for the current site.
    1.11  Bug fixes:
    1.12  ~~~~~~~~~~
    1.13  -
     2.1 --- a/__init__.py
     2.2 +++ b/__init__.py
     2.3 @@ -21,6 +21,8 @@
     2.4  
     2.5  This product is the container for any reusable integration between CPS and Five.
     2.6  """
     2.7 +from zope.interface import implements
     2.8 +
     2.9  from types import StringTypes
    2.10  from zope.schema.interfaces import ITitledTokenizedTerm
    2.11  from zope.i18nmessageid import Message
    2.12 @@ -40,7 +42,6 @@
    2.13  
    2.14  
    2.15  from zope.i18n.interfaces import IUserPreferredCharsets
    2.16 -from zope.interface import implements
    2.17  
    2.18  class ISO15Charset(object):
    2.19      # This object implements the selector function for IUserPreferredCharsets
    2.20 @@ -113,6 +114,65 @@
    2.21  
    2.22      self.update_status = status
    2.23      return status
    2.24 +
    2.25 +from zope.i18n.interfaces import IUserPreferredLanguages, ILanguageAvailability
    2.26 +
    2.27 +class UserLanguages(object):
    2.28 +    """Return the preferred language.
    2.29 +    
    2.30 +    This one cares about the LOCALIZER_LANGUAGE used by CPS/Localizer to
    2.31 +    explicitly select a language. The LocalizerLanguages component in Five
    2.32 +    also does this, but via the overcomplicated Localizer.AcceptLanguage 
    2.33 +    who seldom returns what we want or expect.
    2.34 +    So I use this instead. //Lennart."""
    2.35 +    implements(IUserPreferredLanguages)
    2.36 +
    2.37 +    def __init__(self, request):
    2.38 +        self.request = request
    2.39 +
    2.40 +    def getPreferredLanguages(self):
    2.41 +        accept_language = self.request.HTTP_ACCEPT_LANGUAGE
    2.42 +        localizer_language = self.request.get('LOCALIZER_LANGUAGE')
    2.43 +
    2.44 +        ## Remove any spaces, change _ to -, make all lowercase:
    2.45 +        #accept_language = accept_language.replace(' ', '')
    2.46 +        #accept_language = accept_language.replace('_', '-')
    2.47 +        #accept_language = accept_language.lower()
    2.48 +        langs = []
    2.49 +        for lang in accept_language.split(','):
    2.50 +            if lang.find(';') != -1:
    2.51 +                lang, q = lang.split(';')
    2.52 +                q = float(q[2:]) # remove the "q=" and make into a float
    2.53 +            else:
    2.54 +                q = 1.0
    2.55 +            if lang == localizer_language:
    2.56 +                q = 2.0
    2.57 +            langs.append((q, lang))
    2.58 +                    
    2.59 +        # Make into a list of tuples (with value first, and key last):
    2.60 +        langs.sort()
    2.61 +        langs.reverse()
    2.62 +        # Return languages in order of quality
    2.63 +        return [l for q, l in langs]
    2.64 +
    2.65 +class CPSLanguages(object):
    2.66 +    """Returns the languages that exist in a CPS site"""
    2.67 +    
    2.68 +    implements(ILanguageAvailability)
    2.69 +
    2.70 +    def __init__(self, context):
    2.71 +        self.context = context
    2.72 +        
    2.73 +    def getAvailableLanguages(self):
    2.74 +        pts = self.context.translation_service
    2.75 +        languages = []
    2.76 +        for lang in [pts.getDefaultLanguage()] + pts.getSupportedLanguages():
    2.77 +            # Localizer uses the format xx_YY instead of xx-yy:
    2.78 +            lang = lang.replace('_', '-')
    2.79 +            lang = lang.lower()
    2.80 +            if lang not in languages:
    2.81 +                languages.append(lang)
    2.82 +        return languages        
    2.83      
    2.84  def initialize(context):
    2.85  
     3.1 --- a/overrides.zcml
     3.2 +++ b/overrides.zcml
     3.3 @@ -14,7 +14,16 @@
     3.4    <adapter
     3.5        for="zope.publisher.interfaces.http.IHTTPRequest" 
     3.6        provides="zope.i18n.interfaces.IUserPreferredLanguages" 
     3.7 -      factory="Products.Five.i18n.LocalizerLanguages"
     3.8 +      factory="Products.CPSonFive.UserLanguages"
     3.9 +      />
    3.10 +
    3.11 +  <!-- I register this for *, so that you don't have to look up the site
    3.12 +       first. It will fail if you try to adapt anything that can't
    3.13 +       acquire the translation_serveice tool /-->
    3.14 +  <adapter
    3.15 +      for="*" 
    3.16 +      provides="zope.i18n.interfaces.ILanguageAvailability" 
    3.17 +      factory="Products.CPSonFive.CPSLanguages"
    3.18        />
    3.19  
    3.20  </configure>