using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Text;
using System.IO;
using System.Net;
using Newtonsoft.Json.Linq;
// The 'text' parameter will contain the text from the:
// - Current Clipboard when run by HotKey
// - History Item when run from the History Menu
// The returned string will be:
// - Placed directly on the Clipboard
// - Ignored by ClipboardFusion if it is 'null'
public static class ClipboardFusionHelper
{
//put your google api key here
//you can find out how to her on here: https://developers.google.com/places/web-service/get-api-key
private static readonly string API_KEY = "API_KEY_HERE";
public static string ProcessText(string text)
{
//build the request using the Newtonsoft.Json library
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter writer = new JsonTextWriter(sw))
{
writer.WriteStartObject();
//tell the google translation api to translate to english
writer.WritePropertyName("target");
writer.WriteValue("en");
//add strings to translate here. to add more things to translate, just repeat the next two lines
writer.WritePropertyName("q");
writer.WriteValue(text);
writer.WriteEndObject();
}
//build the api url and send the data
string url = "https://www.googleapis.com/language/translate/v2?key=" + API_KEY;
string response = "";
using(WebClient client = new WebClient())
{
client.Encoding = Encoding.UTF8;
response = client.UploadString(url, sb.ToString());
}
//parse the response and get the first translation
JObject root = JObject.Parse(response);
return (string)root["data"]["translations"][0]["translatedText"];
}
}