From 78710daa2c0cda9bc9e2cad39205f19f23b31953 Mon Sep 17 00:00:00 2001 From: Matteo Trubini <7964032+matteotrubini@users.noreply.github.com> Date: Mon, 18 Mar 2024 19:19:24 +0100 Subject: [PATCH] Add setTranslatableUseFallback() - deprecate noFallbackLocale() (#69) closes #66 --- README.md | 4 ++-- classes/TranslatableBehavior.php | 19 ++++++++++++++++-- .../behaviors/TranslatableCmsObjectTest.php | 18 +++++++++++++++++ .../unit/behaviors/TranslatableModelTest.php | 18 +++++++++++++++++ tests/unit/behaviors/TranslatablePageTest.php | 20 +++++++++++++++++++ traits/MLControl.php | 2 +- 6 files changed, 76 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index bc40639b..483125a6 100644 --- a/README.md +++ b/README.md @@ -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; diff --git a/classes/TranslatableBehavior.php b/classes/TranslatableBehavior.php index dc1d6476..1a124d8d 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() { - $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 2e8b2f29..a95e9e85 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;