Skip to content

Commit

Permalink
Merge pull request #15 from ben182/develop
Browse files Browse the repository at this point in the history
Throttle Google API Calls & Command Output Improvements
  • Loading branch information
ben182 authored Apr 13, 2019
2 parents 4e6963d + ecb4c17 commit 479a335
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/AutoTranslate.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function fillLanguageFiles(string $language, array $data)
public function array_undot(array $dottedArray, array $initialArray = []) : array
{
foreach ($dottedArray as $key => $value) {
array_set($initialArray, $key, $value);
Arr::set($initialArray, $key, $value);
}

return $initialArray;
Expand Down
9 changes: 9 additions & 0 deletions src/Commands/AllCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,23 @@ public function handle()
{
$targetLanguages = Arr::wrap(config('auto-translate.target_language'));

$this->line('Found '.count($targetLanguages).' languages to translate');

$bar = $this->output->createProgressBar(count($targetLanguages));
$bar->start();

foreach ($targetLanguages as $targetLanguage) {
$sourceTranslations = $this->autoTranslator->getSourceTranslations();

$translated = $this->autoTranslator->translate($targetLanguage, $sourceTranslations);

$this->autoTranslator->fillLanguageFiles($targetLanguage, $translated);

$bar->advance();
}

$bar->finish();

$this->info('Translated '.count(Arr::dot($sourceTranslations)).' language keys.');
}
}
9 changes: 9 additions & 0 deletions src/Commands/MissingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public function handle()
{
$targetLanguages = Arr::wrap(config('auto-translate.target_language'));

$this->line('Found '.count($targetLanguages).' languages to translate');

$bar = $this->output->createProgressBar(count($targetLanguages));
$bar->start();

$missingCount = 0;

foreach ($targetLanguages as $targetLanguage) {
Expand All @@ -53,8 +58,12 @@ public function handle()
$translated = $this->autoTranslator->translate($targetLanguage, $missing);

$this->autoTranslator->fillLanguageFiles($targetLanguage, $translated);

$bar->advance();
}

$bar->finish();

$this->info('Translated '.$missingCount.' missing language keys.');
}
}
13 changes: 13 additions & 0 deletions src/Exceptions/LanguageCodeNotExist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Ben182\AutoTranslate\Exceptions;

use Exception;

class LanguageCodeNotExist extends Exception
{
public static function throw(string $source, string $target): self
{
return new static('The language code "'.$source.'" or "'.$target.'" does not exist.');
}
}
17 changes: 16 additions & 1 deletion src/Translators/SimpleGoogleTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace Ben182\AutoTranslate\Translators;

use Stichoza\GoogleTranslate\GoogleTranslate;
use Ben182\AutoTranslate\Exceptions\LanguageCodeNotExist;

class SimpleGoogleTranslator implements TranslatorInterface
{
protected $translator;
protected $source;
protected $target;

public function __construct()
{
Expand All @@ -15,20 +18,32 @@ public function __construct()

public function setSource(string $source)
{
$this->source = $source;

$this->translator->setSource($source);

return $this;
}

public function setTarget(string $target)
{
$this->target = $target;

$this->translator->setTarget($target);

return $this;
}

public function translate(string $string) : string
{
return $this->translator->translate($string);
try {
sleep(random_int(1, 3));

return $this->translator->translate($string);
} catch (\Throwable $th) {
if ($th->getMessage() === 'Return value of Stichoza\GoogleTranslate\GoogleTranslate::translate() must be of the type string, null returned') {
throw LanguageCodeNotExist::throw($this->source, $this->target);
}
}
}
}
59 changes: 0 additions & 59 deletions tests/CommandTest.php

This file was deleted.

0 comments on commit 479a335

Please sign in to comment.