×

How to add Google Translate Button on your Webpage?

Google translator is a multi-language translator used for translating the webpage. Google develops it. In today's digital age, it is essential because information is available worldwide in some specific language which can be translated in our language. By using language translator, we can increase our website's expandability and reachability. Suppose, our website uses a particular language and we have users where another language is mainly spoken. In that case, the translator has a vital role where it can translate the website language to the spoken language of that area. 

One of the most essential and popular tools for translation is Google translator. It provides accurate results for the user’s query and gives a designer look to the webpage.

There are four steps for creating this:

Step 1: Create an HTML file and save it with the extension name “.html”.

Step 2: After creating the HTML file, we need to create a CSS file with the extension name “.css”.

Step 3: Include JavaScript file to the webpage.

Step 4: Now, we need to include a Google transcript.

Note: Google translator button is designed with the help of HTML, CSS and JavaScript.

HTML code for Google translator-

Create an HTML file and save as with the extension name “.html”.

<div class="ct-topbar">
 <div class="container">
 <ul class="list-unstyled list-inline ct-topbar__list">
 <li class="ct-language">Language <i class="fa fa-arrow-down"></i>
 <ul class="list-unstyled ct-language__dropdown">
 <li><a href="#googtrans(en|en)" class="lang-en lang-select" data-lang="en"><img src="https://www.solodev.com/assets/Google-translate/flag-usa.png" alt="USA"></a></li>
 <li><a href="#googtrans(en|es)" class="lang-es lang-select" data-lang="es"><img src="https://www.solodev.com/assets/Google-translate/flag-mexico.png" alt="MEXICO"></a></li>
 <li><a href="#googtrans(en|fr)" class="lang-es lang-select" data-lang="fr"><img src="https://www.solodev.com/assets/Google-translate/flag-france.png" alt="FRANCE"></a></li>
 <li><a href="#googtrans(en|zh-CN)" class="lang-es lang-select" data-lang="zh-CN"><img src="https://www.solodev.com/assets/Google-translate/flag-china.png" alt="CHINA"></a></li>
 <li><a href="#googtrans(en|ja)" class="lang-es lang-select" data-lang="ja"><img src="https://www.solodev.com/assets/Google-translate/flag-japan.png" alt="JAPAN"></a></li>
 </ul>
 </li>
 </ul>
 </div>
</div>
 
<nav class="navbar navbar-default">
 <div class="container">
 <div class="navbar-header">
 <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
 <span class="sr-only">Toggle navigation</span>
 <span class="icon-bar"></span>
 <span class="icon-bar"></span>
 <span class="icon-bar"></span>
 </button>
 <a class="navbar-brand" href="#"><img src="https://www.solodev.com/assets/email/logo.png" alt="Logo Solodev"></a>
 </div>
 <div id="navbar" class="collapse navbar-collapse">
 <ul class="nav navbar-nav">
 <li><a href="#">Home</a></li>
 <li><a href="#about">About</a></li>
 <li><a href="#contact">Contact</a></li>
 <li class="dropdown">
 <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
 <ul class="dropdown-menu">
 <li><a href="#">Action</a></li>
 <li><a href="#">Another action</a></li>
 <li><a href="#">Something else here</a></li>
 <li role="separator" class="divider"></li>
 <li class="dropdown-header">Nav header</li>
 <li><a href="#">Separated link</a></li>
 <li><a href="#">One more separated link</a></li>
 </ul>
 </li>
 </ul>
 </div><!--/.nav-collapse -->
 </div>
</nav>


<div class="container">
 <h1>Use Google Translate with your Website</h1> 
 <p>Select a language from the upper-right dropdown menu and watch as the content of the page automatically translates.</p> 
 <hr />
 <p> lorem100 </p>
</div>

CSS code-

After creating the HTML file, we need to create a CSS file with the extension name “.css”.

.ct-topbar {
  text-align: right;
  background: #eee;
}
.ct-topbar__list {
  margin-bottom: 0px;
}
.ct-language__dropdown{
     padding-top: 8px;
     max-height: 0;
     overflow: hidden;
     position: absolute;
     top: 110%;
     left: -3px;
     -webkit-transition: all 0.25s ease-in-out;
     transition: all 0.25s ease-in-out;
     width: 100px;
     text-align: center;
     padding-top: 0;
  z-index:200;
}
.ct-language__dropdown li{
     background: #222;
     padding: 5px;
}
.ct-language__dropdown li a{
     display: block;
}
.ct-language__dropdown li:first-child{
     padding-top: 10px;
     border-radius: 3px 3px 0 0;
}
.ct-language__dropdown li:last-child{
     padding-bottom: 10px;
     border-radius: 0 0 3px 3px;
}
.ct-language__dropdown li:hover{
     background: #444;
}
.ct-language__dropdown:before{
     content: '';
     position: absolute;
     top: 0;
     left: 0;
     right: 0;
     margin: auto;
     width: 8px;
     height: 0;
     border: 0 solid transparent;
     border-right-width: 8px;
     border-left-width: 8px;
     border-bottom: 8px solid #222;
}
.ct-language{
     position: relative;
  background: #00aced;
  color: #fff;
  padding: 10px 0;
}
.ct-language:hover .ct-language__dropdown{
     max-height: 200px;
     padding-top: 8px;
}
.list-unstyled {
   padding-left: 0;
   list-style: none;
}


.navbar-brand>img {
  padding-top: 11px;
  width: 130px;
  margin-left: 15px;
}
.navbar-brand {
   height: auto;
   margin: 0;
   padding: 0;
   margin-right: 20px;
}
.navbar {
  background-color: #000000;
}
.navbar-default .navbar-nav > .active > a {
  padding: 8px 19px 9px !important;
}
.navbar-nav > li.active {
  padding: 8px 0px 9px 0;
}
.navbar-right {
  padding-top: 0;
}
.navbar-default .navbar-nav > li > a::after {
  background-color: transparent;
  border-bottom: 3px solid #d2282e;
}
.navbar-default .navbar-nav>li {
  display: inline-block;
  text-align: center;
  float: none;
}
.navbar-default .navbar-nav>li>a {
   color: #fff;
}
.navbar-default .navbar-nav>li>a:hover {
   color: #fff;
   background-color: #0392CC;
}

JavaScript code

Then, include the JavaScript file to the webpage.

<script type="text/JavaScript" src="https://ajax.Googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/JavaScript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

Google transcript code-

We need to include a Google transcript.

<script type="text/JavaScript">
 function GoogleTranslateElementInit() {
 new Google.translate.TranslateElement({pageLanguage: 'en', layout: Google.translate.TranslateElement.FloatPosition.TOP_LEFT}, 'Google_translate_element');
 }


 function triggerHtmlEvent(element, eventName) {
 var event;
 if (document.createEvent) {
 event = document.createEvent('HTMLEvents');
 event.initEvent(eventName, true, true);
 element.dispatchEvent(event);
 } else {
 event = document.createEventObject();
 event.eventType = eventName;
 element.fireEvent('on' + event.eventType, event);
 }
 }


 jQuery('.lang-select').click(function() {
 var theLang = jQuery(this).attr('data-lang');
 jQuery('.goog-te-combo').val(theLang);


 window.location = jQuery(this).attr('href');
 location.reload();
 });
</script>
<script type="text/JavaScript" src="//translate.Google.com/translate_a/element.js?cb=GoogleTranslateElementInit"></script>

Output:

How to add Google Translate Button on your Webpage
How to add Google Translate Button on your Webpage

Related Topics

HTML Hide Element

The HTML hide element is used to hide the element. We place the hidden attribute inside those elements which we want to hide. The hidden attribute is a Boolean attribute. The...

3 minutes read.

HTML &lt;blink> Tag

In HTML, the blink tag is used to create the text which flashes. The blink tag is an inline element in HTML. It is observed that the blink tag used...

2 minutes read.

Html &lt;dd> Tag

In an HTML description list, an item's definition or description is indicated using the definition description ("dd" tag). A <dd> tag can contain paragraphs, line breaks, images, links, and lists. It...

4 minutes read.

Html &lt;Col> Tag

You can write the col tag inside the table tag after the thead, tbody, foot, and tr tags. You can also apply it after the caption tag (in which the...

4 minutes read.

HTML Links

It consist of anchor element which is used to create a link between a source anchor and destination anchor. The anchor tag can be embedded between text, image, video or...

2 minutes read.

HTML Font Size

The HTML <font> tag uses the size attribute to specify the size of the text. We can set the size attribute’s value from 1 to 7. The default size of...

4 minutes read.

HTML5 Attribute

HTML Attribute HTML attributes are special words that provide additional information about the HTML element or attributes.The element or tags have attributes, which define the element's behavior.It also adds attributes with a start...

3 minutes read.

What is a Container tag?

In HTML, there are different types of tags or say instructions which make the developer specify various elements in the document and make the browsers understand what to display. Tags in...

5 minutes read.

Caption Tag in HTML

Caption Tag in HTML: The < caption > tag in HTML describes the heading of a table in the HTML text. Browsers typically make the text found above the table...

4 minutes read.

HTML Data Tag

HTML Data Tag: The HTML < data > tag is used to transmit its content to a machine-readable version. The data are presented in a particular format. It is useful...

3 minutes read.

HTML Background Image

Inserting image in HTML An image (technically digital image) in HTML or even in general technical aspects, is a binary representation of visual information. By visual information we mean, in the...

8 minutes read.

HTML <address> Tag

Hyper Text Markup Language, HTML is the best markup language for creating Web pages. HTML consists of a wide variety of elements. In all of these elements, one element is...

4 minutes read.

HTML Headings

The titles and subtitles of our webpages are known as HTML headings. Let’s not talk about webpages but giving heading to almost everything like normal paragraphs, articles or let it...

2 minutes read.

HTML Event Attributes

HTML Event Attributes If a browser responds to the user behavior, an event is called. For example, if we click on the submit button, a box of details will appear on...

4 minutes read.

HTML Audio

HTML AUDIO: HTML5 provides a method to embed the audio file into a webpage by using HTML5. Before HTML5 audio files is used to play on the browser with the help of...

1 minute read.

HTML SVG

HTML SVG: SVG stands for Scalable Vector Graphics which defines the graphics for the web. It is a language for describing 2-D graphics in XML. Each drawn shape in SVG is...

1 minute read.

HTML Date

The input element in HTML has several attributes. One of its attributes is the type attribute which creates a calendar in an order that a user can select a date...

5 minutes read.

HTML <samp> tag

The Samp tag in HTML is used for defining the sample output from the computer program. This tag is a phrase type tag and a container tag that means the...

2 minutes read.

HTML Anchor

HTML Anchor The HTML anchor tags define a hyperlink that links to other pages on one page. It can create hyperlinks to any web page and files, locations or any other...

3 minutes read.

HTML Form Input Types

HTML Form Input Types An important element of the HTML form is < input type= "> in HTML. The type attribute of the input element can be of different types, which...

8 minutes read.