I'm trying to translate some text by sending a GET request to https://translate.googleapis.com/ from a C# application.
The request should be formatted as following: "/translate_a/single?client=gtx&sl=BG&tl=EN&dt=t&q=Здравей Свят!"
where sl= is the source language, tl= is the target language and q= is the text to be translated.
The response is a JSON array with the translated text and other details.
The problem is that when I try to translate from bulgarian to english the result gets broken like: "Р-РґСЂР ° РІРμР№ РЎРІСЏС,!"
There is no problem when I'm translating from english to bulgarian (no cyrillic in the URL) so my gues is that the problem is in the request.
Also whenever I'm sending the request directly from the browser the result is properly translated text.
How I'm doing it:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net.Http;
using System.Web;
class Program
{
static void Main(string[] args)
{
string ApiUrl = "https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}";
string targetLang = "en";
string sourceLang = "bg";
string text = "Здравей Свят!";
text = HttpUtility.UrlPathEncode(text);
string url = string.Format(ApiUrl, sourceLang, targetLang, text);
using (var client = new HttpClient())
{
var result = client.GetStringAsync(url).Result;
var jRes = (JArray)JsonConvert.DeserializeObject(result);
var translatedText = jRes[0][0][0].ToString();
var originalText = jRes[0][0][1].ToString();
var sourceLanguage = jRes[2].ToString();
}
}
}
Any suggestion will be appreciated.