Export (0) Print
Expand All

Translate Method

[This is preliminary documentation and is subject to change.]

Translates a string into a different language.

URI
http://api.microsofttranslator.com/V1/Http.svc/Translate

Parameter Description

appId

A string representing the identifier of the calling application.

text

A string representing the text to translate.

from

A string representing the language code of the translation text.

to

A string representing the language code to translate the text into.

The text translated into the desired language.

C#
    string translateUri = "http://api.microsofttranslator.com/V1/Http.svc/Translate?appId=myAppId&from=en&to=es";
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(translateUri);
    // Request must be HTTP POST to include translation text 
    httpWebRequest.Method = "POST";
    httpWebRequest.ContentType = "text/plain";
    byte[] bytes = Encoding.ASCII.GetBytes("Translate this text");
    Stream os = null;

    try
    {
        // Text is inserted into the body of the request 
        httpWebRequest.ContentLength = bytes.Length;
        os = httpWebRequest.GetRequestStream();
        os.Write(bytes, 0, bytes.Length);
    }
    finally
    {
        // Close stream if successful 
        if (os != null)
        {
            os.Close();
        }
    }
    
    string output;
    try
    {
        WebResponse response = httpWebRequest.GetResponse();
        Stream stream = response.GetResponseStream();
        StreamReader reader = new StreamReader(stream);
        output = reader.ReadToEnd();
    }
    catch (Exception ex)
    {
        // An error may be thrown if the request body is not recognisable as text 
        // An error may be thrown if the from and to parameters are the same 
        output = "Error - " + ex.Message;
    }
    
    Console.WriteLine("Result: " + output);                                             
                                
Show:
© 2015 Microsoft