Skip to content
Snippets Groups Projects
Commit c215eac3 authored by Nicolò Pretto's avatar Nicolò Pretto
Browse files

wip locale header middleware

parent 688b9ad9
No related branches found
No related tags found
No related merge requests found
......@@ -15,6 +15,7 @@
[metabase.server.middleware.session :as mw.session]
[metabase.server.middleware.ssl :as mw.ssl]
[metabase.server.routes :as routes]
[metabase.util.i18n :as i18n]
[metabase.util.log :as log]
[ring.core.protocols :as ring.protocols]
[ring.middleware.cookies :refer [wrap-cookies]]
......@@ -42,11 +43,22 @@
(name kkey))
response output-stream)))
(defn wrap-locale-binding
"Sets *header-locale* with the value from X-Metabase-Locale"
[handler]
(fn [{ {:strs [x-metabase-locale ]} :headers, :as request} respond raise]
(binding [i18n/*header-locale*(or x-metabase-locale nil)]
(handler request respond raise))))
(def ^:private middleware
;; ▼▼▼ POST-PROCESSING ▼▼▼ happens from TOP-TO-BOTTOM
[#'mw.exceptions/catch-uncaught-exceptions ; catch any Exceptions that weren't passed to `raise`
#'mw.exceptions/catch-api-exceptions ; catch exceptions and return them in our expected format
#'mw.log/log-api-call
#'wrap-locale-binding
#'mw.browser-cookie/ensure-browser-id-cookie ; add cookie to identify browser; add `:browser-id` to the request
#'mw.security/add-security-headers ; Add HTTP headers to API responses to prevent them from being cached
#(ring.json/wrap-json-body % {:keywords? true}) ; extracts json POST body and makes it available on request
......
......@@ -22,6 +22,10 @@
normalized-locale-string
translate])
(def ^:dynamic *header-locale*
"This will contain the value for the locale coming from `X-Metabase-Locale`"
nil)
(def ^:dynamic *user-locale*
"Bind this to a string, keyword, or `Locale` to set the locale for the current User. To get the locale we should
*use*, use the `user-locale` function instead."
......@@ -44,7 +48,9 @@
"Locale string we should *use* for the current User (e.g. `tru` messages) -- `*user-locale*` if bound, otherwise the
system locale as a string."
[]
(or *user-locale*
(or
*header-locale*
*user-locale*
(site-locale-string)))
(defn site-locale
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment