locked
How do you pass a complete web document to be translated and posted back to page not just SomeControl.text? RRS feed

  • Question

  • I have looked at the samples and have been able to put together code that will pass the text I want translated to the Microsoft Translator API and return the translation back successfully where I wanted it.  However, I have been unsuccessful at doing the whole page.  Basically I want to do is what the Bing Widget does for translating a whole page with my code behind placing the attribution in a location that is desirable to our clients.  I have received an exception approval from Microsoft as to the location.

    The only way I see to do this at the moment using the API is to re-write code on the whole site to send each item that I wants translated to the API individually.  

    The reason for not using the widget is that the return from the translate method is undesirable to the page.

    Is there a simple way to send the whole page and post the whole translated page back?

    Any help would be appreciated.

    Thanks

    Wednesday, August 14, 2013 6:11 PM

All replies

  • I have figured out how to pass the page data to the API for the translating however the response back code to put it in place is failing.  The javascript code for this is as follows:

                 <script type="text/javascript">
    
                     onload = function () {
                         var from = "en"
                         var to = document.getElementById('hdnLanguage').value;
                         var text = document.getElementsByTagName("html")[0].innerHTML;
                         var accessToken = document.getElementById('hdnAccessCode').value;
    
                         var s = document.createElement("script");
                         s.src = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate" +
                        "?appId=Bearer " + encodeURIComponent(accessToken) +
                        "&from=" + encodeURIComponent(from) +
                        "&to=" + encodeURIComponent(to) +
                        "&text=" + encodeURIComponent(text) +
                        "&contentType=text/html" +
                        "&oncomplete=myTranslation";
    
    
                         //var p = document.getElementsByTagName('head')[0]; p.insertBefore(s, p.firstChild);
                         //                                document.body.appendChild(s);
    
                     }
    
                     function myTranslation(response) {
                         var element = document.getElementsByTagName("head")[0];
                         var r = response;
                         element.innerHTML = r;
                     }
             
             </script>

    I am passing from the server the access token and the language I want to translate the page to.  This is being done in a site.master page so to try to automatically translate the page on page load from the client side.  The row element.innerHTML = r;  fails.  Someone told me that you can not do that to the head tag.  Which seems obvious since the code failed there.  I also looked at the code that widget was using and that did not seem to see the same results.  Can any of you guru's out know how to place the whole page return back to the page without changing the ViewState of the page?

    Searches seem to be pointing me to the same pages.  If anyone has already done this if you could either point me to sample code for a whole page translation using the API and not the widget I would appreciate it.

    Any help is appreciated really.

    Friday, August 16, 2013 5:08 PM