Skip to content

Commit

Permalink
translate without api key trait added (#2)
Browse files Browse the repository at this point in the history
Translate without API key using stichoza/google-translate-php
  • Loading branch information
tanmuhittin authored Mar 29, 2019
1 parent d0e9196 commit 3eca04a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"illuminate/console": ">=5.1",
"php": ">=7.0.0",
"illuminate/support": "5.5.x|5.6.x|5.7.x|5.8.x",
"illuminate/translation": "5.5.x|5.6.x|5.7.x|5.8.x"
"illuminate/translation": "5.5.x|5.6.x|5.7.x|5.8.x",
"stichoza/google-translate-php": "dev-master"
},
"extra": {
"laravel": {
Expand Down
30 changes: 29 additions & 1 deletion src/commands/TranslateFilesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace Tanmuhittin\LaravelGoogleTranslate\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Finder\Finder; //require
use Stichoza\GoogleTranslate\GoogleTranslate;
use Symfony\Component\Finder\Finder;

class TranslateFilesCommand extends Command
{
Expand Down Expand Up @@ -89,6 +90,33 @@ public function handle()
*/
private function translate($text, $locale)
{
if(config('laravel_google_translate.google_translate_api_key')){
return self::translate_via_api_key($text, $locale);
}else{
return self::translate_via_stichoza($text, $locale);
}
}

/**
* @param $text
* @param $locale
* @return null|string
* @throws \ErrorException
*/
private function translate_via_stichoza($text,$locale){
$tr = new GoogleTranslate();
$tr->setSource($this->base_locale);
$tr->setTarget($locale);
return $tr->translate($text);
}

/**
* @param $text
* @param $locale
* @return mixed
* @throws \Exception
*/
private function translate_via_api_key($text, $locale){
$apiKey = config('laravel_google_translate.google_translate_api_key');
$url = 'https://www.googleapis.com/language/translate/v2?key=' . $apiKey . '&q=' . rawurlencode($text) . '&source=' . $this->base_locale . '&target=' . $locale;
$handle = curl_init();
Expand Down

0 comments on commit 3eca04a

Please sign in to comment.