範例下載
我將提供四種轉換的方法,並分別以四篇文章來做說明及程式碼的呈現,關於一些IHttpModule的觀念及技巧,我預計在之後會一一介紹。
在介紹之前你可以先確認以下幾點:
1.若選擇使用老胡開發的元件,你必須下載老胡烘焙機的簡繁對譯COM元件 http://www.hokoy.com/Works/Program/HokoyWordKit.aspx。
2.你必須先註冊簡繁對譯的COM元件,才能使用Hokoy.WordKit物件進行編碼轉換工作。
3.若你選擇使用Office Word元件進行簡繁對譯,必須額外針對Word.dll作安全性設定(ASPNET及NETWORK SERVICE帳戶),或是在Web.Config設定角色類比的方法,。
4.若使用Visual Basic的元件來進行簡繁對譯的話,需考慮的目前的作業系統的語系,才能正常的測試。
5.自行撰寫簡繁對譯字串。
先Show一下我的組態總管的整個檔案結構:

此篇文章是利用Hokoy.WordKit來進行簡繁轉換。
步驟一、首先要先建立『類別庫』專案myHttpModule,並新增一個類別,此類別是可以單獨處理簡繁轉換的類別。
檔案名稱為:HokoyWordTC2SC.vb
Imports System
Imports System.IO
Imports System.Web
Imports System.Text
Imports System.Globalization
Public Class HokoyWordTC2SC
Inherits Stream
Private _sink As Stream
Public Sub New(ByVal sink As Stream)
_sink = sink
End Sub
' All MustOverride interface members must be included.
Public Overrides ReadOnly Property CanRead() As Boolean
Get
Return True
End Get
End Property
Public Overrides ReadOnly Property CanSeek() As Boolean
Get
Return False
End Get
End Property
Public Overrides ReadOnly Property CanWrite() As Boolean
Get
Return False
End Get
End Property
Public Overrides ReadOnly Property Length() As Long
Get
Return _sink.Length
End Get
End Property
Public Overrides Property Position() As Long
Get
Return _sink.Position
End Get
Set(ByVal Value As Long)
Throw New NotSupportedException
End Set
End Property
Public Overrides Function Read(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer) As Integer
Dim c As Integer = _sink.Read(buffer, offset, count)
Return c
End Function
Public Overrides Function Seek(ByVal offset As Long, ByVal direction As System.IO.SeekOrigin) As Long
Throw New NotSupportedException
End Function
Public Overrides Sub SetLength(ByVal length As Long)
Throw New NotSupportedException
End Sub
Public Overrides Sub Close()
_sink.Close()
End Sub
Public Overrides Sub Flush()
_sink.Flush()
End Sub
Public Overrides Sub Write(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer)
If HttpContext.Current.Response.ContentType = "text/html" Then
Dim e As Encoding = Encoding.GetEncoding(HttpContext.Current.Response.Charset)
Dim s As String = e.GetString(buffer, offset, count)
If s <> "" And Not IsNothing(s) Then
'使用 Office Word 元件,效能不佳,且執行時的權限有些問題。
'使用他人元件,速度快,但要註冊簡繁對譯的COM元件。
Dim obj As New Hokoy.WordKit
s = obj.TCtoSC(s).ToString
'===============================
'使用Visual Basic元件,效果不好,還是會出現亂碼情況
's = Strings.StrConv(s, VbStrConv.SimplifiedChinese, CultureInfo.CurrentCulture.LCID)
'===============================
_sink.Write(e.GetBytes(s), 0, e.GetByteCount(s))
End If
Else
_sink.Write(buffer, offset, count)
End If
End Sub
End Class
步驟二、實作IHttpModule,在組態總管加入新的類別,並命名為myHttpModule.vb
Imports System
Imports System.Web
Imports System.Collections
Public Class StrConvHttpModule
Implements IHttpModule
Public ReadOnly Property ModuleName() As [String]
Get
Return "StrConvHttpModule"
End Get
End Property
Public Sub Init(ByVal application As HttpApplication) Implements IHttpModule.Init
AddHandler application.BeginRequest, AddressOf Me.Application_BeginRequest
AddHandler application.EndRequest, AddressOf Me.Application_EndRequest
End Sub 'Init
Private Sub Application_BeginRequest(ByVal [source] As [Object], ByVal e As EventArgs)
Dim application As HttpApplication = CType([source], HttpApplication)
Dim context As HttpContext = application.Context
context.Response.Filter = New HokoyWordTC2SC(context.Response.Filter)
End Sub
Private Sub Application_EndRequest(ByVal [source] As [Object], ByVal e As EventArgs)
End Sub
Public Sub Dispose() Implements IHttpModule.Dispose
End Sub
End Class
第三步驟、新增一個Web專案,並將原本在myHttpModule專案的Bin資料夾內的,Interop.Hokoy.dll及myHttpModule.dll檔案複製到此Web專案的Bin資料夾內。
第四步驟、修正Web專案的Web.Config檔案內容。
<configuration>
<system.web>
<httpModules>—新增部分-->
<add type="myHttpModule.StrConvHttpModule, myHttpModule" name="StrConvHttpModule" />
httpModules>—新增部分-->
system.web>
configuration>
如此就可以測試你的繁體網頁內容是否轉換為簡體字型了。
2008/4/22 23:46|
閱讀數 : 391
|
我要推薦
|
|
文章分類:
ASP.NET
訂閱