当用户单击我网页上的按钮时,我已经能够使用带有 AJAX 调用的 Microsoft 翻译 API 将一段从英语翻译成西班牙语。我想让他们能够切换回原始文本,而无需将西班牙语文本翻译回英文。当我查看页面源代码时,我可以看到原始文本,但我不确定如何将其显示给用户。
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>