Commits

Meikel Brandmeyer committed fbcc90c

Add translate macro sugar

Comments (0)

Files changed (1)

src/main/clojure/j18n/core.clj

   wrapper around ResourceBundle/getString."
   [^String message ^ResourceBundle bundle]
   (.getString bundle message))
+
+(defmacro translate
+  "Translate the given message. Message may be a qualified or non-qualified
+  keyword or a String. The message is looked up in the given bundle. If no
+  bundle is given, the official bundle of the namespace of the qualified
+  keyword will be used. If the keyword is not qualified or the message key
+  is given as String, the official bundle of the current namespace will be
+  used."
+  ([message]
+   (let [bundle (if (keyword? message)
+                  (if-let [nspace (namespace message)]
+                    (::bundle (meta (the-ns (symbol nspace))))
+                    (::bundle (meta *ns*)))
+                  (::bundle (meta *ns*)))]
+     `(translate ~message ~bundle)))
+  ([message bundle]
+   `(translate* ~(if (keyword? message) (name message) message) ~bundle)))