Guest User

Untitled

a guest
Apr 20th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. use GoogleCloudCoreServiceBuilder;
  2. use GoogleCloudLanguageLanguageClient;
  3. use GoogleCloudCoreExceptionBadRequestException;
  4.  
  5. class Google_natural_language
  6. {
  7.  
  8. protected $cloud;
  9. protected $translate;
  10.  
  11. public function __construct()
  12. {
  13. // parent::__construct();
  14.  
  15. // Authenticate using a keyfile path
  16. $this->cloud = new ServiceBuilder([
  17. 'keyFilePath' => APPPATH.'third_party/service_account.json'
  18. ]);
  19.  
  20. }
  21.  
  22. public function translate($text, $target='en')
  23. {
  24. $this->translate = $this->cloud->translate();
  25.  
  26. try {
  27. //
  28. $translation = $this->translate->translate($text, [
  29. 'target' => $target
  30. ]);
  31.  
  32. } catch (BadRequestException $e) {
  33. dblog_message('ERROR', $e);
  34. return false;
  35. }
  36.  
  37. return $translation;
  38. }
  39. }
Add Comment
Please, Sign In to add comment