Skip to content
Snippets Groups Projects
Unverified Commit 305a91d5 authored by metamben's avatar metamben Committed by GitHub
Browse files

Select locale based on query param (#22600)

parent 095e0d91
No related branches found
No related tags found
No related merge requests found
......@@ -52,9 +52,11 @@
(log/warn (.getMessage e))))))
(fallback-localization locale-string)))
(def ^:private ^{:arglists '([])} load-localization
"Load a JSON-encoded map of localized strings for the current user's Locale."
(comp (memoize load-localization*) #(i18n/user-locale-string)))
(let [load-fn (memoize load-localization*)]
(defn- load-localization
"Load a JSON-encoded map of localized strings for the current user's Locale."
[locale-override]
(load-fn (or locale-override (i18n/user-locale-string)))))
(defn- load-inline-js* [resource-name]
(slurp (io/resource (format "frontend_client/inline_js/%s.js" resource-name))))
......@@ -69,14 +71,14 @@
(log/error e message)
(throw (Exception. message e))))))
(defn- load-entrypoint-template [entrypoint-name embeddable? uri]
(defn- load-entrypoint-template [entrypoint-name embeddable? {:keys [uri params]}]
(load-template
(str "frontend_client/" entrypoint-name ".html")
(let [{:keys [anon-tracking-enabled google-auth-client-id], :as public-settings} (setting/user-readable-values-map :public)]
{:bootstrapJS (load-inline-js "index_bootstrap")
:googleAnalyticsJS (load-inline-js "index_ganalytics")
:bootstrapJSON (escape-script (json/generate-string public-settings))
:localizationJSON (escape-script (load-localization))
:localizationJSON (escape-script (load-localization (:locale params)))
:language (hiccup.util/escape-html (public-settings/site-locale))
:favicon (hiccup.util/escape-html (public-settings/application-favicon-url))
:applicationName (hiccup.util/escape-html (public-settings/application-name))
......@@ -93,10 +95,10 @@
(defn- entrypoint
"Response that serves up an entrypoint into the Metabase application, e.g. `index.html`."
[entrypoint-name embeddable? {:keys [uri]} respond _raise]
[entrypoint-name embeddable? request respond _raise]
(respond
(-> (response/response (if (init-status/complete?)
(load-entrypoint-template entrypoint-name embeddable? uri)
(load-entrypoint-template entrypoint-name embeddable? request)
(load-init-template)))
(response/content-type "text/html; charset=utf-8"))))
......
......@@ -24,7 +24,7 @@
"translations" {"" {"Your database has been added!" {"msgstr" ["¡Tu base de datos ha sido añadida!"]}}}}
(some->
(binding [i18n/*user-locale* "es"]
(#'index/load-localization))
(#'index/load-localization nil))
json/parse-string
(update "translations" select-keys [""])
(update-in ["translations" ""] select-keys ["Your database has been added!"]))))))
......@@ -36,7 +36,7 @@
(mt/suppress-output
(some->
(binding [i18n/*user-locale* "xx"]
(#'index/load-localization))
(#'index/load-localization nil))
json/parse-string))))))
(deftest english-test
......@@ -45,5 +45,32 @@
"translations" {"" {"Metabase" {"msgid" "Metabase", "msgstr" ["Metabase"]}}}}
(some->
(binding [i18n/*user-locale* "en"]
(#'index/load-localization))
(#'index/load-localization nil))
json/parse-string)))))
(deftest override-localization-test
(testing "a valid override is honored no matter what the user locale is"
(is (= {"charset" "utf-8"
"headers" {"mime-version" "1.0"
"content-type" "text/plain; charset=UTF-8"
"content-transfer-encoding" "8bit"
"x-generator" "POEditor.com"
"project-id-version" "Metabase"
"language" "es"
"plural-forms" "nplurals=2; plural=(n != 1);"}
"translations" {"" {"Your database has been added!" {"msgstr" ["¡Tu base de datos ha sido añadida!"]}}}}
(some->
(binding [i18n/*user-locale* "xx"]
(#'index/load-localization "es"))
json/parse-string
(update "translations" select-keys [""])
(update-in ["translations" ""] select-keys ["Your database has been added!"])))))
(testing "an invalid override causes a fallback to English"
(is (= {"headers" {"language" "yy", "plural-forms" "nplurals=2; plural=(n != 1);"}
"translations" {"" {"Metabase" {"msgid" "Metabase", "msgstr" ["Metabase"]}}}}
(mt/suppress-output
(some->
(binding [i18n/*user-locale* "xx"]
(#'index/load-localization "yy"))
json/parse-string))))))
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