Merge branch 'develop'

This commit is contained in:
Simon Brooke 2017-09-08 15:26:31 +01:00
commit b18adb0a69
2 changed files with 41 additions and 29 deletions

View file

@ -1,4 +1,4 @@
(defproject org.clojars.simon_brooke/internationalisation "1.0.1"
(defproject org.clojars.simon_brooke/internationalisation "1.0.2"
:description "Internationalisation library for Clojure"
:url "https://github.com/simon-brooke/internationalisation"
:license {:name "Eclipse Public License"
@ -7,4 +7,6 @@
[com.taoensso/timbre "4.10.0"]
[instaparse "1.4.7"]]
:plugins [[lein-codox "0.10.3"]]
:lein-release {:deploy-via :clojars})
:profiles {:dev {:resource-paths ["resources"]}}
:lein-release {:deploy-via :clojars}
)

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,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.; if no useable file is found, returns nil."
(memoize raw-get-messages))