From f581ae0172a0f0787c5f013f34f0631ed7fbd2b8 Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Thu, 7 Sep 2017 17:10:57 +0100 Subject: [PATCH] Better, I think, but resources not found in test? --- project.clj | 4 ++- src/scot/weft/i18n/core.clj | 64 +++++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/project.clj b/project.clj index 9317f44..c72f0b2 100644 --- a/project.clj +++ b/project.clj @@ -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"]}} + ) diff --git a/src/scot/weft/i18n/core.clj b/src/scot/weft/i18n/core.clj index 9187eea..fb42226 100644 --- a/src/scot/weft/i18n/core.clj +++ b/src/scot/weft/i18n/core.clj @@ -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)) - - - -