Created
April 24, 2018 11:40
-
-
Save jfversluis/b6560d85c471cd68c60e7cabccd53e51 to your computer and use it in GitHub Desktop.
Translation method in my push notification Azure Function
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
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