Skip to content
This repository was archived by the owner on Jun 18, 2019. It is now read-only.

Commit bf40613

Browse files
authored
Merge pull request #576 from dimsav/fix-non-string-translation-values-fallback
add isEmptyTranslatableAttribute() to allow custom decision logic
2 parents f433ca5 + 5d101b0 commit bf40613

File tree

5 files changed

+66
-4
lines changed

5 files changed

+66
-4
lines changed

readme.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,9 @@ $germany->name; // 'Germany'
390390
$germany->{'name:de'} // 'Deutschland'
391391
```
392392

393-
### Fallback locales
393+
### Fallback
394+
395+
#### Fallback locales
394396

395397
If you want to fallback to a default translation when a translation has not been found, enable this in the configuration
396398
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.
433435

434436
If the property fallback is enabled in the configuration, then translatable
435437
will return the translation of the fallback locale for the fields where the translation is empty.
438+
439+
##### customize empty translation property detection
440+
441+
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.
442+
443+
```php
444+
protected function isEmptyTranslatableAttribute(string $key, $value): bool
445+
{
446+
switch($key) {
447+
case 'name':
448+
return empty($value);
449+
case 'price':
450+
return !is_number($value);
451+
default:
452+
return is_null($value);
453+
}
454+
}
455+
```
436456

437457
#### Country based fallback
438458

src/Translatable/Translatable.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ private function getAttributeOrFallback($locale, $attribute)
183183
if (
184184
(
185185
! $translation instanceof Model ||
186-
empty($translation->$attribute)
186+
$this->isEmptyTranslatableAttribute($attribute, $translation->$attribute)
187187
) &&
188188
$this->usePropertyFallback()
189189
) {
@@ -197,6 +197,11 @@ private function getAttributeOrFallback($locale, $attribute)
197197
return null;
198198
}
199199

200+
protected function isEmptyTranslatableAttribute(string $key, $value): bool
201+
{
202+
return empty($value);
203+
}
204+
200205
/**
201206
* @param string $key
202207
*

tests/TestCoreModelExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function test_it_throws_query_exception_if_code_is_null()
5050

5151
public function test_it_throws_query_exception_if_saving_and_name_is_null()
5252
{
53-
$this->expectException('\Exception');
53+
$this->expectException(\Exception::class);
5454

5555
$country = new Country();
5656
$country->code = 'be';

tests/TranslatableTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,4 +742,41 @@ public function test_empty_translated_attribute()
742742
$this->app->setLocale('invalid');
743743
$this->assertSame(null, $country->name);
744744
}
745+
746+
public function test_numeric_translated_attribute()
747+
{
748+
$this->app->config->set('translatable.fallback_locale', 'de');
749+
$this->app->config->set('translatable.use_fallback', true);
750+
751+
$city = new class extends \Dimsav\Translatable\Test\Model\City {
752+
protected $fillable = [
753+
'country_id',
754+
];
755+
protected $table = 'cities';
756+
public $translationModel = \Dimsav\Translatable\Test\Model\CityTranslation::class;
757+
public $translationForeignKey = 'city_id';
758+
759+
protected function isEmptyTranslatableAttribute(string $key, $value): bool
760+
{
761+
if ($key === 'name') {
762+
return is_null($value);
763+
}
764+
765+
return empty($value);
766+
}
767+
};
768+
$city->fill([
769+
'country_id' => Country::first()->getKey(),
770+
'en' => ['name' => '0'],
771+
'de' => ['name' => '1'],
772+
'fr' => ['name' => null],
773+
]);
774+
$city->save();
775+
776+
$this->app->setLocale('en');
777+
$this->assertSame('0', $city->name);
778+
779+
$this->app->setLocale('fr');
780+
$this->assertSame('1', $city->name);
781+
}
745782
}

tests/migrations/2013_11_28_152610_create_tables.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function up()
3838
Schema::create('city_translations', function (Blueprint $table) {
3939
$table->increments('id');
4040
$table->integer('city_id')->unsigned();
41-
$table->string('name');
41+
$table->string('name')->nullable();
4242
$table->string('locale')->index();
4343

4444
$table->unique(['city_id', 'locale']);

0 commit comments

Comments
 (0)