軟體開發的天空


SINCE 2004

最新回應


 

範例下載

我將提供四種轉換的方法,並分別以四篇文章來做說明及程式碼的呈現,關於一些IHttpModule的觀念及技巧,我預計在之後會一一介紹。

在介紹之前你可以先確認以下幾點:

1.若選擇使用老胡開發的元件,你必須下載老胡烘焙機的簡繁對譯COM元件 http://www.hokoy.com/Works/Program/HokoyWordKit.aspx

2.你必須先註冊簡繁對譯的COM元件,才能使用Hokoy.WordKit物件進行編碼轉換工作。

3.若你選擇使用Office Word元件進行簡繁對譯,必須額外針對Word.dll作安全性設定(ASPNETNETWORK 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.dllmyHttpModule.dll檔案複製到此Web專案的Bin資料夾內。

 

第四步驟、修正Web專案的Web.Config檔案內容。

<configuration>

   

  <system.web>

         <httpModules>新增部分-->

           <add type="myHttpModule.StrConvHttpModule, myHttpModule" name="StrConvHttpModule" />

        httpModules>新增部分-->

system.web>

 

configuration>

 

如此就可以測試你的繁體網頁內容是否轉換為簡體字型了。

 

 


回應

  • 你現在有空嗎 2008/8/31 上午 10:17 回覆

    # re: 套用ASP.NET網頁來實作簡繁對譯功能(三)實作

    請問 Hokoy.WordKit vb要如何呼用,已經RegSvr32註冊了,可是出現未宣告

  • 你現在有空嗎 2008/8/31 上午 10:39 回覆

    # re: 套用ASP.NET網頁來實作簡繁對譯功能(三)實作

    換個方式說你比較容易懂,我想在vb上寫個簡繁轉換,必須用調用Hokoy.WordKit.dll來完成,可是在api宣告部份都失敗,沒法應用

  • jameswu 2008/8/31 上午 10:59 回覆

    # re: 套用ASP.NET網頁來實作簡繁對譯功能(三)實作

    to 你現在有空嗎:
    很抱歉我已經很久沒寫VB了,暫時無法立即為你解答,
    建議你將無法執行的程式碼,及錯誤的資訊列出來,
    或許會更快為你找到答案,謝謝.

     

     

  • 你現在有空嗎 2008/9/1 上午 12:41 回覆

    # re: 套用ASP.NET網頁來實作簡繁對譯功能(三)實作

    謝謝您的回應
    我是很想用Hokoy.WordKit.dll
    可是在vb中要先宣告Public Declare Function ShellExecute Lib "Hokoy.WordKit.dll " Alias "*****" (************)
    我並不知道如何下參數,也找不到相當的文章
    我只知道vbs很簡單可以這椒使用
    Set Obj = CreateObject("Hokoy.WordKit")
    MsgBox Obj.GBtoBig5("躓瘣硃毞")
    MsgBox Obj.BIG5toGB("倉頡造字")

    當然~如果沒辦法就算了,我從Strings.StrConv這方面在找看看了

    另外還有一部份問您一下,在TEXTBOX輸入UTF-8碼( E59C8B)顯示(國)字,要用什麼指令?

*標  題:

*姓  名:

  電子郵件: (將不會被顯示)

  個人網頁:

*回應

登入後使用進階評論

Please add 8 and 3 and type the answer here: