Better, I think, but resources not found in test?
This commit is contained in:
parent
53124ab1a8
commit
f581ae0172
|
@ -6,4 +6,6 @@
|
|||
:dependencies [[org.clojure/clojure "1.7.0"]
|
||||
[com.taoensso/timbre "4.10.0"]
|
||||
[instaparse "1.4.7"]]
|
||||
:plugins [[lein-codox "0.10.3"]])
|
||||
:plugins [[lein-codox "0.10.3"]]
|
||||
:profiles {:dev {:resource-paths ["resources"]}}
|
||||
)
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
:author "Simon Brooke"}
|
||||
scot.weft.i18n.core
|
||||
(:require [clojure.java.io :as io]
|
||||
[clojure.string :refer [join]]
|
||||
[instaparse.core :as insta]
|
||||
[taoensso.timbre :as timbre]))
|
||||
|
||||
|
@ -96,30 +97,45 @@
|
|||
(parse-accept-language-header accept-language-header)))))
|
||||
|
||||
|
||||
(defn slurp-resource
|
||||
"Slurp the resource of this name and return its contents as a string; but if it doesn't
|
||||
exist log the fact and return nil, rather than throwing an exception."
|
||||
[name]
|
||||
(try
|
||||
(slurp (io/resource name))
|
||||
(catch Exception any
|
||||
(timbre/error (str "Resource at " name " does not exist."))
|
||||
nil)))
|
||||
|
||||
|
||||
(defn find-language-file-name
|
||||
"Find the name of a messages file on this resource path which matches this `language-spec`.
|
||||
|
||||
* `language-spec` should be either a map as generated by `generate-accept-languages`, or
|
||||
else a string;
|
||||
* `resource-path` should be the fully-qualified path name of the directory in which
|
||||
message files are stored.
|
||||
* `resource-path` should be the path name of the directory in which message files are stored,
|
||||
within the resources on the classpath.
|
||||
|
||||
Returns the name of an appropriate file if any is found, else nil."
|
||||
{:doc/format :markdown}
|
||||
[language-spec resource-path]
|
||||
(cond
|
||||
(and
|
||||
(string? language-spec)
|
||||
(.exists (io/file resource-path (str language-spec ".edn"))))
|
||||
(.getAbsolutePath (io/file resource-path (str language-spec ".edn")))
|
||||
(map? language-spec)
|
||||
(or
|
||||
(find-language-file-name
|
||||
(str (:language language-spec) "-" (:qualifier language-spec))
|
||||
resource-path)
|
||||
(find-language-file-name
|
||||
(:language language-spec)
|
||||
resource-path))))
|
||||
(let [file-path (if
|
||||
(string? language-spec)
|
||||
(join
|
||||
java.io.File/separator
|
||||
[resource-path (str language-spec ".edn")]))
|
||||
contents (if file-path (slurp-resource file-path))]
|
||||
(cond
|
||||
contents
|
||||
file-path
|
||||
(map? language-spec)
|
||||
(or
|
||||
(find-language-file-name
|
||||
(str (:language language-spec) "-" (:qualifier language-spec))
|
||||
resource-path)
|
||||
(find-language-file-name
|
||||
(:language language-spec)
|
||||
resource-path)))))
|
||||
|
||||
|
||||
(defn raw-get-messages
|
||||
|
@ -144,14 +160,12 @@
|
|||
(acceptable-languages accept-language-header))))]
|
||||
(timbre/debug (str "Found i18n file at '" file-path "'"))
|
||||
(try
|
||||
(read-string
|
||||
(slurp
|
||||
(or
|
||||
file-path
|
||||
(.getAbsolutePath
|
||||
(io/file
|
||||
resource-path
|
||||
(str default-locale ".edn"))))))
|
||||
(slurp-resource
|
||||
(or
|
||||
file-path
|
||||
(join java.io.File/separator
|
||||
[resource-path
|
||||
(str default-locale ".edn")])))
|
||||
(catch Exception any
|
||||
(timbre/error (str "Failed to load internationalisation because " (.getMessage any)))
|
||||
nil))))
|
||||
|
@ -168,7 +182,3 @@
|
|||
|
||||
Returns a map of message keys to strings."
|
||||
(memoize raw-get-messages))
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue