Google Translate isn't Hidden
Asked Answered
L

2

6

Having opted for the "Automatic" version of Google Translate Widget, I expected not to see the "Select Language" dropdown if my browser was the same language as the site. However, I see it all the time no matter what I set the html lang attribute to or what I set my browsers preferred language to. I have also noticed that it doesn't seem to make any difference whether the meta "google-translate-customization" tag is there or not, the widget is always in view.

I'd like the site to just be translated if the users browser isn't set to English.

Any advice would be appreciated.

Code used:-

In head:-

<meta name="google-translate-customization" content="6bb255d109276506-b73cb06230e6b6c0-gbb2acb9bc95b4a11-12"></meta>

In Body:-

   <div id="google_translate_element"></div>
            <script>
            function googleTranslateElementInit() {
              new google.translate.TranslateElement({pageLanguage: 'en', layout: google.translate.TranslateElement.FloatPosition.TOP_LEFT}, 'google_translate_element');
            }
            </script>
            <script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
Lemur answered 13/11, 2014 at 12:17 Comment(1)
possible duplicate of Google Website Translator Automatic Display ModeTombstone
P
3

You can try this small jquery script:

var userLang = navigator.language || navigator.userLanguage; 
if(userLang == "en"){
    $("#google_translate_element").css(["display", "none"]);
}

Not sure if the if is right I'm using a dutch browser and it showed nl as userLang. I'm pretty sure the english one should be named en. Otherwise you have to alert userlang and change it to that.

alert(userLang);

Heres the jsfiddle: http://jsfiddle.net/u950mwom/1/

Pollinize answered 13/11, 2014 at 12:27 Comment(4)
Thanks for that. It would probably work, but the point is, it's supposed to do that anyway, according to how I read Google's instructions. I just wondered if anyone else had got over the issue or had discussed it with Google themselves?Lemur
Maybe you can contact the google support? they probally know how to solve your problem! :-) support.google.comPollinize
Already tried that thanks. Just end up in a loop of pages with no actual email address or suitable forum, hence asking on here. But I'm pretty sure it ain't doing what it's s'posed to.Lemur
Edited code to add extra compatability by adding || navigator.languages; - also note that it returns the language with locale, ie normally en_US rather than only en See this answer for many extra English localesTombstone
T
0

Finally a fix for this (which is a long-standing bug in google). The code below hides the language selection drop-down box for English users on an English page. It copdes with locales like en-US also and newer browsers.

<div id="google_translate_element"></div>
<script type="text/javascript">
var userLang = navigator.language || navigator.userLanguage || navigator.languages; 
if (userLang.substr(0,2) != "en"){
  function googleTranslateElementInit() {
    new google.translate.TranslateElement({pageLanguage: 'en', layout: 
    google.translate.TranslateElement.FloatPosition.TOP_LEFT}, 'google_translate_element');
   }
 }
else { 
  document.getElementById("google_translate_element").style.display="none";
  }
</script>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

cross-browser compatibility explained

Tombstone answered 15/8, 2015 at 2:25 Comment(1)
if userLang defaults to navigator.languages it will be an array (at least in the latest version of chrome). It will then throw a type error when calling substrScurrilous

© 2022 - 2024 — McMap. All rights reserved.