taoensso.tempura
Pure Clojure/Script i18n translations library.
get-default-resource-compiler
clj
(get-default-resource-compiler {:keys [default-tag escape-html? experimental/compact-vectors?], :or {default-tag :span}})
Implementation detail.
Good general-purpose resource compiler.
Supports output of text, and Hiccup forms with simple Markdown styles.
load-resource-at-compile-time
macro
clj
(load-resource-at-compile-time rname)
Experimental, subject to change.
Reads and inlines an edn resource on classpath, at compile-time.
Supported by: both clj and cljs.
See also `load-resource-at-runtime`.
load-resource-at-runtime
clj
(load-resource-at-runtime rname)
Experimental, subject to change.
Reads and returns an edn resource on classpath, at runtime.
Supported by: clj only (cljs support not possible).
A {:my-key {:__load-resource "my-file.edn"}} dictionary entry is
equivalent to {:my-key (load-resource-at-runtime "my-file.edn")}.
See also `load-resource-at-compile-time`.
new-tr-fn
clj
(new-tr-fn opts)
Returns a new translate ("tr") function,
(fn tr [locales resource-ids ?resource-args]) -> translation.
Common opts:
:default-locale ; Optional fallback locale to try when given locales don't
; have the requested resource/s.
; Default is `:en`.
:dict ; Dictionary map of resources,
; {<locale> {<k1> ... {<kn> <resource>}}}.
; See `tempura/example-dictionary` for more info.
:resource-compiler ; (fn [resource]) -> <(fn [vargs]) -> <compiled-resource>>.
; Useful if you want to customize any part of how
; dictionary resources are compiled.
:missing-resource-fn ; (fn [{:keys [opts locales resource-ids resource-args]}]).
; Called when requested resource/s cannot be found. Useful
; for logging, etc. May return a non-nil fallback resource
; value.
:cache-dict? ; Cache dictionary compilation? Improves performance,
; usually safe. You probably want this enabled in
; production, though you might want it disabled in
; development if you use `:__load-resource` dictionary
; imports and want resource changes to automatically
; reflect.
;
; Default is `false` for Clj and `:global` for Cljs.
:cache-locales? ; Cache locales processing? Improves performance, safe iff
; the returned `tr` fn will see a limited number of unique
; `locales` arguments (common example: calling
; `tempura/new-tr-fn` for each Ring request).
;
; Default is `false` for Clj and `:global` for Cljs.
:cache-resources? ; Cache resource lookup? Improves performance but will use
; memory for every unique combination of `locales` and
; `resource-ids`. Safe only if these are limited in number.
;
; Default is `false`.
Possible values for `:cach-<x>` options:
falsey ; Use no cache
`:fn-local` ; Use a cache local to the returned `tr` fn
`:global`/truthy ; Use a cache shared among all `tr` fns with `:global` cache
Example:
;; Define a tr fn
(def my-tr ; (fn [locales resource-ids ?resource-args]) -> translation
(new-tr-fn
{:dict
{:en {:missing "Missing translation"
:example {:greet "Hello %1"
:farewell "Goodbye %1, it was nice to meet you!"}}}}))
;; Then call it
(my-tr
[:fr-FR :en-GB-variation1] ; Descending-preference locales to try
;; Descending-preference dictionary resorces to try.
;; May contain a final non-keyword fallback:
[:example/how-are-you? "How are you, %1?"]
;; Optional arbitrary args for insertion into compiled resource:
["Steve"])
=> "How are you, Steve?"
See `tempura/default-tr-opts` for detailed default options.
See also `tempura/tr`.
For further info & examples, Ref.
https://github.com/ptaoussanis/tempura and
https://github.com/ptaoussanis/tempura/wiki/Tempura-documentation
tr
clj
(tr opts locales resource-ids)
(tr opts locales resource-ids resource-args)
Translate ("tr") function,
(fn tr [opts locales resource-ids ?resource-args]) -> translation.
See `tempura/new-tr-fn` for full documentation, and for fn-local caching.
with-tr-opts
macro
clj
(with-tr-opts opts & body)
with-tr-scope
macro
clj
(with-tr-scope scope & body)
`(with-tr-scope :foo.bar (tr _ _ [:baz]))` is equivalent to
`(tr _ _ [:foo.bar/baz])`
wrap-ring-request
clj
(wrap-ring-request handler {:keys [tr-opts locales]})
Alpha, subject to change.
Wraps Ring handler to add the following keys to Ring requests:
:tempura/accept-langs_ ; Possible delay with value parsed from Ring
; request's "Accept-Language" HTTP header.
; E.g. value: ["en-ES" "en-US"].
:tempura/tr ; (partial tempura/new-tr-fn tr-opts
; (or locales (:tempura/locales ring-req) accept-langs_)),
; => (fn [resource-ids ?resource-args]) -> translation
`tr-opts` will by default include {:cache-locales? :fn-local}.
See `tempura/new-tr-fn` for full documentation on `tr-opts`, etc.