Skip to content

Instantly share code, notes, and snippets.

@netfeed
Created December 3, 2012 10:36
Show Gist options
  • Select an option

  • Save netfeed/4194127 to your computer and use it in GitHub Desktop.

Select an option

Save netfeed/4194127 to your computer and use it in GitHub Desktop.
clojure-i18n
(ns omnom.i18n
"This is a simplified bare minimum re-work of https://github.com/ptaoussanis/tower"
(:require [clojure.java.io :as io]
[clojure.string :as string]))
(def ^:dynamic *Locale* :sv)
(defonce ^:private config (atom {}))
(defn- update-config! [dict] (swap! config conj dict))
(defn load-dictionary [resource]
(->> resource
io/resource
io/reader
slurp
read-string
update-config!))
(defn- get-value [key-list hsh]
(if-let [k (first key-list)]
(get-value (next key-list) (k hsh))
hsh))
(defn- key-to-list [key-word]
(flatten (map #(string/split % #"\.") (keep identity
(list
(namespace key-word)
(name key-word))))))
(defmacro with-locale [locale & body]
`(binding [*Locale* ~locale]
~@body))
(defn t [t-key & binds]
(let [key-list (key-to-list t-key)
value (get-value (map keyword key-list) (*Locale* @config))]
(cond
(and (nil? value) (:missing (*Locale* @config))) (format (:missing (*Locale* @config)) t-key)
(and value binds) (apply format value binds)
:else value)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment