How to use php to jump to show different contents?

Discussion in 'PHP' started by Justin King, May 2, 2008.

  1. #1
    I want to design a site with four languages, say English, Japanese, Frence, and Korean, how to use php show different languages content for different visitors?

    I want to use the same domain.
     
    Justin King, May 2, 2008 IP
  2. vishnups

    vishnups Banned

    Messages:
    166
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi,
    This PHP 5 class API works by querying Google translation tools with the text and language pair chosen.

    
    <?php
    
    class Google_API_translator {
        public $opts = array("text" => "", "language_pair" => "en|it");
        public $out = "";
    
        function __construct() {
            echo "Translator API\n(c) 2008\n";
            echo "programmer: Vishnu < contact@learnerstv.com ><br><br>";
        }
    
        function setOpts($opts) {
            if($opts["text"] != "") $this->opts["text"] = $opts["text"];
            if($opts["language_pair"] != "") $this->opts["language_pair"] = $opts["language_pair"];
        }
    
        function translate() {
            $this->out = "";
            $google_translator_url = "http://translate.google.com/translate_t?langpair=".urlencode($this->opts["language_pair"])."&amp;";
            $google_translator_data .= "text=".urlencode($this->opts["text"]);
            $gphtml = $this->postPage(array("url" => $google_translator_url, "data" => $google_translator_data));
            $out = substr($gphtml, strpos($gphtml, "<div id=result_box dir=\"ltr\">"));
            $out = substr($out, 29);
            $out = substr($out, 0, strpos($out, "</div>"));
            $this->out = utf8_encode($out);
            return $this->out;
        }
    
        // post form data to a given url using curl libs
        function postPage($opts) {
            $html = "";
            if($opts["url"] != "" && $opts["data"] != "") {
                $ch = curl_init($opts["url"]);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_HEADER, 1);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt($ch, CURLOPT_TIMEOUT, 15);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $opts["data"]);
                $html = curl_exec($ch);
                if(curl_errno($ch)) $html = "";
                curl_close ($ch);
            }
            return $html;
        }
    }
    ?>
    
    Code (markup):
    Call it like this
    
    <?php
    
    $g = new Google_API_translator();
    $g->setOpts(array("text" => "ciao", "language_pair" => "it|en"));
    $g->translate();
    echo $g->out;
    ?>
    
    Code (markup):
    Here I am using to translate the text ciao from Italian to English which will output "Hello" .
    View the code above $g->setOpts(array("text" => "ciao", "language_pair" => "it|en"));
    You can modify it to meet your requirements..
     
    vishnups, May 3, 2008 IP
  3. Justin King

    Justin King Banned

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I mean I will design different languages pages, what I need is let different countruy visisiters visit their own languages, others all visit English pages.
     
    Justin King, May 3, 2008 IP