Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent Google Translate from changing the html structure of my page?

Google Translate seems to be changing my html, moving my asterisk to a new line.

Here is a live example: jsbin

How can I avoid this?

Before Translating:

enter image description here

After Translating to Spanish:

enter image description here

JS:

function googleTranslateElementInit() {
  new google.translate.TranslateElement({
    pageLanguage: 'en',
    layout: google.translate.TranslateElement.InlineLayout.SIMPLE,
    autoDisplay: false
  }, 'google_translate_element');
}

Css:

.box_content-form-data {
  clear: both;
  float: left;
}

Html:

<div id="google_translate_element"></div>
<span style="clear:both; float:left;">Enter your email account.</span>
<span class="box_content-form-data">
          <label>Email Address:
              <span style="color:red;">*</span>
</label>
</span>
like image 321
Jack Fairfield Avatar asked Feb 07 '18 18:02

Jack Fairfield


3 Answers

do not use a span to wrap around the elements. A label is not allowed to be used within a span. That's invalid HTML and therefore prone to cause errors.

Literally changing your <span class="box_content-form-data"> to a <div> fixed the issue as far as I can see. See here

-- as per your last comment, what counts for spans also counts for labels. They aren't meant for nesting a lot of elements.

http://jsbin.com/botimiwipi/1/edit?html,output

like image 135
NoobishPro Avatar answered Sep 17 '22 15:09

NoobishPro


Instead of using spans, just use labels. It seems the <span> tags are being treated oddly by google translate.

I'm assuming you tie this into a form which makes more sense to use <label> anyways.

Here is the fiddle: http://jsbin.com/rapusexufu/edit?html,css,output

<label style="clear:both; float:left;">Enter your email account.</label>
<label class="box_content-form-data">Email Address:
    <span style="color:red;">*</span>
</label>
like image 45
Badrush Avatar answered Sep 17 '22 15:09

Badrush


Though I wasn't able to find a way to preclude Google from changing the HTML, here is how I handled it and it seems to work just fine.

All I did was remove the * from the HTML completely, and put it into a pseudoelement, and hide one of the elements created by Google translate.

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
  <title>JS Bin</title>
</head>
<body>
  <div id="google_translate_element"></div>
  <span style="clear:both; float:left;">Enter your email account.</span>
  <span class="box_content-form-data">
      <label class="my-email">Email Address:</label>
  </span>
</body>
</html>

And for the CSS:

.box_content-form-data {
    clear: both;
    float: left;
}

.my-email:after {
  content: "*";
  color: red;
}

font + .box_content-form-data {
  display: none;
}
like image 21
Pytth Avatar answered Sep 18 '22 15:09

Pytth