foxlog
4/1/2018 - 7:42 AM

google translate with httpclient

google translate with httpclient

(ns google_translate.10_google_translate_httpclient
  (:require [clojure.string :as string]
            [clj-http.client :as http]
            )
  (:use [alexcoding.util.json])
  )



(defn translate [s options]
  "Translate text using Google Translate API v2."
  (if (string/blank? s) ""
                        (let [params {"key" (:key options)
                                      "source" (:source options)
                                      "target" (:target options)
                                      "q" s}
                              resp (http/post "https://www.googleapis.com/language/translate/v2"
                                              {:as :json
                                               :debug false
                                               :headers {"X-HTTP-Method-Override" "GET"}
                                               :form-params params})]
                          (:translatedText (first (:translations (:data (:body resp)))))
                          )))

(defn -main [& words]
  (print (translate (first words) {:key (get-secret-key ["google-translate" "key"])
                                   :source "en"
                                   :target "zh-CN"})))





(comment
  (-main "what a nice day!"))