Skip to content

Commit f7e595b

Browse files
authored
Merge pull request #49 from Astrotomic/ft-translation-usage
Allows to load `translation` relation instead of load all `translations`
2 parents 0d49fff + 9996bea commit f7e595b

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/Translatable/Translatable.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Astrotomic\Translatable\Traits\Relationship;
1010

1111
/**
12+
* @property-read null|Model $translation
1213
* @property-read Collection|Model[] $translations
1314
* @property-read string $translationModel
1415
* @property-read string $translationForeignKey
@@ -391,6 +392,14 @@ private function getFallbackLocale(?string $locale = null): ?string
391392

392393
private function getTranslationByLocaleKey(string $key): ?Model
393394
{
395+
if (
396+
$this->relationLoaded('translation')
397+
&& $this->translation
398+
&& $this->translation->getAttribute($this->getLocaleKey()) == $key
399+
) {
400+
return $this->translation;
401+
}
402+
394403
return $this->translations->firstWhere($this->getLocaleKey(), $key);
395404
}
396405

tests/TranslatableTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,4 +918,56 @@ public function test_it_returns_first_existing_translation_as_fallback()
918918
$this->assertInstanceOf(CountryTranslation::class, $translation);
919919
$this->assertEquals($helper->getCountryLocale('de', 'DE'), $translation->locale);
920920
}
921+
922+
public function test_it_uses_translation_relation_if_locale_matches()
923+
{
924+
$this->app->make('config')->set('translatable.use_fallback', false);
925+
$this->app->setLocale('de');
926+
927+
/** @var Country $country */
928+
$country = Country::find(1);
929+
$country->load('translation');
930+
$this->assertTrue($country->relationLoaded('translation'));
931+
$this->assertFalse($country->relationLoaded('translations'));
932+
933+
$translation = $country->getTranslation();
934+
$this->assertInstanceOf(CountryTranslation::class, $translation);
935+
$this->assertEquals('de', $translation->locale);
936+
$this->assertFalse($country->relationLoaded('translations'));
937+
}
938+
939+
public function test_it_uses_translations_relation_if_locale_does_not_match()
940+
{
941+
$this->app->make('config')->set('translatable.use_fallback', false);
942+
$this->app->setLocale('de');
943+
944+
/** @var Country $country */
945+
$country = Country::find(1);
946+
$country->load('translation');
947+
$this->assertTrue($country->relationLoaded('translation'));
948+
$this->assertFalse($country->relationLoaded('translations'));
949+
$this->app->setLocale('en');
950+
951+
$translation = $country->getTranslation();
952+
$this->assertInstanceOf(CountryTranslation::class, $translation);
953+
$this->assertEquals('en', $translation->locale);
954+
$this->assertTrue($country->relationLoaded('translations'));
955+
}
956+
957+
public function test_it_does_not_load_translation_relation_if_not_already_loaded()
958+
{
959+
$this->app->make('config')->set('translatable.use_fallback', false);
960+
$this->app->setLocale('de');
961+
962+
/** @var Country $country */
963+
$country = Country::find(1);
964+
$this->assertFalse($country->relationLoaded('translation'));
965+
$this->assertFalse($country->relationLoaded('translations'));
966+
967+
$translation = $country->getTranslation();
968+
$this->assertInstanceOf(CountryTranslation::class, $translation);
969+
$this->assertEquals('de', $translation->locale);
970+
$this->assertFalse($country->relationLoaded('translation'));
971+
$this->assertTrue($country->relationLoaded('translations'));
972+
}
921973
}

0 commit comments

Comments
 (0)