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

Commit

Permalink
use fallback in attributesToArray (#503)
Browse files Browse the repository at this point in the history
* use fallback in attributesToArray

#448

* fix empty line

* define locale that's not present

* fix getAttributeOrFallback()

* fix php cs
  • Loading branch information
Gummibeer authored and dimsav committed Jul 24, 2018
1 parent 35b3837 commit 476e0ea
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/Translatable/Translatable.php
Expand Up @@ -162,17 +162,23 @@ private function usePropertyFallback()
*/
private function getAttributeOrFallback($locale, $attribute)
{
$value = $this->getTranslation($locale)->$attribute;
$translation = $this->getTranslation($locale);

if (
empty($value) &&
$this->usePropertyFallback() &&
($fallback = $this->getTranslation($this->getFallbackLocale(), true))
(
! $translation instanceof Model ||
empty($translation->$attribute)
) &&
$this->usePropertyFallback()
) {
return $fallback->$attribute;
$translation = $this->getTranslation($this->getFallbackLocale(), true);
}

return $value;
if ($translation instanceof Model) {
return $translation->$attribute;
}

return null;
}

/**
Expand Down Expand Up @@ -723,9 +729,7 @@ public function attributesToArray()
continue;
}

if ($translations = $this->getTranslation()) {
$attributes[$field] = $translations->$field;
}
$attributes[$field] = $this->getAttributeOrFallback(null, $field);
}

return $attributes;
Expand Down
1 change: 1 addition & 0 deletions tests/TranslatableTest.php
Expand Up @@ -422,6 +422,7 @@ public function test_fallback_for_country_based_locales_with_no_base_locale()

public function test_to_array_and_fallback_with_country_based_locales_enabled()
{
$this->app->config->set('translatable.locale', 'en-GB');
$this->app->config->set('translatable.use_fallback', true);
$this->app->config->set('translatable.fallback_locale', 'fr');
$this->app->config->set('translatable.locales', ['en' => ['GB'], 'fr']);
Expand Down

0 comments on commit 476e0ea

Please sign in to comment.