1

我正在使用下面的代码进行谷歌翻译,它工作得很好。问题是那些在中国的人无法访问谷歌翻译下拉菜单,因为 google.translate.com 被屏蔽了。但是 google.translate.cn 没有被屏蔽。我试过在脚本部分简单地使用 google.translate.cn 但是语言下拉菜单都是中文的......

如何引导语言选项中选择中文的用户使用google.translate.cn,世界其他地区的用户使用google.translate.com?

我想我需要使用自定义下拉菜单而不是默认的谷歌下拉菜单?任何人都有如何做到这一点的代码?提前致谢


<Style>
.goog-te-banner-frame.skiptranslate {
    display: none !important;
    } 
body {
    top: 0px !important;
font-family: "Segoe UI", "Segoe UI", Segoe, Tahoma, Helvetica, Arial, sans-serif;
font-size: 13px;
font-style: normal;
font-variant-caps: normal;
font-variant-east-asian: normal;
font-variant-ligatures: normal;
font-variant-numeric: normal;
font-weight: 400 ;
    }
 </Style>
<div id="google_translate_element">


<script>
   function googleTranslateElementInit() {
                new google.translate.TranslateElement({pageLanguage: 'en', includedLanguages: 'en,ko,ja,id,ms,th,vi,zh-CN,zh-TW', layout: google.translate.TranslateElement.InlineLayout.HORIZONTAL}, 'google_translate_element');

                 /*
            To remove the "powered by google",
            uncomment one of the following code blocks.
            NB: This breaks Google's Attribution Requirements:
            https://developers.google.com/translate/v2/attribution#attribution-and-logos
        */

        // Native (but only works in browsers that support query selector)
        if(typeof(document.querySelector) == 'function') {
           document.querySelector('.goog-logo-link').setAttribute('style', 'display: none');
            document.querySelector('.goog-te-gadget').setAttribute('style', 'font-size: 0');
        }

        // If you have jQuery - works cross-browser - uncomment this
        jQuery('.goog-logo-link').css('display', 'none');
        jQuery('.goog-te-gadget').css('font-size', '0');



  }




        </script>

                <script type="text/javascript" src="https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script></div>



4

1 回答 1

2

处理这个问题的直接方法是从 google.com 和 google.cn 加载翻译脚本。在这种情况下,如果从 google.com 加载失败,则应该从 google.cn 加载。

唯一需要做的是确保 Google 翻译元素只创建一次。

<script>
var googleTranslateElement; //global var for Google Translate Element

function googleTranslateElementInit() {

    if (googleTranslateElement) return; //var being non-empty means element already created

    //store Google Translate Element in our var
    googleTranslateElement = new google.translate.TranslateElement({pageLanguage: 'en', includedLanguages: 'en,ko,ja,id,ms,th,vi,zh-CN,zh-TW', layout: google.translate.TranslateElement.InlineLayout.HORIZONTAL},'google_translate_element'); 

//----- cut -----

}
</script>

...并且不要忘记从 translate.google.cn 加载翻译脚本:

<script type="text/javascript" src="https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

<!-- Load translate script from translate.google.cn -->

<script type="text/javascript" src="https://translate.google.cn/translate_a/element.js?cb=googleTranslateElementInit"></script>
于 2019-12-03T16:26:44.030 回答