taka2ru avatar taka2ru committed 6818be3

localizing attributes.

Comments (0)

Files changed (3)

 
 => "<html><body><p>Konnichiwa</p><p>Ohayo</p></body></html>"
 
+### Localizing Attributes of HTML document
+
+use "attr_message" attribute, which values are separeted by spaces.
+
+(deftemplate test-page3
+  (java.io.StringReader.
+    (str "<html><body><input"
+         " attr_message=\"value=good_morning alt=hello\""
+         " value=\"GM\" alt=\"HELLO\">"
+         "</input></body></html>"))
+  []
+  [:html] (localize-document :message :ja))
+
+=> "<html><body><input alt=\"Konnichiwa\" value=\"Ohayo\" /></body></html>"
+
 ### Formatting HTML document
 
 transformation "format-content"

src/enlive_utils/core.clj

     (let [v (some #(resource resname locale %) (attr-values nodes :message))]
       (assoc nodes :content [v]))))
 
+(defn localize-attribute [resname locale]
+  (fn [nodes]
+    (let [new-attrs
+          (for [reskey (attr-values nodes :attr_message)
+                :let [[_ attr-key attr-msgid] (re-matches #"([^=]+)=(.*)" reskey)
+                      attr-val (resource resname locale attr-msgid)]]
+            [(keyword attr-key) attr-val])]
+      (update-in nodes [:attrs] #(into % new-attrs)))))
+
 (defn localize-document [resname locale]
   (let [reskeys (set (map name (resource-keys resname locale)))]
     (transformation
       [(attr-some :message reskeys)]
       (do-> (localize-content resname locale)
-            (remove-attr :message)))))
+            (remove-attr :message))
+      [(attr-has :attr_message)]
+      (do-> (localize-attribute resname locale)
+            (remove-attr :attr_message)))))
 
 (defn format-content [& args]
   #(let [[old-content] (:content %)]

test/enlive_utils/test/core.clj

   (is (= "<html><body><p>Korewa 5000 Yen desu.</p></body></html>"
          (apply str (test-page2 5000)))))
 
+(deftemplate test-page3
+  (java.io.StringReader.
+    (str "<html><body><input"
+         " attr_message=\"value=good_morning alt=hello\""
+         " value=\"GM\" alt=\"HELLO\">"
+         "</input></body></html>"))
+  []
+  [:html] (localize-document :message :ja))
+
+(deftest test-attribute-localization
+  (is (= {:attrs {:attr_message "value=hello" :value "Konnichiwa"}}
+         ((localize-attribute :message :ja)
+            {:attrs {:attr_message "value=hello" :value "HELLO"}})))
+  (is (= (str "<html><body><input alt=\"Konnichiwa\" value=\"Ohayo\" />"
+              "</body></html>")
+         (apply str (test-page3)))))
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.