In this article I will explain with an example, how to use the Google Translation (Translate) API in ASP.Net using C# and VB.Net.
Google Translation (Translate) API can be accessed using HTTP GET method and it returns the translated content in JSON format.
 
 
Google Translation API
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.
Note: In order to use the Google Translation API, you will need to enable Billing for your API and select the Trial pack.
 
 
HTML Markup
The following HTML Markup consists of two ASP.Net DropDownLists populated with ISO codes of languages, a Button and two ASP.Net TextBoxes with TextMode property set to Multiline.
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td>
            Source Language
        </td>
        <td>
            <asp:DropDownList ID="ddlSource" runat="server">
                <asp:ListItem Value="AF" Text="Afrikanns" />
                <asp:ListItem Value="SQ" Text="Albanian" />
                <asp:ListItem Value="AR" Text="Arabic" />
                <asp:ListItem Value="HY" Text="Armenian" />
                <asp:ListItem Value="EU" Text="Basque" />
                <asp:ListItem Value="BN" Text="Bengali" />
                <asp:ListItem Value="BG" Text="Bulgarian" />
                <asp:ListItem Value="CA" Text="Catalan" />
                <asp:ListItem Value="KM" Text="Cambodian" />
                <asp:ListItem Value="ZH" Text="Chinese (Mandarin)" />
                <asp:ListItem Value="HR" Text="Croation" />
                <asp:ListItem Value="CS" Text="Czech" />
                <asp:ListItem Value="DA" Text="Danish" />
                <asp:ListItem Value="NL" Text="Dutch" />
                <asp:ListItem Value="EN" Text="English" Selected="True" />
                <asp:ListItem Value="ET" Text="Estonian" />
                <asp:ListItem Value="FJ" Text="Fiji" />
                <asp:ListItem Value="FI" Text="Finnish" />
                <asp:ListItem Value="FR" Text="French" />
                <asp:ListItem Value="KA" Text="Georgian" />
                <asp:ListItem Value="DE" Text="German" />
                <asp:ListItem Value="EL" Text="Greek" />
                <asp:ListItem Value="GU" Text="Gujarati" />
                <asp:ListItem Value="HE" Text="Hebrew" />
                <asp:ListItem Value="HI" Text="Hindi" />
                <asp:ListItem Value="HU" Text="Hungarian" />
                <asp:ListItem Value="IS" Text="Icelandic" />
            </asp:DropDownList>
        </td>
        <td>
        </td>
        <td>
            Target Language
        </td>
        <td>
            <asp:DropDownList ID="ddlTarget" runat="server">
                <asp:ListItem Value="AF" Text="Afrikanns" />
                <asp:ListItem Value="SQ" Text="Albanian" />
                <asp:ListItem Value="AR" Text="Arabic" />
                <asp:ListItem Value="HY" Text="Armenian" />
                <asp:ListItem Value="EU" Text="Basque" />
                <asp:ListItem Value="BN" Text="Bengali" />
                <asp:ListItem Value="BG" Text="Bulgarian" />
                <asp:ListItem Value="CA" Text="Catalan" />
                <asp:ListItem Value="KM" Text="Cambodian" />
                <asp:ListItem Value="ZH" Text="Chinese (Mandarin)" />
                <asp:ListItem Value="HR" Text="Croation" />
                <asp:ListItem Value="CS" Text="Czech" />
                <asp:ListItem Value="DA" Text="Danish" />
                <asp:ListItem Value="NL" Text="Dutch" />
                <asp:ListItem Value="EN" Text="English" />
                <asp:ListItem Value="ET" Text="Estonian" />
                <asp:ListItem Value="FJ" Text="Fiji" />
                <asp:ListItem Value="FI" Text="Finnish" />
                <asp:ListItem Value="FR" Text="French" Selected="True" />
                <asp:ListItem Value="KA" Text="Georgian" />
                <asp:ListItem Value="DE" Text="German" />
                <asp:ListItem Value="EL" Text="Greek" />
                <asp:ListItem Value="GU" Text="Gujarati" />
                <asp:ListItem Value="HE" Text="Hebrew" />
                <asp:ListItem Value="HI" Text="Hindi" />
                <asp:ListItem Value="HU" Text="Hungarian" />
                <asp:ListItem Value="IS" Text="Icelandic" />
            </asp:DropDownList>
        </td>
        <td>
            <asp:Button Text="Translate" runat="server" OnClick="Translate" />
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <asp:TextBox ID="txtSource" runat="server" TextMode="MultiLine" />
        </td>
        <td>
        </td>
        <td colspan="2">
            <asp:TextBox ID="txtTarget" runat="server" TextMode="MultiLine" />
        </td>
        <td>
        </td>
    </tr>
</table>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Net;
using System.Web.Script.Serialization;
 
VB.Net
Imports System.Net
Imports System.Web.Script.Serialization
 
 
Using Google Translation (Translate) API in ASP.Net
The very first thing is to create a class as per the structure of the returned JSON from the Google Translation (Translate) API.
When the Translate Button is clicked, a call is made to the Google Translation (Translate) API using the DownloadString method the WebClient class.
The returned JSON string is deserialized and populated into an object of the JsonData class.
Finally the translated data is displayed in the Target TextBox.
C#
protected void Translate(object sender, EventArgs e)
{
    string url = "https://translation.googleapis.com/language/translate/v2?key=YOUR_API_KEY";
    url += "&source=" + ddlSource.SelectedItem.Value;
    url += "&target=" + ddlTarget.SelectedItem.Value;
    url += "&q=" + Server.UrlEncode(txtSource.Text.Trim());
    WebClient client = new WebClient();
    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
Protected Sub Translate(sender As Object, e As EventArgs)
    Dim url As String = "https://translation.googleapis.com/language/translate/v2?key=YOUR_API_KEY"
    url += "&source=" + ddlSource.SelectedItem.Value
    url += "&target=" + ddlTarget.SelectedItem.Value
    url += "&q=" + Server.UrlEncode(txtSource.Text.Trim())
    Dim client As New WebClient()
    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
Using Google Translation (Translate) API in ASP.Net using C# and VB.Net
 
 
Downloads