Better, I think, but resources not found in test?

This commit is contained in:
Simon Brooke 2017-09-07 17:10:57 +01:00
parent 53124ab1a8
commit f581ae0172
2 changed files with 40 additions and 28 deletions

View file

@ -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"]}}
)

View file

@ -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,22 +97,37 @@
(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
(let [file-path (if
(string? language-spec)
(.exists (io/file resource-path (str language-spec ".edn"))))
(.getAbsolutePath (io/file resource-path (str language-spec ".edn")))
(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
@ -119,7 +135,7 @@
resource-path)
(find-language-file-name
(:language language-spec)
resource-path))))
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
(slurp-resource
(or
file-path
(.getAbsolutePath
(io/file
resource-path
(str default-locale ".edn"))))))
(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))