/
locale.cljc
31 lines (26 loc) · 992 Bytes
/
locale.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
(ns com.fulcrologic.rad.locale
#?(:cljs (:require-macros com.fulcrologic.rad.locale))
(:require
#?@(:cljs [[goog.object :as gobj]]
:clj [[com.fulcrologic.fulcro.components :as comp]]))
#?(:clj
(:import (java.util Locale))))
(def ^:dynamic *current-locale*
"The current locale. In CLJ this will be a Locale object. In CLJS this will be a locale string like en-US."
#?(:clj (Locale/getDefault)
:cljs "en-US"))
(defn current-locale [] *current-locale*)
(defn set-locale!
"Set the locale of the application to the given locale code, e.g. `en-US`."
[code]
#?(:clj
(alter-var-root (var *current-locale*) (constantly (Locale/forLanguageTag code)))
:cljs
(set! *current-locale* code)))
#?(:clj
(defmacro with-locale [nm & body]
(let [locale-expr (if (comp/cljs? &env)
nm
`(java.util.Locale/forLanguageTag ~nm))]
`(binding [*current-locale* ~locale-expr]
~@body))))