Merge branch 'develop'

This commit is contained in:
Simon Brooke 2017-09-07 14:10:44 +01:00
commit 9260c7b553
3 changed files with 22 additions and 16 deletions

View file

@ -1,4 +1,4 @@
(defproject org.clojars.simon_brooke/internationalisation "1.0.1" (defproject org.clojars.simon_brooke/internationalisation "1.0.1-SNAPSHOT"
:description "Internationalisation library for Clojure" :description "Internationalisation library for Clojure"
:url "https://github.com/simon-brooke/internationalisation" :url "https://github.com/simon-brooke/internationalisation"
:license {:name "Eclipse Public License" :license {:name "Eclipse Public License"

View file

@ -1,8 +1,7 @@
(ns ^{:doc "Internationalisation." (ns ^{:doc "Internationalisation."
:author "Simon Brooke"} :author "Simon Brooke"}
scot.weft.i18n.core scot.weft.i18n.core
(:require [clojure.string :as cs] (:require [clojure.java.io :as io]
[clojure.java.io :as io]
[instaparse.core :as insta] [instaparse.core :as insta]
[taoensso.timbre :as timbre])) [taoensso.timbre :as timbre]))
@ -134,9 +133,9 @@
* `default-locale` should be a locale specifier to use if no acceptable locale can be * `default-locale` should be a locale specifier to use if no acceptable locale can be
identified. 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} {:doc/format :markdown}
[accept-language-header resource-path default-locale] [^String accept-language-header ^String resource-path ^String default-locale]
(let [file-path (first (let [file-path (first
(remove (remove
nil? nil?
@ -144,14 +143,18 @@
#(find-language-file-name % resource-path) #(find-language-file-name % resource-path)
(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 "'"))
(read-string (try
(slurp (read-string
(or (slurp
file-path (or
(.getAbsolutePath file-path
(io/file (.getAbsolutePath
resource-path (io/file
(str default-locale ".edn")))))))) resource-path
(str default-locale ".edn"))))))
(catch Exception any
(timbre/error (str "Failed to load internationalisation because " (.getMessage any)))
nil))))
(def get-messages (def get-messages

View file

@ -1,7 +1,7 @@
(ns ^{:doc "Tests for Internationalisation." (ns ^{:doc "Tests for Internationalisation."
:author "Simon Brooke"} scot.weft.i18n.test.core :author "Simon Brooke"} scot.weft.i18n.test.core
(:use clojure.test (:require [clojure.test :refer :all]
scot.weft.i18n.core)) [scot.weft.i18n.core :refer :all]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; ;;;;
@ -203,4 +203,7 @@
(is (is
(= (=
"Ceci n'est pas une pipe." "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.")))