Click here to Skip to main content
15,886,045 members
Articles / Programming Languages / Visual Basic

WebResourceProvider VB.NET style

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
30 Aug 20023 min read 82.5K   263   24  
Ravi Bhavnani's WebResourceProvider ported to the .NET Framework
Public Class TranslationProvider

    ' TranslationProvider.vb
    '
    ' Written by Marcel Spring, ADVIS AG, Switzerland <marcel.spring@advis.ch>
    ' Copyright (c) 2002.  All Rights Reserved.
    '
    ' code adapted from original c++ library from
    ' Ravi Bhavnani <ravib@ravib.com>
    ' http:'www.codeproject.com/internet/WebResourceProvider.asp.
    ' Copyright (c) 2002.  All Rights Reserved.
    '
    ' This code may be used in compiled form in any way you desire. This
    ' file may be redistributed unmodified by any means PROVIDING it is 
    ' not sold for profit without the author's written consent, and 
    ' providing that this notice and the author's name and all copyright 
    ' notices remains intact. 
    '
    ' An email letting us know how you are using it would be nice as well. 
    '
    ' This code is provided "as is" with no expressed or implied warranty.
    ' The author accepts no liability for any damage/loss of business that
    ' this product may cause.
    '
    '
    ' Revision history:
    '
    ' 06 Jun 2002 - Initial release
    '------------------------------------------------------------------------------------------

    Inherits WebResourceProvider

    '==========================================================================================
    'Private stuff 
    '==========================================================================================

    '------------------------------------------------------------------------------------------
    Private myTranslateMode As TranslateModes = TranslateModes.EnglishToFrench
    Private mySourceText As String = ""
    Private myTranslationText As String = ""


    '==========================================================================================
    'Public stuff 
    '==========================================================================================

    '------------------------------------------------------------------------------------------
    Public Enum TranslateModes
        EnglishToFrench = 0
        EnglishToGerman
        EnglishToItalian
        EnglishToSpanish
        FrenchToEnglish
        GermanToEnglish
        ItalianToEnglish
        SpanishToEnglish
    End Enum

    '------------------------------------------------------------------------------------------
    Public Property TranslateMode() As TranslateModes
        Get
            Return myTranslateMode
        End Get
        Set(ByVal Value As TranslateModes)
            myTranslateMode = Value
        End Set
    End Property

    '------------------------------------------------------------------------------------------
    Public Property SourceText() As String
        Get
            Return mySourceText
        End Get
        Set(ByVal Value As String)
            mySourceText = Value
        End Set
    End Property

    '------------------------------------------------------------------------------------------
    Public Property TranslationText() As String
        Get
            If mySourceText <> "" And myTranslateMode <> -1 Then
                Try
                    Me.FetchResource()
                Catch ex As Exception
                    Throw New System.Exception("Error fetching translation from Google", ex)
                End Try
            End If
            Return myTranslationText
        End Get
        Set(ByVal Value As String)
            myTranslationText = Value
        End Set
    End Property

    '------------------------------------------------------------------------------------------
    Public Sub New(ByVal sourceText As String, ByVal translateMode As TranslateModes)
        mySourceText = sourceText
        myTranslateMode = translateMode
    End Sub


    '==========================================================================================
    'Overriden methods from base class
    '==========================================================================================

    '------------------------------------------------------------------------------------------
    Overrides Sub ConstructUrl(ByRef myURL As String)
        Me.URL = "http://translate.google.com/translate_t"
    End Sub

    '------------------------------------------------------------------------------------------
    'we do a call to the website with POST, so this function must return true!!!
    Overrides Function IsPost() As Boolean
        Return True
    End Function

    '------------------------------------------------------------------------------------------
    Overrides Sub GetPostData(ByRef myCollection As System.Collections.Specialized.NameValueCollection)

        Dim langPair As String

        Select Case myTranslateMode
            Case TranslateModes.EnglishToFrench
                langPair = "en|fr"
            Case TranslateModes.EnglishToGerman
                langPair = "en|de"
            Case TranslateModes.EnglishToItalian
                langPair = "en|it"
            Case TranslateModes.EnglishToSpanish
                langPair = "en|es"
            Case TranslateModes.FrenchToEnglish
                langPair = "fr|en"
            Case TranslateModes.GermanToEnglish
                langPair = "de|en"
            Case TranslateModes.ItalianToEnglish
                langPair = "it|en"
            Case TranslateModes.SpanishToEnglish
                langPair = "es|en"
        End Select

        With myCollection
            .Add("langpair", langPair)
            .Add("hl", "en")
            .Add("ie", "ASCII")
            .Add("oe", "ASCII")
            .Add("text", mySourceText)
        End With

    End Sub

    '------------------------------------------------------------------------------------------
    Overrides Sub parseContent()

        myTranslationText = ""
        'get translated text
        If Not skipTo("<textarea name=q rows=5 cols=45 wrap=PHYSICAL>") Then
            Return
        End If
        If Not extractTo("</textarea>", myTranslationText) Then
            Return
        End If

    End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions