Skip to content

Commit

Permalink
Add force option (#6)
Browse files Browse the repository at this point in the history
* wip

* Apply fixes from StyleCI
  • Loading branch information
cbl authored Sep 1, 2021
1 parent 89cdbcc commit 2ad73d5
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 20 deletions.
34 changes: 31 additions & 3 deletions src/Commands/DeeplableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
use AwStudio\Deeplable\Facades\Translator;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class DeeplableCommand extends Command
{
/**
* The name and signature of the console command.
* The console command name.
*
* @var string
*/
protected $signature = 'deeplable:run {locale?}';
protected $name = 'deeplable:run';

/**
* The console command description.
Expand All @@ -22,6 +24,30 @@ class DeeplableCommand extends Command
*/
protected $description = 'Generate missing translations via DeepL.';

/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [
['locale', InputArgument::OPTIONAL, 'The locale of the language to translate to'],
];
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['force', null, InputOption::VALUE_NONE, 'Whether existing records are to be overwritten', null],
];
}

/**
* Execute the console command.
*
Expand Down Expand Up @@ -60,11 +86,13 @@ public function getLocales()
*/
public function translateCollection(Collection $models, $locales, $fallbackLocale): void
{
$force = $this->hasOption('force') && $this->option('force');

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

foreach ($locales as $locale) {
$translator->translate($model, $locale, $fallbackLocale);
$translator->translate($model, $locale, $fallbackLocale, $force);
}

$model->save();
Expand Down
6 changes: 4 additions & 2 deletions src/Contracts/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ interface Translator
* @param array $attributes
* @param string $targetLang
* @param string|null $sourceLanguage
* @param bool $force
* @return void
*/
public function translate(Model $model, string $targetLang, string | null $sourceLanguage = null);
public function translate(Model $model, string $targetLang, string | null $sourceLanguage = null, bool $force = true);

/**
* Translate a list of attributes.
Expand All @@ -24,7 +25,8 @@ public function translate(Model $model, string $targetLang, string | null $sourc
* @param array $attributes
* @param string $targetLang
* @param string|null $sourceLanguage
* @param bool $force
* @return void
*/
public function translateAttributes(Model $model, array $attributes, string $targetLang, string | null $sourceLanguage = null);
public function translateAttributes(Model $model, array $attributes, string $targetLang, string | null $sourceLanguage = null, bool $force = true);
}
13 changes: 8 additions & 5 deletions src/Traits/Deeplable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ trait Deeplable
*
* @param string $targetLang
* @param string|null $sourceLanguage
* @param bool $force
* @return void
*/
public function translateTo(string $targetLang, string | null $sourceLanguage = null)
public function translateTo(string $targetLang, string | null $sourceLanguage = null, bool $force = true)
{
Translator::for($this)->translate($this, $targetLang, $sourceLanguage);
Translator::for($this)->translate($this, $targetLang, $sourceLanguage, $force);

$this->save();
}
Expand All @@ -26,11 +27,12 @@ public function translateTo(string $targetLang, string | null $sourceLanguage =
* @param string $attr
* @param string $targetLang
* @param string|null $sourceLanguage
* @param bool $force
* @return void
*/
public function translateAttributeTo(string $attr, string $targetLang, string | null $sourceLanguage = null)
public function translateAttributeTo(string $attr, string $targetLang, string | null $sourceLanguage = null, bool $force = true)
{
$this->translateAttributesTo([$attr], $targetLang, $sourceLanguage);
$this->translateAttributesTo([$attr], $targetLang, $sourceLanguage, $force);
}

/**
Expand All @@ -39,9 +41,10 @@ public function translateAttributeTo(string $attr, string $targetLang, string |
* @param string $attr
* @param string $targetLang
* @param string|null $sourceLanguage
* @param bool $force
* @return void
*/
public function translateAttributesTo(array $attributes, string $targetLang, string | null $sourceLanguage = null)
public function translateAttributesTo(array $attributes, string $targetLang, string | null $sourceLanguage = null, bool $force = true)
{
Translator::for($this)->translateAttributes($this, $attributes, $targetLang, $sourceLanguage);

Expand Down
17 changes: 13 additions & 4 deletions src/Translators/AstrotomicTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@ class AstrotomicTranslator extends BaseTranslator
* @param string $attribute
* @param string $locale
* @param string $translation
* @param bool $force
* @return void
*/
protected function translateAttribute(Model $model, $attribute, $locale, $translation)
protected function translateAttribute(Model $model, $attribute, $locale, $translation, bool $force = true)
{
$model->translateOrNew($locale)->setAttribute($attribute, $translation);
$translationModel = $model->translateOrNew($locale);

// Ignore attribute when it has a value and it should not be overriten.
if (! $force && $translationModel->getAttribute($attribute)) {
return;
}

$translationModel->setAttribute($attribute, $translation);
}

/**
Expand All @@ -39,14 +47,15 @@ public function getTranslatedAttributes(Model $model, $locale)
* @param array $attributes
* @param string $targetLang
* @param string|null $sourceLanguage
* @param bool $force
* @return void
*/
public function translate(Model $model, string $targetLang, string | null $sourceLanguage = null)
public function translate(Model $model, string $targetLang, string | null $sourceLanguage = null, bool $force = true)
{
if (! $model->hasTranslation($sourceLanguage)) {
return;
}

return parent::translate($model, $targetLang, $sourceLanguage);
return parent::translate($model, $targetLang, $sourceLanguage, $force);
}
}
11 changes: 6 additions & 5 deletions src/Translators/BaseTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class BaseTranslator implements Translator
* @param string $translation
* @return void
*/
abstract protected function translateAttribute(Model $model, $attribute, $locale, $translation);
abstract protected function translateAttribute(Model $model, $attribute, $locale, $translation, bool $force = true);

/**
* Get a list of the translated attributes of a model.
Expand Down Expand Up @@ -49,7 +49,7 @@ public function __construct(
* @param string|null $sourceLanguage
* @return void
*/
public function translate(Model $model, string $targetLang, string | null $sourceLanguage = null)
public function translate(Model $model, string $targetLang, string | null $sourceLanguage = null, bool $force = true)
{
$translatedAttributes = $this->getTranslatedAttributes($model, $sourceLanguage);

Expand All @@ -61,7 +61,8 @@ public function translate(Model $model, string $targetLang, string | null $sourc
$model,
$translatedAttributes,
$targetLang,
$sourceLanguage
$sourceLanguage,
$force
);
}

Expand All @@ -74,7 +75,7 @@ public function translate(Model $model, string $targetLang, string | null $sourc
* @param string|null $sourceLanguage
* @return void
*/
public function translateAttributes(Model $model, array $attributes, string $targetLang, string | null $sourceLanguage = null)
public function translateAttributes(Model $model, array $attributes, string $targetLang, string | null $sourceLanguage = null, bool $force = true)
{
foreach ($attributes as $attribute) {
$translation = $this->api->translate(
Expand All @@ -83,7 +84,7 @@ public function translateAttributes(Model $model, array $attributes, string $tar
$sourceLanguage
);

$this->translateAttribute($model, $attribute, $targetLang, $translation);
$this->translateAttribute($model, $attribute, $targetLang, $translation, $force);
}
}
}
47 changes: 47 additions & 0 deletions tests/AstrotomicTranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ protected function getPackageProviders($app)
];
}

protected function getEnvironmentSetUp($app)
{
$config = $app['config'];
$config->set('translatable.fallback_locale', 'en');
$config->set('translatable.locales', [
'en', 'de',
]);
}

public function testItTranslatesModelAttributes()
{
$api = Mockery::mock(Deepl::class);
Expand All @@ -33,12 +42,25 @@ public function testItTranslatesModelAttributes()

$post = new DummyTranslatablePost([
'en' => ['title' => 'Hello World'],
'de' => ['title' => 'Foo'],
]);

$translator->translateAttributes($post, ['title'], 'de', 'en');

$this->app->setLocale('de');
$this->assertSame($post->title, 'Hallo Welt');

$this->app->setLocale('en');

$post = new DummyTranslatablePost([
'en' => ['title' => 'Hello World'],
'de' => ['title' => 'Foo'],
]);

$translator->translate($post, 'de', 'en');

$this->app->setLocale('de');
$this->assertSame($post->title, 'Hallo Welt');
}

public function testItGetsTranslatedAttributes()
Expand All @@ -53,6 +75,31 @@ public function testItGetsTranslatedAttributes()

$this->assertEquals(['title'], $translator->getTranslatedAttributes($post, 'en'));
}

public function testAttributesAreNotOverritenWhenForceIsFalse()
{
$api = Mockery::mock(Deepl::class);
$api->shouldReceive('translate')->andReturn('Hallo Welt');
$translator = new AstrotomicTranslator($api);

$post = new DummyTranslatablePost([
'en' => ['title' => 'Hello World'],
'de' => ['title' => 'Foo'],
]);
$translator->translateAttributes($post, ['title'], 'de', 'en', false);

$this->app->setLocale('de');
$this->assertSame($post->title, 'Foo');

$this->app->setLocale('en');
$post = new DummyTranslatablePost([
'en' => ['title' => 'Hello World'],
'de' => ['title' => 'Foo'],
]);
$translator->translate($post, 'de', 'en', false);
$this->app->setLocale('de');
$this->assertSame($post->title, 'Foo');
}
}

class DummyTranslatablePostTranslation extends Model
Expand Down
2 changes: 1 addition & 1 deletion tests/BaseTranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function getTranslatedAttributes(Model $model, $locale)
return ['title', 'text'];
}

protected function translateAttribute(Model $model, $attribute, $locale, $translation)
protected function translateAttribute(Model $model, $attribute, $locale, $translation, bool $force = true)
{
$this->calledTimes++;
$this->calledParams [] = new AssertableJsonString([
Expand Down
41 changes: 41 additions & 0 deletions tests/DeeplableCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,47 @@ public function test_deeplable_run_command()
$this->assertSame('bar', $post->title);
}
}

public function test_deeplable_run_command_with_force_option()
{
$api = Mockery::mock(Deepl::class);
$api->shouldReceive('translate')->andReturn('bar');

$this->app->singleton('deeplable.api', function () use ($api) {
return $api;
});

DummyPost::create([
'en' => ['title' => 'foo'],
'de' => ['title' => 'baz'],
]);
DummyPost::create([
'en' => ['title' => 'foo'],
'de' => ['title' => 'baz'],
]);

// Assuming force is disabled by default.
$this->app->setLocale('en');
$this->artisan('deeplable:run', ['locale' => 'de']);
$this->app->setLocale('de');
foreach (DummyPost::all() as $post) {
$this->assertSame('baz', $post->title);
}

$this->app->setLocale('en');
$this->artisan('deeplable:run', ['locale' => 'de', '--force' => false]);
$this->app->setLocale('de');
foreach (DummyPost::all() as $post) {
$this->assertSame('baz', $post->title);
}

$this->app->setLocale('en');
$this->artisan('deeplable:run', ['locale' => 'de', '--force' => true]);
$this->app->setLocale('de');
foreach (DummyPost::all() as $post) {
$this->assertSame('bar', $post->title);
}
}
}

class DummyPostTranslation extends Model
Expand Down

0 comments on commit 2ad73d5

Please sign in to comment.