English 中文(简体)
google API translate, only a div into page
原标题:

I have an HTML page, and I would use Google Translate to translate only a div into my page.

<div id="google_translate_element"></div><script>
function googleTranslateElementInit() {
  new google.translate.TranslateElement({pageLanguage:  it },  google_translate_element );
}
</script><script src="https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

With this code, google translate everything in the page. But if I wanna translate only a piece. How I can do it?

Thanks!

最佳回答

Check the examples at Google AJAX Language API.

google.language.translate("Hello world", "en", "es", function(result) {
  if (!result.error) {
    var container = document.getElementById("translation");
    container.innerHTML = result.translation;
  }
});

use innerHTML to get hold of the contents of your DIV. You may have to strip out formatting first, though.

问题回答

ok, but how to open any named <div> of a internet web page in my page with <iframe> tag? Is it possible in general, not to display (in iframe) the whole page, but only the part of it (without balast arround)?

I know e.g. this for whole pages :

<div>
  <iframe src ="http://www.colosseum.cz/vzdelavani-a-informace/zpravy" width="98%" height="600" style="border: 3px;">
    <p>unsupported</p>
  </iframe> 
</div>

If you want to continue using the script instead of the API. You can add a notranslate class to all elements that you don t want translated.

You can try it here https://www.w3schools.com/howto/tryit.asp?filename=tryhow_google_translate

Try changing the h1 tag to

<h1 class="notranslate">My Web Page</h1>

Answer found here https://laracasts.com/discuss/channels/laravel/translating-only-a-portion-of-page





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签