The old machine translation utility has been deprecated. This article will go over converting the machine language translation provider to Microsoft Translate. It is a fairly painless process, but does require some workarea modifications.
An alternative approach is presented in another KB article, which uses Google Translate and is easier to set up: /KB/1125/
In the workarea, if you click on the translate button, the content will either not translate or it will throw an error.
- Create a Microsoft account. This will be needed for the clientid and clientsecret in the code.
- Sign up for the Microsoft Translator API. There is a free option that allows you to translate up to 2 million words a month.
- Add a service reference in Visual Studio (right click on App_WebReferences Add Service Reference)
- Point to the following URL as the reference and name it "MicrosoftTranslator"
- Go to the workarea folder and open up the worldlingo.aspx.cs file.
- Find the btnTranslate_Click method and replace it with the following code.
protected void btnTranslate_Click(System.Object sender, System.EventArgs e)
{
string retContent;
Ektron.Cms.LocalizationAPI LocalizeAPI = new Ektron.Cms.LocalizationAPI();
AdmAccessToken admToken;
string headerValue;
//Get Client Id and Client Secret from https://datamarket.azure.com/developer/applications/
//Refer obtaining AccessToken (http://msdn.microsoft.com/en-us/library/hh454950.aspx)
AdmAuthentication admAuth = new AdmAuthentication("clientid", "clientsecret");
try
{
admToken = admAuth.GetAccessToken();
DateTime tokenReceived = DateTime.Now;
// Create a header with the access_token property of the returned token
headerValue = "Bearer " + admToken.access_token;
retContent = TranslateMethod(headerValue, wl_data.Value, (string)wl_trglang.SelectedValue, (string)wl_srclang.SelectedValue);
}
catch (Exception ex)
{
retContent = ex.Message;
}
System.ServiceModel.Channels.HttpRequestMessageProperty httpRequestProperty = newSystem.ServiceModel.Channels.HttpRequestMessageProperty();
MicrosoftTranslator.SoapService ss = new MicrosoftTranslator.SoapService();
//retContent = LocalizeAPI.TranslateUsingWorldLingo((string) wl_data.Value, "text/html", (string) wl_srclang.SelectedValue, (string) wl_trglang.SelectedValue, (string) wl_gloss.Value);
displaycontent.InnerHtml = retContent;
returnContent.Value = retContent;
targetLanguage.Value = wl_trglang.SelectedValue;
}
- Replace the clientid and clientsecret with the values you generated at step 1
- Add in the following method right below the btnTranslate_Click method
privatestaticstring TranslateMethod(stringauthToken, string text, string to, string from, string type = "general")
{
// Add TranslatorService as a service reference,Address:http://api.microsofttranslator.com/V2/Soap.svc
MicrosoftTranslation.LanguageServiceClientclient = new MicrosoftTranslation.LanguageServiceClient();
//Set Authorization header before sending the request
System.ServiceModel.Channels.HttpRequestMessagePropertyhttpRequestProperty = newSystem.ServiceModel.Channels.HttpRequestMessageProperty();
httpRequestProperty.Method = "POST";
httpRequestProperty.Headers.Add("Authorization", authToken);
string translationResult;
// Creates a block within which an OperationContext objectis in scope.
using (System.ServiceModel.OperationContextScopescope = new System.ServiceModel.OperationContextScope(client.InnerChannel))
{
System.ServiceModel.OperationContext.Current.OutgoingMessageProperties[System.ServiceModel.Channels.HttpRequestMessageProperty.Name] = httpRequestProperty;
//Keep appId parameter blank as we are sending access tokenin authorization header.
translationResult = client.Translate("", text, from, to, "text/html", type);
//translationResult = client.Translate("",sourceText, "en", "de", "text/html","general");
}
return translationResult;
}
- After the main class add the following code.
[System.Runtime.Serialization.DataContract]
publicclassAdmAccessToken
{
[System.Runtime.Serialization.DataMember]
publicstringaccess_token { get; set; }
[System.Runtime.Serialization.DataMember]
publicstringtoken_type { get; set; }
[System.Runtime.Serialization.DataMember]
publicstringexpires_in { get; set; }
[System.Runtime.Serialization.DataMember]
publicstring scope {get; set; }
}
publicclassAdmAuthentication
{
publicstaticreadonlystringDatamarketAccessUri = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13";
privatestringclientId;
privatestringcientSecret;
privatestringrequest;
public AdmAuthentication(stringclientId, string clientSecret)
{
this.clientId = clientId;
this.cientSecret = clientSecret;
//If clientid or client secret has special characters,encode before sending request
this.request = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=http://api.microsofttranslator.com", System.Web.HttpUtility.UrlEncode(clientId), System.Web.HttpUtility.UrlEncode(clientSecret));
}
publicAdmAccessTokenGetAccessToken()
{
return HttpPost(DatamarketAccessUri, this.request);
}
privateAdmAccessTokenHttpPost(string DatamarketAccessUri, string requestDetails)
{
//Prepare OAuth request
System.Net.WebRequest webRequest = System.Net.WebRequest.Create(DatamarketAccessUri);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(requestDetails);
webRequest.ContentLength = bytes.Length;
using (System.IO.StreamoutputStream = webRequest.GetRequestStream())
{
outputStream.Write(bytes, 0, bytes.Length);
}
using (System.Net.WebResponsewebResponse = webRequest.GetResponse())
{
System.Runtime.Serialization.Json.DataContractJsonSerializerserializer = newSystem.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(AdmAccessToken));
//Get deserialized object from JSON stream
AdmAccessToken token = (AdmAccessToken)serializer.ReadObject(webResponse.GetResponseStream());
return token;
}
}
}
- Save the file and try to translate the content again in the workarea. If your clientid and clientsecret are correct the code should translate the content just as before.