c# – 将文本框输入翻译成西班牙语,中文,deutsch

前端之家收集整理的这篇文章主要介绍了c# – 将文本框输入翻译成西班牙语,中文,deutsch前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想将文本框值转换为特定语言,如西班牙语,Chinse,Deutsch等,这些都在下面的下拉列表中,我想在标签显示文本框翻译的值,但不是在标签显示转换值.
  1. <asp:TextBox ID="txtmessage" runat="server" />
  2. <asp:DropDownList ID="drop" runat="server" AutoPostBack="true"
  3. onselectedindexchanged="drop_SelectedIndexChanged" >
  4. <asp:ListItem Value="en-US">English</asp:ListItem>
  5. <asp:ListItem Value="ja-JP">Japanese</asp:ListItem>
  6. <asp:ListItem Value="zh-CN">Chinse</asp:ListItem>
  7. <asp:ListItem Value="de-DE">Deutsch</asp:ListItem>
  8. </asp:DropDownList>
  9.  
  10. <asp:Label ID="lblWelcome" Meta:resourcekey="lblWelcome"
  11. Text="Welcome" runat="server" ></asp:Label>

后台代码

  1. protected void drop_SelectedIndexChanged(object sender,EventArgs e)
  2. {
  3.  
  4. System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(this.drop.SelectedValue);
  5. System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(this.drop.SelectedValue);
  6.  
  7. lblWelcome.text=txtmessage.text;
  8. }

解决方法

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

这是一个代码示例,您需要将其更改为适合您正在执行的操作.

  1. public static string Translate(string input,string languagePair,Encoding encoding)
  2. {
  3. string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}",input,languagePair);
  4.  
  5. string result = String.Empty;
  6.  
  7. using (WebClient webClient = new WebClient())
  8. {
  9. webClient.Encoding = encoding;
  10. result = webClient.DownloadString(url);
  11. }
  12.  
  13. HtmlDocument doc = new HtmlDocument();
  14. doc.LoadHtml(result);
  15. return doc.DocumentNode.SelectSingleNode("//textarea[@name='utrans']").InnerText;
  16. }
  17.  
  18. //Get the HtmlAgilityPack here: http://www.codeplex.com/htmlagilitypack

猜你在找的C#相关文章