Source for file googleTranslate.class.php

Documentation is available at googleTranslate.class.php

  1. <?php
  2.  
  3. /**
  4.  * GoogleTranslateWrapper: PHP wrapper for Google Translation services
  5.  * Copyright (C) 2010  Sameer Borate
  6.  * 
  7.  * GoogleTranslateWrapper is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version.
  11.  * 
  12.  * GoogleTranslateWrapper is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with GoogleTranslateWrapper; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  *
  21.  * @category    GoogleTranslateWrapper
  22.  * @package     GoogleTranslateWrapper
  23.  * @author      Sameer Borate
  24.  * @copyright   2010 Sameer Borate
  25.  */
  26.  
  27.  
  28. /**
  29.  * GoogleTranslateWrapper Main Class
  30.  * 
  31.  * @category    GoogleTranslateWrapper
  32.  * @package     GoogleTranslateWrapper
  33.  * @author      Sameer Borate
  34.  * @link        http://www.codediesel.com
  35.  * @copyright   2010 Sameer Borate
  36.  * @version     1.5
  37.  */
  38.  
  39. {
  40.     /**
  41.      * URL of Google translate
  42.      * @var string 
  43.      */
  44.     private $_googleTranslateUrl = 'http://ajax.googleapis.com/ajax/services/language/translate';
  45.     
  46.     /**
  47.      * URL of Google language detection
  48.      * @var string 
  49.      */
  50.     private $_googleDetectUrl = 'http://ajax.googleapis.com/ajax/services/language/detect';
  51.     
  52.     /**
  53.      * Language to translate from
  54.      * @var string 
  55.      */
  56.     private $_fromLang = '';
  57.     
  58.     /**
  59.      * Language to translate to
  60.      * @var string 
  61.      */
  62.     private $_toLang = '';
  63.     
  64.     /**
  65.      * API version
  66.      * @var string 
  67.      */
  68.     private $_version = '1.0';
  69.     
  70.     /**
  71.      * Text to translate
  72.      * @var string 
  73.      */
  74.     private $_text = '';
  75.     
  76.     /**
  77.      * Site url using the code
  78.      * @var string 
  79.      */
  80.     private $_siteUrl = '';
  81.     
  82.     /**
  83.      * Google API key
  84.      * @var string 
  85.      */
  86.     private $_apiKey = '';
  87.     
  88.     /**
  89.      * Host IP address
  90.      * @var string 
  91.      */
  92.     private $_ip = '';
  93.     
  94.     /**
  95.      * POST fields
  96.      * @var string 
  97.      */
  98.     private $_postFields;
  99.     
  100.     /**
  101.      * Translated Text
  102.      * @var string 
  103.      */
  104.     private $_translatedText;
  105.     
  106.     /**
  107.      * Service Error
  108.      * @var string 
  109.      */
  110.     private $_serviceError = "";
  111.     
  112.     /**
  113.      * Detected source language
  114.      * @var string 
  115.      */
  116.     private $_detectedSourceLanguage = "";
  117.     
  118.     const DETECT = 1;
  119.     const TRANSLATE = 2;
  120.  
  121.     
  122.     /**
  123.      * Build a POST url to query Google
  124.      *
  125.      */
  126.     private function _composeUrl($type
  127.     {
  128.         if($type == self::TRANSLATE)
  129.         {
  130.             $fields = array('v'         => $this->_version,
  131.                             'q'         => $this->_text,
  132.                             'langpair'  => $this->_fromLang . "|" $this->_toLang);
  133.         }
  134.         elseif($type == self::DETECT)
  135.         {
  136.             $fields = array('v'         => $this->_version,
  137.                         'q'         => $this->_text);
  138.         }
  139.         
  140.         if($this->_apiKey != ""$fields['key'$this->_apiKey;
  141.         if($this->_ip != ""$fields['userip'$this->_ip;
  142.  
  143.         $this->_postFields = http_build_query($fields''"&");
  144.     }
  145.  
  146.     
  147.     /**
  148.      * Process the built query using cURL and POST
  149.      *
  150.      * @param string POST fields
  151.      * @return string response
  152.      */
  153.     private function _remoteQuery($query)
  154.     {
  155.         if(!function_exists('curl_init'))
  156.         {
  157.             return "";
  158.         }
  159.         
  160.         /* Setup CURL and its options*/
  161.         $ch curl_init();
  162.         curl_setopt($chCURLOPT_URL,$this->_googleTranslateUrl);
  163.         curl_setopt($chCURLOPT_REFERER$this->_siteUrl);
  164.         curl_setopt($chCURLOPT_RETURNTRANSFER1);
  165.         curl_setopt($chCURLOPT_TIMEOUT15);
  166.         curl_setopt($chCURLOPT_POST1);
  167.         curl_setopt($chCURLOPT_POSTFIELDS$query);
  168.  
  169.         $response curl_exec($ch)
  170.  
  171.         return $response;
  172.     }
  173.     
  174.     
  175.     /**
  176.      * Process the built query using cURL and GET
  177.      *
  178.      * @param string GET fields
  179.      * @return string response
  180.      */
  181.     private function _remoteQueryDetect($query)
  182.     {
  183.         if(!function_exists('curl_init'))
  184.         {
  185.             return "";
  186.         }
  187.         
  188.         $ch curl_init();
  189.         $url $this->_googleDetectUrl . "?" $query;
  190.         curl_setopt($chCURLOPT_URL$url);
  191.         curl_setopt($chCURLOPT_RETURNTRANSFER1);
  192.         curl_setopt($chCURLOPT_REFERER$this->_siteUrl);
  193.  
  194.         $response curl_exec($ch)
  195.         return $response;
  196.     }
  197.     
  198.     
  199.     /**
  200.      * Self test the class
  201.      *
  202.      * @return boolean 
  203.      */
  204.     public function selfTest()
  205.     {
  206.         if(!function_exists('curl_init'))
  207.         {
  208.             echo "cURL not installed.";
  209.         }
  210.         else
  211.         {
  212.             $testText $this->translate("hello""fr""en");
  213.             echo ($testText == "bonjour""Test Ok." "Test Failed.";
  214.         }
  215.     }
  216.     
  217.     
  218.     /**
  219.      * Get the last generated service error
  220.      *
  221.      * @return String 
  222.      */
  223.     public function getLastError()
  224.     {
  225.         return $this->_serviceError;
  226.     }
  227.     
  228.     
  229.     /**
  230.      * Get the detected source language, if the source is not provided
  231.      * during query
  232.      *
  233.      * @return String 
  234.      */
  235.     public function getDetectedSource()
  236.     {
  237.         return $this->_detectedSourceLanguage;
  238.     }
  239.     
  240.     
  241.     /**
  242.      * Set credentials (optional) when accessing Google translation services
  243.      *
  244.      * @param string $apiKey your google api key
  245.      */
  246.     public function setCredentials($apiKey$ip)
  247.     {
  248.         $this->_apiKey = $apiKey;
  249.         $this->_ip = $ip;
  250.     }
  251.     
  252.     
  253.     
  254.     /**
  255.      * Translate the given text
  256.      * @param string $text text to translate
  257.      * @param string $to language to translate to
  258.      * @param string $from optional language to translate from
  259.      * @return boolean | string
  260.      */
  261.     public function translate($text ''$to$from '')
  262.     {
  263.     
  264.         if($text == '' || $to == '')
  265.         {
  266.             return false;
  267.         }
  268.         else
  269.         {
  270.             $this->_text = $text;
  271.             $this->_toLang = $to;
  272.             $this->_fromLang = $from;
  273.         }
  274.         
  275.         $this->_composeUrl(self::TRANSLATE);
  276.         
  277.         if($this->_text != '' && $this->_postFields != '')
  278.         {
  279.         
  280.             $contents $this->_remoteQuery($this->_postFields);
  281.             $json json_decode($contentstrue);
  282.             
  283.             if($json['responseStatus'== 200)
  284.             {   
  285.                 $this->_translatedText = $json['responseData']['translatedText'];
  286.                 if(isset($json['responseData']['detectedSourceLanguage']))
  287.                 {
  288.                     $this->_detectedSourceLanguage = $json['responseData']['detectedSourceLanguage'];   
  289.                 }
  290.                 
  291.                 return $this->_translatedText;
  292.             }
  293.             else
  294.             
  295.                 $this->_serviceError =     $json['responseDetails'];
  296.                 return false;
  297.             }
  298.         }
  299.         else
  300.         {
  301.             return false;
  302.         }
  303.     }
  304.     
  305.     /**
  306.      * Detect the language of the given text
  307.      * @param string $text text language to detect
  308.      * @return boolean | string
  309.      */
  310.     public function detectLanguage($text)
  311.     {
  312.     
  313.         if($text == '')
  314.         {
  315.             return false;
  316.         }
  317.         else
  318.         {
  319.             $this->_text = $text;
  320.         }
  321.         
  322.         
  323.         $this->_composeUrl(self::DETECT);
  324.         
  325.         if($this->_text != '' && $this->_postFields != '')
  326.         {
  327.         
  328.             $contents $this->_remoteQueryDetect($this->_postFields);
  329.             $json json_decode($contentstrue);
  330.             
  331.             if($json['responseStatus'== 200)
  332.             {   
  333.                 return $json['responseData'];
  334.             }
  335.             else
  336.             
  337.                 $this->_serviceError =     $json['responseDetails'];
  338.                 return false;
  339.             }
  340.         }
  341.         else
  342.         {
  343.             return false;
  344.         }
  345.  
  346.     }
  347.     
  348. }
  349.  
  350.  
  351. ?>

Documentation generated on Mon, 22 Mar 2010 12:42:33 +0000 by phpDocumentor 1.4.1