Translating Webpage content into other language by using Google Translate in Javascript.

edited December 2022 in Web Development

As we want to translate language in the whole website, We have 2 ways, but the better way is using the javascript library of google translate.

Before adding javascript code, I want to explain the working process of this library.

It works by using the GET variable in URL links, By that, we have to pass a language code by which our javascript code will call to translation to the library then the translation process is complete.

Google Library link: //translate.google.com/translate_a/element.js?cb=googleTranslateElementInit

Include it in script like:

<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

Then in other script tags place the below function.

          function googleTranslateElementInit() {
          new google.translate.TranslateElement({
           pageLanguage: 'en', 
            includedLanguages: 'ar,en,es,fr,km,ru,sq,vi,zh-CN,pt', // This are the language code to translate
             layout: google.translate.TranslateElement.InlineLayout.SIMPLE, 
             multilanguagePage: true, 
             gaTrack: true, 
           }, 'google_translate_element'); 
         } 

After placing the above code, Add " #googtrans/es " to the end of our URL, Here es means Spanish.

Tagged:
Sign In or Register to comment.