GetTranslationsArray Method
Use the GetTranslationsArray method to retrieve multiple translation candidates for multiple source texts.
| Request URI |
|---|
| http://api.microsofttranslator.com/V2/Ajax.svc/GetTranslationsArray |
| 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 10. |
|
from |
Required. A string representing the language code of the translation text. |
|
to |
Required. A string representing the language code to translate the text into. |
|
maxTranslations |
Required. An int representing the maximum number of translations to return. |
|
options |
Optional. A TranslateOptions object which contains the values
listed below. They all default to the most common settings. You do not need to set
any of the values.
|
| Name | Description |
|---|---|
| Array of GetTranslationsResponse |
Returns a GetTranslationsResponse array. Each GetTranslationsResponse
has the following elements:
|
| TranslationMatch |
A TranslationMatch object consists of the following:
|
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 languageFrom = "es";
var languageTo = "en";
var texts = "[\"" + encodeURIComponent("a veces los errores son divertidos") + "\", \"" +
encodeURIComponent("una importante contribución a la rentabilidad de la empresa") + "\"]";
var sourceText = new Array();
sourceText[0] = "a veces los errores son divertidos";
sourceText[1] = "una importante contribución a la rentabilidad de la empresa";
var maxTranslations = 5;
var options = "{\"User\":\"TestUserId\"}";
function getTranslationsArray() {
PageMethods.GetAccessToken(OnSucceeded, OnFailed);
}
function OnSucceeded(result, usercontext, methodName) {
window.mycallback = function (response) {
var array = response;
var translations = "";
for (var i = 0; i < array.length; i++) {
translations = translations + "<br /><br />Source text: " + sourceText[i] + "<br />";
for (var count = 0; count < array[i].Translations.length; count++) {
translations = translations + "Matched original text: " + array[i].Translations[count].MatchedOriginalText +
"<br />Translated text: " + array[i].Translations[count].TranslatedText +
"<br />Match degree: " + array[i].Translations[count].MatchDegree +
"<br />Rating: " + array[i].Translations[count].Rating +
"<br />count: " + array[i].Translations[count].Count + "<br />";
}
}
document.getElementById('<%= lblResult.ClientID %>').innerHTML = translations;
}
var s = document.createElement("script");
s.src = "http://api.microsofttranslator.com/V2/Ajax.svc/GetTranslationsArray?oncomplete=mycallback&appId=Bearer " + encodeURIComponent(result.access_token) + "&texts=" + texts + "&from=" + languageFrom +
"&to=" + languageTo + "&maxTranslations=" + maxTranslations + "&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="buttonGetTransArray" runat="server" Text="GetTranslationsArray" OnClientClick="getTranslationsArray();return false;" />
<br />
<asp:Label ID="lblResult" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
Note