最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to change google translator select language text in coding - Stack Overflow

programmeradmin3浏览0评论

I am using the google translator in my website, I want to change the select language text in the drop down. Anyone suggest for this. Here is my code;

function googleTranslateElementInit() {
       new google.translate.TranslateElement({pageLanguage: 'en', includedLanguages: 'ar', layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, 'google_translate_element');
}

Thanks in advance.

I am using the google translator in my website, I want to change the select language text in the drop down. Anyone suggest for this. Here is my code;

function googleTranslateElementInit() {
       new google.translate.TranslateElement({pageLanguage: 'en', includedLanguages: 'ar', layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, 'google_translate_element');
}

Thanks in advance.

Share Improve this question asked Mar 29, 2016 at 10:36 LavanyaLavanya 1051 gold badge2 silver badges10 bronze badges 1
  • I would suggest not to change the text of dropdown. Instead use custom button or link for functionality. have a look at this stack thread. stackoverflow./questions/1565887/… – Prashant Kabade Commented Mar 29, 2016 at 10:46
Add a ment  | 

2 Answers 2

Reset to default 8

According to this question you can modify the element as you want.

Like this:

<script src="https://code.jquery./jquery-2.1.4.js"></script>
<div id="google_translate_element"></div>
<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./translate_a/element.js?cb=googleTranslateElementInit"></script>
<script>
  $(document).ready(function(){
    $('#google_translate_element').bind('DOMNodeInserted', function(event) {
      $('.goog-te-menu-value span:first').html('Translate');
      $('.goog-te-menu-frame.skiptranslate').load(function(){
        setTimeout(function(){
          $('.goog-te-menu-frame.skiptranslate').contents().find('.goog-te-menu2-item-selected .text').html('Translate');    
        }, 100);
      });
    });
  });
</script>

http://output.jsbin./jucoba

I spent a lot of time trying to get rid of "RangeError: Maximum call stack size exceeded." error. Here is the solution I came up with. Just assign a new value to yourContent variable and you are ready to go. Hope someone will find this helpful.

    var targetElement = document.getElementById("google_translate_element");
    var yourContent = "Your text or <br /> HTML element"
    targetElement.addEventListener("DOMNodeInserted", () => {
      $(".goog-te-menu-value span:first").each(function (i, obj) {
        if (obj.childNodes[0] && obj.childNodes[0].textContent == "Select Language") {
          $(obj).html(yourContent);
        }
      });
    });

Also, if you want to add image instead of text, you will need to add the following css to clean up unnecessary borders:

.goog-te-menu-value span:nth-child(3), 
.goog-te-menu-value span:nth-child(5), 
.goog-te-gadget-icon, 
.goog-te-banner-frame.skiptranslate {
    display:none !important;
  }

div#google_translate_element div.goog-te-gadget-simple {
    border: none;
    background-color: transparent;
}
发布评论

评论列表(0)

  1. 暂无评论