| | |
About language translotor??
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
I answered a simular question on phpfreaks.com about embedding the google translator into your website and the script I came up with is as follows:
And of course another option is you can also convert the ajax function to a php function like the member Daniel0 on phpfreaks did and is as follows:
Also to help understand how to use the functions above (both scripts are used the same way) is by entering the text to be translated in the first field. The language from in the second field. And the language to in the third field. REMEMBER: Languages inputed into the second and third field must be abbreviated and the abbreviations are as follows:
And to translate the entire page, perhaps you can submit through the translator script every string you have on the page. Hope that helps.
php Syntax (Toggle Plain Text)
<?php function translate($sentence,$languagefrom,$languageto) { $homepage = file_get_contents('http://translate.google.com/translate_t'); if ($homepage==false) {$homepage='';} preg_match_all("/<form[^>]+ction=\"\/translate_t\".*<\/form>/",$homepage,$botmatch); $botmatch[0][0]=preg_replace("/<\/form>.*/",'',$botmatch[0][0]); preg_match_all("/<input[^>]+>/",$botmatch[0][0],$botinput); $id=0; while (isset($botinput[0][$id])) { preg_match_all("/value=(\"|'|[^\"'])[^\"']+(\"|'|[^\"'])?( |>| )/",$botinput[0][$id],$tmp); $tmp[0][0]=preg_replace('/((\'|")?[^\'"]+)/','$1',$tmp[0][0]); $tmp[0][0]=preg_replace('/(\'|")/','',$tmp[0][0]); $tmp[0][0]=preg_replace('/.*value=/i','',$tmp[0][0]); $len=strlen($tmp[0][0]); $len-=1; $tmp[0][0]=substr($tmp[0][0],0,$len); preg_match_all("/name=(\"|'|[^\"'])[^\"']+(\"|'|[^\"'])?( |>| )/",$botinput[0][$id],$tmpname); $tmpname[0][0]=preg_replace('/((\'|")?[^\'"]+)/','$1',$tmpname[0][0]); $tmpname[0][0]=preg_replace('/(\'|")/','',$tmpname[0][0]); $tmpname[0][0]=preg_replace('/.*name=/i','',$tmpname[0][0]); $len=strlen($tmpname[0][0]); $len-=1; $tmpname[0][0]=substr($tmpname[0][0],0,$len); if (strlen($tmpname[0][0])>0 && !in_array($tmpname[0][0],array('text','sl','tl'))) { $vars.=$tmpname[0][0]."=".$tmp[0][0].'&'; } unset($tmp); unset($tmpname); $id+=1; } $curl_handle=curl_init('http://translate.google.com/translate_t'); curl_setopt($curl_handle, CURLOPT_HEADER, true); curl_setopt($curl_handle, CURLOPT_FAILONERROR, true); curl_setopt($curl_handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox curl_setopt($curl_handle, CURLOPT_POST, true); curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $vars.'text='.$sentence.'&sl='.$languagefrom.'&tl='.$languageto); curl_setopt($curl_handle, CURLOPT_NOBODY, false); curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,false); curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); $buffer = curl_exec($curl_handle); curl_close($curl_handle); $buffer=strip_tags($buffer,'<div>'); preg_match_all("/\<div id\=result_box dir\=\"[^\"]+\"\>[^<]+\<\/div\>/",$buffer,$match); $match[0][0]=strip_tags($match[0][0]); return $match[0][0]; } //now to use the translate function echo translate('This is the text','en','iw'); ?>
And of course another option is you can also convert the ajax function to a php function like the member Daniel0 on phpfreaks did and is as follows:
php Syntax (Toggle Plain Text)
<?php function translate($textSource, $langSource, $langTarget) { $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_URL => 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($textSource) . '&langpair=' . urlencode($langSource . '|' . $langTarget), CURLOPT_RETURNTRANSFER => true )); $ret = json_decode(curl_exec($ch), true); curl_close($ch); if ($ret['responseStatus'] != '200') { throw Exception('Translation failed.'); } return $ret['responseData']['translatedText']; } echo translate('this is the text', 'es', 'en'); ?>
Also to help understand how to use the functions above (both scripts are used the same way) is by entering the text to be translated in the first field. The language from in the second field. And the language to in the third field. REMEMBER: Languages inputed into the second and third field must be abbreviated and the abbreviations are as follows:
PHP Syntax (Toggle Plain Text)
ar = Arabic bg = Bulgarian ca = Catalan zh-CN = Chinese hr = Croatian cs = Czech da = Danish nl = Dutch en = English tl = Filipino fi = Finnish fr = French de = German el = Greek iw = Hebrew hi = Hindi id = Indonesian it = Italian ja = Japanese ko = Korean lv = Latvian lt = Lithuanian no = Norwegian pl = Polish pt = Portuguese ro = Romanian ru = Russian sr = Serbian sk = Slovak sl = Slovenian es = Spanish sv = Swedish uk = Ukrainian vi = Vietnamese
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
My favourite PC. - Oopy Doopy Do 2U2!
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*` My favourite PC. - Oopy Doopy Do 2U2!
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 11
hi, cwarn23
( Also to help understand how to use the functions above (both scripts are used the same way) is by entering the text to be translated in the first field. The language from in the second field. And the language to in the third field. REMEMBER: Languages inputed into the second and third field must be abbreviated )
Please post the form related to this??
( Also to help understand how to use the functions above (both scripts are used the same way) is by entering the text to be translated in the first field. The language from in the second field. And the language to in the third field. REMEMBER: Languages inputed into the second and third field must be abbreviated )
Please post the form related to this??
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 11
•
•
•
•
hi, cwarn23
( Also to help understand how to use the functions above (both scripts are used the same way) is by entering the text to be translated in the first field. The language from in the second field. And the language to in the third field. REMEMBER: Languages inputed into the second and third field must be abbreviated )
Please post the form related to this??
•
•
•
•
Originally Posted by Aamit
Please post the form related to this??
php Syntax (Toggle Plain Text)
<?php function translate($textSource, $langSource, $langTarget='') { $textSource=explode('<>',$textSource); if ($langTarget=='') {$langTarget='en';} $idz=0; while (isset($textSource[$idz])) { $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_URL => 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($textSource[$idz]) . '&langpair=' . urlencode($langSource . '|' . $langTarget), CURLOPT_RETURNTRANSFER => true )); $ret = json_decode(curl_exec($ch), true); curl_close($ch); $endresult.=$ret['responseData']['translatedText']; $idz+=2; } return $endresult; } echo "<form method='get' style='padding:0px; margin:0px;'> <select name='language'> <option value='ar'>Arabic <option value='bg'>Bulgarian <option value='ca'>Catalan <option value='zh-CN'>Chinese <option value='hr'>Croatian <option value='cs'>Czech <option value='da'>Danish <option value='nl'>Dutch <option value='en'>English <option value='tl'>Filipino <option value='fr'>French <option value='de'>German <option value='el'>Greek <option value='iw'>Hebrew <option value='hi'>Hindi <option value='id'>Indonesian <option value='it'>Italian <option value='ja'>Japanese <option value='ko'>Korean <option value='lv'>Latvian <option value='lt'>Lithuanian <option value='no'>Norwegian <option value='pl'>Polish <option value='pt'>Portuguese <option value='ro'>Romanian <option value='ru'>Russian <option value='sr'>Serbian <option value='sk'>Slovak <option value='sl'>Slovenian <option value='es'>Spanish <option value='sv'>Swedish <option value='uk'>Ukrainian <option value='vi'>Vietnamese </select><input type='submit' value='submit'></form>"; //now to use the translate function echo translate("<font face='arial'><b>This is the text.</b> You may place all your html in here and it will translate the text and not the code. <br>But note that whenever you use a quote example:\" You will need a backslash before it but that is only for the type of quote which surrounds this entire translation.</font>",'en',$_GET['language']); ?>
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
My favourite PC. - Oopy Doopy Do 2U2!
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*` My favourite PC. - Oopy Doopy Do 2U2!
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 11
hi, cwarn23...
Sorry for late replay
In your post no--24
Please give me example how to place html or php file translate text??
Sorry for late replay
In your post no--24
PHP Syntax (Toggle Plain Text)
echo translate("<font face='arial'><b>This is the text.</b> You may place all your html in here and it will translate the text and not the code. <br>But note that whenever you use a quote example:\" You will need a backslash before it but that is only for the type of quote which surrounds this entire translation.</font>",'en',$_GET['language']);
Please give me example how to place html or php file translate text??
php Syntax (Toggle Plain Text)
<? $file=$_GET['page']; echo translate(file_get_contents($file),'en',$_GET['language']); ?>
So basically, the example above acts like the include() function which will translate while including. That will give you something to fiddle with. Hope it helps solve the puzzle.
Edit: You will need the full fuction code in index.php or whatever page the above example which was provided earlier.
Last edited by cwarn23; Feb 6th, 2009 at 1:53 am. Reason: Important information added
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
My favourite PC. - Oopy Doopy Do 2U2!
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*` My favourite PC. - Oopy Doopy Do 2U2!
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 11
Hi,
my file is at C:\xampp\htdocs\translator\xyz.php
I putting all code ...here
please tell me what is wrong in this code......
i am not getting correct output.
not changing body text also....
my file is at C:\xampp\htdocs\translator\xyz.php
I putting all code ...here
please tell me what is wrong in this code......
i am not getting correct output.
not changing body text also....
PHP Syntax (Toggle Plain Text)
<?php function translate($textSource, $langSource, $langTarget='') { $textSource=explode('<>',$textSource); if ($langTarget=='') {$langTarget='en';} $idz=0; while (isset($textSource[$idz])) { $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_URL => 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($textSource[$idz]) . '&langpair=' . urlencode($langSource . '|' . $langTarget), CURLOPT_RETURNTRANSFER => true )); $ret = json_decode(curl_exec($ch), true); curl_close($ch); $endresult.=$ret['responseData']['translatedText']; $idz+=2; } return $endresult; } echo "<form method='get' style='padding:0px; margin:0px;'> <select name='language'> <option value='ar'>Arabic <option value='bg'>Bulgarian <option value='ca'>Catalan <option value='zh-CN'>Chinese <option value='hr'>Croatian <option value='cs'>Czech <option value='da'>Danish <option value='nl'>Dutch <option value='en'>English <option value='tl'>Filipino <option value='fr'>French <option value='de'>German <option value='el'>Greek <option value='iw'>Hebrew <option value='hi'>Hindi <option value='id'>Indonesian <option value='it'>Italian <option value='ja'>Japanese <option value='ko'>Korean <option value='lv'>Latvian <option value='lt'>Lithuanian <option value='no'>Norwegian <option value='pl'>Polish <option value='pt'>Portuguese <option value='ro'>Romanian <option value='ru'>Russian <option value='sr'>Serbian <option value='sk'>Slovak <option value='sl'>Slovenian <option value='es'>Spanish <option value='sv'>Swedish <option value='uk'>Ukrainian <option value='vi'>Vietnamese </select><input type='submit' value='submit'></form>"; //now to use the translate function /* $url=$_SERVER['PHP_SELF']; $uu=$_SERVER['REQUEST_URI']; $urls .= $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);*/ echo $url = $_SERVER['REQUEST_URI'] ; $urls = preg_split('/\?/', $url); echo print_r($urls); echo $url = $urls[0]; //echo '$_SERVER[PHP_SELF]: ' . $_SERVER['PHP_SELF'] . '<br />'; $url1="C:\\xampp\htdocstranslator/xyz.php"; //echo 'Dirname($_SERVER[PHP_SELF]: ' . dirname($_SERVER['PHP_SELF']) . '<br>'; echo $p_file = dirname(__FILE__)."$url"; /* echo translate(file_get_contents($url1),'en',$_GET['language']); */ echo translate("<font face='arial'><b>This is the text.</b> You may place all your html in here and it will translate the text and not the code. <br>But note that whenever you use a quote example:\" You will need a backslash before it but that is only for the type of quote which surrounds this entire translation.\ </font>",'en',$_GET['language']); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Untitled Document</title> </head> <body> <div align="left" style="color:gray; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10px; text-align:justify" > <span class="bluenormal"> Formally, DFS is an uninformed search that progresses by expanding the first child node of the search tree that appears and thus going deeper and deeper until a goal node is found, or until it hits a node that has no children. Then the search backtracks, returning to the most recent node it hadn't finished exploring. In a non-recursive implementation, all freshly expanded nodes are added to a LIFO stack for exploration. </span><br/><br/> </div> </body> </html>
Last edited by Aamit; Feb 6th, 2009 at 2:21 am.
I had a try at embeding it in myself and descovered a few bugs but have now fixed them. Also I discovered that for the translator, it can take one or two minutes to translate the entire page. The more html the longer to translate. So there are the following two pages and they must have the names I gave them unless you can alter the code to change their names but the only page visible in the url bar is index.php. To set the up, first place in index.php the following code:
Then in the same directory as index.php place in default.php the following code:
Also when creating links, just link to the page like index.php?page=pagename.php
That will link to a page within the same directory or for a page in a subdirectory the url will be index.php?page=directoryname/pagename.php
Note: You can also use html files
Hope that helps.
php Syntax (Toggle Plain Text)
<? set_time_limit(0); function translate($textSource, $langSource, $langTarget='') { if ($langTarget=='') {$langTarget='en';} if ($langSource!==$langTarget) { $textSource=preg_split('/(<|>)/',$textSource); $idz=0; $openbracket=1; while (isset($textSource[$idz])) { $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_URL => 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($textSource[$idz]) . '&langpair=' . urlencode($langSource . '|' . $langTarget), CURLOPT_RETURNTRANSFER => true )); $ret = json_decode(curl_exec($ch), true); curl_close($ch); if ($openbracket==1) { $bracket='<'; $openbracket=0; $endresult.=$ret['responseData']['translatedText'].$bracket; } else { $bracket='>'; $openbracket=1; $endresult.=$textSource[$idz].$bracket; } $idz+=1; } $endresult=preg_replace('/(.*)\</','$1',$endresult); return $endresult; } else { return $textSource; } } if (!isset($_GET['page'])) { $file='default.php'; } else { $file=$_GET['page']; } $data=file_get_contents('http://'.preg_replace('/(.*)\/[^\/]+/','$1/',$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']).$file); echo translate($data,'en',$_GET['language']); ?>
Then in the same directory as index.php place in default.php the following code:
php Syntax (Toggle Plain Text)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Untitled Document</title> </head> <body> <form method='get' style='padding:0px; margin:0px;' action='index.php'> <select name='language'> <option value='ar'>Arabic <option value='bg'>Bulgarian <option value='ca'>Catalan <option value='zh-CN'>Chinese <option value='hr'>Croatian <option value='cs'>Czech <option value='da'>Danish <option value='nl'>Dutch <option value='en'>English <option value='tl'>Filipino <option value='fr'>French <option value='de'>German <option value='el'>Greek <option value='iw'>Hebrew <option value='hi'>Hindi <option value='id'>Indonesian <option value='it'>Italian <option value='ja'>Japanese <option value='ko'>Korean <option value='lv'>Latvian <option value='lt'>Lithuanian <option value='no'>Norwegian <option value='pl'>Polish <option value='pt'>Portuguese <option value='ro'>Romanian <option value='ru'>Russian <option value='sr'>Serbian <option value='sk'>Slovak <option value='sl'>Slovenian <option value='es'>Spanish <option value='sv'>Swedish <option value='uk'>Ukrainian <option value='vi'>Vietnamese </select><input type='submit' value='submit'><input type=hidden value='<? echo basename($_SERVER['REQUEST_URI']); ?>' name='page'></form><br> <div align="left" style="color:gray; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10px; text-align:justify" > <span class="bluenormal"> Formally, DFS is an uninformed search that progresses by expanding the first child node of the search tree that appears and thus going deeper and deeper until a goal node is found, or until it hits a node that has no children. Then the search backtracks, returning to the most recent node it hadn't finished exploring. In a non-recursive implementation, all freshly expanded nodes are added to a LIFO stack for exploration. </span><br/><br/> </div> </body> </html>
Also when creating links, just link to the page like index.php?page=pagename.php
That will link to a page within the same directory or for a page in a subdirectory the url will be index.php?page=directoryname/pagename.php
Note: You can also use html files
Hope that helps.
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
My favourite PC. - Oopy Doopy Do 2U2!
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*` My favourite PC. - Oopy Doopy Do 2U2!
![]() |
Other Threads in the PHP Forum
- Previous Thread: Need help
- Next Thread: ***** User Login problem *****
Views: 4057 | Replies: 33
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access action ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date directory display download dynamic echo email error file files folder form forms function functions google header href htaccess html image include insert integration ip java javascript joomla jquery limit link login loop mail menu methods mlm mod_rewrite multiple mysql neutrality oop parse paypal pdf php problem query radio random record recursion regex remote script search security select server sessions sms soap source space speed sql structure syntax system table tutorial update updates upload url validation validator variable video web xml youtube






