From 8e5d19c3eb7f11cdd9696b9eca4e5218a6db74c7 Mon Sep 17 00:00:00 2001 From: Matteo Trubini <7964032+matteotrubini@users.noreply.github.com> Date: Fri, 9 Jun 2023 15:21:11 +0200 Subject: [PATCH] Add setTranslatableUseFallback() - deprecate noFallbackLocale() closes #66 --- README.md | 4 ++-- classes/TranslatableBehavior.php | 21 ++++++++++++++++--- .../behaviors/TranslatableCmsObjectTest.php | 18 ++++++++++++++++ .../unit/behaviors/TranslatableModelTest.php | 18 ++++++++++++++++ tests/unit/behaviors/TranslatablePageTest.php | 20 ++++++++++++++++++ traits/MLControl.php | 2 +- 6 files changed, 77 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d559c0c8..d1827aa0 100644 --- a/README.md +++ b/README.md @@ -251,11 +251,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; diff --git a/classes/TranslatableBehavior.php b/classes/TranslatableBehavior.php index 403a5630..321ea91f 100644 --- a/classes/TranslatableBehavior.php +++ b/classes/TranslatableBehavior.php @@ -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() + public function noFallbackLocale(): \Winter\Storm\Database\Model { - $this->translatableUseFallback = false; + $this->setTranslatableUseFallback(false); return $this->model; } diff --git a/tests/unit/behaviors/TranslatableCmsObjectTest.php b/tests/unit/behaviors/TranslatableCmsObjectTest.php index c876775d..5be028ac 100644 --- a/tests/unit/behaviors/TranslatableCmsObjectTest.php +++ b/tests/unit/behaviors/TranslatableCmsObjectTest.php @@ -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(); diff --git a/tests/unit/behaviors/TranslatableModelTest.php b/tests/unit/behaviors/TranslatableModelTest.php index 5acf20d5..04871a3b 100755 --- a/tests/unit/behaviors/TranslatableModelTest.php +++ b/tests/unit/behaviors/TranslatableModelTest.php @@ -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(); diff --git a/tests/unit/behaviors/TranslatablePageTest.php b/tests/unit/behaviors/TranslatablePageTest.php index b2b772fe..b81543c5 100644 --- a/tests/unit/behaviors/TranslatablePageTest.php +++ b/tests/unit/behaviors/TranslatablePageTest.php @@ -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([ diff --git a/traits/MLControl.php b/traits/MLControl.php index a463cb61..a0334655 100644 --- a/traits/MLControl.php +++ b/traits/MLControl.php @@ -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;