Google translate, placeholder text in input type='text'
Asked Answered
C

4

7

Problem translating placeholder text in input type="text"

This is my sample code:

Html:

<div id="google_translate_element" style="float:left; padding-left:15px"></div>


<!-- Need to translate this placeholder text -->

<form><input type="text" placeholder= "Enter your name" />
<input type="submit" value="Submit" />
</form>

JavaScipt:

<script type="text/javascript">
function googleTranslateElementInit() {
        new google.translate.TranslateElement({pageLanguage: 'en', includedLanguages: 'ca,da,de,el,en,es,fr,it,ja,ko,nl,pl,pt,ru,sv,tl', layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, 'google_translate_element');
}
</script>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

CSS:

<style>
div#google_translate_element div.goog-te-gadget-simple{background-color:green;}
    div#google_translate_element div.goog-te-gadget-simple a.goog-te-menu-value span{color:yellow}

    div#google_translate_element div.goog-te-gadget-simple a.goog-te-menu-value span:hover{color:#ffffff}
</style>

translater example is in

JSFiddle:

http://jsfiddle.net/ulak/zDUYL/

Please mention any other way to translate placeholder text using google translate

Cower answered 13/12, 2013 at 7:53 Comment(2)
The jsfiddle does not contain any input element. And what do you mean by any other way? Have you found a way?Frivolous
jsfiddle example containe only translate function, i tested with adding <input type="text" placeholder= "Enter your name" /> in it, that is not working for me, my real implementation also use the same wayCower
C
10

As stated in previous answers, Google Translate does not translate placeholders.

I found this javascript solution and it does work.

http://gamon.org/blog/2015/03/12/translate-placeholders-with-google-translate-widget/

JSfiddle demo here:

// Find all placeholders
var placeholders = document.querySelectorAll('input[placeholder]');

if (placeholders.length) {
     // convert to array
    placeholders = Array.prototype.slice.call(placeholders);

    // copy placeholder text to a hidden div
    var div = $('<div id="placeholders" style="display:none;"></div>');

    placeholders.forEach(function(input){
      var text = input.placeholder;
      div.append('<div>' + text + '</div>');    
    });

    $('body').append(div);

    // save the first placeholder in a closure
    var originalPH = placeholders[0].placeholder;

    // check for changes and update as needed
    setInterval(function(){
      if (isTranslated()) {
        updatePlaceholders();
        originalPH = placeholders[0].placeholder;
      }
    }, 500);

    // hoisted ---------------------------
    function isTranslated() { // true if the text has been translated
       var currentPH = $($('#placeholders > div')[0]).text();
       return !(originalPH == currentPH);
    }

    function updatePlaceholders() {
      $('#placeholders > div').each(function(i, div){
        placeholders[i].placeholder = $(div).text();
      });
    }
}
Continuity answered 20/8, 2015 at 12:0 Comment(0)
F
1

As long as Google Translate does not want to translate a placeholder attribute (and they suggest no way to ask for it), the answer is “you can’t”.

As a workaround, you could put the placeholder text into a normal element, say a label element, and then, with JavaScript, after translation copy its content into a placeholder attribute and delete the normal element.

However, it is much better to avoid creating the problem and simply use a label element instead of the placeholder attribute in a situation where you would use the latter in the rôle of a label – against the HTML5 CR which clearly says: “The placeholder attribute should not be used as a replacement for a label.”. So simply use normal markup and have it normally (mis)translated by Google:

<label for=name>Your name:</label> <input type="text" id=name>
Frivolous answered 13/12, 2013 at 8:55 Comment(0)
V
-3
<div id="google_translate_element"></div><script type="text/javascript">
    function googleTranslateElementInit() {
      new google.translate.TranslateElement({
          pageLanguage: 'en'
          , layout: google.translate.TranslateElement.InlineLayout.SIMPLE}
          , 'google_translate_element');
    }
</script>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit">
</script>
Vicar answered 22/4, 2014 at 14:44 Comment(3)
please add some explanatory notes to your post, showing how your code somves the problem and what the OP was missing or getting wrongDesantis
This is just google translate example.. not helped with placeholder text translatingCower
Same code is not working in IE9 what will be the problem.Share your thoughts.Ja
A
-3
<div id="google_translate_element"></div>
<script type="text/javascript">
    function googleTranslateElementInit() {
        new google.translate.TranslateElement({
            pageLanguage: 'en',
            includedLanguages: 'ar', 
            layout: google.translate.TranslateElement.InlineLayout.SIMPLE
        }, 'google_translate_element');
    }
</script>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
Aguilera answered 17/3, 2016 at 2:3 Comment(1)
what is this, really?Ostracism

© 2022 - 2024 — McMap. All rights reserved.