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.

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:


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;
    ?>
    

After:


?>

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