Export (0) Print
Expand All

Translate Method

Translates a text string from one language to another.

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

URI parameters
Parameter Description

appId

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

text

Required. A string representing the text to translate. The size of the text must not exceed 10000 characters.

from

Optional. A string representing the language code of the translation text. If left empty the response will include the result of language auto-detection.

to

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

contentType

Required. The format of the text being translated. The supported formats are "text/plain" and "text/html". Any HTML needs to be well-formed.

category

Optional. A string containing the category (domain) of the translation. Defaults to "general".


Response
Return value
A string representing the translated text. If you previously use AddTranslation or AddTranslationArray to enter a translation with a rating of 5 or higher for the same source sentence, Translate returns only the top choice that is available to your system. The "same source sentence" means the exact same (100% matching), except for capitalization, white space, tag values, and punctuation at the end of a sentence. If no rating is stored with a rating of 5 or above then the returned result will be the automatic translation by Microsoft Translator.

Ff512406.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">
        var languageFrom = "en";
        var languageTo = "de";
        var text = "Use pixels to express measurements for padding and margins.";
        function translate() {
            PageMethods.GetAccessToken(OnSucceeded, OnFailed);

        }
        function OnSucceeded(result, usercontext, methodName) {

            window.mycallback = function (response) {
                document.getElementById('<%= lblResult.ClientID %>').innerHTML = "Translation for <br />source text: '" + text + "'<br /> from en to de <br /> " + response;
            }

            var s = document.createElement("script");
            s.src = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?oncomplete=mycallback&appId=Bearer " + encodeURIComponent(result.access_token) + "&from=" + languageFrom + "&to=" + languageTo + "&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:Button ID="buttonTranslate" runat="server" Text="Translate" OnClientClick="translate();return false;" />
        <br />
        <asp:Label ID="lblResult" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>
                                            
Show:
© 2014 Microsoft