Define i18n dictionaries
masterTongue uses standard Clojure maps as dictionaries. Dictionaries support several features:
- Simple keys: Direct mapping to strings.
- Namespaced keys: Use keywords like
:weather/rain. - Nested maps: Nested maps are automatically unpacked into namespaced keys (e.g.,
:animals {:dog "Dog"}becomes:animals/dog). - Substitutions: Use
{1},{2}, etc., for positional arguments, or use a map for named substitutions (e.g.,{user "Tom"}). - Aliases: Map a key to another existing key to reuse strings.
- Arbitrary functions: Use a function as a translation value. If the key resolves to a function, Tongue calls it with the provided arguments. This is useful for complex logic like pluralization.
- Fallback Locales: Use the
:tongue/fallbackkey to specify a default locale if a requested locale is missing. - Custom Missing Key Message: Override the default error message using the
:tongue/missing-keykey.
(def dicts
{ :en { ;; simple keys
:color "Color"
;; namespaced keys
:weather/rain "Rain"
;; nested maps unpacked into namespaced keys
:animals { :dog "Dog" ;; => :animals/dog
:cat "Cat" } ;; => :animals/cat
;; substitutions
:welcome "Hello, {1}!"
:between "Value must be between {1} and {2}"
;; For using a map
:mail-title "{user}, {title} - Message received."
;; aliases
:frontpage-greeting :welcome
;; arbitrary functions
:count (fn [x]
(cond
(zero? x) "No items"
(= 1 x) "1 item"
:else "{1} items"))
;; optional -- override “Missing key” message
:tongue/missing-key "Missing key {1}"
}
:en-GB { :color "colour" }
:tongue/fallback :en })