DetectArray Method
Use the DetectArray Method to identify the language of an array of strings all at once. Performs independent detection of each individual array element and returns a result for each element of the array.
| Request URI |
|---|
| http://api.microsofttranslator.com/V2/Ajax.svc/DetectArray |
| Parameter | Description |
|---|---|
|
appId |
Required. A string containing "Bearer" + " " + access token. |
|
texts[] |
Required. A string array representing the text from an unknown language. The size of the text must not exceed 10000 characters. |
| Return value |
|---|
| A string array containing a two-character Language codes for each row of the input array. |
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;
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
var texts = "[\"les erreurs sont parfois amusants.\", \"you can try to enter a longer phrase.\", \"Questo un testo italiano.\"]";
function detectArray() {
PageMethods.GetAccessToken(OnSucceeded, OnFailed);
}
function OnSucceeded(result, usercontext, methodName) {
window.mycallback = function (response) {
var array = response;
var detectedLanguages = "";
for (var i = 0; i < array.length; i++) {
detectedLanguages = detectedLanguages + array[i] + "\n";
}
alert(detectedLanguages);
}
var s = document.createElement("script");
s.src = "http://api.microsofttranslator.com/V2/Ajax.svc/DetectArray?oncomplete=mycallback&appId=Bearer " + encodeURIComponent(result.access_token) + "&texts=" + texts;
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="buttonDetectArray" runat="server" Text="Detect array" OnClientClick="detectArray();return false;" />
</div>
</form>
</body>
</html>
Note