将文本框输入翻译为西班牙语,中文,deutsch

时间:2011-12-02 15:01:43

标签: c# asp.net globalization

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

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

1 个答案:

答案 0 :(得分:11)

谷歌是一个很棒的工具,可以在寻找这样的东西时使用。 谷歌有谷歌翻译。

这是一个代码示例,您需要将其更改为适合您正在做的事情。

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