Commits

Meikel Brandmeyer  committed 79a1ace

Allow more dynamic behaviour for translate*

translate – the macro – will go through the fast path
as much as possible.

  • Participants
  • Parent commits 197919e

Comments (0)

Files changed (1)

File src/main/clojure/j18n/core.clj

   (doseq [[v p] @bundles]
     (alter-var-root v (constantly (ResourceBundle/getBundle p)))))
 
+(defn ^{:private true} message-to-bundle
+  [message]
+  (when (keyword? message)
+    (when-let [nspace (namespace message)]
+      (::bundle (the-ns (symbol nspace))))))
+
 (defn translate*
-  "Translate the given message in the given bundle. This is basically a
-  wrapper around ResourceBundle/getString."
-  [^String message ^ResourceBundle bundle]
-  (.getString bundle message))
+  "Translate the given message in the given bundle. For resolution rules see
+  translate. However, if message is a string or an unqualified keyword, the
+  bundle has to be given! Only qualified keywords may be used without explicit
+  bundle!"
+  ([message]
+   {:pre [(keyword? message) (namespace message)]}
+   (let [bundle @(resolve (message-to-bundle message))]
+     (translate* message bundle)))
+  ([message ^ResourceBundle bundle]
+   (.getString bundle (name message))))
 
 (defmacro translate
   "Translate the given message. Message may be a qualified or non-qualified