【问题标题】:Translating textbox input to spanish, chinese, deutsch将文本框输入翻译成西班牙语、中文、德语
【发布时间】:2011-12-02 15:01:43
【问题描述】:

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

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

【问题讨论】:

  • 您是否需要自动翻译功能? .NET 框架中没有这样的东西,本地化提供了完全不同的功能。

标签: c# asp.net globalization


【解决方案1】:

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

【讨论】:

  • 尝试将此添加到 using: using System.Web.UI.HtmlControls;
猜你喜欢
  • 2019-07-14
  • 1970-01-01
  • 1970-01-01
  • 2017-10-26
  • 1970-01-01
  • 2016-04-16
  • 2018-02-24
  • 1970-01-01
  • 2018-05-09
相关资源
最近更新 更多