ユーザーが Web ページのボタンをクリックしたときに、Microsoft トランスレータ API と AJAX 呼び出しを使用して、段落を英語からスペイン語に翻訳することができました。スペイン語のテキストを英語に翻訳し直さなくても、元のテキストに切り替えることができるようにしたいと考えています。ページのソースを表示すると元のテキストが表示されますが、それをユーザーに表示する方法がわかりません。
function Translate()
{
var from = "en", to = "es", text = $('.Translate').text();
var s = document.createElement("script");
s.src = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate" +
"?appId=Bearer " + encodeURIComponent(window.accessToken) +
"&from=" + encodeURIComponent(from) +
"&to=" + encodeURIComponent(to) +
"&text=" + encodeURIComponent(text) +
"&oncomplete=MyCallback";
document.body.appendChild(s);
}
function MyCallback(response)
{
$('.Translate').text(response);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btnTranslate" onclick="Translate()" class="etsButton">Translate</button>
<button id="btnRestore" onclick="Restore()" class="etsButton">Restore</button>
<div style="padding:10px;" class="Translate">
To be, or not to be: that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles,
And by opposing end them?
</div>