Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
basic translation system for strings, compile-time resolved
- Loading branch information
Showing
10 changed files
with
100 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
|
||
wallet: | ||
welcome: "Welcome to Freecoin" | ||
name: "Name" | ||
email: "Email" | ||
balance: "Balance" | ||
send: "Send amount" | ||
list: "List transactions" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
|
||
wallet: | ||
welcome: "Benvenuti su Freecoin" | ||
name: "Nome" | ||
balance: "Bilancio" | ||
send: "Invia somma" | ||
list: "Elenco transazioni" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
a: | ||
first: first | ||
second: second | ||
third: third |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
a: | ||
first: primo | ||
second: secondo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
)) |