Google Translate madness for Drupal - stick it in with a simple template addition

This short article explains how to quickly and easily add Google Translate functionality to Drupal content without hacking any blocks or the actual content itself. You simply stick it into a page template where it belongs.

I guess this should be done up as a module like the Google Analytics module, which helps you configure the addition of the necessary HTML and Javascript code to the pages you want through an admin interface, especially considering you need a Google key for Analytics.

 

But for Google Translate you do not need any keys yet and this is so simple it's hardly worth the CVS.

The amazing and amazingier all the time Google Translate has already wowed the masses just a few days ago, with "translation as you type" functionality. Just go to http://translate.google.com/# and with the Translate from option set to "Detect language", and the Translate to option set to English, just typing "¿Cómo te va?" will have the Google gremlins detecting that the sentence is Spanish and already putting in a colloquial "How you doing" in the correctly labeled Spanish to English results area. And as an extra special bonus, there is a speaker icon where you can actually hear the phrase spoken. Impressive.

Well, if you are at all like me, "how can I get some of this good stuff into my Drupal pages", I hear you saying?

 

On the left hand side menu, click on "tools and resources" option, check out what it says there and grab the following snippet of code:

<div id="google_translate_element"></div><script>
function googleTranslateElementInit() {
  new google.translate.TranslateElement({
    pageLanguage: 'en'
  }, 'google_translate_element');
}
</script><script src="http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

Adding this code to any web page will place a drop down Select language menu for the whole page to be immediately translated into any of a zillion languages. You can try it out on the same page.

Now, where do we put this in Drupal?

Lately I have been upgrading a bunch of Drupal 5 sites to Drupal 6, and one of the worst things I always come across is people sticking php, javascript, and all kinds of non-content code right into custom blocks and even worse, right into the content of pages, making it impossible for the end user to edit any content (which is why they are using Drupal in the first place) without wrecking the pages.

If you need to make smart custom blocks, and it's not to long or convoluted, make a block module. If you need to stick in CSS or Javascript or whatever, use a page template.

This Google Translate snippet is ideal for a page template.

Let's say we only want the Google Translate icon and drop down language selection box to appear on node 2. Go into your current theme and copy page.tpl.php to page-node-2.tpl.php and stick the snippet write before the "print $content" line.

 

You'll need to close the php code and open it up again in order to insert pure HTML. (Of course if you want Google Translate to appear on all pages, just modify page.tpl.php.) Here's the before and after:

 

Before:

      print $content;
    ?>
    </div>

After:

?>
<div id="google_translate_element"></div><script>
function googleTranslateElementInit() {
  new google.translate.TranslateElement({
    pageLanguage: 'en'
  }, 'google_translate_element');
}
</script><script src="http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
<?php
      print $content;
    ?>

OK, enjoy! Let me know how it turns out!

works fine as a panel block

Rather than past this into my page template, which would give me no control over where the Google Translate tool appears, I simply created a block with your code snippet, and activated it to appear in the right sidebar panel. Since this is a block, I could make it appear anywhere a block can, and keep control over who sees it and on what pages. I'm using Drupal 6.

Thanks for a very helpful post.

A solution for websites with more than one source language

Here's a simple method to a) detect the source language (for nodes at least) and b) suppress those nagging Google drop-downs that distort pages in other than default languages.

vacilando

Annoying Google menu on non-English pages

This is indeed easy and cool. It works if both your browser and your web page are in English - Google will translate it to another language of your choice.

But it is a total nightmare if you have an English browser installation and a page in another language.

See e.g. this page in Slovak:
http://vacilando.net/node/264493

In the above script, I am automatically setting "pageLanguage" to 'sk'
(instead of pageLanguage: 'en' I use pageLanguage: 'en' there).

If you have an English browser (or another language than Slovak, I assume), you will see a dropdown menu by Google offering to "Translate this page to" something else.
Which is what I do not want -- indeed, there can be pages in many languages in a website, right?
But how to remove it? I've spent several evenings looking for some CSS magic that would suppress it but I cannot figure it out. Any ideas?

Thanks,

vacilando

The advantage of having a

The advantage of having a module for this like GA is not just for the keys, but because it allows a fairly inexperienced user of Drupal to plug it in quickly without messing around in any of the theme templates. So for that reason, I'd say this would be the perfect candidate for a small module - opening the functionality up to the masses.

On another note, I've just tried the new Google Translate interface. Great! It always amazes me how they are so far ahead of the rest on these things. Good for them. We need to make use of tools like these.

A module for Google Translate will make it easier for people

to use, absolutely. You are right.

But, when depending on a third party as might and as upity as the big G, things tend to change so quickly... I don't think that snippet of code is going to stay very stable.

When they come out with an API (maybe they already have, don't know) then it will be worth making a module.

In any case, Josh Miller has upgraded his module and in the nick of time too, so enjoy http://drupal.org/project/gtrans (mentioned in Josh's comment on this page).

Yes, we certainly need to make use of tools like these.

Thanks for your comment,

Victor

#rule35

http://drupal.org/project/gtrans

And from my twitter stream 1 day ago...

http://twitter.com/xcv/status/6012171342

We must be reading the same news ;)

Glad to see gtrans is available for D6

I actually searched for a Google Translate module, but found one that was only good for Drupal 5.

Glad to see you made the effort to upgrade!

Victor

Google y el control del mundo

Great article! I will make good use of it.
I love Google's new products, but I can't help thinking we are falling into their nets... scary!
¡Un abrazo, amigazo!

Sí, la verdad es que asusta bastante :)

Pero vamos pensando que todo lo que producen los trabajadores tarde o temprano (ojalá más temprano que tarde) tiene que llegar a ser controlado por los mismos trabajadores.

Entonces lo que nos interesa es la búsqueda de la mayor calidad posible, no importa quién lo hizo: porque al final, todo está hecho por los trabajadores...

Igualmente,

Victor