[Solved] Google Translate API returns non UTF-8 characters in ASP.Net

Last Reply one month ago By dharmendr

Posted one month ago

i am using google translation api for translate english to urdu/hindi 

getting incorect urdu /hindi format

i am passing "shahzad" and getting " शहजाद " from json 

in c#

{ "data": {    "translations": [    { "translatedText": "शहजाद"  } ] }}

Posted one month ago

Hi Shahzad786,

WebClient.DownloadString uses WebClient.Encoding as its encoding when it converts the remote resource into a string.

Set it to System.Text.Encoding.UTF8.

Check the below code.

Using the below article i have modified the code.

Using Google Translation (Translate) API in ASP.Net using C# and VB.Net


protected void Translate(object sender, EventArgs e)
    string url = "https://translation.googleapis.com/language/translate/v2?key=API_Key";
    url += "&source=" + ddlSource.SelectedItem.Value;
    url += "&target=" + ddlTarget.SelectedItem.Value;
    url += "&q=" + Server.UrlEncode(txtSource.Text.Trim());
    WebClient client = new WebClient();
    client.Encoding = System.Text.Encoding.UTF8;
    string json = client.DownloadString(url);
    JsonData jsonData = (new JavaScriptSerializer()).Deserialize<JsonData>(json);
    txtTarget.Text = jsonData.Data.Translations[0].TranslatedText;

public class JsonData
    public Data Data { get; set; }
public class Data
    public List<Translation> Translations { get; set; }
public class Translation
    public string TranslatedText { get; set; }


Protected Sub Translate(sender As Object, e As EventArgs)
    Dim url As String = "https://translation.googleapis.com/language/translate/v2?key=API_Key"
    url += "&source=" + ddlSource.SelectedItem.Value
    url += "&target=" + ddlTarget.SelectedItem.Value
    url += "&q=" + Server.UrlEncode(txtSource.Text.Trim())
    Dim client As New WebClient()
    client.Encoding = System.Text.Encoding.UTF8
    Dim json As String = client.DownloadString(url)
    Dim jsonData As JsonData = (New JavaScriptSerializer()).Deserialize(Of JsonData)(json)
    txtTarget.Text = jsonData.Data.Translations(0).TranslatedText
End Sub

Public Class JsonData
    Public Property Data() As Data
End Class
Public Class Data
    Public Property Translations() As List(Of Translation)
End Class
Public Class Translation
    Public Property TranslatedText() As String
End Class