开发者

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/tr开发者_Go百科anslate_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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜