如何自定义此代码,以便在加载时自动翻译页面而不单击选择按钮。例如,我想用西班牙语自动加载页面吗?
<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");
});
答案 0 :(得分:1)
在&{34;插件设置&#34;中的Website Translator形式选项卡选择&#34;翻译语言&#34; &GT; &#34;特定语言&#34; &GT; &#34;西班牙语&#34 ;.设置&#34;自动&#34;在&#34;显示模式&#34;并选中复选框&#34;自动向说出您网页语言以外语言的用户显示翻译横幅。&#34; in&#34; Advanced&#34;块。
以下是HTML页面的代码示例。使用CSS隐藏包含语言名称的Google翻译栏和下拉列表。 JS代码单击翻译的选择选项:
<!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>