PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/www/app/AdminModule/components/Translator/GoogleTranslator.php

https://github.com/bazo/Mokuji
PHP | 333 lines | 223 code | 39 blank | 71 comment | 10 complexity | 9a157557a0a109ba2bbd9f48f3c0b7e2 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. /**
  3. * Google Translator
  4. *
  5. * Copyright (c) 2009, 2010 Roman Novák
  6. *
  7. * This source file is subject to the New-BSD licence.
  8. *
  9. * For more information please see http://nettephp.com
  10. *
  11. * @copyright Copyright (c) 2009, 2010 Roman Novák
  12. * @license New-BSD
  13. * @link http://nettephp.com/cs/extras/googletranslator
  14. * @version 0.1
  15. */
  16. /**
  17. * Google translator service class
  18. *
  19. * @author Roman Novák
  20. * @copyright Copyright (c) 2009, 2010 Roman Novák
  21. * @package nette-google-translator
  22. */
  23. class GoogleTranslator extends Object implements ITranslator
  24. {
  25. /** LANGUAGES */
  26. const AFRIKAANS = 'af';
  27. const ALBANIAN = 'sq';
  28. const AMHARIC = 'am';
  29. const ARABIC = 'ar';
  30. const ARMENIAN = 'hy';
  31. const AZERBAIJANI = 'az';
  32. const BASQUE = 'eu';
  33. const BELARUSIAN = 'be';
  34. const BENGALI = 'bn';
  35. const BIHARI = 'bh';
  36. const BULGARIAN = 'bg';
  37. const BURMESE = 'my';
  38. const CATALAN = 'ca';
  39. const CHEROKEE = 'chr';
  40. const CHINESE = 'zh';
  41. const CHINESE_SIMPLIFIED = 'zh-CN';
  42. const CHINESE_TRADITIONAL = 'zh-TW';
  43. const CROATIAN = 'hr';
  44. const CZECH = 'cs';
  45. const DANISH = 'da';
  46. const DHIVEHI = 'dv';
  47. const DUTCH = 'nl';
  48. const ENGLISH = 'en';
  49. const ESPERANTO = 'eo';
  50. const ESTONIAN = 'et';
  51. const FILIPINO = 'tl';
  52. const FINNISH = 'fi';
  53. const FRENCH = 'fr';
  54. const GALICIAN = 'gl';
  55. const GEORGIAN = 'ka';
  56. const GERMAN = 'de';
  57. const GREEK = 'el';
  58. const GUARANI = 'gn';
  59. const GUJARATI = 'gu';
  60. const HEBREW = 'iw';
  61. const HINDI = 'hi';
  62. const HUNGARIAN = 'hu';
  63. const ICELANDIC = 'is';
  64. const INDONESIAN = 'id';
  65. const INUKTITUT = 'iu';
  66. const ITALIAN = 'it';
  67. const JAPANESE = 'ja';
  68. const KANNADA = 'kn';
  69. const KAZAKH = 'kk';
  70. const KHMER = 'km';
  71. const KOREAN = 'ko';
  72. const KURDISH = 'ku';
  73. const KYRGYZ = 'ky';
  74. const LAOTHIAN = 'lo';
  75. const LATVIAN = 'lv';
  76. const LITHUANIAN = 'lt';
  77. const MACEDONIAN = 'mk';
  78. const MALAY = 'ms';
  79. const MALAYALAM = 'ml';
  80. const MALTESE = 'mt';
  81. const MARATHI = 'mr';
  82. const MONGOLIAN = 'mn';
  83. const NEPALI = 'ne';
  84. const NORWEGIAN = 'no';
  85. const ORIYA = 'or';
  86. const PASHTO = 'ps';
  87. const PERSIAN = 'fa';
  88. const POLISH = 'pl';
  89. const PORTUGUESE = 'pt-PT';
  90. const PUNJABI = 'pa';
  91. const ROMANIAN = 'ro';
  92. const RUSSIAN = 'ru';
  93. const SANSKRIT = 'sa';
  94. const SERBIAN = 'sr';
  95. const SINDHI = 'sd';
  96. const SINHALESE = 'si';
  97. const SLOVAK = 'sk';
  98. const SLOVENIAN = 'sl';
  99. const SPANISH = 'es';
  100. const SWAHILI = 'sw';
  101. const SWEDISH = 'sv';
  102. const TAJIK = 'tg';
  103. const TAMIL = 'ta';
  104. const TAGALOG = 'tl';
  105. const TELUGU = 'te';
  106. const THAI = 'th';
  107. const TIBETAN = 'bo';
  108. const TURKISH = 'tr';
  109. const UKRAINIAN = 'uk';
  110. const URDU = 'ur';
  111. const UZBEK = 'uz';
  112. const UIGHUR = 'ug';
  113. const VIETNAMESE = 'vi';
  114. const UNKNOWN = '';
  115. const AUTODETECT = null;
  116. /** CONTENT TYPES */
  117. const TEXT = 'text';
  118. const HTML = 'html';
  119. public $disableCache = false;
  120. public $fromCache;
  121. /** URL's */
  122. /** @var string translation service url */
  123. protected $translationServiceUrl = 'http://ajax.googleapis.com/ajax/services/language/translate';
  124. /** @var string detect language service url */
  125. protected $detectServiceUrl = 'http://ajax.googleapis.com/ajax/services/language/detect';
  126. /** VARIABLES */
  127. /** @var string locale */
  128. protected $locale = null;
  129. /** @var string from locale */
  130. protected $from = null;
  131. /** @var string google service protocol version */
  132. public $protocolVersion = '1.0';
  133. /** @var string application key */
  134. public $key = '';
  135. /** @var array last service request */
  136. protected $lastRequest = null;
  137. /** @var array last service response */
  138. protected $lastResponse = null;
  139. /** @var Cache cache */
  140. protected $cache = null;
  141. /** METHODS */
  142. /**
  143. * GoogleTranslator contructor
  144. * @param string to locale
  145. * @param string from locale
  146. */
  147. function __construct($locale = null, $from = null)
  148. {
  149. $this->locale = $locale;
  150. $this->from = $from;
  151. }
  152. /**
  153. * Gets translation
  154. * @param string text to translate
  155. * @param string item count (google doesn't support)
  156. * @return string translation
  157. */
  158. function translate($text, $count = null)
  159. {
  160. $cache = $this->getCache();
  161. if(!$this->disableCache && isset($cache[$this->locale]) && isset($cache[$this->locale][$text])) {
  162. $this->fromCache = true;
  163. return $cache[$this->locale][$text];
  164. }
  165. $this->fromCache = false;
  166. $from = $this->from;
  167. if(empty($this->from)) {
  168. $from = $this->detect($text);
  169. }
  170. $query = array(
  171. 'q' => $text,
  172. 'langpair' => $from . '|' . $this->locale,
  173. 'v' => $this->protocolVersion);
  174. if(!empty($this->key)) {
  175. $query['key'] = $this->key;
  176. }
  177. $translation = $this->translateByQuery($query);
  178. if(empty($translation->responseData)) {
  179. throw new InvalidTranslationException($translation->responseDetails, $translation->responseStatus, $this->getLastRequest(), $this->getLastResponse());
  180. }
  181. $translate = $translation->responseData->translatedText;
  182. if(!$this->disableCache) {
  183. $cache->save($this->locale, array($text => $translate));
  184. }
  185. return $translate;
  186. }
  187. /**
  188. * Gets translation by given query array
  189. * @param array request query
  190. * @return array translation service response
  191. */
  192. function translateByQuery($query)
  193. {
  194. $this->lastRequest = $query;
  195. $queryString = http_build_query($query);
  196. $url = $this->translationServiceUrl . '?' . $queryString;
  197. $ch = curl_init();
  198. curl_setopt($ch, CURLOPT_URL, $url);
  199. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  200. //curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_HOST']);
  201. $body = curl_exec($ch);
  202. curl_close($ch);
  203. $json = json_decode($body);
  204. $this->lastResponse = $json;
  205. return $json;
  206. }
  207. /*
  208. * Detect language
  209. * @param string text to detect
  210. * @return string language code
  211. */
  212. function detect($text)
  213. {
  214. $query = array(
  215. 'q' => $text,
  216. 'v' => $this->protocolVersion);
  217. if(!empty($this->key)) {
  218. $query['key'] = $this->key;
  219. }
  220. $language = $this->detectByQuery($query);
  221. if(empty($language->responseData)) {
  222. throw new InvalidTranslationException($language->responseDetails, $language->responseStatus, $this->getLastRequest(), $this->getLastResponse());
  223. }
  224. return $language->responseData->language;
  225. }
  226. /* Detect language by given query array
  227. * @param array request query
  228. * @return array response
  229. */
  230. function detectByQuery($query)
  231. {
  232. $this->lastRequest = $query;
  233. $queryString = http_build_query($query);
  234. $url = $this->detectServiceUrl . '?' . $queryString;
  235. $ch = curl_init();
  236. curl_setopt($ch, CURLOPT_URL, $url);
  237. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  238. curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_HOST']);
  239. $body = curl_exec($ch);
  240. curl_close($ch);
  241. $json = json_decode($body);
  242. $this->lastResponse = $json;
  243. return $json;
  244. }
  245. /** PROPERTIES */
  246. /**
  247. * Gets last service request
  248. * @return array http://code.google.com/apis/ajaxlanguage/documentation/reference.html#_fonje_args
  249. */
  250. function getLastRequest()
  251. {
  252. return $this->lastRequest;
  253. }
  254. /**
  255. * Gets last service response
  256. * @return array http://code.google.com/apis/ajaxlanguage/documentation/reference.html#_fonje_response
  257. */
  258. function getLastResponse()
  259. {
  260. return $this->lastResponse;
  261. }
  262. function getCache()
  263. {
  264. if(null === $this->cache) {
  265. $this->cache = Environment::getCache('google-translator');
  266. }
  267. return $this->cache;
  268. }
  269. function setCache(Cache $cache)
  270. {
  271. $this->cache = $cache;
  272. }
  273. }
  274. /** Google translator alias */
  275. class GT extends GoogleTranslator {}
  276. class InvalidTranslationException extends Exception
  277. {
  278. private $request;
  279. private $response;
  280. function __construct($message = '', $code = 0, $request = null, $response = null)
  281. {
  282. parent::__construct($message, $code);
  283. $this->request = $request;
  284. $this->response = $response;
  285. }
  286. function getRequest()
  287. {
  288. return $this->request;
  289. }
  290. function getResponse()
  291. {
  292. return $this->response;
  293. }
  294. }