Search code examples
.netajaxgoogle-translate

Retrieving data using WebRequest


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?


Solution

  • 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.