Export (0) Print
Expand All

TranslateArray Method

Translates an array of text strings from one language to another.

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

URI parameters
Parameter Description

appId

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

texts

Required. An array containing the texts for translation. All strings must be of the same language. The total of all texts to be translated must not exceed 10000 characters. The maximum number of array elements is 2000.

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

options

Optional. A TranslateOptions element containing the values below. They are all optional and default to the most common settings.
  • Category: A string containing the category (domain) of the translation. Defaults to "general".
  • ContentType: The format of the text being translated. The supported formats are "text/plain", "text/xml" and "text/html". Any HTML needs to be well-formed.
  • Uri: A string containing the content location of this translation.
  • User: A string used to track the originator of the submission.
  • State: User state to help correlate request and response. The same contents will be returned in the response.

Response
Return value
Returns a TranslateArrayResponse array. Each TranslateArrayResponse has the following elements:
  • Error: Indicates an error if one has occurred. Otherwise set to null.
  • OriginalSentenceLengths: An array of integers indicating the length of each sentence in the original source text. The length of the array indicates the number of sentences.
  • TranslatedText: The translated text.
  • TranslatedSentenceLengths: An array of integers indicating the length of each sentence in the translated text. The length of the array indicates the number of sentences.
  • State: User state to help correlate request and response. Returns the same content as in the request.

Ff512407.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 = "fr";
        var texts = "[\"The answer lies in machine translation.\", \"Simply copy and paste a code snippet anywhere\", \"the best machine translation technology cannot always provide translations tailored to a site or users like a human\"]";
        var options = "{\"User\":\"TestUserId\"}";
        function translateArray() {
            PageMethods.GetAccessToken(OnSucceeded, OnFailed);

        }
        function OnSucceeded(result, usercontext, methodName) {

            window.mycallback = function (response) {
                var array = response;
                var translations = "Translation output for given array <br />";
                for (var i = 0; i < array.length; i++) {
                    translations = translations + array[i].TranslatedText + "<br />";
                }
                document.getElementById('<%= lblResult.ClientID %>').innerHTML = translations;
            }

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