Luigi Di Biasi


XP Programming Style
Sondaggio
Argomento Spider e WebCrawler

Interessante
Non interessante

[t] Sfruttare google translator da .net (senza api key etc...) BETA

Il mio blog si è trasferito all'indirizzo http://www.luigidibiasi.it


Di recente ho avuto la necessità di tradurre una quantità di dati abbastanza grande ma avevo poca voglia di farlo a mano... 
Girando un po' in rete ho trovato diversi metodi per sfruttare google Translator senza metter usare API, AJAX, JSON e compagnia bella...

Però, mosso dalla curiosità ho deciso di cercare di capire come costruire una funzione che nascondesse tutte le "zozzonerie" che vanno fatte
per tirar fuori il risultato della traduzione che viene fuori quando si utilizza il link http://translate.google.it/translate_t?

Riporto la prima bozza scritta (funzionante al 12/01/2012) ... Tuttavia, bisogna tener conto che i mid() e gli instr() vanno sostituiti con le Regular Exppression nel caso si desideri avere un codice più pulito (e professionale rispetto a quello scritto qui sotto)


    Public Class gTWrap

       Private wc As System.Net.WebClient 

        Public Sub New()
            wc = New System.Net.WebClient()
        End Sub

        Public Function Translate(ByVal s As String, ByVal SourceLanguage As String, ByVal DestinationLanguage As String)

            If s = "" Then
                Return ""
            End If
            Dim o As String = wc.DownloadString("http://translate.google.it/translate_t?hl=&ie=UTF-8&text=" & s & "&sl=" & SourceLanguage & "&tl=" & DestinationLanguage & "")
            Dim w As String = Mid(o, InStr(o, "result_box") + 10, Len(o))
            Dim toReturn As String = ""
            Dim i As Integer

            w = Mid(w, 1, InStr(w, "<div id=spell-pl") - 1)
RECUT:
            i = InStr(w, "='#fff'") + 9
            w = Mid(w, InStr(w, "='#fff'") + 9, Len(w))
            toReturn &= Mid(w, 1, InStr(w, "</span") - 1)
            w = Mid(w, i, Len(w))
            If w.Contains("='#fff'") Then
                GoTo RECUT
            End If
            Return toReturn

        End Function

    End Class

Un esempio di utilizzo può essere:

dim gt as new gTWrap()
msgbox(gt.translate("hello world!","en","it"))



Categoria: Come fare a ...
giovedì, 12 gen 2012 Ore. 09.57
Statistiche
  • Views Home Page: 25.831
  • Views Posts: 49.665
  • Views Gallerie: 0
  • n° Posts: 41
  • n° Commenti: 33
Archivio Posts
Anno 2012

Anno 2011

Anno 2010

Anno 2009

Anno 2008
Copyright © 2002-2007 - Blogs 2.0
dotNetHell.it | Home Page Blogs
ASP.NET 2.0 Windows 2003