From aadf7f46a091fdfaa4664df4392e1e5071d66e97 Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Wed, 4 Jan 2023 22:55:29 +0000 Subject: [PATCH] Considerably improved reliability, return token if no message found. --- src/scot/weft/i18n/core.clj | 46 +++++++++++++++++++------------ test/scot/weft/i18n/test/core.clj | 3 +- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/scot/weft/i18n/core.clj b/src/scot/weft/i18n/core.clj index 0e2117c..1ed6a45 100644 --- a/src/scot/weft/i18n/core.clj +++ b/src/scot/weft/i18n/core.clj @@ -119,7 +119,7 @@ (try (slurp (io/resource name)) (catch Exception _ - (timbre/error (str "Resource at " name " does not exist.")) + (timbre/warn (str "Resource at " name " does not exist.")) nil))) @@ -167,23 +167,31 @@ 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 - (remove - nil? + (let [file-paths (remove + empty? (map #(find-language-file-name % resource-path) - (acceptable-languages accept-language-header))))] - (timbre/debug (str "Found i18n file at '" file-path "'")) - (try - (read-string - (slurp-resource - (or - file-path - (join java.io.File/separator + (acceptable-languages accept-language-header))) + default-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))) + (str default-locale ".edn")]) + paths (concat file-paths (list default-path)) + text (first + (remove empty? + (map + slurp-resource + paths)))] + (if text + (try + (read-string text) + (catch Exception any + (timbre/error "Failed to load internationalisation because " + (.getName (.getClass any)) + (.getMessage any)) + nil)) + ;; else + (doall + (timbre/error "No valid i18n files found, not even default. Tried" paths) nil)))) (def get-messages @@ -200,7 +208,8 @@ (def get-message "Return the message keyed by this `token` from the most acceptable messages collection - we have given this `accept-language-header`. + we have given this `accept-language-header`, if passed, or the current default language + otherwise. If no message is found, return the token. * `token` should be a clojure keyword identifying the message to be retrieved; * `accept-language-header` should be the value of an RFC2616 `Accept-Language` header; @@ -209,8 +218,9 @@ * `default-locale` should be a locale specifier to use if no acceptable locale can be identified." (fn ([^Keyword token ^String accept-language-header ^String resource-path ^String default-locale] - ((get-messages accept-language-header resource-path default-locale) token)) + (let [message ((get-messages accept-language-header resource-path default-locale) token)] + (or message (name token)))) ([^Keyword token ^String accept-language-header] (get-message token accept-language-header *resource-path* *default-language*)) ([^Keyword token] - (get-message token nil *resource-path* *default-language*)))) \ No newline at end of file + (get-message token *default-language* *resource-path* *default-language*)))) \ No newline at end of file diff --git a/test/scot/weft/i18n/test/core.clj b/test/scot/weft/i18n/test/core.clj index de3038d..9b1d7f4 100644 --- a/test/scot/weft/i18n/test/core.clj +++ b/test/scot/weft/i18n/test/core.clj @@ -220,4 +220,5 @@ (is (= "Ceci n'est pas une pipe." (get-message :pipe "en-GB;q=0.9, fr-FR"))) - (is (= "это не труба." (get-message :pipe "de-DE" "i18n" "ru")))))) + (is (= "это не труба." (get-message :pipe "de-DE" "i18n" "ru"))) + (is (= "froboz" (get-message :froboz))))))