使用Google翻译代码在加载时自动翻译网页 [英] Using Google Translate Code to Auto Translate Webpage onLoad
问题描述
如何自定义此代码,以便在加载后无需单击选择按钮即可自动翻译页面.例如,我想用西班牙语自动加载页面?
How do you customize this code so that upon loading it will auto translate the page without clicking the select button. example i want to load the page automatically in spanish??
<div id="google_translate_element"></div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
function googleTranslateElementInit() {
new google.translate.TranslateElement({ pageLanguage: "en" }, "google_translate_element");
};
$.getScript("//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit");
});
推荐答案
在网站翻译器在插件设置"标签中选择翻译语言">特定语言">西班牙语".在显示模式"中将自动"设置为自动",然后选中自动向使用您页面语言以外的其他语言的用户显示翻译标语"复选框.在高级"块中.
In Website Translator form in "Plugin Settings" tab choose "Translation languages" > "Specific languages" > "Spanish". Set "Automatic" in "Display mode" and check the checkbox "Automatically display translation banner to users speaking languages other than the language of your page." in "Advanced" block.
以下是HTML页面的代码作为示例.使用CSS隐藏了带有语言名称的Google翻译栏和下拉列表.JS代码单击翻译的选择选项:
Here is the code of the HTML page as example. Google translation bar and drop-down list with names of languages are hidden using CSS. JS code makes a click on the select option for the translation:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test Google translate</title>
<style>
.skiptranslate, #google_translate_element {display: none;}
body {min-height: 0px !important; position: static !important; top: 0px !important;}
</style>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" crossorigin="anonymous"></script>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
</head>
<body>
The text is translated using Google translate.
<div id="google_translate_element"></div>
<script type="text/javascript">
function googleTranslateElementInit() {
$.when(
new google.translate.TranslateElement({pageLanguage: 'en', includedLanguages: 'es',
layout: google.translate.TranslateElement.FloatPosition.TOP_LEFT}, 'google_translate_element')
).done(function(){
var select = document.getElementsByClassName('goog-te-combo')[0];
select.selectedIndex = 1;
select.addEventListener('click', function () {
select.dispatchEvent(new Event('change'));
});
select.click();
});
}
</script>
</body>
</html>
这篇关于使用Google翻译代码在加载时自动翻译网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!