0

I am trying to convert the whole web page in different language based on the user who is logged in. I am using the following code

  <div id="google_translate_element"></div>
  <script type="text/javascript">
    function googleTranslateElementInit() {
      new google.translate.TranslateElement({
        pageLanguage: 'en',
        includedLanguages: 'es',
        layout: google.translate.TranslateElement.InlineLayout.SIMPLE,
        autoDisplay: true,

      }, 'google_translate_element');
    }
  </script>
  <script type="text/javascript"
          src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

This is working as expected but user has to choose the language. Is there a way I can automate this?

Also as the translate is deprecated does any body has a guide or tutorial to use the new google translate API.

Any have searched too many posts but none of them auto translate option or using the new API's

LReddy
  • 117
  • 17

1 Answers1

0

So you would sniff the user's preferred language using NavigatorLanguage and set that as a language.

I don't know about the latest Translate API, but a quick search gave me this: https://dev.to/robertchen/how-to-use-google-translate-api-27l9

ravb79
  • 722
  • 5
  • 8
  • Thanks, But I already know the users preferred language. The API link you provided is translating only a certain text. In my case I want to translate the whole web page. I was able to do that with the code above. But my issue with that code is user needs to select the preferred language from drop down – LReddy Apr 08 '20 at 14:00
  • So if it's just about activating the script: https://stackoverflow.com/questions/20353866/how-to-force-google-translate-to-translate-the-page-after-the-script-loads – ravb79 Apr 08 '20 at 14:09