Export (0) Print
Expand All
42 out of 67 rated this helpful - Rate this topic

Detect Method

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

Request
Request URI
http://api.microsofttranslator.com/V2/Ajax.svc/Detect

URI parameters
Parameter Description

appId

Required. A string containing "Bearer" + " " + access token.

text

Required. A string representing the text from an unknown language. The size of the text must not exceed 10000 characters.


Response
Return value
A string containing a two-character Language code for the given text.

Ff512396.alert_note(en-us,MSDN.10).gif Note

Bing App ID is deprecated and is no longer supported.   Please obtain an access token to use the Microsoft Translator API. For details, go here.


Example

Obtain an Access Token on the server-side

                                               
[System.Web.Services.WebMethod]
public static AdmAccessToken GetAccessToken()
{
    AdmAccessToken admToken;
    string headerValue;
    //Get Client Id and Client Secret from https://datamarket.azure.com/developer/applications/
    AdmAuthentication admAuth = new AdmAuthentication("clientid", "clientsecret");

    admToken = admAuth.GetAccessToken();
    // Create a header with the access_token property of the returned token
    headerValue = "Bearer" + " " + HttpUtility.UrlEncode(admToken.access_token);

    return admToken;
} 
                                            
Invoke the AJAX method on the client-side

                                                
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function detectLanguage() {


            var textToDetect = document.getElementById('<%= txtDetectLan.ClientID %>').value;
            if (textToDetect != "") {

                PageMethods.GetAccessToken(OnSucceeded, OnFailed);
            }
        }
        function OnSucceeded(result, usercontext, methodName) {
            var textToDetect = document.getElementById('<%= txtDetectLan.ClientID %>').value;
            if (textToDetect != "") {
                window.mycallback = function (response) {

                    document.getElementById('<%= lblLangDetect.ClientID %>').innerHTML = "The detected language is:" + response;
                }
                var text = encodeURIComponent(textToDetect);
                var s = document.createElement("script");
                s.src = "http://api.microsofttranslator.com/V2/Ajax.svc/Detect?oncomplete=mycallback&appId=Bearer " + encodeURIComponent(result.access_token) + "&text=" + text;
                document.getElementsByTagName("head")[0].appendChild(s);

            }

        }
        function OnFailed(error, userContext, methodName) {
            alert("Error");
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager" runat="server" EnablePageMethods="true" />
        <asp:Label ID="labelDetect" runat="server" Text="Enter Text to detect language"></asp:Label>
        <asp:TextBox ID="txtDetectLan" runat="server">
        </asp:TextBox>
        <asp:Button ID="buttonDetetc" runat="server" Text="Detect language" OnClientClick="detectLanguage();return false;" />
        <br />
        <asp:Label ID="lblLangDetect" runat="server">
        </asp:Label>
        <br />
    </div>
    </form>
</body>
</html>
                                            
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.