Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
76 lines (48 sloc) 1.36 KB
(ns kasta.i18n
(:import [java.nio.file Paths])
(:require [clojure.string :as str]
[ :as io]
[kasta.i18n.scan :as scan]
[kasta.i18n.po :as po]))
;;; Config
(def LANGS (some-> (or (System/getenv "KASTA_I18N")
(System/getProperty "kasta.i18n"))
(str/split #", ")))
(def PO-DIR (or (System/getenv "KASTA_I18N_DIR")
(System/getProperty "kasta.i18n.dir")
;;; Language switch
(def ^:dynamic *lang* nil)
(defmacro with-lang [lang & body]
`(binding [*lang* ~lang]
;;; Finding translations
(def read-translations
(fn [lang]
(let [path (Paths/get PO-DIR (into-array [(str lang ".po")]))]
(po/read-po (io/resource (str path)))))))
(defn get-trans [lang input]
(or (not-empty (get (read-translations lang) input))
;;; Data reader
(defn cljs-t [input]
(if *lang*
(get-trans *lang* input)
(defn clj-t [input]
(if (seq LANGS)
`(case *lang*
~@(mapcat (fn [lang] [lang (get-trans lang input)]) LANGS)
(defmacro t [input]
(if (:ns &env)
(cljs-t input)
(clj-t input)))
(defn reader-t [input]
`(t ~input))
(def scan-codebase! scan/scan-codebase!)
(def update-codebase! scan/update-codebase!)
(def read-po po/read-po)
You can’t perform that action at this time.