Machine Language Translation

  • Updated

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.

  1. Create a Microsoft account. This will be needed for the clientid and clientsecret in the code. 
  2. Sign up for the Microsoft Translator API. There is a free option that allows you to translate up to 2 million words a month.
  3. Add a service reference in Visual Studio (right click on App_WebReferences Add Service Reference)
  4. Point to the following URL as the reference and name it "MicrosoftTranslator"
  5. Go to the workarea folder and open up the worldlingo.aspx.cs file.
  6. 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 

            //Refer obtaining AccessToken (  

            AdmAuthentication admAuth = new AdmAuthentication("clientid", "clientsecret"); 



                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; 


  7. Replace the clientid and clientsecret with the values you generated at step 1
  8. 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:

           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;


  9. After the main class add the following code. 





        publicstringaccess_token { get; set; }


        publicstringtoken_type { get; set; }


        publicstringexpires_in { get; set; }


        publicstring scope {get; set; }




        publicstaticreadonlystringDatamarketAccessUri = "";





        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=", System.Web.HttpUtility.UrlEncode(clientId), System.Web.HttpUtility.UrlEncode(clientSecret));




            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;







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