Considerably improved reliability, return token if no message found.
This commit is contained in:
parent
341462a903
commit
aadf7f46a0
|
@ -119,7 +119,7 @@
|
||||||
(try
|
(try
|
||||||
(slurp (io/resource name))
|
(slurp (io/resource name))
|
||||||
(catch Exception _
|
(catch Exception _
|
||||||
(timbre/error (str "Resource at " name " does not exist."))
|
(timbre/warn (str "Resource at " name " does not exist."))
|
||||||
nil)))
|
nil)))
|
||||||
|
|
||||||
|
|
||||||
|
@ -167,23 +167,31 @@
|
||||||
Returns a map of message keys to strings; if no useable file is found, returns nil."
|
Returns a map of message keys to strings; if no useable file is found, returns nil."
|
||||||
{:doc/format :markdown}
|
{:doc/format :markdown}
|
||||||
[^String accept-language-header ^String resource-path ^String default-locale]
|
[^String accept-language-header ^String resource-path ^String default-locale]
|
||||||
(let [file-path (first
|
(let [file-paths (remove
|
||||||
(remove
|
empty?
|
||||||
nil?
|
|
||||||
(map
|
(map
|
||||||
#(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 "'"))
|
default-path (join java.io.File/separator
|
||||||
(try
|
|
||||||
(read-string
|
|
||||||
(slurp-resource
|
|
||||||
(or
|
|
||||||
file-path
|
|
||||||
(join java.io.File/separator
|
|
||||||
[resource-path
|
[resource-path
|
||||||
(str default-locale ".edn")]))))
|
(str default-locale ".edn")])
|
||||||
(catch Exception any
|
paths (concat file-paths (list default-path))
|
||||||
(timbre/error (str "Failed to load internationalisation because " (.getMessage any)))
|
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))))
|
nil))))
|
||||||
|
|
||||||
(def get-messages
|
(def get-messages
|
||||||
|
@ -200,7 +208,8 @@
|
||||||
|
|
||||||
(def get-message
|
(def get-message
|
||||||
"Return the message keyed by this `token` from the most acceptable messages collection
|
"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;
|
* `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;
|
* `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
|
* `default-locale` should be a locale specifier to use if no acceptable locale can be
|
||||||
identified."
|
identified."
|
||||||
(fn ([^Keyword token ^String accept-language-header ^String resource-path ^String default-locale]
|
(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]
|
([^Keyword token ^String accept-language-header]
|
||||||
(get-message token accept-language-header *resource-path* *default-language*))
|
(get-message token accept-language-header *resource-path* *default-language*))
|
||||||
([^Keyword token]
|
([^Keyword token]
|
||||||
(get-message token nil *resource-path* *default-language*))))
|
(get-message token *default-language* *resource-path* *default-language*))))
|
|
@ -220,4 +220,5 @@
|
||||||
(is
|
(is
|
||||||
(=
|
(=
|
||||||
"Ceci n'est pas une pipe." (get-message :pipe "en-GB;q=0.9, fr-FR")))
|
"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))))))
|
||||||
|
|
Loading…
Reference in a new issue