Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BadUncleX/14984ae8897614c33c2360938772dd9e to your computer and use it in GitHub Desktop.
Save BadUncleX/14984ae8897614c33c2360938772dd9e to your computer and use it in GitHub Desktop.
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!"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment