Weborum Webmaster Forum > Adding Google Translate to your website
Help - Search - Members - Calendar
Full Version: Adding Google Translate to your website
Weborum Webmaster Forum > TUTORIAL ARCHIVE - tutorials & scripts to save you scouring the internet. Please feel free to add your own. > PHP Tutorials & scripts
sjthomas
Taken from http://www.tazr.com/view.php?area=article&...al-151205140116

Lots of sites do it, and theres good reason. It will broaden your audience significantly and provide a more complete experience. Of course I'm talking about making your site multi-lingual. Don't panic though, you don't have to pay a small fortune for translation services, there are services out there that will do it for free! Of course automatic translation services aren't perfect but lets face it, there better than nothing.

For this I'll be using Google translate and a smidgen of PHP. You can still do it without the PHP and I'll point you in the right direction on how to do that.

We should start by getting the basics down, how does the google translation service actually work. Well thats quite simple, you give it a webpage, it translates words and common phrases and then outputs the translated version. They key though, is having it not translate the code of your page, which is something Google handles very well. Once you've put your URL into Googles form a page will load containing the translated version of your page. The good thing about this is that Google uses GET variables, those passed through the URL, to get the page you want to translate. This allows us to take advantage of this to bypass the form altogether and provide a direct link.

Lets have a quick look at the Google translate URL, this is one for translating this page on XHTML Compliant Meta Tags:

http://www.google.com/translate? u=http%3A%2F%2Fwww.tazr.com%2Fview.php%3Farea%3Darticle%26sectionTag%3DXHTML-Tutorials%26articleName%3DXHTML-compliant-meta-tags-101205140306 &langpair=en%7Ces&hl=en&ie=UTF8

I've split the URL into three sections for ease of reading (and so it doesn't break the layout!). The first part is simply the page on Google's server that does the translating. The second part, the "u" variable is the URL that you want to translate and the third part are some options. The important bit is the "langpair=en%7Ces" part. That determines the language you want to translate to. This is telling google that the page is currently in English (en) and we want it in Spanish (es). These two letter language codes are used pretty much everywhere and should be fairly recognisable. They are seperated with a pipe ( | ) which is url encoded to %7C here.

So how can we use this on our sites. Well this is a simplified version of the URL above:

http://www.google.com/translate?u=*web address*&langpair=*language*%7C*desired language*&hl=*language*&ie=UTF8

The bits surrounded in asterisks (*) are what we need to change. *language* should be the language of your site, *desired language* is the language you want to translate to and *web address* is the full URL of the page you want to translate.

Below is the simple version of a single link to translate the current page into spanish. You could easily iterate this to add the other languages (put them in an array) but I'll leave that for you to figure out. I'll give you the code and then walk you through it:


CODE
<?php
$_SERVER['FULL_URL'] = 'http';
    if($_SERVER['HTTPS']=='on'){$_SERVER['FULL_URL'] .=  's';}
    $_SERVER['FULL_URL'] .=  '://';
    if($_SERVER['SERVER_PORT']!='80') $_SERVER['FULL_URL'] .=  $_SERVER['HTTP_HOST'].':'.$_SERVER['SERVER_PORT'].    $_SERVER['SCRIPT_NAME'];
    else
    $_SERVER['FULL_URL'] .=  $_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
    if($_SERVER['QUERY_STRING']>' '){$_SERVER['FULL_URL'] .=  '?'.$_SERVER['QUERY_STRING'];}
?>



CODE
<!--  Put this where you want to link -->
<a href="http://www.google.com/translate?u=<?php echo (urlencode($_SERVER['FULL_URL'])); ?>&langpair=en%7Cde&hl=en&ie=UTF8" target="_blank">
Translate to Spanish
</a>



The first bit of PHP code simply gets the current URL including any variables. We then just echo this out in the relevant part of the link, while using url encode to make sure it works. It really is that easy.

Why not check out the other languages available and get some flag graphics to use, if you do you'll end up with something like the translation area on this very page (up there on the right). If you don't want to use PHP, then have a look into Javascript to get the current URL, of course you can hard code it as well.
Waleed
Good one, Si. It's already been pinned! biggrin.gif
hahaman
great,i will use,very useful.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.