Pomoc - Szukaj - U¿ytkownicy - Kalendarz
Pe³na wersja: Klasa google translate
Forum PHP.pl > Forum > Gotowe rozwi±zania > Algorytmy, klasy, funkcje
Watt
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):

  1. class GoogleTranslate
  2. {
  3. public $langIn = 'pl';
  4. public $langOut = 'en';
  5.  
  6. private $cache = array();
  7. private $cacheDir = 'gt_cache/';
  8.  
  9. //languages available on GoogleTranslate on 30.05.2009
  10. public $outLangs = array(
  11. "pl" => "Polski",
  12. "en" => "English",
  13.  
  14. "ar" => "اÙ?رØ?ب٩Ø?",
  15. "bg" => "Ð?Ñ©Ð?гаÑ?Ñ?ки",
  16. "ca" => "Català ",
  17. "cs" => "Ħeŷtina",
  18. "da" => "Dansk",
  19. "de" => "Deutsch",
  20. "el" => "Î?Î?Î?Î?νακÎ?",
  21. "es" => "EspaÃ?ol",
  22. "et" => "Eesti",
  23. "fi" => "Suomi",
  24. "fr" => "Français",
  25. "gl" => "Galego",
  26. "hi" => "ऱिन࡫��",
  27. "hr" => "Hrvatski",
  28. "hu" => "Magyar",
  29. "id" => "Bahasa Indonesia",
  30. "it" => "Italiano",
  31. "iw" => "×¢×?ר×?ת",
  32. "ja" => "��誾",
  33. "ko" => "���",
  34. "lt" => "Lietuvių",
  35. "lv" => "LatvieÅ·u",
  36. "mt" => "Malti",
  37. "nl" => "Nederlands",
  38. "no" => "Norsk",
  39. "pt" => "Português",
  40. "ro" => "Români",
  41. "ru" => "Ð Ñ?Ñ?Ñ?киб",
  42. "sk" => "Slovenský",
  43. "sl" => "Slovenski",
  44. "sq" => "Shqipe",
  45. "sr" => "зÑ?пÑ?ки",
  46. "sv" => "Svenska",
  47. "th" => "��ย",
  48. "tl" => "Philippine Wika",
  49. "tr" => "Tårkçe",
  50. "uk" => "УкÑ?аÑ?нÑ?Ѧка",
  51. "vi" => "Tiếng Viáquestionmark.gift",
  52. "zh-CN" => "ç±?ä½?å­?", //chinese simplified
  53. "zh-TW" => "ç±?équestionmark.gifå­?" //chinese
  54. );
  55.  
  56. //TODO: translate these
  57. public $inLangs = array(
  58. "sq" => "albaÅ?ski",
  59. "en" => "angielski",
  60. "ar" => "arabski",
  61. "bg" => "buÅ?garski",
  62. "zh-CN" => "chiÅ?ski",
  63. "hr" => "chorwacki",
  64. "cs" => "czeski",
  65. "da" => "duÅ?ski",
  66. "et" => "estoÅ?ski",
  67. "tl" => "filipiÅ?ski",
  68. "fi" => "fiÅ?ski",
  69. "fr" => "francuski",
  70. "gl" => "galicyjski",
  71. "el" => "grecki",
  72. "iw" => "hebrajski",
  73. "hi" => "hindi",
  74. "es" => "hiszpaÅ?ski",
  75. "nl" => "holenderski",
  76. "id" => "indonezyjski",
  77. "ja" => "japoÅ?ski",
  78. "ca" => "kataloÅ?ski",
  79. "ko" => "koreaÅ?ski",
  80. "lt" => "litewski",
  81. "lv" => "Å?otewski",
  82. "mt" => "maltaÅ?ski",
  83. "de" => "niemiecki",
  84. "no" => "norweski",
  85. "pl" => "polski",
  86. "pt" => "portugalski",
  87. "ru" => "rosyjski",
  88. "ro" => "rumuÅ?ski",
  89. "sr" => "serbski",
  90. "sk" => "sÅ?owacki",
  91. "sl" => "sÅ?oweÅ?ski",
  92. "sv" => "szwedzki",
  93. "th" => "tajski",
  94. "tr" => "turecki",
  95. "uk" => "ukraiÅ?ski",
  96. "hu" => "wÄ?gierski",
  97. "vi" => "wietnamski",
  98. "it" => "wÅ?oski"
  99. );
  100.  
  101. public function browserLang()
  102. {
  103. return substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
  104. }
  105.  
  106. public function codeToLang($code, $input = true)
  107. {
  108. if ($input) return $this->inLangs[$code];
  109. else return $this->outLangs[$code];
  110. }
  111.  
  112. public function langToCode($lang, $input = true)
  113. {
  114. if ($input) return array_search($lang, $this->inLangs);
  115. else return array_search($lang, $this->outLangs);
  116. }
  117.  
  118. private function loadCache()
  119. {
  120. chdir(__DIR__);
  121. if (!isset($this->cache[$this->langIn][$this->langOut]))
  122. {
  123. $cacheFile = $this->cacheDir . $this->langIn . '_' . $this->langOut . '.gtc';
  124. if (file_exists($cacheFile))
  125. {
  126. $data = unserialize(file_get_contents($cacheFile));
  127. $this->cache[$this->langIn][$this->langOut] = $data;
  128. }
  129. else
  130. {
  131. $this->cache[$this->langIn][$this->langOut] = array();
  132. }
  133. }
  134. }
  135.  
  136. private function saveCache()
  137. {
  138. chdir(__DIR__);
  139. $data = serialize($this->cache[$this->langIn][$this->langOut]);
  140. $cacheFile = $this->cacheDir . $this->langIn . '_' . $this->langOut . '.gtc';
  141. file_put_contents($cacheFile, $data);
  142. }
  143.  
  144. private function getCached($text)
  145. {
  146. $this->loadCache();
  147.  
  148. if (isset($this->cache[$this->langIn][$this->langOut][$text]))
  149. {
  150. return $this->cache[$this->langIn][$this->langOut][$text];
  151. }
  152. return false;
  153. }
  154.  
  155. public function translate($text)
  156. {
  157. if ($this->langIn == $this->langOut) return $text;
  158.  
  159. if ($res = $this->getCached($text))
  160. {
  161. return $res;
  162. }
  163.  
  164. $url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' .
  165. urlencode($text) .
  166. '&langpair=' . $this->langIn . '%7C' .
  167. $this->langOut;
  168.  
  169. $json_data = file_get_contents($url);
  170.  
  171. $j = json_decode($json_data);
  172.  
  173. if (isset($j->responseStatus) and $j->responseStatus == 200)
  174. {
  175. $t = $j->responseData->translatedText;
  176. $this->cache[$this->langIn][$this->langOut][$text] = $t;
  177. return $t;
  178. }
  179. else return false; // <----- W tym miejscu skrypt pada
  180. }
  181.  
  182. public function __destruct()
  183. {
  184. $this->saveCache();
  185. }
  186. }
  187.  
  188.  
  189. // Simple JSON decoder
  190. // in case json_decode is not available..
  191. if ( !function_exists('json_decode') )
  192. {
  193. function json_decode($json)
  194. {
  195. $comment = false;
  196. $out = '$x=';
  197.  
  198. for ($i=0; $i<strlen($json); $i++)
  199. {
  200. if (!$comment)
  201. {
  202. if ($json[$i] == '{') $out .= ' array(';
  203. else if ($json[$i] == '}') $out .= ')';
  204. else if ($json[$i] == ':') $out .= '=>';
  205. else $out .= $json[$i];
  206. }
  207. else $out .= $json[$i];
  208. if ($json[$i] == '"') $comment = !$comment;
  209. }
  210. eval($out . ';');
  211. return $x;
  212. }
  213. }


Odwo³uje siê:

  1. $t = new GoogleTranslate;
  2.  
  3. //set input and output language:
  4.  
  5. $t->inLang = 'en';
  6. $t->outLang = 'de';
  7.  
  8. //translate
  9. $contents = $t->translate(htmlspecialchars($contents));
bim2
daj przed t± ifk±
  1. echo $j->responseStatus;

i zobacz co wyrzuca.
To jest wersja lo-fi g³ównej zawarto¶ci. Aby zobaczyæ pe³n± wersjê z wiêksz± zawarto¶ci±, obrazkami i formatowaniem proszê kliknij tutaj.
Invision Power Board © 2001-2024 Invision Power Services, Inc.