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

Commit

Permalink
Merge pull request #576 from dimsav/fix-non-string-translation-values…
Browse files Browse the repository at this point in the history
…-fallback

add isEmptyTranslatableAttribute() to allow custom decision logic
  • Loading branch information
Gummibeer authored Jun 3, 2019
2 parents f433ca5 + 5d101b0 commit bf40613
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
22 changes: 21 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion src/Translatable/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private function getAttributeOrFallback($locale, $attribute)
if (
(
! $translation instanceof Model ||
empty($translation->$attribute)
$this->isEmptyTranslatableAttribute($attribute, $translation->$attribute)
) &&
$this->usePropertyFallback()
) {
Expand All @@ -197,6 +197,11 @@ private function getAttributeOrFallback($locale, $attribute)
return null;
}

protected function isEmptyTranslatableAttribute(string $key, $value): bool
{
return empty($value);
}

/**
* @param string $key
*
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCoreModelExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
37 changes: 37 additions & 0 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion tests/migrations/2013_11_28_152610_create_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down

0 comments on commit bf40613

Please sign in to comment.