943,532 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 5524
  • PHP RSS
You are currently viewing page 3 of this multi-page discussion thread; Jump to the first page
Jan 21st, 2009
0

Re: About language translotor??

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:
php Syntax (Toggle Plain Text)
  1. <?php
  2. function translate($sentence,$languagefrom,$languageto)
  3. {
  4. $homepage = file_get_contents('http://translate.google.com/translate_t');
  5. if ($homepage==false) {$homepage='';}
  6. preg_match_all("/<form[^>]+ction=\"\/translate_t\".*<\/form>/",$homepage,$botmatch);
  7. $botmatch[0][0]=preg_replace("/<\/form>.*/",'',$botmatch[0][0]);
  8. preg_match_all("/<input[^>]+>/",$botmatch[0][0],$botinput);
  9. $id=0;
  10. while (isset($botinput[0][$id]))
  11. {
  12. preg_match_all("/value=(\"|'|[^\"'])[^\"']+(\"|'|[^\"'])?( |>| )/",$botinput[0][$id],$tmp);
  13. $tmp[0][0]=preg_replace('/((\'|")?[^\'"]+)/','$1',$tmp[0][0]);
  14. $tmp[0][0]=preg_replace('/(\'|")/','',$tmp[0][0]);
  15. $tmp[0][0]=preg_replace('/.*value=/i','',$tmp[0][0]);
  16. $len=strlen($tmp[0][0]);
  17. $len-=1;
  18. $tmp[0][0]=substr($tmp[0][0],0,$len);
  19.  
  20. preg_match_all("/name=(\"|'|[^\"'])[^\"']+(\"|'|[^\"'])?( |>| )/",$botinput[0][$id],$tmpname);
  21. $tmpname[0][0]=preg_replace('/((\'|")?[^\'"]+)/','$1',$tmpname[0][0]);
  22. $tmpname[0][0]=preg_replace('/(\'|")/','',$tmpname[0][0]);
  23. $tmpname[0][0]=preg_replace('/.*name=/i','',$tmpname[0][0]);
  24. $len=strlen($tmpname[0][0]);
  25. $len-=1;
  26. $tmpname[0][0]=substr($tmpname[0][0],0,$len);
  27.  
  28. if (strlen($tmpname[0][0])>0 && !in_array($tmpname[0][0],array('text','sl','tl')))
  29. {
  30. $vars.=$tmpname[0][0]."=".$tmp[0][0].'&';
  31. }
  32. unset($tmp);
  33. unset($tmpname);
  34. $id+=1;
  35. }
  36. $curl_handle=curl_init('http://translate.google.com/translate_t');
  37. curl_setopt($curl_handle, CURLOPT_HEADER, true);
  38. curl_setopt($curl_handle, CURLOPT_FAILONERROR, true);
  39. 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
  40. curl_setopt($curl_handle, CURLOPT_POST, true);
  41. curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $vars.'text='.$sentence.'&sl='.$languagefrom.'&tl='.$languageto);
  42. curl_setopt($curl_handle, CURLOPT_NOBODY, false);
  43. curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,false);
  44. curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
  45. $buffer = curl_exec($curl_handle);
  46. curl_close($curl_handle);
  47.  
  48. $buffer=strip_tags($buffer,'<div>');
  49. preg_match_all("/\<div id\=result_box dir\=\"[^\"]+\"\>[^<]+\<\/div\>/",$buffer,$match);
  50. $match[0][0]=strip_tags($match[0][0]);
  51. return $match[0][0];
  52. }
  53.  
  54.  
  55. //now to use the translate function
  56. echo translate('This is the text','en','iw');
  57. ?>

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)
  1. <?php
  2. function translate($textSource, $langSource, $langTarget)
  3. {
  4. $ch = curl_init();
  5. curl_setopt_array($ch, array(
  6. CURLOPT_URL => 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($textSource) . '&langpair=' . urlencode($langSource . '|' . $langTarget),
  7. CURLOPT_RETURNTRANSFER => true
  8. ));
  9.  
  10. $ret = json_decode(curl_exec($ch), true);
  11. curl_close($ch);
  12.  
  13. if ($ret['responseStatus'] != '200') {
  14. throw Exception('Translation failed.');
  15. }
  16.  
  17. return $ret['responseData']['translatedText'];
  18. }
  19.  
  20. echo translate('this is the text', 'es', 'en');
  21. ?>

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)
  1. ar = Arabic
  2. bg = Bulgarian
  3. ca = Catalan
  4. zh-CN = Chinese
  5. hr = Croatian
  6. cs = Czech
  7. da = Danish
  8. nl = Dutch
  9. en = English
  10. tl = Filipino
  11. fi = Finnish
  12. fr = French
  13. de = German
  14. el = Greek
  15. iw = Hebrew
  16. hi = Hindi
  17. id = Indonesian
  18. it = Italian
  19. ja = Japanese
  20. ko = Korean
  21. lv = Latvian
  22. lt = Lithuanian
  23. no = Norwegian
  24. pl = Polish
  25. pt = Portuguese
  26. ro = Romanian
  27. ru = Russian
  28. sr = Serbian
  29. sk = Slovak
  30. sl = Slovenian
  31. es = Spanish
  32. sv = Swedish
  33. uk = Ukrainian
  34. vi = Vietnamese
And to translate the entire page, perhaps you can submit through the translator script every string you have on the page. Hope that helps.
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Jan 22nd, 2009
0

Re: About language translotor??

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??
Reputation Points: 3
Solved Threads: 15
Posting Whiz
Aamit is offline Offline
341 posts
since Apr 2008
Jan 23rd, 2009
0

Re: About language translotor??

Click to Expand / Collapse  Quote originally posted by Aamit ...
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??
Please post the form related to this??
Reputation Points: 3
Solved Threads: 15
Posting Whiz
Aamit is offline Offline
341 posts
since Apr 2008
Jan 23rd, 2009
0

Re: About language translotor??

Quote originally posted by Aamit ...
Please post the form related to this??
I shall post a page with the translator fully embeded and you can select from the menu what language you want. However this requires a $_GET variable in the url bar when not viewing english pages. The only other alternative to not having the $_GET variable in the url bar is to use sessions. Anyway below is a fully setup example that you can use.
php Syntax (Toggle Plain Text)
  1. <?php
  2. function translate($textSource, $langSource, $langTarget='')
  3. {
  4. $textSource=explode('<>',$textSource);
  5. if ($langTarget=='') {$langTarget='en';}
  6. $idz=0;
  7. while (isset($textSource[$idz]))
  8. {
  9. $ch = curl_init();
  10. curl_setopt_array($ch, array(
  11. CURLOPT_URL => 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($textSource[$idz]) . '&langpair=' . urlencode($langSource . '|' . $langTarget),
  12. CURLOPT_RETURNTRANSFER => true
  13. ));
  14.  
  15. $ret = json_decode(curl_exec($ch), true);
  16. curl_close($ch);
  17.  
  18. $endresult.=$ret['responseData']['translatedText'];
  19. $idz+=2;
  20. }
  21. return $endresult;
  22. }
  23.  
  24. echo "<form method='get' style='padding:0px; margin:0px;'>
  25. <select name='language'>
  26. <option value='ar'>Arabic
  27. <option value='bg'>Bulgarian
  28. <option value='ca'>Catalan
  29. <option value='zh-CN'>Chinese
  30. <option value='hr'>Croatian
  31. <option value='cs'>Czech
  32. <option value='da'>Danish
  33. <option value='nl'>Dutch
  34. <option value='en'>English
  35. <option value='tl'>Filipino
  36. <option value='fr'>French
  37. <option value='de'>German
  38. <option value='el'>Greek
  39. <option value='iw'>Hebrew
  40. <option value='hi'>Hindi
  41. <option value='id'>Indonesian
  42. <option value='it'>Italian
  43. <option value='ja'>Japanese
  44. <option value='ko'>Korean
  45. <option value='lv'>Latvian
  46. <option value='lt'>Lithuanian
  47. <option value='no'>Norwegian
  48. <option value='pl'>Polish
  49. <option value='pt'>Portuguese
  50. <option value='ro'>Romanian
  51. <option value='ru'>Russian
  52. <option value='sr'>Serbian
  53. <option value='sk'>Slovak
  54. <option value='sl'>Slovenian
  55. <option value='es'>Spanish
  56. <option value='sv'>Swedish
  57. <option value='uk'>Ukrainian
  58. <option value='vi'>Vietnamese
  59. </select><input type='submit' value='submit'></form>";
  60. //now to use the translate function
  61. echo translate("<font face='arial'><b>This is the text.</b>
  62. You may place all your html in here and it will translate the text and not the code.
  63. <br>But note that whenever you use a quote example:\" You will need a backslash before it but
  64. that is only for the type of quote which surrounds this entire translation.</font>",'en',$_GET['language']);
  65.  
  66. ?>
Just let me know if you would like more changed.
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Jan 24th, 2009
0

Re: About language translotor??

Reputation Points: 18
Solved Threads: 6
Junior Poster in Training
lordspace is offline Offline
90 posts
since May 2006
Feb 5th, 2009
0

Re: About language translotor??

hi, cwarn23...

Sorry for late replay

In your post no--24
PHP Syntax (Toggle Plain Text)
  1.  
  2. echo translate("<font face='arial'><b>This is the text.</b>
  3.  
  4. You may place all your html in here and it will translate the text and not the code.
  5.  
  6. <br>But note that whenever you use a quote example:\" You will need a backslash before it but
  7.  
  8. 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??
Reputation Points: 3
Solved Threads: 15
Posting Whiz
Aamit is offline Offline
341 posts
since Apr 2008
Feb 6th, 2009
0

Re: About language translotor??

php Syntax (Toggle Plain Text)
  1. <?
  2. $file=$_GET['page'];
  3. echo translate(file_get_contents($file),'en',$_GET['language']);
  4. ?>
To make life easier, I have made an example of another way to use the function so that you can include an external php (or html) file to include the contents instead of having to worry about escaping the quotes. So basically, you have one or more page(s) that contains just that function and nothing else. Then when linking to another page, just link to index.php?page=newpage.php to load the the page named newpage.php in the same directory. And that may seem ugly so you can then use a htaccess file the rewrite the url to something more pritty like /newpage/.

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
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Feb 6th, 2009
0

Re: About language translotor??

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....

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. function translate($textSource, $langSource, $langTarget='')
  4. {
  5. $textSource=explode('<>',$textSource);
  6. if ($langTarget=='') {$langTarget='en';}
  7. $idz=0;
  8. while (isset($textSource[$idz]))
  9. {
  10.  
  11. $ch = curl_init();
  12. curl_setopt_array($ch, array(
  13. CURLOPT_URL => 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($textSource[$idz]) . '&langpair=' . urlencode($langSource . '|' . $langTarget),
  14. CURLOPT_RETURNTRANSFER => true
  15. ));
  16. $ret = json_decode(curl_exec($ch), true);
  17. curl_close($ch);
  18.  
  19. $endresult.=$ret['responseData']['translatedText'];
  20. $idz+=2;
  21. }
  22. return $endresult;
  23. }
  24.  
  25. echo "<form method='get' style='padding:0px; margin:0px;'>
  26. <select name='language'>
  27. <option value='ar'>Arabic
  28. <option value='bg'>Bulgarian
  29. <option value='ca'>Catalan
  30. <option value='zh-CN'>Chinese
  31. <option value='hr'>Croatian
  32. <option value='cs'>Czech
  33. <option value='da'>Danish
  34. <option value='nl'>Dutch
  35. <option value='en'>English
  36. <option value='tl'>Filipino
  37. <option value='fr'>French
  38. <option value='de'>German
  39. <option value='el'>Greek
  40. <option value='iw'>Hebrew
  41. <option value='hi'>Hindi
  42. <option value='id'>Indonesian
  43. <option value='it'>Italian
  44. <option value='ja'>Japanese
  45. <option value='ko'>Korean
  46. <option value='lv'>Latvian
  47. <option value='lt'>Lithuanian
  48. <option value='no'>Norwegian
  49. <option value='pl'>Polish
  50. <option value='pt'>Portuguese
  51. <option value='ro'>Romanian
  52. <option value='ru'>Russian
  53. <option value='sr'>Serbian
  54. <option value='sk'>Slovak
  55. <option value='sl'>Slovenian
  56. <option value='es'>Spanish
  57. <option value='sv'>Swedish
  58. <option value='uk'>Ukrainian
  59. <option value='vi'>Vietnamese
  60. </select><input type='submit' value='submit'></form>";
  61. //now to use the translate function
  62. /* $url=$_SERVER['PHP_SELF'];
  63. $uu=$_SERVER['REQUEST_URI'];
  64.  $urls .= $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);*/
  65.  
  66. echo $url = $_SERVER['REQUEST_URI'] ;
  67.  
  68. $urls = preg_split('/\?/', $url);
  69. echo print_r($urls);
  70. echo $url = $urls[0];
  71. //echo '$_SERVER[PHP_SELF]: ' . $_SERVER['PHP_SELF'] . '<br />';
  72.  
  73. $url1="C:\\xampp\htdocstranslator/xyz.php";
  74.  
  75. //echo 'Dirname($_SERVER[PHP_SELF]: ' . dirname($_SERVER['PHP_SELF']) . '<br>';
  76. echo $p_file = dirname(__FILE__)."$url";
  77. /* echo translate(file_get_contents($url1),'en',$_GET['language']); */
  78.  
  79. echo translate("<font face='arial'><b>This is the text.</b>
  80. You may place all your html in here and it will translate the text and not the code.
  81. <br>But note that whenever you use a quote example:\" You will need a backslash before it but
  82. that is only for the type of quote which surrounds this entire translation.\
  83. </font>",'en',$_GET['language']);
  84. ?>
  85.  
  86. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  87. <html>
  88. <head>
  89. <title>Untitled Document</title>
  90.  
  91. </head>
  92.  
  93. <body>
  94. <div align="left" style="color:gray; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10px; text-align:justify" >
  95.  
  96. <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/>
  97.  
  98.  
  99. </div>
  100. </body>
  101. </html>
  102.  
Last edited by Aamit; Feb 6th, 2009 at 2:21 am.
Reputation Points: 3
Solved Threads: 15
Posting Whiz
Aamit is offline Offline
341 posts
since Apr 2008
Feb 6th, 2009
-2

Re: About language translotor??

hi,
I posted code in post in-28
please tell me what is wrong in this code......
i am not getting correct output.
not changing body text also....
Reputation Points: 3
Solved Threads: 15
Posting Whiz
Aamit is offline Offline
341 posts
since Apr 2008
Feb 6th, 2009
0

Re: About language translotor??

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:
php Syntax (Toggle Plain Text)
  1. <?
  2. set_time_limit(0);
  3. function translate($textSource, $langSource, $langTarget='')
  4. {
  5. if ($langTarget=='') {$langTarget='en';}
  6. if ($langSource!==$langTarget)
  7. {
  8. $textSource=preg_split('/(<|>)/',$textSource);
  9. $idz=0;
  10. $openbracket=1;
  11. while (isset($textSource[$idz]))
  12. {
  13. $ch = curl_init();
  14. curl_setopt_array($ch, array(
  15. CURLOPT_URL => 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($textSource[$idz]) . '&langpair=' . urlencode($langSource . '|' . $langTarget),
  16. CURLOPT_RETURNTRANSFER => true
  17. ));
  18. $ret = json_decode(curl_exec($ch), true);
  19. curl_close($ch);
  20. if ($openbracket==1)
  21. {
  22. $bracket='<';
  23. $openbracket=0;
  24. $endresult.=$ret['responseData']['translatedText'].$bracket;
  25. } else {
  26. $bracket='>';
  27. $openbracket=1;
  28. $endresult.=$textSource[$idz].$bracket;
  29. }
  30. $idz+=1;
  31. }
  32. $endresult=preg_replace('/(.*)\</','$1',$endresult);
  33. return $endresult;
  34. } else {
  35. return $textSource;
  36. }
  37. }
  38. if (!isset($_GET['page']))
  39. {
  40. $file='default.php';
  41. } else {
  42. $file=$_GET['page'];
  43. }
  44. $data=file_get_contents('http://'.preg_replace('/(.*)\/[^\/]+/','$1/',$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']).$file);
  45. echo translate($data,'en',$_GET['language']);
  46.  
  47. ?>

Then in the same directory as index.php place in default.php the following code:
php Syntax (Toggle Plain Text)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <title>Untitled Document</title>
  5.  
  6. </head>
  7.  
  8. <body>
  9. <form method='get' style='padding:0px; margin:0px;' action='index.php'>
  10. <select name='language'>
  11. <option value='ar'>Arabic
  12. <option value='bg'>Bulgarian
  13. <option value='ca'>Catalan
  14. <option value='zh-CN'>Chinese
  15. <option value='hr'>Croatian
  16. <option value='cs'>Czech
  17. <option value='da'>Danish
  18. <option value='nl'>Dutch
  19. <option value='en'>English
  20. <option value='tl'>Filipino
  21. <option value='fr'>French
  22. <option value='de'>German
  23. <option value='el'>Greek
  24. <option value='iw'>Hebrew
  25. <option value='hi'>Hindi
  26. <option value='id'>Indonesian
  27. <option value='it'>Italian
  28. <option value='ja'>Japanese
  29. <option value='ko'>Korean
  30. <option value='lv'>Latvian
  31. <option value='lt'>Lithuanian
  32. <option value='no'>Norwegian
  33. <option value='pl'>Polish
  34. <option value='pt'>Portuguese
  35. <option value='ro'>Romanian
  36. <option value='ru'>Russian
  37. <option value='sr'>Serbian
  38. <option value='sk'>Slovak
  39. <option value='sl'>Slovenian
  40. <option value='es'>Spanish
  41. <option value='sv'>Swedish
  42. <option value='uk'>Ukrainian
  43. <option value='vi'>Vietnamese
  44. </select><input type='submit' value='submit'><input type=hidden value='<? echo basename($_SERVER['REQUEST_URI']); ?>' name='page'></form><br>
  45. <div align="left" style="color:gray; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10px; text-align:justify" >
  46.  
  47. <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/>
  48.  
  49.  
  50. </div>
  51. </body>
  52. </html>
  53.  

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.
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Need help
Next Thread in PHP Forum Timeline: ***** User Login problem *****





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC