Skip to content

Commit

Permalink
Merge pull request #7 from jewome62/feature/fix-get-translation
Browse files Browse the repository at this point in the history
Translation has not key with locale
  • Loading branch information
antonioperic authored Jul 26, 2019
2 parents 9df2126 + 0e08ce6 commit b4ddcee
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/Model/TranslatableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Locastic\ApiPlatformTranslationBundle\Model;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Expr\Comparison;
use Doctrine\ORM\PersistentCollection;

/**
Expand Down Expand Up @@ -68,8 +70,10 @@ public function getTranslation(?string $locale = null): TranslationInterface
return $this->translationsCache[$locale];
}

$translation = $this->translations->get($locale);
if (null !== $translation) {
$expr = new Comparison('locale', '=', $locale);
$translation = $this->translations->matching(new Criteria($expr))->first();

if (false !== $translation) {
$this->translationsCache[$locale] = $translation;

return $translation;
Expand All @@ -79,9 +83,10 @@ public function getTranslation(?string $locale = null): TranslationInterface
return $this->translationsCache[$this->fallbackLocale];
}

$fallbackTranslation = $this->translations->get($this->fallbackLocale);
$expr = new Comparison('locale', '=', $this->fallbackLocale);
$fallbackTranslation = $this->translations->matching(new Criteria($expr))->first();

if (null !== $fallbackTranslation) {
if (false !== $fallbackTranslation) {
$this->translationsCache[$this->fallbackLocale] = $fallbackTranslation; //@codeCoverageIgnore

return $fallbackTranslation; //@codeCoverageIgnore
Expand Down

0 comments on commit b4ddcee

Please sign in to comment.