Location: PHPKode > scripts > GTranslate > gtranslate/GTranslate.php
<?php
/**
 * Google Translator
 * @package WebService :: GTranslate
 * @author ntf
 * @version 1.0
 * @copyright Creative Commons Attribution-Non-Commercial 3.0 Hong Kong http://creativecommons.org/licenses/by-nc/3.0/hk/
 */
class GTranslate{
	protected $lang_from='en';
	protected $lang_to;

	protected $language_list = array( 'ar'=>'Arabic', 'bg'=>'Bulgarian', 'zh-CN'=>'Simplified Chinese', 'zh-TW'=>'Traditional Chinese', 'hr'=>'Croatian', 'cs'=>'Czech', 'da'=>'Danish', 'nl'=>'Dutch', 'en'=>'English', 'fi'=>'Finnish', 'fr'=>'French', 'de'=>'German', 'el'=>'Greek', 'hi'=>'Hindi', 'it'=>'Italian', 'ja'=>'Japanese', 'ko'=>'Korean', 'pl'=>'Polish', 'pt'=>'Portuguese', 'ro'=>'Romanian', 'ru'=>'Russian', 'es'=>'Spanish', 'sv'=>'Swedish');
        
	protected $translateContent;
	protected $translateMode=false;

	function __construct(){

	}

	function Text($text){
		$this->translateMode = '_TranslateMode_Text';
		$this->translateContent = $text;
		return $this;
	}
	
	function Url($url){
		$this->translateMode = '_TranslateMode_Url';
		$this->translateContent = $url;
		return $this;
	}

	function From($language){

		if( $this->_in_language_list($language) != false ){
		 	$this->lang_from = $this->_in_language_list($language);
		}else{
			throw new Exception(__METHOD__ .' Required Language not in the Translation list');
			$this->lang_from = 'auto';
		}

		return $this;
	}

	function To($language){

		if( $this->_in_language_list($language) != false ){
		 	$this->lang_to = $this->_in_language_list($language);		
			return  $this->Translate();		
		}else{
			throw new Exception(__METHOD__ .' Required Language not in the Translation list');
			return false;
		}

	}
	
	function GetLanguageList(){
		return $this->language_list;
	}
	function Translate(){

		if(!method_exists( $this , $this->translateMode) ){
			return false;
		}
		
		$result = call_user_func( array($this , $this->translateMode) );
		
		$this->translateMode=false;
		return $result;
	}
	
	protected function _in_language_list( $language ){

		if( array_key_exists($language , $this->language_list) ){
			return $language;
		}elseif( in_array( $language , $this->language_list ) ){
			return array_search ( $language , $this->language_list );
		}else{
			return false;
		}

	}
	
	protected function _TranslateMode_Text(){
		$json = file_get_contents('http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q='.urlencode($this->translateContent).'&langpair='. $this->lang_from .'%7C'. $this->lang_to );
		$translate_object = json_decode($json);
	
		return $translate_object->responseData->translatedText;
	}

	protected function _TranslateMode_Url(){
   		header('location: http://66.102.9.104/translate_c?hl=en&safe=off&ie=UTF-8&oe=UTF-8&prev=%2Flanguage_tools&u='.urlencode($this->translateContent).'&langpair='. $this->lang_from .'%7C'. $this->lang_to .';');

	}
}


?>
Return current item: GTranslate