Skip to content

Commit

Permalink
hasTranslation method : boolean (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmaga authored and freekmurze committed Feb 5, 2019
1 parent 7268e49 commit 9b8d03b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/HasTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ public function isTranslatableAttribute(string $key) : bool
return in_array($key, $this->getTranslatableAttributes());
}

public function hasTranslation($key, $locale = null)
{
$locale = $locale ?: $this->getLocale();
if (isset($this->getTranslations($key)[$locale])) {
return true;
}

return false;
}

protected function guardAgainstNonTranslatableAttribute(string $key)
{
if (! $this->isTranslatableAttribute($key)) {
Expand Down
12 changes: 12 additions & 0 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,18 @@ public function it_can_check_if_an_attribute_is_translatable()
$this->assertFalse($this->testModel->isTranslatableAttribute('other'));
}

/** @test */
public function it_can_check_if_an_attribute_has_translation()
{
$this->testModel->setTranslation('name', 'en', 'testValue_en');
$this->testModel->setTranslation('name', 'nl', null);
$this->testModel->save();

$this->assertTrue($this->testModel->hasTranslation('name', 'en'));

$this->assertFalse($this->testModel->hasTranslation('name', 'pt'));
}

/** @test */
public function it_can_correctly_set_a_field_when_a_mutator_is_defined()
{
Expand Down

0 comments on commit 9b8d03b

Please sign in to comment.