mirror of
https://github.com/penpot/penpot.git
synced 2025-03-16 01:31:22 -05:00
♻️ Refactor storage and i18n internals.
This commit is contained in:
parent
51c39d169f
commit
e9d5eccd16
2 changed files with 30 additions and 29 deletions
|
@ -8,16 +8,17 @@
|
||||||
(ns uxbox.util.i18n
|
(ns uxbox.util.i18n
|
||||||
"A i18n foundation."
|
"A i18n foundation."
|
||||||
(:require
|
(:require
|
||||||
[cuerdas.core :as str]
|
|
||||||
[rumext.alpha :as mf]
|
|
||||||
[beicon.core :as rx]
|
[beicon.core :as rx]
|
||||||
|
[cuerdas.core :as str]
|
||||||
[goog.object :as gobj]
|
[goog.object :as gobj]
|
||||||
|
[okulary.core :as l]
|
||||||
|
[rumext.alpha :as mf]
|
||||||
[uxbox.config :as cfg]
|
[uxbox.config :as cfg]
|
||||||
[uxbox.util.transit :as t]
|
[uxbox.util.storage :refer [storage]]
|
||||||
[uxbox.util.storage :refer [storage]]))
|
[uxbox.util.transit :as t]))
|
||||||
|
|
||||||
(defonce locale (get storage ::locale cfg/default-language))
|
(defonce locale (l/atom (or (get storage ::locale)
|
||||||
(defonce locale-sub (rx/subject))
|
cfg/default-language)))
|
||||||
(defonce translations #js {})
|
(defonce translations #js {})
|
||||||
|
|
||||||
;; The traslations `data` is a javascript object and should be treated
|
;; The traslations `data` is a javascript object and should be treated
|
||||||
|
@ -33,8 +34,7 @@
|
||||||
(defn set-current-locale!
|
(defn set-current-locale!
|
||||||
[v]
|
[v]
|
||||||
(swap! storage assoc ::locale v)
|
(swap! storage assoc ::locale v)
|
||||||
(set! locale v)
|
(reset! locale v))
|
||||||
(rx/push! locale-sub v))
|
|
||||||
|
|
||||||
(defn set-default-locale!
|
(defn set-default-locale!
|
||||||
[]
|
[]
|
||||||
|
@ -76,15 +76,12 @@
|
||||||
(apply str/format value (map #(if (c? %) @% %) args)))))
|
(apply str/format value (map #(if (c? %) @% %) args)))))
|
||||||
|
|
||||||
(defn tr
|
(defn tr
|
||||||
([code] (t locale code))
|
([code] (t @locale code))
|
||||||
([code & args] (apply t locale code args)))
|
([code & args] (apply t @locale code args)))
|
||||||
|
|
||||||
|
|
||||||
|
;; DEPRECATED
|
||||||
(defn use-locale
|
(defn use-locale
|
||||||
[]
|
[]
|
||||||
(let [[locale set-locale] (mf/useState locale)]
|
(mf/deref locale))
|
||||||
(mf/useEffect (fn []
|
|
||||||
(let [sub (rx/sub! locale-sub #(set-locale %))]
|
|
||||||
#(rx/dispose! sub)))
|
|
||||||
#js [])
|
|
||||||
locale))
|
|
||||||
|
|
||||||
|
|
|
@ -9,24 +9,27 @@
|
||||||
|
|
||||||
(defn- persist
|
(defn- persist
|
||||||
[alias value]
|
[alias value]
|
||||||
(let [key (name alias)
|
(when-not (or (= *target* "nodejs")
|
||||||
value (t/encode value)]
|
(not (exists? js/window)))
|
||||||
(.setItem js/localStorage key value)))
|
(let [key (name alias)
|
||||||
|
value (t/encode value)]
|
||||||
|
(.setItem js/localStorage key value))))
|
||||||
|
|
||||||
(defn- load
|
(defn- load
|
||||||
[alias]
|
[alias]
|
||||||
(if (or (= *target* "nodejs") (not (exists? js/window)))
|
(when-not (or (= *target* "nodejs")
|
||||||
{}
|
(not (exists? js/window)))
|
||||||
(let [data (.getItem js/localStorage (name alias))]
|
(let [data (.getItem js/localStorage (name alias))]
|
||||||
(if data
|
(try
|
||||||
(t/decode data)
|
(t/decode data)
|
||||||
{}))))
|
(catch :default e
|
||||||
|
(js/console.error "Error on loading data from local storage." e)
|
||||||
|
nil)))))
|
||||||
|
|
||||||
(defn make-storage
|
(defn- make-storage
|
||||||
[alias]
|
[alias]
|
||||||
(let [data (atom (load alias))]
|
(let [data (atom (load alias))]
|
||||||
(when (not= *target* "nodejs")
|
(add-watch data :sub #(persist alias %4))
|
||||||
(add-watch data :sub #(persist alias %4)))
|
|
||||||
(reify
|
(reify
|
||||||
Object
|
Object
|
||||||
(toString [_]
|
(toString [_]
|
||||||
|
@ -56,8 +59,9 @@
|
||||||
|
|
||||||
ILookup
|
ILookup
|
||||||
(-lookup [_ key]
|
(-lookup [_ key]
|
||||||
(-lookup @data key nil))
|
(get @data key nil))
|
||||||
(-lookup [_ key not-found]
|
(-lookup [_ key not-found]
|
||||||
(-lookup @data key not-found)))))
|
(get @data key not-found)))))
|
||||||
|
|
||||||
(def storage (make-storage "uxbox"))
|
(def storage
|
||||||
|
(make-storage "uxbox"))
|
||||||
|
|
Loading…
Add table
Reference in a new issue