|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +use Astrotomic\Translatable\Locales; |
3 | 4 | use Astrotomic\Translatable\Test\Model\Food; |
4 | 5 | use Astrotomic\Translatable\Test\Model\Person; |
5 | 6 | use Astrotomic\Translatable\Test\Model\Country; |
@@ -851,4 +852,70 @@ public function test_can_fill_conflicting_attribute_locale() |
851 | 852 | $this->assertEquals('id:my city', $city->getTranslation('id', false)->name); |
852 | 853 | $this->assertEquals('en:my city', $city->getTranslation('en', false)->name); |
853 | 854 | } |
| 855 | + |
| 856 | + public function test_it_returns_first_existing_translation_as_fallback() |
| 857 | + { |
| 858 | + /** @var Locales $helper */ |
| 859 | + $helper = $this->app->make(Locales::class); |
| 860 | + |
| 861 | + $this->app->make('config')->set('translatable.locales', [ |
| 862 | + 'xyz', |
| 863 | + 'en', |
| 864 | + 'de' => [ |
| 865 | + 'DE', |
| 866 | + 'AT', |
| 867 | + ], |
| 868 | + 'fr', |
| 869 | + 'el', |
| 870 | + ]); |
| 871 | + $this->app->make('config')->set('translatable.fallback_locale', null); |
| 872 | + $this->app->make('config')->set('translatable.use_fallback', true); |
| 873 | + $this->app->setLocale('xyz'); |
| 874 | + $helper->load(); |
| 875 | + |
| 876 | + CountryTranslation::create([ |
| 877 | + 'country_id' => 1, |
| 878 | + 'locale' => $helper->getCountryLocale('de', 'DE'), |
| 879 | + 'name' => 'Griechenland', |
| 880 | + ]); |
| 881 | + |
| 882 | + /** @var Country $country */ |
| 883 | + $country = Country::find(1); |
| 884 | + $this->assertNull($country->getTranslation(null, false)); |
| 885 | + |
| 886 | + // returns first existing locale |
| 887 | + $translation = $country->getTranslation(); |
| 888 | + $this->assertInstanceOf(CountryTranslation::class, $translation); |
| 889 | + $this->assertEquals('en', $translation->locale); |
| 890 | + |
| 891 | + // still returns simple locale for country based locale |
| 892 | + $translation = $country->getTranslation($helper->getCountryLocale('de', 'AT')); |
| 893 | + $this->assertInstanceOf(CountryTranslation::class, $translation); |
| 894 | + $this->assertEquals('de', $translation->locale); |
| 895 | + |
| 896 | + $this->app->make('config')->set('translatable.locales', [ |
| 897 | + 'xyz', |
| 898 | + 'de' => [ |
| 899 | + 'DE', |
| 900 | + 'AT', |
| 901 | + ], |
| 902 | + 'en', |
| 903 | + 'fr', |
| 904 | + 'el', |
| 905 | + ]); |
| 906 | + $helper->load(); |
| 907 | + |
| 908 | + // returns simple locale before country based locale |
| 909 | + $translation = $country->getTranslation(); |
| 910 | + $this->assertInstanceOf(CountryTranslation::class, $translation); |
| 911 | + $this->assertEquals('de', $translation->locale); |
| 912 | + |
| 913 | + $country->translations()->where('locale', 'de')->delete(); |
| 914 | + $country->unsetRelation('translations'); |
| 915 | + |
| 916 | + // returns country based locale before next simple one |
| 917 | + $translation = $country->getTranslation(); |
| 918 | + $this->assertInstanceOf(CountryTranslation::class, $translation); |
| 919 | + $this->assertEquals($helper->getCountryLocale('de', 'DE'), $translation->locale); |
| 920 | + } |
854 | 921 | } |
0 commit comments