diff --git a/readme.md b/readme.md index eed1e1d..0cbc93f 100644 --- a/readme.md +++ b/readme.md @@ -390,7 +390,9 @@ $germany->name; // 'Germany' $germany->{'name:de'} // 'Deutschland' ``` -### Fallback locales +### Fallback + +#### Fallback locales If you want to fallback to a default translation when a translation has not been found, enable this in the configuration using the `use_fallback` key. And to select the default locale, use the `fallback_locale` key. @@ -433,6 +435,24 @@ Of course the fallback locales must be enabled to use this feature. If the property fallback is enabled in the configuration, then translatable will return the translation of the fallback locale for the fields where the translation is empty. + +##### customize empty translation property detection + +This package is made to translate strings, but in general it's also able to translate numbers, bools or whatever you want to. By default a simple `empty()` call is used to detect if the translation value is empty or not. If you want to customize this or use different logic per property you can override `isEmptyTranslatableAttribute()` in your main model. + +```php +protected function isEmptyTranslatableAttribute(string $key, $value): bool +{ + switch($key) { + case 'name': + return empty($value); + case 'price': + return !is_number($value); + default: + return is_null($value); + } +} +``` #### Country based fallback diff --git a/src/Translatable/Translatable.php b/src/Translatable/Translatable.php index 35dd8a8..b1ea394 100644 --- a/src/Translatable/Translatable.php +++ b/src/Translatable/Translatable.php @@ -183,7 +183,7 @@ private function getAttributeOrFallback($locale, $attribute) if ( ( ! $translation instanceof Model || - empty($translation->$attribute) + $this->isEmptyTranslatableAttribute($attribute, $translation->$attribute) ) && $this->usePropertyFallback() ) { @@ -197,6 +197,11 @@ private function getAttributeOrFallback($locale, $attribute) return null; } + protected function isEmptyTranslatableAttribute(string $key, $value): bool + { + return empty($value); + } + /** * @param string $key * diff --git a/tests/TestCoreModelExtension.php b/tests/TestCoreModelExtension.php index edfd552..56d8b66 100644 --- a/tests/TestCoreModelExtension.php +++ b/tests/TestCoreModelExtension.php @@ -50,7 +50,7 @@ public function test_it_throws_query_exception_if_code_is_null() public function test_it_throws_query_exception_if_saving_and_name_is_null() { - $this->expectException('\Exception'); + $this->expectException(\Exception::class); $country = new Country(); $country->code = 'be'; diff --git a/tests/TranslatableTest.php b/tests/TranslatableTest.php index c9360a1..85c2180 100644 --- a/tests/TranslatableTest.php +++ b/tests/TranslatableTest.php @@ -742,4 +742,41 @@ public function test_empty_translated_attribute() $this->app->setLocale('invalid'); $this->assertSame(null, $country->name); } + + public function test_numeric_translated_attribute() + { + $this->app->config->set('translatable.fallback_locale', 'de'); + $this->app->config->set('translatable.use_fallback', true); + + $city = new class extends \Dimsav\Translatable\Test\Model\City { + protected $fillable = [ + 'country_id', + ]; + protected $table = 'cities'; + public $translationModel = \Dimsav\Translatable\Test\Model\CityTranslation::class; + public $translationForeignKey = 'city_id'; + + protected function isEmptyTranslatableAttribute(string $key, $value): bool + { + if ($key === 'name') { + return is_null($value); + } + + return empty($value); + } + }; + $city->fill([ + 'country_id' => Country::first()->getKey(), + 'en' => ['name' => '0'], + 'de' => ['name' => '1'], + 'fr' => ['name' => null], + ]); + $city->save(); + + $this->app->setLocale('en'); + $this->assertSame('0', $city->name); + + $this->app->setLocale('fr'); + $this->assertSame('1', $city->name); + } } diff --git a/tests/migrations/2013_11_28_152610_create_tables.php b/tests/migrations/2013_11_28_152610_create_tables.php index 2fb6647..1700c3e 100644 --- a/tests/migrations/2013_11_28_152610_create_tables.php +++ b/tests/migrations/2013_11_28_152610_create_tables.php @@ -38,7 +38,7 @@ public function up() Schema::create('city_translations', function (Blueprint $table) { $table->increments('id'); $table->integer('city_id')->unsigned(); - $table->string('name'); + $table->string('name')->nullable(); $table->string('locale')->index(); $table->unique(['city_id', 'locale']);