[dba-VB] Google Translate API

Salakhetdinov Shamil mcp2004 at mail.ru
Fri Feb 17 06:20:57 CST 2012


Hi Gustav --

Google could have blocked translation service by using "grabbing approach" - here is the code that returns nothing:

public static string Translate(
    string sentence,
    string sourceLanguageCode,
    string targetLanguageCode)
{
    string url = string.Format(
            "http://translate.google.com/#{0}|{1}|{2}",
            sourceLanguageCode,
            targetLanguageCode,
            System.Web.HttpUtility.HtmlEncode(sentence)
            .Replace(" ", "%20"));

    System.Net.WebClient webClient = new System.Net.WebClient();
    webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
    string html = webClient.DownloadString(url);
 
    string startTag = "<span class=\"hps\">";
    string endTag = "</span>";

    System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(html, 
        string.Format("(?<={0})(.*?)(?={1})", startTag, endTag));

    return match.Value;             
}

They probably fill HTML textbox with translation by using JavaScript, or my code above is wrong...

If you have the code which works it would be interesting to see it here.

Anyway I will use Google API for my batch translation.

Thank you.

-- Shamil


17 февраля 2012, 13:21 от "Gustav Brock" <Gustav at cactus.dk>:
> Hi Shamil
> 
> You can "attack" the web interface via code. It's very easy - you post the wording to be translated, grab the returned html stream, and parse it for the returned translation. Much slower than the API, of course, but totally free as far as I understand. I didn't check for upper/lower case issues however.
> 
> I have code for it somewhere should anyone be interested.
> 
> /gustav
> 
> >>> Salakhetdinov Shamil <mcp2004 at mail.ru> 16-02-2012 14:50 >>>
> Hi All --
> 
> JFYI: I have tried to use .NET Google Translate API today (http://code.google.com/p/google-api-dotnet-client/wiki/Setup) and it worked rather well - rather well because it somehow failed to translate words "Event Viewer" into Russian but translated them well into French. And http://translate.google.com/ is able to translate words "Event Viewer" well into Russian...
> 
> Google Translate API also translated well lowcased words "event viewer" into Russian.
> 
> Google Translate API is a paid service - USD20 for 1 million translated chars - so to not waste money (I need to translate about 800,000 chars) I'd probably need to submit only lowcased words to Google Translation API. We will see.
> 
> Thank you.
> 
> -- Shamil
> 
> _______________________________________________
> dba-VB mailing list
> dba-VB at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/dba-vb
> http://www.databaseadvisors.com
> 
> 



More information about the dba-VB mailing list