利用ASP.NET做一個結合Microsoft與Google翻譯網頁的小工具

最近在看別人的blog時,都會在自己的blog掛一個網頁翻譯的小工具..
小弟做了一個範例,主要是透過使用者輸入要翻譯的網頁...
並且選擇翻譯的模式....透過Microsoft或Google提供的翻譯平台..來即時翻譯網頁...c#範例..

最近在看別人的blog時,都會在自己的blog掛一個網頁翻譯的小工具..
小弟做了一個範例,主要是透過使用者輸入要翻譯的網頁...
並且選擇翻譯的模式....透過Microsoft或Google提供的翻譯平台..來即時翻譯網頁...c#範例..

WebPageTranslate.aspx

   1:  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebPageTranslate.aspx.cs" Inherits="WebPageTranslate" %>
   2:   
   3:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   4:   
   5:  <html xmlns="http://www.w3.org/1999/xhtml" >
   6:  <head runat="server">
   7:      <title>WebPageTranslate</title>
   8:  </head>
   9:  <body>
  10:      <form id="form1" runat="server">
  11:      <div>
  12:          <asp:TextBox ID="TextBox1" runat="server" Width="500px">http://www.dotblogs.com.tw/puma/archive/2008/03/02/1126.aspx</asp:TextBox>
  13:          <br />
  14:          <asp:DropDownList ID="DropDownList1" runat="server">
  15:              <asp:ListItem Value="1">Microsoft(繁-英)</asp:ListItem>
  16:              <asp:ListItem Value="2">Google(繁-英)</asp:ListItem>
  17:              <asp:ListItem Value="3">Google(繁-簡)</asp:ListItem>
  18:          </asp:DropDownList>
  19:          <asp:Button ID="Button1" runat="server" Text="翻譯" OnClick="Button1_Click" /></div>
  20:      </form>
  21:  </body>
  22:  </html>

WebPageTranslate.aspx.cs

   1:  using System;
   2:  using System.Data;
   3:  using System.Configuration;
   4:  using System.Collections;
   5:  using System.Web;
   6:  using System.Web.Security;
   7:  using System.Web.UI;
   8:  using System.Web.UI.WebControls;
   9:  using System.Web.UI.WebControls.WebParts;
  10:  using System.Web.UI.HtmlControls;
  11:   
  12:  public partial class WebPageTranslate : System.Web.UI.Page
  13:  {
  14:      protected void Page_Load(object sender, EventArgs e)
  15:      {
  16:   
  17:      }
  18:      protected void Button1_Click(object sender, EventArgs e)
  19:      {
  20:          //microsoft翻譯網址
  21:          string microsoft = "http://www.windowslivetranslator.com/BV.aspx?&MKT=zh-TW#{0}";
  22:   
  23:          //google翻譯網址
  24:          string google = "http://translate.google.com/translate?client=tmpg&hl=zh-TW&u={0}&langpair={1}";
  25:   
  26:          string url = "";
  27:   
  28:          switch (this.DropDownList1.SelectedItem.Value)
  29:          {
  30:              case "1":
  31:                  url = string.Format(microsoft, this.TextBox1.Text);
  32:                  break;
  33:              case "2":
  34:                  url = string.Format(google, this.TextBox1.Text, "zh-TW|en");
  35:                  break;
  36:              case "3":
  37:                  url = string.Format(google, this.TextBox1.Text, "zh-TW|zh-CN");
  38:                  break;
  39:              default:
  40:                  break;
  41:          }
  42:   
  43:          this.Page.Form.Controls.Add(new LiteralControl("<script>window.open('" + url + "')</script>"));
  44:   
  45:   
  46:      }
  47:  }   

執行結果:

WebPageTranslate

原始程式碼: WebPageTranslate.rar