在网页加载时将网站翻译成任何特定语言

时间:2012-10-23 12:11:48

标签: javascript jquery javascript-events google-translate google-translator-toolkit

任何人都知道如何在加载时使用Google翻译将网页翻译成任何语言?

我不想要下拉菜单,我只想在加载时将网站转换为特定语言。我尝试过很多东西 http://code.google.com/p/jquery-translate/但都是徒劳的。任何好的建议都会受到赞赏。

4 个答案:

答案 0 :(得分:11)

<!--
  This code will translate page contents automatically (without user input)
  Settings located at line 9, current script will translate english to estonian
-->
<style>#google_translate_element,.skiptranslate{display:none;}body{top:0!important;}</style>
<div id="google_translate_element"></div>
<script>
    function googleTranslateElementInit() {
        new google.translate.TranslateElement({
            pageLanguage: 'en', 
            includedLanguages: 'et', 
            autoDisplay: false
        }, 'google_translate_element');
        var a = document.querySelector("#google_translate_element select");
        a.selectedIndex=1;
        a.dispatchEvent(new Event('change'));
    }
</script>
<script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

从这个要点: https://gist.github.com/artturik/15bed885bcec6faa95eb73acb2e2ae54

答案 1 :(得分:6)

来自Google Translate Help

  

创建自动翻译您网站的链接   启用转换器的页面,无需提示您的用户,请使用   参数#googtrans(en|TARGET_LANG_CODE)

     

例如,链接   自动http://translate.google.com/support/#googtrans(en|fr)   将页面http://translate.google.com/support/翻译成法语。

答案 2 :(得分:4)

您可以通过提供网站网址,从https://translate.google.com获取谷歌翻译代码。

就像这样:

<div id="google_translate_element"></div><script type="text/javascript">
function googleTranslateElementInit() {
  new google.translate.TranslateElement({pageLanguage: 'ar', layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, 'google_translate_element');
}
</script><script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

答案 3 :(得分:0)

当前正在使用的工作代码

<!DOCTYPE html>
<html lang="en-US">
<head>
<script type="text/javascript">
function googleTranslateElementInit() {
  new google.translate.TranslateElement({pageLanguage: 'en', layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, 'google_translate_element');
}
</script>

<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
<script type="text/javascript" src="//translate.google.com/#en/hi/Hello"></script>
</head>

<style>
body > .skiptranslate {
    //display: none;
}

.goog-te-banner-frame.skiptranslate {
    display: none !important;
    } 
body {
    top: 0px !important; 
    }

@media print {
  #google_translate_element {display: none;}
}
</style>

<body>
<div id="google_translate_element"></div>

<p>This example displays a simple translate button, with no text.</p>
<p>This example displays a simple translate button, with no text.</p>

<div class="notranslate">
<p >This is a paragraph.</p>
<p >
A particle is kept at rest at the top of a sphere of diameter \(42 m\). When disturbed slightly,
it slides down. At what height \( h \) from the bottom, the particle will leave the sphere <br\>
(a) \( 14m \)
(b) \( 16m \)
(c) \( 35m \)
(d) \( 70m \)

</p>
</div>

</body>
</html>

它将翻译您的网页内容。如果您不想翻译任何特定的div内容,请使用类notranslate。则Google翻译器将不会翻译该div内容。