products/CPSonFive.old

changeset 44:0659b76afdf7

Normalize all the languages to xx-nn, instead of xx_NN. Also simplified the code a bit.
author lregebro
date Tue, 13 Jun 2006 12:18:55 +0000
parents 68965a419966
children 9481aaa45418
files __init__.py
diffstat 1 files changed, 11 insertions(+), 10 deletions(-) [+]
line diff
     1.1 --- a/__init__.py
     1.2 +++ b/__init__.py
     1.3 @@ -133,23 +133,24 @@
     1.4      def getPreferredLanguages(self):
     1.5          accept_language = self.request.HTTP_ACCEPT_LANGUAGE
     1.6          localizer_language = self.request.get('LOCALIZER_LANGUAGE')
     1.7 -
     1.8 -        ## Remove any spaces, change _ to -, make all lowercase:
     1.9 -        #accept_language = accept_language.replace(' ', '')
    1.10 -        #accept_language = accept_language.replace('_', '-')
    1.11 -        #accept_language = accept_language.lower()
    1.12 -        langs = []
    1.13 +        accept_language = '%s;q=2.0,%s' % (localizer_language, accept_language)
    1.14 +        # Normalize: Remove any spaces, change _ to -, make all lowercase:
    1.15 +        accept_language = accept_language.replace(' ', '')
    1.16 +        accept_language = accept_language.replace('_', '-')
    1.17 +        accept_language = accept_language.lower()
    1.18 +        langs = {}
    1.19          for lang in accept_language.split(','):
    1.20              if lang.find(';') != -1:
    1.21                  lang, q = lang.split(';')
    1.22                  q = float(q[2:]) # remove the "q=" and make into a float
    1.23              else:
    1.24                  q = 1.0
    1.25 -            if lang == localizer_language:
    1.26 -                q = 2.0
    1.27 -            langs.append((q, lang))
    1.28 +            if lang not in langs:
    1.29 +                langs[lang] = q
    1.30                      
    1.31 -        # Make into a list of tuples (with value first, and key last):
    1.32 +        # Make into a list of tuples with value first, and key last 
    1.33 +        # for easy sorting:
    1.34 +        langs = [(q,l) for (l,q) in langs.items()]
    1.35          langs.sort()
    1.36          langs.reverse()
    1.37          # Return languages in order of quality