Skip to content

Commit 90acbf0

Browse files
committed
Merge remote-tracking branch 'remotes/origin/issue-309'
# Conflicts: # src/Translatable/Translatable.php
2 parents bcfdd05 + d13e12f commit 90acbf0

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

src/Translatable/Contracts/Translatable.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\Eloquent\Model;
66
use Illuminate\Database\Eloquent\Relations\HasMany;
7+
use Illuminate\Database\Eloquent\Relations\HasOne;
78

89
interface Translatable
910
{
@@ -13,8 +14,6 @@ public static function disableAutoloadTranslations(): void;
1314

1415
public static function enableAutoloadTranslations(): void;
1516

16-
public function translations(): HasMany;
17-
1817
public function deleteTranslations($locales = null): void;
1918

2019
public function getDefaultLocale(): ?string;
@@ -40,4 +39,8 @@ public function translate(?string $locale = null, bool $withFallback = false): ?
4039
public function translateOrDefault(?string $locale = null): ?Model;
4140

4241
public function translateOrNew(?string $locale = null): Model;
42+
43+
public function translation(): HasOne;
44+
45+
public function translations(): HasMany;
4346
}

src/Translatable/Traits/Relationship.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Astrotomic\Translatable\Traits;
44

55
use Illuminate\Database\Eloquent\Relations\HasMany;
6+
use Illuminate\Database\Eloquent\Relations\HasOne;
67

78
/**
89
* @property-read string $translationModel
@@ -60,6 +61,19 @@ public function getTranslationRelationKey(): string
6061
return $this->getForeignKey();
6162
}
6263

64+
public function translation(): HasOne
65+
{
66+
if ($this->useFallback() && ! $this->translations()->where('locale', $this->locale())->exists()) {
67+
return $this
68+
->hasOne($this->getTranslationModelName(), $this->getTranslationRelationKey())
69+
->where('locale', $this->getFallbackLocale());
70+
}
71+
72+
return $this
73+
->hasOne($this->getTranslationModelName(), $this->getTranslationRelationKey())
74+
->where('locale', $this->locale());
75+
}
76+
6377
public function translations(): HasMany
6478
{
6579
return $this->hasMany($this->getTranslationModelName(), $this->getTranslationRelationKey());

tests/TranslatableTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Astrotomic\Translatable\Test\Model\Person;
55
use Astrotomic\Translatable\Test\Model\Country;
66
use Astrotomic\Translatable\Test\Model\CountryStrict;
7+
use Astrotomic\Translatable\Test\Model\CountryTranslation;
78
use Astrotomic\Translatable\Test\Model\CountryWithCustomLocaleKey;
89
use Astrotomic\Translatable\Test\Model\CountryWithCustomTranslationModel;
910

@@ -785,4 +786,36 @@ protected function isEmptyTranslatableAttribute(string $key, $value): bool
785786
$this->app->setLocale('fr');
786787
$this->assertSame('1', $city->name);
787788
}
789+
790+
public function test_translation_relation()
791+
{
792+
$this->app->make('config')->set('translatable.fallback_locale', 'fr');
793+
$this->app->make('config')->set('translatable.use_fallback', true);
794+
$this->app->setLocale('en');
795+
796+
$translation = Country::find(1)->translation;
797+
$this->assertInstanceOf(CountryTranslation::class, $translation);
798+
$this->assertEquals('en', $translation->locale);
799+
}
800+
801+
public function test_translation_relation_fallback()
802+
{
803+
$this->app->make('config')->set('translatable.fallback_locale', 'fr');
804+
$this->app->make('config')->set('translatable.use_fallback', true);
805+
$this->app->setLocale('xyz');
806+
807+
$translation = Country::find(1)->translation;
808+
$this->assertInstanceOf(CountryTranslation::class, $translation);
809+
$this->assertEquals('fr', $translation->locale);
810+
}
811+
812+
public function test_translation_relation_not_found()
813+
{
814+
$this->app->make('config')->set('translatable.fallback_locale', 'xyz');
815+
$this->app->make('config')->set('translatable.use_fallback', true);
816+
$this->app->setLocale('xyz');
817+
818+
$translation = Country::find(1)->translation;
819+
$this->assertNull($translation);
820+
}
788821
}

0 commit comments

Comments
 (0)