From 5539c863d145a08801c6fb75401a421a3778d823 Mon Sep 17 00:00:00 2001 From: simon Date: Wed, 9 Aug 2017 16:25:23 +0100 Subject: [PATCH 1/3] Fixed all issues detected by Eastwood. --- src/scot/weft/i18n/core.clj | 2 +- test/scot/weft/i18n/test/core.clj | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/scot/weft/i18n/core.clj b/src/scot/weft/i18n/core.clj index 99fcc9b..31e6c09 100644 --- a/src/scot/weft/i18n/core.clj +++ b/src/scot/weft/i18n/core.clj @@ -136,7 +136,7 @@ Returns a map of message keys to strings." {:doc/format :markdown} - [accept-language-header resource-path default-locale] + [^String accept-language-header ^String resource-path ^String default-locale] (let [file-path (first (remove nil? diff --git a/test/scot/weft/i18n/test/core.clj b/test/scot/weft/i18n/test/core.clj index 41f74f2..f1acff6 100644 --- a/test/scot/weft/i18n/test/core.clj +++ b/test/scot/weft/i18n/test/core.clj @@ -1,7 +1,7 @@ (ns ^{:doc "Tests for Internationalisation." :author "Simon Brooke"} scot.weft.i18n.test.core - (:use clojure.test - scot.weft.i18n.core)) + (:require [clojure.test :refer :all] + [scot.weft.i18n.core :refer :all])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; From a574b527c09ad0a69b3b0edd7c2fba026e41141c Mon Sep 17 00:00:00 2001 From: simon Date: Wed, 9 Aug 2017 16:36:33 +0100 Subject: [PATCH 2/3] Unused using removed. --- src/scot/weft/i18n/core.clj | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/scot/weft/i18n/core.clj b/src/scot/weft/i18n/core.clj index 31e6c09..da12bb3 100644 --- a/src/scot/weft/i18n/core.clj +++ b/src/scot/weft/i18n/core.clj @@ -1,8 +1,7 @@ (ns ^{:doc "Internationalisation." :author "Simon Brooke"} scot.weft.i18n.core - (:require [clojure.string :as cs] - [clojure.java.io :as io] + (:require [clojure.java.io :as io] [instaparse.core :as insta] [taoensso.timbre :as timbre])) From 53124ab1a8a3320b29abaddb657ad465ba1b2043 Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Thu, 7 Sep 2017 14:09:53 +0100 Subject: [PATCH 3/3] Don't throw exception if no valid file found; instead log it, and return nil. --- src/scot/weft/i18n/core.clj | 22 +++++++++++++--------- test/scot/weft/i18n/test/core.clj | 5 ++++- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/scot/weft/i18n/core.clj b/src/scot/weft/i18n/core.clj index da12bb3..9187eea 100644 --- a/src/scot/weft/i18n/core.clj +++ b/src/scot/weft/i18n/core.clj @@ -133,7 +133,7 @@ * `default-locale` should be a locale specifier to use if no acceptable locale can be identified. - Returns a map of message keys to strings." + Returns a map of message keys to strings; if no useable file is found, returns nil." {:doc/format :markdown} [^String accept-language-header ^String resource-path ^String default-locale] (let [file-path (first @@ -143,14 +143,18 @@ #(find-language-file-name % resource-path) (acceptable-languages accept-language-header))))] (timbre/debug (str "Found i18n file at '" file-path "'")) - (read-string - (slurp - (or - file-path - (.getAbsolutePath - (io/file - resource-path - (str default-locale ".edn")))))))) + (try + (read-string + (slurp + (or + file-path + (.getAbsolutePath + (io/file + resource-path + (str default-locale ".edn")))))) + (catch Exception any + (timbre/error (str "Failed to load internationalisation because " (.getMessage any))) + nil)))) (def get-messages diff --git a/test/scot/weft/i18n/test/core.clj b/test/scot/weft/i18n/test/core.clj index f1acff6..49621de 100644 --- a/test/scot/weft/i18n/test/core.clj +++ b/test/scot/weft/i18n/test/core.clj @@ -203,4 +203,7 @@ (is (= "Ceci n'est pas une pipe." - (:pipe (get-messages "en-GB;q=0.9, fr-FR" "resources/i18n" "en-GB")))))) + (:pipe (get-messages "en-GB;q=0.9, fr-FR" "resources/i18n" "en-GB")))) + (is + (= nil (get-messages "xx-XX;q=0.5, yy-YY" "resources/i18n" "zz-ZZ")) + "If no usable file is found, an exception should not be thrown.")))