diff --git a/project.clj b/project.clj index fe266e4..a6d01ab 100644 --- a/project.clj +++ b/project.clj @@ -7,6 +7,7 @@ [org.clojure/clojurescript "1.9.229"] [org.clojure/core.async "0.2.391"] [gettext "0.1.1"] + [sonian/carica "1.2.2"] [jline/jline "2.8"] [cljs-ajax "0.5.8"] [org.clojure/data.json "0.2.6"] diff --git a/src/advenjure/gettext/core.cljc b/src/advenjure/gettext/core.cljc index c4b45d9..2b6b7db 100644 --- a/src/advenjure/gettext/core.cljc +++ b/src/advenjure/gettext/core.cljc @@ -1,25 +1,32 @@ (ns advenjure.gettext.core (:require - [advenjure.text.en-past] + #?(:clj [carica.core :as carica]) #?(:cljs [goog.string :refer [format]]) #?(:cljs [goog.string.format]))) -(def ^:dynamic *text-source* advenjure.text.en-past/dictionary) +(defmacro resolve-source + "In order to make a config symbol available both in clj and cljs, wrap its + evaluation in a macro, so it's done at compile time by clojure, when the + config is available." + [] + (let [sym (carica/config :gettext-source) + nspace (symbol (namespace sym))] + (require nspace) + sym)) -;; copypasted from clojure-gettext for now, until I figure out how -;; to properly make it work for both clj and cljs +(def text-source (resolve-source)) (defn gettext "Look up the given key in the current text source dictionary. If not found return the key itself." [text-key & replacements] - (let [text-value (get *text-source* text-key text-key) + (let [text-value (get text-source text-key text-key) text-value (if (fn? text-value) (text-value nil) text-value)] (apply format text-value replacements))) (defn pgettext [ctx text-key & replacements] - (let [text-value (get *text-source* text-key text-key) + (let [text-value (get text-source text-key text-key) text-value (if (fn? text-value) (text-value ctx) text-value)] (apply format text-value replacements)))