Tuesday 5 February 2013

Langauge Translator using c# in asp.net.



In this post I have explained Langauge translator using c# in asp.net.

Description:-

Language translator is used in every web site or web application. The benifite of translator is user can read or see website in their own language. It is easy to understand the content in own language. In this article I have used microsoftTranslator appId to translate the language in specific language.


Now open your visual -> open new website -> click ok.

Design your page like this:-

Default.aspx:-

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 50%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
   
        <table class="style1">
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Enter Text</td>
                <td align="left">
                    <asp:TextBox ID="txtValue" runat="server" Height="79px" Width="460px"
                        TextMode="MultiLine" BackColor="#CCCCCC"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Select Langauge</td>
                <td align="left">
                    <asp:DropDownList ID="ddlLangauge" runat="server">
                        <asp:ListItem Text="Hindi" Value="hi"></asp:ListItem>
                        <asp:ListItem Text="French" Value="fr"></asp:ListItem>
                        <asp:ListItem Text="German" Value="de"></asp:ListItem>
                        <asp:ListItem Text="Chinese Simplified" Value="zh-CN"></asp:ListItem>
                        <asp:ListItem Text="Japanise" Value="ja"></asp:ListItem>
                        <asp:ListItem Text="Swedish" Value="sv"></asp:ListItem>
                        <asp:ListItem Text="Russian" Value="ru"></asp:ListItem>
                    </asp:DropDownList>
                    <asp:Button ID="btnTranslate" runat="server" Text="Translate"
                        onclick="btnTranslate_Click" />
                </td>
            </tr>
            <tr>
                <td>
                    Translated Langauge:</td>
                <td align="left" bgcolor="#CCCCCC">
                    <asp:Label ID="ltTranslatetxt" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>

Code Behind Page:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public void Translate(string textvalue, string to)
    {
        string appId = "A70C584051881A30549986E65FF4B92B95B353A5";
        string from = "en";
        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);
                ltTranslatetxt.Text = translation + ".";
            }
        }
        catch (WebException e)
        {
            ProcessWebException(e, "Failed to Translate");
        }
        finally
        {
            if (response != null)
            {
                response.Close();
                response = null;
            }
        }
    }
    private void ProcessWebException(WebException e, string message)
    {
        ltTranslatetxt.Text = message + ": :" + e.ToString();
        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();
                }
            }
        }
        ltTranslatetxt.Text = "Http status code=" + e.Status + ",error message=" + strResponse;
    }

    protected void btnTranslate_Click(object sender, EventArgs e)
    {
        Translate(txtValue.Text,ddlLangauge.SelectedItem.Value.ToString());
    }
    string lng = @"ca,ca-es,da,da-dk,de,de-de,
            en
            en-au
            en-ca
            en-gb
            en-in
            en-us
            es
            es-es
            es-mx
            fi
            fi-fi
            fr
            fr-ca
            fr-fr
            it
            it-it
            ja
            ja-jp
            ko
            ko-kr
            nb-no
            nl
            nl-nl
            no
            pl
            pl-pl
            pt
            pt-br
            pt-pt
            ru
            ru-ru
            sv
            sv-se
            zh-chs
            zh-cht
            zh-cn
            zh-hk
            zh-tw";
}

Output:-



Click Here to Download the code.


No comments:

Post a Comment

Note: only a member of this blog may post a comment.