JavaScript (or maybe php) help

biscoolcool

New Member
Messages
53
Reaction score
0
Points
0
Can someone please help me with a JavaScript? For example my page is in English and you want to translate it into Spanish you will click on the Spanish flag.
It will take you to
http://translate.google.com/translatehl=en&sl=auto&tl=es&u=http://www.google.com/

but instead of google.com there should be the url which the visitor was on. Can some write a someone a script for this. Thanks

P.S. I'm not sure but I think you can do this with a php script too. I don't really care which language it is in :drool:. Also can some tell me what this is called?
 
Last edited:

kbjradmin

New Member
Messages
512
Reaction score
2
Points
0
i'm more familiar with php, so that's what i'll use.

first, you need to get the url of the current page. this can be done using the $_SERVER variable.

then, simply use that url to build the url of the google translate page.

PHP:
$url = 'http://translate.google.com/translatehl=en&sl=auto&tl=es&u='.$yourCurrentUrl;

and then use that for the href for your link.
 

biscoolcool

New Member
Messages
53
Reaction score
0
Points
0
i'm more familiar with php, so that's what i'll use.

first, you need to get the url of the current page. this can be done using the $_SERVER variable.

then, simply use that url to build the url of the google translate page.

PHP:
$url = 'http://translate.google.com/translatehl=en&sl=auto&tl=es&u='.$yourCurrentUrl;

and then use that for the href for your link.

As you see I don't know php, so if my code is
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<p>Hello World</p>
<p>&nbsp;</p>
<p>Click Here to translate this page to spanish</p>
</body>
</html>

where will the "$_SERVER variable" go?
If it is not hard can you please give me the whole code that I need?
Thanks
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
What you want is the text on your page to be translated, but you do not want to go to Google, you want to stay on the same page the user was visiting?
 

biscoolcool

New Member
Messages
53
Reaction score
0
Points
0
What you want is the text on your page to be translated, but you do not want to go to Google, you want to stay on the same page the user was visiting?

No the visitor clicks translate and it takes them to

http://translate.google.com/translatehl=en&sl=auto&tl=es&u=THEURLWHERETHEYPRESSEDTHELINK
After the = is the URL from which they pressed translate.I need a script that will automatically put the URL from which the visitor pressed translate after the =.
Edit:
i'm more familiar with php, so that's what i'll use.

first, you need to get the url of the current page. this can be done using the $_SERVER variable.

then, simply use that url to build the url of the google translate page.

PHP:
$url = 'http://translate.google.com/translatehl=en&sl=auto&tl=es&u='.$yourCurrentUrl;

and then use that for the href for your link.

kbjradmin told me what to do but don't know where to put the "$_SERVER variable"
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Javascript:

You can change the page by assingning to location.href

location.href = "http://www.google.com"

You can get the current location by asking location.href

i_am_here = location.href

If you look at the query string for google translation, it encodes everything, including / and :

So you have to encode the href before you can use it.

i_am_here = encodeURIComponent( location.href )

If you think about it, google translate also needs to know the language to and language from.

If you look at the url it generates, the second half has

'&sl=en&tl=es&history_state0='

en => english es => spanish (espanol)

So, you are going to have to insert the language where the 'es' is located.

Putting it all together, the link will look like:

<a href="#to Spanish" onclick='trans( "es" ); return false;' >Spanish</a>

You have to get the "es" etc codes you want.

Then, you add, in the HEAD section:

Code:
<script>

function trans( lang ){

  google = "http://translate.google.com/translate?js=y&prev=_t&hl=en&ie=UTF-8&u=" ;
  google_middle = encodeURIComponent(   location.href ) ; 
  google_tail = '&sl=en&tl=' + lang + '&history_state0=' ;

  location.href = google + google_middle + google_tail ;

}

</script>
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
PHP:
<?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:
<?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>
 
Last edited:

biscoolcool

New Member
Messages
53
Reaction score
0
Points
0
This is the code that I used
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page to translate</title>
<?php 
$languages = array('es' => 'Español', 'de' => 'Deutsch', 'it' => 'Italiano', 'fr' => 'Français'); 

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]"; 
} 
?> 
</head>

<body>
Pick a language that you want this page to be translated to.
<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>
</body>
</html>

I get this error message
Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in /home/biscool/public_html/lang/index.php on line 40

this is my line 40
Code:
    echo "<option value="$lid">$language</option>";

Why did I get this? How do I fix it?
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Code:
  echo [B][COLOR=red]"[/COLOR][/B]<option value=[B][COLOR=red]"[/COLOR][/B]$lid[B][COLOR=red]"[/COLOR]>[/B]$language</option>[B][COLOR=red]"[/COLOR][/B];

You have a problem with your quotation marks. Inside the outer quotation marks you either have to use single quotes:

Code:
  echo [B][COLOR=#ff0000]"[/COLOR][/B]<option value=[B][COLOR=#ff0000]'[/COLOR][/B]$lid[B][COLOR=red]'[/COLOR]>[/B]$language</option>[B][COLOR=red]"[/COLOR][/B];

or you have to backslash escape the double quotes:

Code:
  echo [B][COLOR=red]"[/COLOR][/B]<option value=\[B][COLOR=red]"[/COLOR][/B]$lid\[B][COLOR=red]"[/COLOR]>[/B]$language</option>[B][COLOR=red]"[/COLOR][/B];
 

biscoolcool

New Member
Messages
53
Reaction score
0
Points
0
Thanks mission for your long but good code and thanks descalzo for correcting it.

Here is a sample page using this code:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page to translate</title>
<?php 
$languages = array('es' => 'Español', 'de' => 'Deutsch', 'it' => 'Italiano', 'fr' => 'Français'); 

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]"; 
} 
?> 
</head>

<body>
Pick a language that you want this page to be translated to.
<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>
</body>
</html>
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Code:
  echo [B][COLOR=red]"[/COLOR][/B]<option value=[B][COLOR=red]"[/COLOR][/B]$lid[B][COLOR=red]"[/COLOR]>[/B]$language</option>[B][COLOR=red]"[/COLOR][/B];

You have a problem with your quotation marks. Inside the outer quotation marks you either have to use single quotes:
Teach me not to test my code or take a close look at the colorized version.

@biscoolcool: Note that you can put the translation code in one script (named e.g. "translator.php") and include it on every page you want translation for.
PHP:
<!DOCTYPE ...>
<html>
  ...
  <body>
    ...
    <?php include("$_SERVER[DOCUMENT_ROOT]/translator.php"); ?>
    ...
  </body>
</html>

To make this easier to manage, you can add the directory containing translator.php to the include path. In the following example, translator.php is in DOCUMENT_ROOT/../include/php
init.php (in document root):
PHP:
<?php
set_include_path(get_include_path() . ':' . dirname($_SERVER['DOCUMENT_ROOT']) . '/include/php');
?>
PHP:
<?php include_once("$_SERVER[DOCUMENT_ROOT]/init.php"); ?><!DOCTYPE ...>
<html>
  ...
  <body>
    ...
    <?php include('translator.php'); ?>
    ...
  </body>
</html>
 

biscoolcool

New Member
Messages
53
Reaction score
0
Points
0
Thanks again, the last part was very useful because I have almost no chances of messing up the translator code!!!:lockd:
 
Top