+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: JavaScript (or maybe php) help

  1. #1
    biscoolcool's Avatar
    biscoolcool is offline x10Hosting Member biscoolcool is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    53

    Exclamation JavaScript (or maybe php) help

    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/translat...ww.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 by biscoolcool; 08-11-2009 at 10:24 PM. Reason: added more info
    If you found me useful why not add some reputation or donating some credits?(for the Recipient name write biscoolcool):drool:

    Click here to visit my website. What do you think?

  2. #2
    kbjradmin's Avatar
    kbjradmin is offline x10 Elder kbjradmin is an unknown quantity at this point
    Join Date
    Feb 2008
    Location
    Washington State, USA
    Posts
    512

    Re: JavaScript (or maybe php) help

    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 Code:
    $url 'http://translate.google.com/translatehl=en&sl=auto&tl=es&u='.$yourCurrentUrl
    and then use that for the href for your link.

  3. #3
    biscoolcool's Avatar
    biscoolcool is offline x10Hosting Member biscoolcool is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    53

    Re: JavaScript (or maybe php) help

    Quote Originally Posted by kbjradmin View Post
    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 Code:
    $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
    If you found me useful why not add some reputation or donating some credits?(for the Recipient name write biscoolcool):drool:

    Click here to visit my website. What do you think?

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: JavaScript (or maybe php) help

    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?
    Nothing is always absolutely so.

  5. #5
    biscoolcool's Avatar
    biscoolcool is offline x10Hosting Member biscoolcool is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    53

    Re: JavaScript (or maybe php) help

    Quote Originally Posted by descalzo View Post
    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/translat...PRESSEDTHELINK
    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:
    Quote Originally Posted by kbjradmin View Post
    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 Code:
    $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 by biscoolcool; 08-12-2009 at 01:26 PM. Reason: Automerged Doublepost
    If you found me useful why not add some reputation or donating some credits?(for the Recipient name write biscoolcool):drool:

    Click here to visit my website. What do you think?

  6. #6
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: JavaScript (or maybe php) help

    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>
    Nothing is always absolutely so.

  7. #7
    biscoolcool's Avatar
    biscoolcool is offline x10Hosting Member biscoolcool is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    53

    Re: JavaScript (or maybe php) help

    Thanks descalzo. Can some one also tell me the php way?
    If you found me useful why not add some reputation or donating some credits?(for the Recipient name write biscoolcool):drool:

    Click here to visit my website. What do you think?

  8. #8
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: JavaScript (or maybe php) help

    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>
    Last edited by misson; 08-13-2009 at 12:12 AM. Reason: fixed typo
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  9. #9
    biscoolcool's Avatar
    biscoolcool is offline x10Hosting Member biscoolcool is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    53

    Question Re: JavaScript (or maybe php) help

    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?
    If you found me useful why not add some reputation or donating some credits?(for the Recipient name write biscoolcool):drool:

    Click here to visit my website. What do you think?

  10. #10
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: JavaScript (or maybe php) help

    Code:
      echo "<option value="$lid">$language</option>";
    You have a problem with your quotation marks. Inside the outer quotation marks you either have to use single quotes:

    Code:
      echo "<option value='$lid'>$language</option>";
    or you have to backslash escape the double quotes:

    Code:
      echo "<option value=\"$lid\">$language</option>";
    Nothing is always absolutely so.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  2. Replies: 0
    Last Post: 12-24-2008, 03:59 PM
  3. My 2cents on Ruby vs PHP.
    By jwillia in forum Programming Help
    Replies: 0
    Last Post: 03-15-2008, 11:18 PM
  4. Replies: 3
    Last Post: 03-10-2008, 12:22 PM
  5. Using JavaScript ads in my PHP site
    By fattony in forum Scripts & 3rd Party Apps
    Replies: 4
    Last Post: 11-07-2007, 12:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers