taoensso / tempura

Simple text localization library for Clojure/Script
https://www.taoensso.com/tempura
Eclipse Public License 1.0
260 stars 16 forks source link

Simple Ring middleware for attaching `tr` partial to requests #1

Closed ptaoussanis closed 7 years ago

ptaoussanis commented 7 years ago

i.e. parse HTTP "accept-language", etc.

Please ping if there's demand, and I'll try get this added.

sveri commented 7 years ago

I would like to have that as it is something that I added myself. Also my middleware reads the accept-language header from the request. You can find it here: https://github.com/sveri/getless-clj/blob/08f02a63b0d90ef6b78588d1e4b303f93ff45ec3/src/clj/de/sveri/getless/middleware.clj#L25

(defn add-locale [handler]
  (fn [req]
    (let [accept-language (get-in req [:headers "accept-language"])
          short-lang (cond
                       (.contains accept-language "de") "de"
                       :else "en")]
      (handler (assoc req :localize (partial tr
                                             {:default-locale :en
                                              :dict           loc/local-dict}
                                             [(keyword short-lang)]))))))

It is very simple and only for my current needs. There should be a better parser of course.

ptaoussanis commented 7 years ago

Hi @sveri, this was added to Tempura some time ago: https://github.com/ptaoussanis/tempura/blob/fbb6cfde7ecff25e5d4a6543565752df59871a21/src/taoensso/tempura.cljc#L314

Hope that helps?

Cheers! :-)

sveri commented 7 years ago

Awesome, I did not see that function, should have looked at the other issues first. Thanks :-)

ptaoussanis commented 7 years ago

No problem, appreciate you checking about this :-)