Skip to content

Commit

Permalink
basic translation system for strings, compile-time resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
jaromil committed Feb 22, 2016
1 parent 5f9f77a commit 93ec939
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 13 deletions.
4 changes: 3 additions & 1 deletion project.clj
Expand Up @@ -35,13 +35,15 @@
[clj.qrgen "0.4.0"]
[clavatar "0.3.0"]
[cc.artifice/lein-gossip "0.2.1"]
[circleci/clj-yaml "0.5.5"]
]


:source-paths ["src"]
:resource-paths ["resources" "test-resources"]
:jvm-opts ["-Djava.security.egd=file:/dev/random" ;use a proper random source (install haveged)
"-XX:-OmitStackTraceInFastThrow" ; prevent JVM exceptions without stack trace
]
]
:env [[:base-url "http://localhost:8000"]]
:aliases {"dev" ["with-profile" "dev" "ring" "server"]
"prod" ["with-profile" "production" "run"]
Expand Down
9 changes: 9 additions & 0 deletions resources/lang/en.yml
@@ -0,0 +1,9 @@
---

wallet:
welcome: "Welcome to Freecoin"
name: "Name"
email: "Email"
balance: "Balance"
send: "Send amount"
list: "List transactions"
8 changes: 8 additions & 0 deletions resources/lang/it.yml
@@ -0,0 +1,8 @@
---

wallet:
welcome: "Benvenuti su Freecoin"
name: "Nome"
balance: "Bilancio"
send: "Invia somma"
list: "Elenco transazioni"
1 change: 1 addition & 0 deletions src/freecoin/core.clj
Expand Up @@ -41,6 +41,7 @@
[freecoin.db.mongo :as mongo]
[freecoin.db.storage :as storage]
[freecoin.blockchain :as blockchain]
[freecoin.translation :as translation]
[freecoin.routes :as routes]
[freecoin.config :as config]
[freecoin.handlers.sign-in :as sign-in]
Expand Down
13 changes: 9 additions & 4 deletions src/freecoin/params.clj
Expand Up @@ -47,16 +47,21 @@
:secure false ;restrict the cookie to HTTPS URLs if true
:http-only true}})

;; translation
(def locale
{:language "it"}
)

;; defaults
(def encryption
{:version 1
:total 9
:quorum 5

:prime 'prime4096

:description "Freecoin"

;; versioning every secret
:prefix "FXC1"

Expand All @@ -77,7 +82,7 @@
;; reject if body is more than 1MB
:insecure? true
;; Need to contact a server with an untrust

:max-redirects 10 ; Max redirects to follow
;; whether follow 301/302 redirects automatically, default
;; to true
Expand Down
35 changes: 35 additions & 0 deletions src/freecoin/translation.clj
@@ -0,0 +1,35 @@
(ns freecoin.translation
(:require
[freecoin.params :as param]
[clojure.tools.logging :as log]
[clj-yaml.core :as yaml]
[clojure.java.io :as io]
))

(defn load-translations-from-string [s]
(yaml/parse-string s))

(defn load-translations-from-file [file-name]
(-> file-name
io/resource
slurp
load-translations-from-string))

(defn deep-merge
"Recursively merges maps. If keys are not maps, the last value wins."
[& vals]
(if (every? map? vals)
(apply merge-with deep-merge vals)
(last vals)))

(defn translation-map [lang]
(deep-merge
(load-translations-from-file (str "lang/en.yml"))
(load-translations-from-file (str "lang/" lang ".yml"))
)
)


(defn locale [items]
(get-in (translation-map (:language param/locale)) items)
)
18 changes: 10 additions & 8 deletions src/freecoin/views/account_page.clj
Expand Up @@ -28,15 +28,17 @@

(ns freecoin.views.account-page
(:require [clavatar.core :as clavatar]
[freecoin.routes :as routes]))
[freecoin.routes :as routes]
[freecoin.translation :as t]
))

(defn render-wallet [wallet]
(let [email (:email wallet)]
[:div {:class "wallet-details"}
[:div {:class "card"}
[:span (str "Name: " (:name wallet))]
[:span (str (t/locale [:wallet :name]) ": " (:name wallet))]
[:br]
[:span (str "Email: ") [:a {:href (str "mailto:" email)} email]]
[:span (str (t/locale [:wallet :email]) ": ") [:a {:href (str "mailto:" email)} email]]
[:br]
[:span {:class "qrcode pull-left"}
[:img {:src (routes/path :qrcode :uid (:uid wallet))}]]
Expand All @@ -48,17 +50,17 @@
(let [wallet (:wallet context)
balance (:balance context)]
{:body-class "func--account-page--body"
:title "Welcome to Freecoin"
:title (t/locale [:wallet :welcome])
:body [:div
(render-wallet wallet)
(render-wallet wallet)
[:div {:class "balance"}
(str "Balance: ")
(str (t/locale [:wallet :balance]) ": ")
[:span {:class "func--account-page--balance"}
balance]]
[:div
[:a.btn.btn-primary {:href (routes/path :get-transaction-form)}
(str "Send currency")]
(t/locale [:wallet :send])]
[:a.btn.btn-default {:href (routes/path :get-user-transactions :uid (:uid wallet))}
(str "View transactions")]
(t/locale [:wallet :list])]
]]
}))
4 changes: 4 additions & 0 deletions test-resources/test-fallback.yml
@@ -0,0 +1,4 @@
a:
first: first
second: second
third: third
3 changes: 3 additions & 0 deletions test-resources/test-translations.yml
@@ -0,0 +1,3 @@
a:
first: primo
second: secondo
18 changes: 18 additions & 0 deletions test/freecoin/test/translation.clj
@@ -0,0 +1,18 @@
(ns freecoin.test.translation
(:require [midje.sweet :refer :all]
[freecoin.translation :as trans]
[clojure.tools.logging :as log]
))

(facts "Can load translations from a file"
(trans/load-translations-from-file "test-translations.yml") => {:a {:first "primo" :second "secondo"}})

(facts "Translation has working fallback"
(let [tmap (trans/deep-merge
(trans/load-translations-from-file "test-fallback.yml")
(trans/load-translations-from-file "test-translations.yml"))
]
(get-in tmap [:a :first]) => "primo"
(get-in tmap [:a :first]) =not=> "first"
(get-in tmap [:a :third]) => "third"
))

0 comments on commit 93ec939

Please sign in to comment.