Skip to content

Instantly share code, notes, and snippets.

@pmn
Created March 7, 2011 03:55
Show Gist options
  • Save pmn/858043 to your computer and use it in GitHub Desktop.
Save pmn/858043 to your computer and use it in GitHub Desktop.
public class TranslateAPI
{
private string TranslateURL { get; set; }
public TranslateAPI()
{
var appConfig = ConfigurationManager.AppSettings;
var baseurl = appConfig["TranslateBaseURL"];
var apiKey = appConfig["TranslateAPIKey"];
TranslateURL = baseurl + "?key=" + apiKey;
}
public string Translate(string content, string fromLanguage, string toLanguage)
{
// build up the url
var requestUrl = TranslateURL + "&q=" + HttpUtility.UrlEncode(content) + "&source=" + fromLanguage + "&target=" + toLanguage;
// connect to the url and download the result string
var translateClient = new WebClient();
var result = translateClient.DownloadString(requestUrl);
// ship it back to the caller
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment