/application/models/LanguageTranslator.php

https://gitlab.com/klausmig/CloudSemanticWeb · PHP · 239 lines · 139 code · 47 blank · 53 comment · 19 complexity · 4e12ce3c78ab5d07285ebdebf96d321c MD5 · raw file

  1. <?php
  2. class LanguageTranslator
  3. {
  4. // this is the API endpoint, as specified by Google
  5. const ENDPOINT = 'https://www.googleapis.com/language/translate/v2';
  6. const ENDPOINTDETECT = 'https://www.googleapis.com/language/translate/v2/detect';
  7. const ENDPOINTLANG = 'https://www.googleapis.com/language/translate/v2/languages';
  8. // holder for you API key, specified when an instance is created
  9. protected $_apiKey;
  10. public $languages;
  11. // constructor, accepts Google API key as its only argument
  12. public function __construct($apiKey)
  13. {
  14. $this->_apiKey = $apiKey;
  15. $this->languages = $this->languages();
  16. }
  17. public function detect($data){
  18. if (strlen($data) >= 100){
  19. $length = 100;
  20. }else{
  21. $length = strlen($data);
  22. }
  23. $small_data = substr($data, 0, $length);
  24. $values = array(
  25. 'key' => $this->_apiKey,
  26. 'q' => $small_data
  27. );
  28. $language = 'en';
  29. // turn the form data array into raw format so it can be used with cURL
  30. $formData = http_build_query($values);
  31. // create a connection to the API endpoint
  32. $ch = curl_init(self::ENDPOINTDETECT);
  33. // tell cURL to return the response rather than outputting it
  34. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  35. //for the problems of SSL
  36. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  37. // write the form data to the request in the post body
  38. curl_setopt($ch, CURLOPT_POSTFIELDS, $formData);
  39. // include the header to make Google treat this post request as a get request
  40. curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET'));
  41. // execute the HTTP request
  42. $json = curl_exec($ch);
  43. curl_close($ch);
  44. // decode the response data
  45. $data = json_decode($json, true);
  46. // ensure the returned data is valid
  47. if (!is_array($data) || !array_key_exists('data', $data)) {
  48. throw new Exception('Unable to find data key');
  49. }
  50. // ensure the returned data is valid
  51. if (!array_key_exists('detections', $data['data'])) {
  52. throw new Exception('Unable to find detections key');
  53. }
  54. if (!is_array($data['data']['detections'])) {
  55. throw new Exception('Expected array for detections');
  56. }
  57. // loop over the translations and return the first one.
  58. // if you wanted to handle multiple translations in a single call
  59. // you would need to modify how this returns data
  60. $conf = $data['data']['detections'][0][0]['confidence'];
  61. foreach ($data['data']['detections'] as $detection) {
  62. if ($detection[0]['confidence'] >= $conf){
  63. $conf = $detection[0]['confidence'];
  64. $language = $detection[0]['language'];
  65. }
  66. }
  67. return $language;
  68. // assume failure since success would've returned just above
  69. throw new Exception('Translation failed');
  70. }
  71. public function getLanguageName($language){
  72. $languages = array();
  73. $values = array(
  74. 'key' => $this->_apiKey,
  75. 'target' => 'en'
  76. );
  77. // turn the form data array into raw format so it can be used with cURL
  78. $formData = http_build_query($values);
  79. // create a connection to the API endpoint
  80. $ch = curl_init(self::ENDPOINTLANG);
  81. // tell cURL to return the response rather than outputting it
  82. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  83. //for the problems of SSL
  84. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  85. // write the form data to the request in the post body
  86. curl_setopt($ch, CURLOPT_POSTFIELDS, $formData);
  87. // include the header to make Google treat this post request as a get request
  88. curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET'));
  89. // execute the HTTP request
  90. $json = curl_exec($ch);
  91. curl_close($ch);
  92. // decode the response data
  93. $data = json_decode($json, true);
  94. // ensure the returned data is valid
  95. if (!is_array($data) || !array_key_exists('data', $data)) {
  96. throw new Exception('Unable to find data key');
  97. }else{
  98. $languages = $data['data']['languages'];
  99. foreach ($languages as $full_lang){
  100. if ($full_lang['language'] == $language){
  101. return $full_lang['name'];
  102. }
  103. }
  104. }
  105. }
  106. public function languages(){
  107. $languages = array();
  108. $values = array(
  109. 'key' => $this->_apiKey
  110. );
  111. // turn the form data array into raw format so it can be used with cURL
  112. $formData = http_build_query($values);
  113. // create a connection to the API endpoint
  114. $ch = curl_init(self::ENDPOINTLANG);
  115. // tell cURL to return the response rather than outputting it
  116. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  117. //for the problems of SSL
  118. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  119. // write the form data to the request in the post body
  120. curl_setopt($ch, CURLOPT_POSTFIELDS, $formData);
  121. // include the header to make Google treat this post request as a get request
  122. curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET'));
  123. // execute the HTTP request
  124. $json = curl_exec($ch);
  125. curl_close($ch);
  126. // decode the response data
  127. $data = json_decode($json, true);
  128. // ensure the returned data is valid
  129. if (!is_array($data) || !array_key_exists('data', $data)) {
  130. throw new Exception('Unable to find data key');
  131. }else{
  132. foreach ($data['data']['languages'] as $language) {
  133. $languages[] = $language['language'];
  134. }
  135. return $languages;
  136. }
  137. }
  138. // translate the text/html in $data. Translates to the language
  139. // in $target. Can optionally specify the source language
  140. public function translate($data, $target, $source = ''){
  141. if (!$source || !in_array($source, $this->languages)){
  142. return -1;
  143. }
  144. // this is the form data to be included with the request
  145. $values = array(
  146. 'key' => $this->_apiKey,
  147. 'target' => $target,
  148. 'q' => $data
  149. );
  150. // only include the source data if it's been specified
  151. if (strlen($source) > 0) {
  152. $values['source'] = $source;
  153. }
  154. // turn the form data array into raw format so it can be used with cURL
  155. $formData = http_build_query($values);
  156. // create a connection to the API endpoint
  157. $ch = curl_init(self::ENDPOINT);
  158. // tell cURL to return the response rather than outputting it
  159. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  160. //for the problems of SSL
  161. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  162. // write the form data to the request in the post body
  163. curl_setopt($ch, CURLOPT_POSTFIELDS, $formData);
  164. // include the header to make Google treat this post request as a get request
  165. curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET'));
  166. // execute the HTTP request
  167. $json = curl_exec($ch);
  168. curl_close($ch);
  169. // decode the response data
  170. $data = json_decode($json, true);
  171. // ensure the returned data is valid
  172. if (!is_array($data) || !array_key_exists('data', $data)) {
  173. throw new Exception('Unable to find data key');
  174. }
  175. // ensure the returned data is valid
  176. if (!array_key_exists('translations', $data['data'])) {
  177. throw new Exception('Unable to find translations key');
  178. }
  179. if (!is_array($data['data']['translations'])) {
  180. throw new Exception('Expected array for translations');
  181. }
  182. // loop over the translations and return the first one.
  183. // if you wanted to handle multiple translations in a single call
  184. // you would need to modify how this returns data
  185. foreach ($data['data']['translations'] as $translation) {
  186. return $translation['translatedText'];
  187. }
  188. // assume failure since success would've returned just above
  189. throw new Exception('Translation failed');
  190. }
  191. }
  192. ?>