PHP Code:
<?php
$languages = array('es' => 'spanish', 'de' => 'german', 'it' => 'italian', 'fr' => 'french');
if (! isset($_SERVER['SCRIPT_URI']) ) {
switch ($_SERVER['SERVER_PORT']) {
case 80:
$scheme='http';
$port='';
break;
case 443:
$scheme='https';
$port='';
break;
default:
$scheme='http';
$port=':' . $_SERVER['SERVER_PORT'];
break;
}
$_SERVER['SCRIPT_URI'] = "$scheme://$_SERVER[SERVER_NAME]$port$_SERVER[REQUEST_URI]";
}
function translater($lang) {
return "http://translate.google.com/translate?hl=$lang&js=y&sl=en&tl=$lang&history_state0=&u=" . urlencode($_SERVER['SCRIPT_URI']);
}
?>
...
<?php
foreach ($languages as $lid => $language) {
echo "<a href='", echo translater($lid); "'><img src='/images/flags/$language' alt='translate to $language' /></a>";
}
?>
Add additional languages to the $languages array in the same format as the others: 'language id' => 'language name', where language id must be one of the two letter codes used by Google Translate (which are probably ISO 639-1 language codes).
Edit: If you have a large number of languages, it might be better to use a select:
PHP Code:
<?php
$languages = array('es' => 'Spanish', 'de' => 'German', 'it' => 'Italian', 'fr' => 'French');
if (! isset($_SERVER['SCRIPT_URI']) ) {
switch ($_SERVER['SERVER_PORT']) {
case 80:
$scheme='http';
$port='';
break;
case 443:
$scheme='https';
$port='';
break;
default:
$scheme='http';
$port=':' . $_SERVER['SERVER_PORT'];
break;
}
$_SERVER['SCRIPT_URI'] = "$scheme://$_SERVER[SERVER_NAME]$port$_SERVER[REQUEST_URI]";
}
?>
...
<form action="http://translate.google.com/translate" method="GET">
<input type="hidden" name="u" value="<?php echo $_SERVER['SCRIPT_URI']; ?>" />
<input type="hidden" name="hl" value="en" />
<input type="hidden" name="js" value="y" />
<input type="hidden" name="history_state0" value="" />
<input type="hidden" name="sl" value="en" />
<select name="tl">
<?php
foreach ($languages as $lid => $language) {
echo "<option value='$lid'>$language</option>";
}
?>
</select>
<input type="submit" value="Translate" />
</form>