 (ns mdr2.dtbook
   "Functionality around [DTBook XML](http://www.daisy.org/z3986/2005/Z3986-2005.html)"
-  (:require [clojure.data.xml :as xml]))
+  (:require [clojure.data.xml :as xml]
+            [clojure.string :as s]))
+
+;; FIXME: we should probably use some ready made library for i18n.
+;; There is https://github.com/ptaoussanis/tower,
+;; https://github.com/asmala/clj18n or
+;; https://github.com/feldi/clojure-i18n. The first one seems like the
+;; most mature but has a number of weird quirks and dependencies
+(def ^:private translations
+  {:de
+   {:about "Zu diesem DAISY-Buch"
+    :bib "Bibliographische Angaben"
+    :description "Produktbeschreibung des Hörbuchverlages"
+    :blurb "Klappentexte"
+    :contributors "Mitwirkende"
+    :enclosures "Hörbuchbeilagen"
+    :end "Ende des Buches"}
+   :en
+   {:about "About this DAISY book"
+    :bib "Bibliographic information"
+    :description "Product description by talking book publisher"
+    :blurb "Blurbs"
+    :contributors "Contributors"
+    :enclosures "Talking book enclosures"
+    :end "End of book"}})
+
+(defn- fallback
+  "Return the `language` or the fallback language if `language` is blank"
+  [language]
+  (if (s/blank? language)
+    "de"
+    language))
+
+(defn- translate
+  "Return a string for the given `key` and `locale`"
+  [locale key]
+  (get-in translations [locale key] key))
 
 (defn default-book
   "Return an default book sexp"


(defn commercial-audiobook
  "Return an sexp for the body of a commercial audiobook"
  [title creator]
  [:book
   [:frontmatter
    [:doctitle title]
    [:docauthor creator]
    [:level1
     [:h1 "Zu diesem DAISY-Buch"]
     [:p]]
    [:level1
     [:h1 "Bibliographische Angaben"]
     [:p]]
    [:level1
     [:h1 "Klappentexte"]
     [:p]]]
   [:bodymatter
    [:level1 [:h1 "[Audioimport]"] [:p]]]
   [:rearmatter
    [:level1
     [:h1 "Mitwirkende"]
     [:p]]
    [:level1
     [:h1 "Hörbuchbeilagen"]
     [:p]]
    [:level1
     [:h1 "Ende des Buches"]
     [:p]]]])

(defn- dtbook-sexp 
  [{:keys [title creator subject description publisher date identifier source language rights 

    [:meta {:name "dtb:totalTime" :content totaltime}]
    [:meta {:name "dtb:audioFormat" :content audioformat}]]
   (if librarynumber
     (commercial-audiobook title creator language)
     (default-book title creator))])

(defn dtbook 
