Exemplo n.º 1
0
        //Traduz o texto inserido no campo de entrada
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            String Ie = TextIdiomaDetect.Text; //Idioma de entrada
            string Is = TextIdSaida.Text;      //Idioma de saida

            string route = "translate?to=" + Is + "&api-version=3.0&from=" + Ie + "&profanityAction=NoAction&textType=plain";

            string textToTranslate = TextFrom.Text;

            object[] body        = new object[] { new { Text = textToTranslate } };
            var      requestBody = JsonConvert.SerializeObject(body);
            var      client      = new HttpClient();
            var      request     = new HttpRequestMessage();


            request.Method     = HttpMethod.Post;
            request.RequestUri = new Uri(endpoint + route);
            request.Content    = new StringContent(requestBody, Encoding.UTF8, "application/json");
            request.Headers.Add("x-rapidapi-key", subscriptionKey);
            request.Headers.Add("x-rapidapi-Region", location);


            HttpResponseMessage thing = client.SendAsync(request).Result;
            string actualResponse     = thing.Content.ReadAsStringAsync().Result;



            var    Saida = JsonConvert.DeserializeObject(actualResponse);
            string S     = Saida.ToString();

            S = S.Remove(0, 48);
            S = S.Remove(S.Length - 23);
            S = S.Replace("\"", "'");

            string json = @"{" + S + "}";

            TradutorSaida tradu = JsonConvert.DeserializeObject <TradutorSaida>(json);
            string        name  = tradu.text;

            TextTo.Text = name;
        }
Exemplo n.º 2
0
        //Detecta o idioma inserido
        private void DetectaIdioma()
        {
            string textToTranslate = TextFrom.Text;
            var    body            = new object[] { new { Text = textToTranslate } };
            var    requestBody     = JsonConvert.SerializeObject(body);
            var    client          = new HttpClient();
            var    request         = new HttpRequestMessage();

            request.Method     = HttpMethod.Post;
            request.RequestUri = new Uri(endpoint + "Detect?api-version=3.0");
            request.Content    = new StringContent(requestBody, Encoding.UTF8, "application/json");
            request.Headers.Add("x-rapidapi-key", subscriptionKey);
            request.Headers.Add("x-rapidapi-Region", location);
            HttpResponseMessage thing    = client.SendAsync(request).Result;
            string        actualResponse = thing.Content.ReadAsStringAsync().Result;
            string        json           = @"{" + JsonConvert.DeserializeObject(actualResponse).ToString().Remove(0, 10).Remove(S.Length - 7).Replace("\"", "'").ToString() + "}";
            TradutorSaida tradu          = JsonConvert.DeserializeObject <TradutorSaida>(json);

            TextIdiomaDetect.Text = tradu.Language;
            SelecionaIdioma();
        }