google api 翻译
Last active
April 11, 2018 02:30
-
-
Save BadUncleX/14984ae8897614c33c2360938772dd9e to your computer and use it in GitHub Desktop.
google translate with httpclient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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