How to change google translator select language text in coding
Asked Answered
G

2

7

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.

Gwynethgwynne answered 29/3, 2016 at 10:36 Comment(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. #1566387Selfwill
L
8

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

Like this:

<script src="https://code.jquery.com/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.com/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.com/jucoba

Lucais answered 29/3, 2016 at 10:50 Comment(2)
Hi thanks Mosu.. Its working. But in the drop down, 2 select language text are there, From your script 1st one is changed, and 2nd one which is placed after click on the drop down, that too want to change.Gwynethgwynne
My pleasure ;) Good luck.Lucais
N
2

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;
}
Navarra answered 31/8, 2021 at 6:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.