任何人都知道如何在加载时使用Google翻译将网页转换为任何语言?
我不希望下拉菜单,我只想在加载时将网站转换为特定的语言。我尝试了很多事情http://code.google.com/p/jquery-translate/却徒劳无功。任何好的建议都将不胜感激。
<!--
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
来自Google Translate帮助:
创建一个自动翻译您网站的链接 启用翻译器的页面而不提示您的用户,请使用 参数
#googtrans(en|TARGET_LANG_CODE)
。例如,链接 http://translate.google.com/support/#googtrans(en|fr)自动 翻译页面http://translate.google.com/support/将页面翻译成法语。
您可以通过给您网站URL来从https://translate.google.com获取Google Translator代码。
喜欢:
<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>
Google翻译的问题是它与一段时间滞后一起工作。我尝试了10,100和1000ms,但只有1000ms(表示1秒)效果很好。我希望这是为您完成工作的。
样式Google_translate_element作为显示:无要隐藏。
<script type="text/javascript">
function googleTranslateElementInit() {
new google.translate.TranslateElement({
pageLanguage: 'en',
includedLanguages: 'en,es',
}, 'google_translate_element');
setTimeout(function() {
// Set the default language to Spanish
var selectElement = document.querySelector('#google_translate_element select');
selectElement.value = 'es';
selectElement.dispatchEvent(new Event('change'));
}, 1000);
}
</script>
这将把页面转换为页面加载的西班牙语(" es"
当前正在使用它正在使用
<!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内容,则只需使用class notranslate即可。然后,Google Translator不会翻译该DIV内容。