Location: PHPKode > scripts > Translate with Altavista Babelfish > translate-with-altavista-babelfish/translate_altavista_babelfish.class.php
<?

	/**
	* Simple string translator using Altavista's Babelfish interface
	*
	* Give it a string in english, chinese, french, german, italian, japanese, korean, portuguese or russian, and obtain it translated to some other language.
	* Attention: This class uses the Babelfish engine by Systran, implemented by Altavista at http://babelfish.altavista.com
	* Please, don't forget to include the "Powered by Systran" somewhere in your site if you want to use this. You can find this logo at http://babelfish.altavista.com.
	*
	* @author		Llorenç Herrera <hide@address.com>
	* @version		1.0
	* @package		translate
	*/

	define("TRANSLATE_BABELFISH_ALTAVISTA_TRANSLATION_ENGLISH_TO_CHINESE",			"en_zh");
	define("TRANSLATE_BABELFISH_ALTAVISTA_TRANSLATION_ENGLISH_TO_FRENCH",			"en_fr");
	define("TRANSLATE_BABELFISH_ALTAVISTA_TRANSLATION_ENGLISH_TO_GERMAN",			"en_de");
	define("TRANSLATE_BABELFISH_ALTAVISTA_TRANSLATION_ENGLISH_TO_ITALIAN",			"en_it");
	define("TRANSLATE_BABELFISH_ALTAVISTA_TRANSLATION_ENGLISH_TO_JAPANESE",			"en_ja");
	define("TRANSLATE_BABELFISH_ALTAVISTA_TRANSLATION_ENGLISH_TO_KOREAN",			"en_ko");
	define("TRANSLATE_BABELFISH_ALTAVISTA_TRANSLATION_ENGLISH_TO_PORTUGUESE",		"en_pt");
	define("TRANSLATE_BABELFISH_ALTAVISTA_TRANSLATION_ENGLISH_TO_SPANISH",			"en_es");
	define("TRANSLATE_BABELFISH_ALTAVISTA_TRANSLATION_CHINESE_TO_ENGLISH",			"zh_en");
	define("TRANSLATE_BABELFISH_ALTAVISTA_TRANSLATION_FRENCH_TO_ENGLISH",			"fr_en");
	define("TRANSLATE_BABELFISH_ALTAVISTA_TRANSLATION_FRENCH_TO_GERMAN",			"fr_de");
	define("TRANSLATE_BABELFISH_ALTAVISTA_TRANSLATION_GERMAN_TO_ENGLISH",			"de_en");
	define("TRANSLATE_BABELFISH_ALTAVISTA_TRANSLATION_GERMAN_TO_FRENCH",			"de_fr");
	define("TRANSLATE_BABELFISH_ALTAVISTA_TRANSLATION_ITALIAN_TO_ENGLISH",			"it_en");
	define("TRANSLATE_BABELFISH_ALTAVISTA_TRANSLATION_JAPANESE_TO_ENGLISH",			"ja_en");
	define("TRANSLATE_BABELFISH_ALTAVISTA_TRANSLATION_KOREAN_TO_ENGLISH",			"ko_en");
	define("TRANSLATE_BABELFISH_ALTAVISTA_TRANSLATION_PORTUGUESE_TO_ENGLISH",		"pt_en");
	define("TRANSLATE_BABELFISH_ALTAVISTA_TRANSLATION_RUSSIAN_TO_ENGLISH",			"ru_en");
	define("TRANSLATE_BABELFISH_ALTAVISTA_TRANSLATION_SPANISH_TO_ENGLISH",			"es_en");
	
	class translate_babelfish_altavista
	{
	
		var $url = "http://babelfish.altavista.com/babelfish/tr";
	
		function translate($string, $languages)
		{
		
			$query = $this->url."?doit=done&tt=urltext&intl=1&urltext=".urlencode($string)."&lp=".$languages;

			$fp = fopen($query, "r");
			
			while(!feof($fp))
				$retr .= fread($fp, 1024);
			
			fclose($fp);
			
			list(, $finallanguage) = explode("_", $languages);
			$tofind_start = "<Div style=padding:10px; lang=".$finallanguage.">";
			$tofind_end = "</div>";
			
			if(!$a = stristr($retr, $tofind_start))
				return false;
			else
			{
				$retr = substr($a, strlen($tofind_start));
				if(!$endpos = strpos($retr, $tofind_end))
					return false;
				else
				{
					$retr = substr($retr, 0, $endpos);
					return $retr;
				}
			}
			
		}
		
	}

?>
Return current item: Translate with Altavista Babelfish