Hi ashishk,
Please refer below sample.
You will need to register and generate and get Google API key and also enable the Google Translation (Translate) API in the Google Console.
For more details refer here.
Namespaces
C#
using System.Net;
using System.Web.Script.Serialization;
VB.Net
Imports System.Net
Imports System.Web.Script.Serialization
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("LanguageName", typeof(string)), new DataColumn("ISOCode", typeof(string)) });
dt.Rows.Add("Hindi", "HI");
dt.Rows.Add("English", "EN");
ddlSource.DataSource = dt;
ddlSource.ValueMember = "ISOCode";
ddlSource.DisplayMember = "LanguageName";
DataTable dt1 = new DataTable();
dt1.Columns.AddRange(new DataColumn[] { new DataColumn("LanguageName", typeof(string)), new DataColumn("ISOCode", typeof(string)) });
dt1.Rows.Add("Hindi", "HI");
dt1.Rows.Add("English", "EN");
dt1.Rows.Add("Marathi", "MR");
dt1.Rows.Add("Gujarati", "GU");
ddlTarget.DataSource = dt1;
ddlTarget.ValueMember = "ISOCode";
ddlTarget.DisplayMember = "LanguageName";
}
private void button1_Click(object sender, EventArgs e)
{
string url = "https://translation.googleapis.com/language/translate/v2?key=API Key";
url += "&source=" + ddlSource.SelectedValue;
url += "&target=" + ddlTarget.SelectedValue;
url += "&q=" + Uri.EscapeUriString(txtSource.Text);
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; }
}
VB.Net
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("LanguageName", GetType(String)), New DataColumn("ISOCode", GetType(String))})
dt.Rows.Add("Hindi", "HI")
dt.Rows.Add("English", "EN")
ddlSource.DataSource = dt
ddlSource.ValueMember = "ISOCode"
ddlSource.DisplayMember = "LanguageName"
Dim dt1 As DataTable = New DataTable()
dt1.Columns.AddRange(New DataColumn() {New DataColumn("LanguageName", GetType(String)), New DataColumn("ISOCode", GetType(String))})
dt1.Rows.Add("Hindi", "HI")
dt1.Rows.Add("English", "EN")
dt1.Rows.Add("Marathi", "MR")
dt1.Rows.Add("Gujarati", "GU")
ddlTarget.DataSource = dt1
ddlTarget.ValueMember = "ISOCode"
ddlTarget.DisplayMember = "LanguageName"
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim url As String = "https://translation.googleapis.com/language/translate/v2?key=API Key"
url += "&source=" & ddlSource.SelectedValue
url += "&target=" & ddlTarget.SelectedValue
url += "&q=" & Uri.EscapeUriString(txtSource.Text)
Dim client As WebClient = 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
Screenshot