Skip to content

Commit

Permalink
Add setTranslatableUseFallback() - deprecate noFallbackLocale() (#69)
Browse files Browse the repository at this point in the history
closes #66
  • Loading branch information
matteotrubini authored Mar 18, 2024
1 parent 88892a7 commit 78710da
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,11 @@ It is also possible to translate theme customisation options. Just mark your for

## Fallback attribute values

By default, untranslated attributes will fall back to the default locale. This behavior can be disabled by calling the `noFallbackLocale` method.
By default, untranslated attributes will fall back to the default locale. This behavior can be disabled by calling the `setTranslatableUseFallback()` method.

$user = User::first();

$user->noFallbackLocale()->lang('fr');
$user->setTranslatableUseFallback(false)->lang('fr');

// Returns NULL if there is no French translation
$user->name;
Expand Down
19 changes: 17 additions & 2 deletions classes/TranslatableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,28 @@ public function isTranslatable($key)
return in_array($key, $this->model->getTranslatableAttributes());
}

/**
* Enables/disables translation fallback locale.
* @param bool $value
* @return \Winter\Storm\Database\Model
* @since 2.1.5
*/
public function setTranslatableUseFallback(bool $value)
{
$this->translatableUseFallback = $value;

return $this->model;
}

/**
* Disables translation fallback locale.
* @return self
* @return \Winter\Storm\Database\Model
* @deprecated 2.1.5
* @see setTranslatableUseFallback()
*/
public function noFallbackLocale()
{
$this->translatableUseFallback = false;
$this->setTranslatableUseFallback(false);

return $this->model;
}
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/behaviors/TranslatableCmsObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ public function testGetTranslationValue()
$this->assertEquals('Awww yiss', $obj->markup);
}

/**
* @since 2.1.5
*/
public function testGetTranslationValueUseFallbackFalse()
{
$obj = FeatureModel::first();

$this->assertEquals('Awww yiss', $obj->markup);

$obj->setTranslatableUseFallback(false)->translateContext('fr');

$this->assertEquals(null, $obj->markup);
}

/**
* @deprecated 2.1.5
* @see testGetTranslationValueUseFallbackFalse()
*/
public function testGetTranslationValueNoFallback()
{
$obj = FeatureModel::first();
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/behaviors/TranslatableModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ public function testGetTranslationValue()
$this->assertEquals('Australia', $obj->name);
}

/**
* @since 2.1.5
*/
public function testGetTranslationValueUseFallbackFalse()
{
$obj = CountryModel::first();

$this->assertEquals('Australia', $obj->name);

$obj->setTranslatableUseFallback(false)->translateContext('fr');

$this->assertEquals(null, $obj->name);
}

/**
* @deprecated 2.1.5
* @see testGetTranslationValueUseFallbackFalse()
*/
public function testGetTranslationValueNoFallback()
{
$obj = CountryModel::first();
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/behaviors/TranslatablePageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ public function tearDown(): void
parent::tearDown();
}

/**
* @since 2.1.5
*/
public function testUseFallbackFalse()
{
$page = TranslatablePage::create([
'fileName' => 'translatable',
'title' => 'english title',
'url' => '/test',
]);
$page->translateContext('fr');
$this->assertEquals('english title', $page->title);
$page->setTranslatableUseFallback(false)->translateContext('fr');
$this->assertEquals(null, $page->title);
}

/**
* @deprecated 2.1.5
* @see testUseFallbackFalse()
*/
public function testUseFallback()
{
$page = TranslatablePage::create([
Expand Down
2 changes: 1 addition & 1 deletion traits/MLControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function getLocaleValue($locale)
$value = $this->model->$mutateMethod($locale);
}
elseif ($this->objectMethodExists($this->model, 'getAttributeTranslated') && $this->defaultLocale->code != $locale) {
$value = $this->model->noFallbackLocale()->getAttributeTranslated($key, $locale);
$value = $this->model->setTranslatableUseFallback(false)->getAttributeTranslated($key, $locale);
}
else {
$value = $this->formField->value;
Expand Down

0 comments on commit 78710da

Please sign in to comment.