Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » VB.NET » General » Downloads
 
Add your own
alternative version

WebResourceProvider VB.NET style

, 30 Aug 2002
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

Share

About the Author

Cello
Web Developer
Switzerland Switzerland
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 31 Aug 2002
Article Copyright 2002 by Cello
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid