Detect Method

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

Use the Detect Method to identify the language of a selected piece of text.

Dd576282.clear(en-us,MSDN.10).gif Syntax

URI

https://api.microsofttranslator.com/V1/Http.svc/Detect

Dd576282.clear(en-us,MSDN.10).gif Parameters

Parameter Description

appId

A string representing the identifier of the calling application.

text

A string representing the text to translate.

Dd576282.clear(en-us,MSDN.10).gif Return Value

The text translated into the desired language.

Dd576282.clear(en-us,MSDN.10).gif Example

C#

    string detectUri = "https://api.microsofttranslator.com/V1/Http.svc/Detect?appId=myAppId";
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(detectUri);
    // Request must be HTTP POST to include translation text 
    httpWebRequest.Method = "POST";
    httpWebRequest.ContentType = "text/plain";
    byte[] bytes = Encoding.ASCII.GetBytes("Detect 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 
        output = "Error - " + ex.Message;
    }
    
    Console.WriteLine("Detected Language: " + output);