Skip to content

Commit

Permalink
Merge branch 'main' into feat/add-opt-api-params-and-check-json
Browse files Browse the repository at this point in the history
  • Loading branch information
jannescb authored Jan 20, 2022
2 parents 2c06603 + 6e2f0d7 commit 7b5057f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Commands/DeeplableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AwStudio\Deeplable\Commands;

use AwStudio\Deeplable\Facades\Translator;
use AwStudio\Deeplable\Jobs\TranslateModelJob;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -46,6 +47,7 @@ protected function getOptions()
{
return [
['force', null, InputOption::VALUE_NONE, 'Whether existing records are to be overwritten', null],
['queue', null, InputOption::VALUE_NONE, 'Whether the job should be queued', null],
];
}

Expand Down Expand Up @@ -88,15 +90,20 @@ public function getLocales()
public function translateCollection(Collection $models, $locales, $fallbackLocale): void
{
$force = $this->hasOption('force') && $this->option('force');
$shouldQueue = $this->hasOption('queue') && $this->option('queue');

foreach ($models as $model) {
$translator = Translator::for($model);

foreach ($locales as $locale) {
try {
$translator->translate($model, $locale, $fallbackLocale, $force);
} catch (GuzzleException $e) {
$this->error('Failed to translate '.get_class($model));
if ($shouldQueue) {
TranslateModelJob::dispatch($model, $locale, $fallbackLocale, $force);
} else {
try {
$translator->translate($model, $locale, $fallbackLocale, $force);
} catch (GuzzleException $e) {
$this->error('Failed to translate '.get_class($model));
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Deepl.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Deepl
* @param string $apiToken
* @param string $apiUrl
* @param string $fallbackLocale
* @return void
*/
public function __construct(
protected string $apiToken,
Expand Down
45 changes: 45 additions & 0 deletions src/Jobs/TranslateModelJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace AwStudio\Deeplable\Jobs;

use AwStudio\Deeplable\Facades\Translator;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class TranslateModelJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct(
protected $model,
protected $locale,
protected $fallbackLocale,
protected $force
) {
//
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$translator = Translator::for($this->model);
try {
$translator->translate($this->model, $this->locale, $this->fallbackLocale, $this->force);
} catch (GuzzleException $e) {
$this->error('Failed to translate '.get_class($this->model));
}
}
}

0 comments on commit 7b5057f

Please sign in to comment.