taoensso / tower

i18n & L10n library for Clojure/Script
https://www.taoensso.com/tower
Eclipse Public License 1.0
277 stars 24 forks source link

Issue with ring middleware #26

Closed vincentjames501 closed 11 years ago

vincentjames501 commented 11 years ago

Right I have an Android device set to Spanish (the entire device). The Accept-Language header appears like this: "es-ES, en-US". The issue is that it still prefers the en-US locale when it should use the first one. The issue is in the parse-http-accept-header function.

(defn parse-http-accept-header
  "Parses HTTP Accept header and returns sequence of [choice weight] pairs
  sorted by weight."
  [header]
  (->> (for [choice (->> (str/split (str header) #",")
                         (filter (complement str/blank?)))]
         (let [[lang q] (str/split choice #";")]
           [(-> lang str/trim)
            (or (when q (Float/parseFloat (second (str/split q #"="))))
                1)]))
       (sort-by second) reverse))

Simply change

 (sort-by second) reverse

To

(sort-by second >)

As this accomplishes the same thing and preserves the order.

user=> (parse-http-accept-header "es-ES, en-US")
(["es-ES" 1] ["en-US" 1])
user=> (parse-http-accept-header "en-GB  ,  en; q=0.8, en-US;  q=0.6")
(["en-GB" 1] ["en" 0.8] ["en-US" 0.6])
user=> (parse-http-accept-header "en-GB")
(["en-GB" 1])
user=> (parse-http-accept-header "en-GB,en;q=0.8,en-US;q=0.6")
(["en-GB" 1] ["en" 0.8] ["en-US" 0.6])
user=> (parse-http-accept-header "en-GB;q=0.1,en;q=0.8,en-US;q=0.6")
(["en" 0.8] ["en-US" 0.6] ["en-GB" 0.1])
ptaoussanis commented 11 years ago

Good catch Vincent, thanks! Fixed as of 1.7.1 (just pushed).