2

I am pretty new to Javascript and Web Programming. I am trying to learn how I can have a website page automatically translated into a language that a user chooses. I have a backend page where the user can decide to set their default language and it would automatically translate to that language on any page load. The problem I am encountering is that if a user changes a language from the google drop down, despite the language chosen in their profile, it will use the last language selected. I have this code here:

<div id="google_translate_element"></div><script type="text/javascript">
function googleTranslateElementInit() {
  new google.translate.TranslateElement({pageLanguage: 'nl', includedLanguages: 'nl,it,en', layout: google.translate.TranslateElement.InlineLayout.SIMPLE, autoDisplay: true, multilanguagePage: false}, 'google_translate_element');
}
</script><script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>


<style>
.goog-te-banner-frame.skiptranslate {
    display: none !important;
} body { top: 0px !important; }
.goog-tooltip {
    display: none !important;
}
.goog-tooltip:hover {
    display: none !important;
}
.goog-text-highlight {
    background-color: transparent !important;
    border: none !important; 
    box-shadow: none !important;
}
</style>
<p>Here is the Area where Google fetches the part to be translated</p>

This is my fiddle here: https://jsfiddle.net/k0j2ts72/

In this example, I want to pretnd that the user entered into their profile a default language of Dutch. If on one page they choose from the translation drop down: Italian, no matter how I code it or what is set up in the back end, it will always use Italian. This also makes it harder for me for once a langauge is auto detected on a system/browser because the site wont change it to that language initially. It will always be English or whatever language they choose from another website.

I have looked at the following answers on stack overflow that are all not helping: Answer 1, Answer 2, Answer 3. But nothing is doing what I need. Can anyone provide assistance here? Thank you!

Joey Gilda
  • 31
  • 5

0 Answers0