From a381edfbe909a838c32fc9788b4030134221bcab Mon Sep 17 00:00:00 2001 From: Charles Huyghues-Despointes Date: Fri, 20 Oct 2017 16:34:58 +0200 Subject: [PATCH 1/2] PoC using macro for clojurescript static dictionary linking --- project.clj | 1 + resources/config.clj | 2 +- src/advenjure/gettext/core.cljc | 6 +++++- src/advenjure/gettext/macro.clj | 5 +++++ 4 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 src/advenjure/gettext/macro.clj 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/resources/config.clj b/resources/config.clj index aa44ad3..22ef578 100644 --- a/resources/config.clj +++ b/resources/config.clj @@ -1 +1 @@ -{:gettext-source 'advenjure.text.en-past/dictionary} +{:gettext-source 'advenjure.text.en-present/dictionary} diff --git a/src/advenjure/gettext/core.cljc b/src/advenjure/gettext/core.cljc index c4b45d9..8d473d7 100644 --- a/src/advenjure/gettext/core.cljc +++ b/src/advenjure/gettext/core.cljc @@ -1,10 +1,14 @@ (ns advenjure.gettext.core + #?(:cljs (:require-macros [advenjure.gettext.macro :refer [config]])) (:require [advenjure.text.en-past] + #?(:clj [advenjure.gettext.macro :refer [config]]) #?(:cljs [goog.string :refer [format]]) #?(:cljs [goog.string.format]))) -(def ^:dynamic *text-source* advenjure.text.en-past/dictionary) +(if (config :gettext-source) + (require [(symbol (namespace (symbol (config :gettext-source))))])) +(def ^:dynamic *text-source* (eval (symbol (config :gettext-source)))) ;; copypasted from clojure-gettext for now, until I figure out how ;; to properly make it work for both clj and cljs diff --git a/src/advenjure/gettext/macro.clj b/src/advenjure/gettext/macro.clj new file mode 100644 index 0000000..ccbbd38 --- /dev/null +++ b/src/advenjure/gettext/macro.clj @@ -0,0 +1,5 @@ +(ns advenjure.gettext.macro + (:require [carica.core :as carica])) + +(defmacro config [kw] + (str (carica/config kw))) From 0115794404eae95b5511dda2713eb2711581b59e Mon Sep 17 00:00:00 2001 From: Facundo Olano Date: Sat, 21 Oct 2017 20:11:06 -0300 Subject: [PATCH 2/2] set en past as default dictionary and cleanup the code --- resources/config.clj | 2 +- src/advenjure/gettext/core.cljc | 23 +++++++++++++---------- src/advenjure/gettext/macro.clj | 5 ----- 3 files changed, 14 insertions(+), 16 deletions(-) delete mode 100644 src/advenjure/gettext/macro.clj diff --git a/resources/config.clj b/resources/config.clj index 22ef578..aa44ad3 100644 --- a/resources/config.clj +++ b/resources/config.clj @@ -1 +1 @@ -{:gettext-source 'advenjure.text.en-present/dictionary} +{:gettext-source 'advenjure.text.en-past/dictionary} diff --git a/src/advenjure/gettext/core.cljc b/src/advenjure/gettext/core.cljc index 8d473d7..2b6b7db 100644 --- a/src/advenjure/gettext/core.cljc +++ b/src/advenjure/gettext/core.cljc @@ -1,29 +1,32 @@ (ns advenjure.gettext.core - #?(:cljs (:require-macros [advenjure.gettext.macro :refer [config]])) (:require - [advenjure.text.en-past] - #?(:clj [advenjure.gettext.macro :refer [config]]) + #?(:clj [carica.core :as carica]) #?(:cljs [goog.string :refer [format]]) #?(:cljs [goog.string.format]))) -(if (config :gettext-source) - (require [(symbol (namespace (symbol (config :gettext-source))))])) -(def ^:dynamic *text-source* (eval (symbol (config :gettext-source)))) +(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))) diff --git a/src/advenjure/gettext/macro.clj b/src/advenjure/gettext/macro.clj deleted file mode 100644 index ccbbd38..0000000 --- a/src/advenjure/gettext/macro.clj +++ /dev/null @@ -1,5 +0,0 @@ -(ns advenjure.gettext.macro - (:require [carica.core :as carica])) - -(defmacro config [kw] - (str (carica/config kw)))