The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 20, 2019, 11:39:24 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: PHP Translate with Google Translate  (Read 6023 times)
KaptainKrayola
Keeper of Pie
Global Moderator
Lifer
*****
Offline Offline

Posts: 994



View Profile WWW
« on: May 15, 2007, 01:59:43 PM »

Quick, simple, effective.  You need the Snoopy class to make it work.  Enjoy
Code:
include("Snoopy.class.php");
function translate($content,$inlang,$outlang){
        $snoopy = new Snoopy;
        //http://google.com/translate_t?hl=en&ie=UTF8&text=eat+pie&langpair=en%7Cit
$trans_url = "http://google.com/translate_t?hl=en&ie=UTF8&text=".urlencode($content)."&langpair=".$inlang."|".$outlang;
$page = $snoopy->fetch($trans_url);
$page = $snoopy->results;
 
//parse out the data we need
preg_match("/<div\s+id=result_box\s+dir=ltr>(.*?)<\/div>/i",$page,$match);
 
return $match[1];
}
Logged

We can't stop here, this is bat country.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: May 15, 2007, 02:36:14 PM »

Lookatchu bad pirate posting in the PHP repos. I guess Cold Fusion is thawing out, ef?  ROFLMAO

You go, Kapn.
/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
KaptainKrayola
Keeper of Pie
Global Moderator
Lifer
*****
Offline Offline

Posts: 994



View Profile WWW
« Reply #2 on: May 15, 2007, 02:39:32 PM »

Watch out, the Kaptain may be posting some stuff in the C# section also. 

CF is still a decent language but the Kaptain has had little use for it lately.  PHP is nice because 9 times out of 10 you nerds have already done all the hard work for us so we can just butcher your code and away we go.  Hooray code butchery!
Logged

We can't stop here, this is bat country.
m0nkeymafia
Expert
****
Offline Offline

Posts: 240


Check it!


View Profile
« Reply #3 on: May 16, 2007, 01:50:09 AM »

Nice dude nice
Logged

I am Tyler Durden
basura
Rookie
**
Offline Offline

Posts: 13


View Profile
« Reply #4 on: June 14, 2007, 08:36:50 AM »

Quick, simple, effective.  You need the Snoopy class to make it work.  Enjoy
Code:
include("Snoopy.class.php");
function translate($content,$inlang,$outlang){
        $snoopy = new Snoopy;
        //http://google.com/translate_t?hl=en&ie=UTF8&text=eat+pie&langpair=en%7Cit
$trans_url = "http://google.com/translate_t?hl=en&ie=UTF8&text=".urlencode($content)."&langpair=".$inlang."|".$outlang;
$page = $snoopy->fetch($trans_url);
$page = $snoopy->results;
 
//parse out the data we need
preg_match("/<div\s+id=result_box\s+dir=ltr>(.*?)<\/div>/i",$page,$match);
 
return $match[1];
}







Try this PHP class:

http://www.phpclasses.org/browse/package/3898.html

Or maybe this code in PHP5 till Google API allows do that....

<?php
class Google_API_translator {
    public $opts = array("text" => "", "language_pair" => "en|it");
    public $out = "";
 
    function __construct() {}
 
    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://google.com/translate_t?langpair=";
        $google_translator_url .= urlencode($this->opts["language_pair"])."&";
        $google_translator_url .= "text=".urlencode($this->opts["text"]);
        $gphtml = $this->getPage(array("url" => $google_translator_url));
        preg_match('/(.*?)</div>/', $gphtml, $out);
        $this->out = utf8_encode($out[0]);
        return $this->out;
    }
 
    function getPage($opts) {
        $html = "";
        if($opts["url"] != "") {
            $ch = curl_init($opts["url"]);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT, 15);
            $html = curl_exec($ch);
            if(curl_errno($ch)) {
                $html = "";
            }
            curl_close ($ch);
        }
        return $html;
    }
}
?>

Implementation Example:

<?php
$g = new Google_API_translator();
$g->setOpts(array("text" => "ciao", "language_pair" => "it|en"));
$g->translate();
echo $g->out;
?>



A good example it could be some others "and more effectives" translators, like Babefish from Y! and TranEx, Russian in translator.ru...

^_^ It could be interesting if someone have done the code.

Slime
Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #5 on: June 14, 2007, 08:54:58 AM »

oh the perk LIKES. I'll betcha that NBs will as well...  Wink

Gonna try that out today just for giggles. Thanks Bas!

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #6 on: June 14, 2007, 11:24:25 AM »

lol my stuff looks bad enough already, if i ran it through a translator i think it would turn into martianese.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #7 on: June 14, 2007, 11:37:44 AM »

Does G ban for Martianese? I'll bet that's an untapped resource.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #8 on: June 14, 2007, 02:09:22 PM »

no but it already bans for what I do, so making it work probably wouldnt help.  ROFLMAO

though actually the strange thing is that my landing sites are starting to rank, and are actually ranking well for some phrases. stupid fucking google.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #9 on: June 14, 2007, 02:12:24 PM »

hmmm... perhaps it's time to celebrate and you know "travel somewhere?" hehe...
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
basura
Rookie
**
Offline Offline

Posts: 13


View Profile
« Reply #10 on: June 15, 2007, 02:40:50 AM »

Ummh, i think that automatic translators desnt do very well his homeworks...

Table of Good translated words, ENGLISH<->SPANISH

www.worldlingo.com
http://www.worldlingo.com/wl/lgtranslate/UP61481/T1/l/es/traductor.html?wl_lp=EN-ES&wl_fl=0&wl_rurl=http%3A%2F%2Fwww.miweb.com

babelfish.altavista.com
http://babelfish.altavista.com/babelfish/tr

www.online-translator.com

Google.
http://translate.google.com/translate_t


All i have managed, doesnt keep the right words, they doesnt translate properly the words...



####
######What Translator Calls do you use for your auto translationg articles dudeS?
########

Slime
Logged

No links in signatures please
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!