Witam,
Mam pytanie, dlaczego klasa google translate zwarca mi cały czas 'false'? Próbowałem zmienić kod całego forum na angielski.
Klasa (zaznaczyłem miejsce gdzie skrypt pada):
class GoogleTranslate
{
public $langIn = 'pl';
public $langOut = 'en';
private $cache = array(); private $cacheDir = 'gt_cache/';
//languages available on GoogleTranslate on 30.05.2009
public $outLangs = array( "pl" => "Polski",
"en" => "English",
"ar" => "اŮ?ŘąŘ?بيŘ?",
"bg" => "Đ?ŃŠĐ?гаŃ?Ń?ки",
"ca" => "CatalĂ ",
"cs" => "Čeština",
"da" => "Dansk",
"de" => "Deutsch",
"el" => "Î?Î?Î?Î?νικÎ?",
"es" => "EspaĂ?ol",
"et" => "Eesti",
"fi" => "Suomi",
"fr" => "Français",
"gl" => "Galego",
"hi" => "हिन्��",
"hr" => "Hrvatski",
"hu" => "Magyar",
"id" => "Bahasa Indonesia",
"it" => "Italiano",
"iw" => "ע×?ר×?ת",
"ja" => "��語",
"ko" => "í?śę?ě?´",
"lt" => "LietuviĹł",
"lv" => "Latviešu",
"mt" => "Malti",
"nl" => "Nederlands",
"no" => "Norsk",
"pt" => "PortuguĂŞs",
"ro" => "Români",
"ru" => "Đ Ń?Ń?Ń?кий",
"sk" => "SlovenskĂ˝",
"sl" => "Slovenski",
"sq" => "Shqipe",
"sr" => "СŃ?ĐżŃ?ки",
"sv" => "Svenska",
"th" => "��ย",
"tl" => "Philippine Wika",
"tr" => "Türkçe",
"uk" => "ĐŁĐşŃ?аŃ?Đ˝Ń?ька",
"zh-CN" => "çą?ä˝?ĺ?", //chinese simplified
);
//TODO: translate these
"sq" => "albaĹ?ski",
"en" => "angielski",
"ar" => "arabski",
"bg" => "buĹ?garski",
"zh-CN" => "chiĹ?ski",
"hr" => "chorwacki",
"cs" => "czeski",
"da" => "duĹ?ski",
"et" => "estoĹ?ski",
"tl" => "filipiĹ?ski",
"fi" => "fiĹ?ski",
"fr" => "francuski",
"gl" => "galicyjski",
"el" => "grecki",
"iw" => "hebrajski",
"hi" => "hindi",
"es" => "hiszpaĹ?ski",
"nl" => "holenderski",
"id" => "indonezyjski",
"ja" => "japoĹ?ski",
"ca" => "kataloĹ?ski",
"ko" => "koreaĹ?ski",
"lt" => "litewski",
"lv" => "Ĺ?otewski",
"mt" => "maltaĹ?ski",
"de" => "niemiecki",
"no" => "norweski",
"pl" => "polski",
"pt" => "portugalski",
"ru" => "rosyjski",
"ro" => "rumuĹ?ski",
"sr" => "serbski",
"sk" => "sĹ?owacki",
"sl" => "sĹ?oweĹ?ski",
"sv" => "szwedzki",
"th" => "tajski",
"tr" => "turecki",
"uk" => "ukraiĹ?ski",
"hu" => "wÄ?gierski",
"vi" => "wietnamski",
"it" => "wĹ?oski"
);
public function browserLang()
{
return substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0
, 2
); }
public function codeToLang($code, $input = true)
{
if ($input) return $this->inLangs[$code];
else return $this->outLangs[$code];
}
public function langToCode($lang, $input = true)
{
}
private function loadCache()
{
if (!isset($this->cache[$this->langIn][$this->langOut])) {
$cacheFile = $this->cacheDir . $this->langIn . '_' . $this->langOut . '.gtc';
{
$this->cache[$this->langIn][$this->langOut] = $data;
}
else
{
$this->cache[$this->langIn][$this->langOut] = array(); }
}
}
private function saveCache()
{
$data = serialize($this->cache[$this->langIn][$this->langOut]); $cacheFile = $this->cacheDir . $this->langIn . '_' . $this->langOut . '.gtc';
file_put_contents($cacheFile, $data);
}
private function getCached($text)
{
$this->loadCache();
if (isset($this->cache[$this->langIn][$this->langOut][$text])) {
return $this->cache[$this->langIn][$this->langOut][$text];
}
return false;
}
public function translate($text)
{
if ($this->langIn == $this->langOut) return $text;
if ($res = $this->getCached($text))
{
return $res;
}
$url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' .
'&langpair=' . $this->langIn . '%7C' .
$this->langOut;
$j = json_decode($json_data);
if (isset($j->responseStatus) and
$j->responseStatus == 200
) {
$t = $j->responseData->translatedText;
$this->cache[$this->langIn][$this->langOut][$text] = $t;
return $t;
}
else return false; // <----- W tym miejscu skrypt pada
}
public function __destruct()
{
$this->saveCache();
}
}
// Simple JSON decoder
// in case json_decode is not available..
if ( !function_exists('json_decode') )
{
function json_decode($json)
{
$comment = false;
$out = '$x=';
for ($i=0; $i<strlen($json); $i++)
{
if (!$comment)
{
if ($json[$i] == '{') $out .= ' array(';
else if ($json[$i] == '}') $out .= ')';
else if ($json[$i] == ':') $out .= '=>';
else $out .= $json[$i];
}
else $out .= $json[$i];
if ($json[$i] == '"') $comment = !$comment;
}
return $x;
}
}
Odwołuje się:
$t = new GoogleTranslate;
//set input and output language:
$t->inLang = 'en';
$t->outLang = 'de';
//translate
Ten post edytował Watt 1.04.2010, 17:48:37