Search code examples
c#asp.netglobalization

Translating textbox input to spanish, chinese, deutsch


I want to translate textbox value to specific language like spanish ,Chinse,Deutsch etc which all are having in below dropdown and i want to display textbox translated value in label but not gettin transalted value in label.

<asp:TextBox ID="txtmessage" runat="server" />
    <asp:DropDownList ID="drop" runat="server"  AutoPostBack="true"
        onselectedindexchanged="drop_SelectedIndexChanged" >
        <asp:ListItem Value="en-US">English</asp:ListItem>
        <asp:ListItem Value="ja-JP">Japanese</asp:ListItem>
         <asp:ListItem Value="zh-CN">Chinse</asp:ListItem>
         <asp:ListItem Value="de-DE">Deutsch</asp:ListItem>
        </asp:DropDownList>

        <asp:Label ID="lblWelcome" meta:resourcekey="lblWelcome" 
           Text="Welcome" runat="server"  ></asp:Label>

code-behind:

        protected void drop_SelectedIndexChanged(object sender, EventArgs e)
        {

             System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(this.drop.SelectedValue);
             System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(this.drop.SelectedValue);

             lblWelcome.text=txtmessage.text;
        }

Solution

  • Google is an amazing tool to use when looking for something like this. Google has Google Translate.

    here is a code example you will need to alter it to work for what it is you are doing.

    public static string Translate(string input, string languagePair, Encoding encoding)
    {
    string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
    
    string result = String.Empty;
    
    using (WebClient webClient = new WebClient())
    {
    webClient.Encoding = encoding;
    result = webClient.DownloadString(url);
    }
    
    HtmlDocument doc = new HtmlDocument();
    doc.LoadHtml(result);
    return doc.DocumentNode.SelectSingleNode("//textarea[@name='utrans']").InnerText;
    }
    
    //Get the HtmlAgilityPack here: http://www.codeplex.com/htmlagilitypack