dragon456101 2017-10-27 02:59
浏览 657
已采纳

使用Google翻译代码自动翻译网页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");
});
  • 写回答

1条回答 默认 最新

  • doubi1931 2017-10-27 03:58
    关注

    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.

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏