views:

203

answers:

2

Having nicked some code from msdn I'm miffed that it doesn't work exactly as I want straight away. I'm trying to use google translate to, well, translate some stuff for me on the fly. The problem is that t5he responseFromServer doesn't contain the translated text, nor does the source when I look at it using a browser although when looking at the page itself chien is proudly displayed.

void getTranslation()
    {
        WebRequest request = WebRequest.Create("http://translate.google.com/translate_t?hl=en#en|fr|dog");
        // Get the response.
        WebResponse response = request.GetResponse();
        // Display the status.
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        Stream dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        int index = 0;
        while (responseFromServer.Substring(index).Contains("dog"))
        {
            index = responseFromServer.IndexOf("dog", index + 1);
            Console.WriteLine(responseFromServer.Substring(index < 50 ? 0 : index - 50, 100));
            Console.WriteLine(" ");
        }
    }

Does anyone know what I'm failing to understand here? Or of a website that returns a translation as simple as the request?

+3  A: 

The reason is that the translation request itself is an asynchronous AJAX request. If you view the source of the page you are trying to retrieve, you won't find the word chien.

You could take a look at the Google AJAX Language API to achieve what you want.

Ronald Wildenberg
@Patrick: Did you manage to make your requirements work or have you chosen another approach in the end?
Ronald Wildenberg
I was using this as a learning exercise but didn't have time to follow through in the end. I had the fall back option of having my colleagues in France translate what I needed manually.
Patrick
+2  A: 

It doesn't work because this application uses javascript to automatically post.

If you want to do this via screenscraping you'll have to do a POST request to the URL of the form with the correct parameters.

However, it would be more advisable for you to just use their API rather than webscraping.

Kirschstein
to be fair it also works with JS disabled
annakata
Not when you switch off JS and nagivate to the URL, you then have to manually submit the form.My bad, I should have been more explicit in my explanation
Kirschstein