Skip to content

Commit

Permalink
Allow 0 to be used a translation value (#144)
Browse files Browse the repository at this point in the history
* added tests for 'zero' value issue

* prevent stripping 0 from translation values

* Apply fixes from StyleCI
  • Loading branch information
robinmartini authored and freekmurze committed Dec 19, 2018
1 parent 1c2480f commit 22d1b86
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/HasTranslations.php
Expand Up @@ -64,7 +64,9 @@ public function getTranslations(string $key = null) : array
if ($key !== null) {
$this->guardAgainstNonTranslatableAttribute($key);

return array_filter(json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true) ?: []);
return array_filter(json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true) ?: [], function ($value) {
return $value !== null && $value !== false && $value !== '';
});
}

return array_reduce($this->getTranslatableAttributes(), function ($result, $item) {
Expand Down
33 changes: 33 additions & 0 deletions tests/TranslatableTest.php
Expand Up @@ -416,4 +416,37 @@ public function it_will_return_fallback_locale_translation_when_getting_an_empty

$this->assertSame('testValue_en', $this->testModel->getTranslation('name', 'nl'));
}

/** @test */
public function it_will_return_correct_translation_value_if_value_is_set_to_zero()
{
$this->testModel->setTranslation('name', 'nl', '0');
$this->testModel->save();

$this->assertSame('0', $this->testModel->getTranslation('name', 'nl'));
}

/** @test */
public function it_will_not_return_fallback_value_if_value_is_set_to_zero()
{
$this->app['config']->set('translatable.fallback_locale', 'en');

$this->testModel->setTranslation('name', 'en', '1');
$this->testModel->setTranslation('name', 'nl', '0');
$this->testModel->save();

$this->assertSame('0', $this->testModel->getTranslation('name', 'nl'));
}

/** @test */
public function it_will_not_remove_zero_value_of_other_locale_in_database()
{
$this->app['config']->set('translatable.fallback_locale', 'en');

$this->testModel->setTranslation('name', 'nl', '0');
$this->testModel->setTranslation('name', 'en', '1');
$this->testModel->save();

$this->assertSame('0', $this->testModel->getTranslation('name', 'nl'));
}
}

0 comments on commit 22d1b86

Please sign in to comment.