Added the ability to automatically translate simple text.
This commit is contained in:
@@ -3,8 +3,10 @@
|
||||
namespace App\Services\Admin\Project;
|
||||
|
||||
use App\Dto\Service\Admin\Project\Translation\Translations;
|
||||
use App\Dto\Service\Admin\Project\Translation\Translation;
|
||||
use App\Dto\Service\Admin\Project\Translation\Update;
|
||||
use App\Enums\CacheTag;
|
||||
use App\Jobs\Translate\ProcessTranslationText;
|
||||
use App\Models\ProjectTranslation;
|
||||
use App\Models\User;
|
||||
use App\Repositories\ProjectRepository;
|
||||
@@ -60,6 +62,7 @@ final class TranslationService extends Service
|
||||
'language' => $language,
|
||||
'projectTranslations' => $this->projectTranslationRepository->getProjectTranslations($projectId, $languageId)->all()->pluck('text', 'code')->toArray(),
|
||||
'translations' => Translations::getTranslationCodes(),
|
||||
'serviceTranslationEnable' => config('translation_service.enable', false),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -81,6 +84,9 @@ final class TranslationService extends Service
|
||||
$this->translationModelSyncCommand->execute($project, $language, $data->getTranslations());
|
||||
});
|
||||
$this->clearCacheCommandHandler->byTag(CacheTag::ProjectTranslation);
|
||||
if (\config('translation_service.enable', false)) {
|
||||
$this->translateContent($projectId, $languageId, $data);
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
return $this->errService(__('Server Error'));
|
||||
@@ -88,4 +94,19 @@ final class TranslationService extends Service
|
||||
|
||||
return $this->ok(__('Translations successfully updated'));
|
||||
}
|
||||
|
||||
private function translateContent(int $projectId, int $languageId, Update $data): void
|
||||
{
|
||||
if (! $data->isTranslateAutomatically()) {
|
||||
return;
|
||||
}
|
||||
$translateExceptLanguages = [$languageId];
|
||||
$translateTextCode = [];
|
||||
foreach ($data->getTranslations()->getTranslations() as $translation) {
|
||||
/** @var Translation $translation */
|
||||
$translateTextCode[] = $translation->getCode();
|
||||
}
|
||||
|
||||
ProcessTranslationText::dispatch($projectId, $languageId, $translateTextCode, $translateExceptLanguages);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user