#12197: parse_accept_lang_header parse HTTP_ACCEPT_LANGUAGE with wrong Quality
factors
-------------------------------------------+--------------------------------
          Reporter:  [email protected]    |         Owner:  nobody
            Status:  new                   |     Milestone:  1.2   
         Component:  Internationalization  |       Version:  1.1   
        Resolution:                        |      Keywords:        
             Stage:  Unreviewed            |     Has_patch:  1     
        Needs_docs:  0                     |   Needs_tests:  0     
Needs_better_patch:  0                     |  
-------------------------------------------+--------------------------------
Old description:

> According to the RFC 2616
> <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html>,
> HTTP_ACCEPT_LANGUAGE could have several languages options, such as
>
> da, en-gb;q=0.8, en;q=0.7
>
> So, django parse those string with parse_accept_lang_header method, and
> order the options base on its 'q' quality factor.
>
> But the re.split seems return a list with a empty string as first
> element, such as
>
> s = 'da, en-gb;q=0.8, en;q=0.7'
>
> r.split(s)
>
> ['', 'da', None, '', 'en-gb', '0.8', '', 'en', '0.7', '']
>
> But parse_accept_lang_header use it from index 0, it means they align to
> wrong index
>
> To fix the issues, you could modify the accept_language_re, or use the
> return list from index 1.
>
> def parse_accept_lang_header(lang_string):
>     """
>     Parses the lang_string, which is the body of an HTTP Accept-Language
>     header, and returns a list of (lang, q-value), ordered by 'q' values.
>
>     Any format errors in lang_string results in an empty list being
> returned.
>     """
>     result = []
>     pieces = accept_language_re.split(lang_string)
>     if pieces[-1]:
>         return []
>     for i in range(0, len(pieces) - 1, 3):
>         first, lang, priority = pieces[i : i + 3]
>         if first:
>             return []
>         priority = priority and float(priority) or 1.0
>         result.append((lang, priority))
>     result.sort(lambda x, y: -cmp(x[1], y[1]))
>     return result

New description:

 According to the RFC 2616
 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html>,
 HTTP_ACCEPT_LANGUAGE could have several languages options, such as

 da, en-gb;q=0.8, en;q=0.7

 So, django parse those string with parse_accept_lang_header method, and
 order the options base on its 'q' quality factor.

 But the re.split seems return a list with a empty string as first element,
 such as

 {{{
 s = 'da, en-gb;q=0.8, en;q=0.7'

 r.split(s)

 ['', 'da', None, '', 'en-gb', '0.8', '', 'en', '0.7', '']
 }}}

 But parse_accept_lang_header use it from index 0, it means they align to
 wrong index

 To fix the issues, you could modify the accept_language_re, or use the
 return list from index 1.

 {{{
 def parse_accept_lang_header(lang_string):
     """
     Parses the lang_string, which is the body of an HTTP Accept-Language
     header, and returns a list of (lang, q-value), ordered by 'q' values.

     Any format errors in lang_string results in an empty list being
 returned.
     """
     result = []
     pieces = accept_language_re.split(lang_string)
     if pieces[-1]:
         return []
     for i in range(0, len(pieces) - 1, 3):
         first, lang, priority = pieces[i : i + 3]
         if first:
             return []
         priority = priority and float(priority) or 1.0
         result.append((lang, priority))
     result.sort(lambda x, y: -cmp(x[1], y[1]))
     return result
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/12197#comment:2>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.


Reply via email to