Thursday, 20 June 2013

Language Translator Using Asp.net through microsoft link...

Language Translator Using Asp.net through microsoft link..



http://dotnetvino.blogspot.in/



Language Translator using ASP.NET........


Code in ASPX page:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Translator</title>
</head>
<body style="background-color: Gray;">
    <form id="form1" runat="server">
    <div>
        <div style="background-color: Olive; width: 1300px; height: 150px; margin-top: -25px;
            margin-left: -8px;">
            <asp:Label ID="Label1" runat="server" BorderStyle="None" Font-Bold="True" Font-Size="25pt"
                Style="top: 39px; left: 312px; position: absolute; height: 37px; width: 597px"
                Text="Language Translator Using Microsoft link"></asp:Label>
        </div>
        <asp:TextBox ID="TextBox1" runat="server" Height="272px" OnTextChanged="TextBox1_TextChanged"
            Width="582px" TextMode="MultiLine"></asp:TextBox>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
            Style="top: 442px; left: 301px; position: absolute; height: 22px; width: 278px;
            background-color: #3399FF; right: 668px;">
            <asp:ListItem Value="0">----------Select Language To Translate---------- </asp:ListItem>
            <asp:ListItem Value="en">English </asp:ListItem>
            <asp:ListItem Value="zh-CN">Chinese</asp:ListItem>
            <asp:ListItem Value="de">Deutsch</asp:ListItem>
            <asp:ListItem Value="hi">Hindi</asp:ListItem>
            <asp:ListItem Value="ta">Tamil</asp:ListItem>
        </asp:DropDownList>
        <%-- <asp:Button ID="Button1" runat="server" Text="Translate" Style="top: 442px; left: 502px;
            position: absolute; width: 81px; background-color: #3399FF" OnClick="Button1_Click" />
       --%>
        <asp:TextBox ID="TextBox2" runat="server" Style="top: 133px; left: 622px; position: absolute;
            height: 272px; width: 620px" OnTextChanged="TextBox2_TextChanged" TextMode="MultiLine"></asp:TextBox>





Code In ASPX.cs Page:


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Globalization;
using System.Threading;
using System.Net;
using System.IO;


public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        // Translate();
        if (IsPostBack)
        {


          Translate(TextBox1.Text, DropDownList1.SelectedItem.Value.ToString());
         
         
        }
     


    }
    public void Translate(string textvalue, string to)
    {
     
       string appId = "A70C584051881A30549986E65FF4B92B95B353A5";//go to http://msdn.microsoft.com/en-us/library/ff512386.aspx to obtain AppId.
        // string textvalue = "Translate this for me";"http://translate.google.com/#"+from+ "/" + to + "/" + textvalue;
        string from = "en";
        //string to = "es";

        string uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=" + appId + "&text=" + textvalue + "&from=" + from + "&to=" + to;

        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);

        WebResponse response = null;
        try
        {
            response = httpWebRequest.GetResponse();
            using (Stream stream = response.GetResponseStream())
            {
                System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String"));
                string translation = (string)dcs.ReadObject(stream);

                TextBox2.Text = "'" + translation + "'";
            }
         
        }
        catch (WebException e)
        {
            ProcessWebException(e, "Failed to translate");
        }
        finally
        {
            if (response != null)
            {
                response.Close();
                response = null;
            }

        }
     
    }//through error msg

    private void ProcessWebException(WebException e, string message)
    {
        TextBox2.Text = message + " : :" + e.ToString();

        // Obtain detailed error information
        string strResponse = string.Empty;
        using (HttpWebResponse response = (HttpWebResponse)e.Response)
        {
            using (Stream responseStream = response.GetResponseStream())
            {
                using (StreamReader sr = new StreamReader(responseStream, System.Text.Encoding.ASCII))
                {
                    strResponse = sr.ReadToEnd();
                }
            }
        }
        TextBox2.Text = "Http status code=" + e.Status + " ,error message=" + strResponse;
        //TextBox2.Text = "";
     
    }

}

1 comment: