예제 #1
0
        internal async Task <string> Translate(string textToTranslate, string to)
        {
            string url   = "http://api.microsofttranslator.com/v2/Http.svc/Translate";
            string query = $"?text={WebUtility.UrlEncode(textToTranslate)}&to={to}&contentType=text/plain";

            var accessToken = await _authToken.GetAccessTokenAsync(_httpClient).ConfigureAwait(false);

            // The HTTPClient is shared across multiple requests, meaning we can't simply
            // set the default headers for this query. To do that, we create a specific HTTP Request
            // and add the relevant headers to it.
            using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, url + query))
            {
                // Add the headers into the HTTP Request.
                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                var response = await _httpClient.SendAsync(requestMessage);

                var result = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode)
                {
                    return("ERROR: " + result);
                }

                var translatedText = XElement.Parse(result).Value;
                return(translatedText);
            }
        }
예제 #2
0
        /// <summary>
        /// detects language of input text
        /// </summary>
        /// <param name="textToDetect">Input text</param>
        /// <returns></returns>
        public async Task <string> Detect(string textToDetect)
        {
            textToDetect = PreprocessMessage(textToDetect, false);
            string url   = "http://api.microsofttranslator.com/v2/Http.svc/Detect";
            string query = $"?text={System.Net.WebUtility.UrlEncode(textToDetect)}";

            using (var client = new HttpClient())
                using (var request = new HttpRequestMessage())
                {
                    var accessToken = await _authToken.GetAccessTokenAsync().ConfigureAwait(false);

                    request.Headers.Add("Authorization", accessToken);
                    request.RequestUri = new Uri(url + query);
                    var response = await client.SendAsync(request);

                    var result = await response.Content.ReadAsStringAsync();

                    if (!response.IsSuccessStatusCode)
                    {
                        return("ERROR: " + result);
                    }

                    var detectedLang = XElement.Parse(result).Value;
                    return(detectedLang);
                }
        }
예제 #3
0
        internal async Task <string> Translate(string textToTranslate, string to)
        {
            string url   = "http://api.microsofttranslator.com/v2/Http.svc/Translate";
            string query = $"?text={System.Net.WebUtility.UrlEncode(textToTranslate)}&to={to}&contentType=text/plain";

            using (var client = new HttpClient())
            {
                var accessToken = await authToken.GetAccessTokenAsync().ConfigureAwait(false);

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                var response = await client.GetAsync(url + query);

                var result = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode)
                {
                    return("ERROR: " + result);
                }

                var translatedText = XElement.Parse(result).Value;
                return(translatedText);
            }
        }