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"]
|
:dependencies [[org.clojure/clojure "1.7.0"]
|
||||||
[com.taoensso/timbre "4.10.0"]
|
[com.taoensso/timbre "4.10.0"]
|
||||||
[instaparse "1.4.7"]]
|
[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"}
|
:author "Simon Brooke"}
|
||||||
scot.weft.i18n.core
|
scot.weft.i18n.core
|
||||||
(:require [clojure.java.io :as io]
|
(:require [clojure.java.io :as io]
|
||||||
|
[clojure.string :refer [join]]
|
||||||
[instaparse.core :as insta]
|
[instaparse.core :as insta]
|
||||||
[taoensso.timbre :as timbre]))
|
[taoensso.timbre :as timbre]))
|
||||||
|
|
||||||
|
@ -96,22 +97,37 @@
|
||||||
(parse-accept-language-header accept-language-header)))))
|
(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
|
(defn find-language-file-name
|
||||||
"Find the name of a messages file on this resource path which matches this `language-spec`.
|
"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
|
* `language-spec` should be either a map as generated by `generate-accept-languages`, or
|
||||||
else a string;
|
else a string;
|
||||||
* `resource-path` should be the fully-qualified path name of the directory in which
|
* `resource-path` should be the path name of the directory in which message files are stored,
|
||||||
message files are stored.
|
within the resources on the classpath.
|
||||||
|
|
||||||
Returns the name of an appropriate file if any is found, else nil."
|
Returns the name of an appropriate file if any is found, else nil."
|
||||||
{:doc/format :markdown}
|
{:doc/format :markdown}
|
||||||
[language-spec resource-path]
|
[language-spec resource-path]
|
||||||
(cond
|
(let [file-path (if
|
||||||
(and
|
|
||||||
(string? language-spec)
|
(string? language-spec)
|
||||||
(.exists (io/file resource-path (str language-spec ".edn"))))
|
(join
|
||||||
(.getAbsolutePath (io/file resource-path (str language-spec ".edn")))
|
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)
|
(map? language-spec)
|
||||||
(or
|
(or
|
||||||
(find-language-file-name
|
(find-language-file-name
|
||||||
|
@ -119,7 +135,7 @@
|
||||||
resource-path)
|
resource-path)
|
||||||
(find-language-file-name
|
(find-language-file-name
|
||||||
(:language language-spec)
|
(:language language-spec)
|
||||||
resource-path))))
|
resource-path)))))
|
||||||
|
|
||||||
|
|
||||||
(defn raw-get-messages
|
(defn raw-get-messages
|
||||||
|
@ -144,14 +160,12 @@
|
||||||
(acceptable-languages accept-language-header))))]
|
(acceptable-languages accept-language-header))))]
|
||||||
(timbre/debug (str "Found i18n file at '" file-path "'"))
|
(timbre/debug (str "Found i18n file at '" file-path "'"))
|
||||||
(try
|
(try
|
||||||
(read-string
|
(slurp-resource
|
||||||
(slurp
|
|
||||||
(or
|
(or
|
||||||
file-path
|
file-path
|
||||||
(.getAbsolutePath
|
(join java.io.File/separator
|
||||||
(io/file
|
[resource-path
|
||||||
resource-path
|
(str default-locale ".edn")])))
|
||||||
(str default-locale ".edn"))))))
|
|
||||||
(catch Exception any
|
(catch Exception any
|
||||||
(timbre/error (str "Failed to load internationalisation because " (.getMessage any)))
|
(timbre/error (str "Failed to load internationalisation because " (.getMessage any)))
|
||||||
nil))))
|
nil))))
|
||||||
|
@ -168,7 +182,3 @@
|
||||||
|
|
||||||
Returns a map of message keys to strings."
|
Returns a map of message keys to strings."
|
||||||
(memoize raw-get-messages))
|
(memoize raw-get-messages))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue