利用ASP.NET做一個結合Microsoft與Google翻譯網頁的小工具
最近在看別人的blog時,都會在自己的blog掛一個網頁翻譯的小工具..
小弟做了一個範例,主要是透過使用者輸入要翻譯的網頁...
並且選擇翻譯的模式....透過Microsoft或Google提供的翻譯平台..來即時翻譯網頁...c#範例..
WebPageTranslate.aspx
01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebPageTranslate.aspx.cs"
02 Inherits="WebPageTranslate" %>
03
04 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
05 <html xmlns="http://www.w3.org/1999/xhtml">
06 <head id="Head1" runat="server">
07 <title>WebPageTranslate</title>
08 </head>
09 <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>
23
02 Inherits="WebPageTranslate" %>
03
04 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
05 <html xmlns="http://www.w3.org/1999/xhtml">
06 <head id="Head1" runat="server">
07 <title>WebPageTranslate</title>
08 </head>
09 <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>
23
WebPageTranslate.aspx.cs
01
using System;
02
using System.Data;
03
using System.Configuration;
04
using System.Collections;
05
using System.Web;
06
using System.Web.Security;
07
using System.Web.UI;
08
using System.Web.UI.WebControls;
09
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
}

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23 //google翻譯網址
24

25

26 string url = "";
27

28 switch (this.DropDownList1.SelectedItem.Value)
29

30

31

32

33

34

35

36

37

38

39

40

41

42

43 this.Page.Form.Controls.Add(new LiteralControl("<script>window.open('" + url + "')</script>"));
44

45

46

執行結果: