Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gettext working again #43

Merged
merged 2 commits into from
Oct 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
19 changes: 13 additions & 6 deletions src/advenjure/gettext/core.cljc
Original file line number Diff line number Diff line change
@@ -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)))

Expand Down