Location: PHPKode > scripts > Translate > translate/class_Translate.php
<?PHP
/**
 * @category Google
 * @package Translate
 * @author Peter Schmalfeldt <hide@address.com>
 * @link http://www.manifestinteractive.com
 */

/**
 * Begin Document
 */

class Translate {
	
	/**
     * Returned Array from Google API
     *
     * @var    array
     * @access private
     */
	private $json;
	
	/**
     * utf8 String to Convert prepared with rawurlencode
     *
     * @var    string
     * @access private
     */
	private $translate;
	
	/**
     * Available Language Options (Use Keys)
     *
     * @var    array
     * @access private
     */
	private $languages = array(
		'ar'	=>'arabic',
		'bg'	=>'bulgarian',
		'ca'	=>'catalan',
		'zh'	=>'chinese',
		'zh-CN'	=>'chinese_simplified',
		'zh-TW'	=>'chinese_traditional',
		'hr'	=>'croatian',
		'cs'	=>'czech',
		'da'	=>'danish',
		'nl'	=>'dutch',
		'en'	=>'english',
		'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-PT'	=>'portuguese',
		'ro'	=>'romanian',
		'ru'	=>'russian',
		'sr'	=>'serbian',
		'sk'	=>'slovak',
		'sl'	=>'slovenian',
		'es'	=>'spanish',
		'sv'	=>'swedish',
		'uk'	=>'ukrainian',
		'vi'	=>'vietnamese'
	);
	
	/**
     * Google Translated Text
     *
     * @var    string
     * @access private
     */
	public $translatedText;
	
	/**
     * Google Translate Response Details
     *
     * @var    string
     * @access private
     */
	public $responseDetails;
	
	/**
     * Google Translate Response Status
     *
     * @var    int
     * @access private
     */
	public $responseStatus;
	
	/**
     * Constructor
	 *
	 * Create Google Translate Object and Convert $string
	 * <code>
	 * <?php
	 * $tl = new Translate("Let's translate this from English to Italian.", "en", "it");
	 * echo $tl->translatedText;
	 * ?>
 	 * </code>
     *
     * @param 	string $string utf8 String to Convert
	 * @param 	string $from Supported Key from $languages Array
	 * @param 	string $to Supported Key from $languages Array
     * @access 	public
     */
	
	function __construct($string, $from, $to){
		
		$this->translate = rawurlencode($string);
		if(strlen($this->translate)==0) $this->translatedText = 'Translation String is Empty.';
		else if(!array_key_exists($from, $this->languages) || !array_key_exists($to, $this->languages)) $this->translatedText = 'Unsupported Language Option.';
		
		if(strlen($this->translatedText)==0){
			$ch = curl_init();
			$protocol = ($_SERVER['HTTPS'] == 'on') ? 'https://':'http://';
			$referer = $protocol.$_SERVER['HTTP_HOST'];
			curl_setopt($ch, CURLOPT_URL, "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q={$this->translate}&langpair={$from}%7C{$to}");
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_REFERER, $referer);
			$body = curl_exec($ch);
			curl_close($ch);
			
			$this->json = json_decode($body, true);
			$this->translatedText = $this->json['responseData']['translatedText'];
			$this->responseDetails = $this->json['responseDetails'];
			$this->responseStatus = $this->json['responseStatus'];
		}
	}
}
?>
Return current item: Translate