Skip to content

Instantly share code, notes, and snippets.

@jfversluis
Created April 24, 2018 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfversluis/b6560d85c471cd68c60e7cabccd53e51 to your computer and use it in GitHub Desktop.
Save jfversluis/b6560d85c471cd68c60e7cabccd53e51 to your computer and use it in GitHub Desktop.
Translation method in my push notification Azure Function
static async Task<string> TranslateText(string inputText, string language, string accessToken)
{
string url = "http://api.microsofttranslator.com/v2/Http.svc/Translate";
string query = $"?text={System.Net.WebUtility.UrlEncode(inputText)}&to={language}&contentType=text/plain";
using (var client = new HttpClient())
{
// Set API key
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", accessToken);
var response = await client.GetAsync(url + query);
var result = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
return "Error: " + result;
System.Xml.XmlDocument xmlResponse = new System.Xml.XmlDocument();
xmlResponse.LoadXml(result);
// Return the translation
return xmlResponse.InnerText;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment