翻译文本框输入到西班牙语,中文,德语

本文关键字:中文 德语 西班牙语 文本 输入 翻译 | 更新日期: 2023-09-27 18:16:33

我想将文本框值翻译为特定的语言,如西班牙语,中文,德语等,这些都在下面的下拉列表中,我想在标签中显示文本框翻译值,但不获得标签中的翻译值。

<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>

后台代码:

        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;
        }

翻译文本框输入到西班牙语,中文,德语

当你在寻找这样的东西时,Google是一个非常棒的工具。谷歌有谷歌翻译。

这里是一个代码示例,您需要修改它以适应您正在做的工作。

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