Fork me on GitHub
#specter
<
2017-09-10
>
andrea.crotti10:09:46

I am trying to go recursively through a hiccup data structure

andrea.crotti10:09:08

something like this

(def nested
  [:div {:id "hello"}
   [:a {:href ""} :welcome]])

andrea.crotti10:09:35

and simply replace automaticaly (using the tongue library) every known symbol with its translation

andrea.crotti10:09:21

so given this dictionary

(def dicts
  "List of all the words/sentences that need localization"
  {:en {:welcome "Welcome"}
   :it {:welcome "Benvenuto"}})

andrea.crotti10:09:31

I just tried this first

(s/transform
 [s/ALL]
 #(do
    (print %)
    (if (contains? dicts %)
      (translate :en %)
      %))
 nested)

andrea.crotti10:09:14

but it doesn't actually check :welcome, I also tried to follow the recursive example in the docs

(s/transform
 (s/subselect TreeValues)
 #(do
    (print %)
    (if (contains? dicts %)
      (translate :en %)
      %))
 nested)

andrea.crotti10:09:20

but it looks like it's the same thing

andrea.crotti10:09:32

is it actually possible to do this somehow?

nathanmarz11:09:43

where can the target keywords reside? only map values or also in arbitrary vector values or map keys?

andrea.crotti11:09:49

I think only in nested vectors

andrea.crotti11:09:52

Since it's html text really

nathanmarz11:09:16

(def TreeValues
  (recursive-path [] p
    (if-path vector?
      [ALL p]
      STAY
      )))

(def nested
  [:div {:id "hello"}
   [:a {:href ""} :welcome]])

(def dicts
  {:en {:welcome "Welcome"}
   :it {:welcome "Benvenuto"}})

(let [dict (:en dicts)]
  (transform [TreeValues #(contains? dict %)] dict nested))

nathanmarz11:09:36

that's one way to do it

nathanmarz11:09:56

you can make it more precise by making a recursive path that doesn't go to the first 2 elements of vectors, so that you don't inadvertently translate the node type if something like :div is used in the dictionary

andrea.crotti14:09:02

Ah nice thanks I'll try it out