Skip to content

Commit 78112f5

Browse files
Oleksandr VasylenkoAkenRoberts
authored andcommitted
FX consider null values as missing translation while keeping falsy translations as valid
Contains reproduce for #2152 and keeps #1709 use case intract
1 parent 6cae803 commit 78112f5

File tree

4 files changed

+182
-15
lines changed

4 files changed

+182
-15
lines changed

lib/Gedmo/Translatable/TranslatableListener.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -470,17 +470,15 @@ public function postLoad(EventArgs $args)
470470
);
471471
// translate object's translatable properties
472472
foreach ($config['fields'] as $field) {
473-
$translated = '';
474-
$is_translated = false;
473+
$translated = null;
475474
foreach ((array) $result as $entry) {
476475
if ($entry['field'] == $field) {
477476
$translated = isset($entry['content']) ? $entry['content'] : null;
478-
$is_translated = true;
479477
break;
480478
}
481479
}
482480
// update translation
483-
if ($is_translated
481+
if ($translated !== null
484482
|| (!$this->translationFallback && (!isset($config['fallback'][$field]) || !$config['fallback'][$field]))
485483
|| ($this->translationFallback && isset($config['fallback'][$field]) && !$config['fallback'][$field])
486484
) {
@@ -518,7 +516,7 @@ protected function validateLocale($locale)
518516
throw new \Gedmo\Exception\InvalidArgumentException('Locale or language cannot be empty and must be set through Listener or Entity');
519517
}
520518
}
521-
519+
522520
/**
523521
* Check if the given locale is valid
524522
*
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Translatable\Fixture\Issue2152;
6+
7+
use Doctrine\ORM\Mapping as ORM;
8+
use Gedmo\Mapping\Annotation as Gedmo;
9+
10+
/**
11+
* @ORM\Entity
12+
* @ORM\Table("entity")
13+
*/
14+
class EntityWithTranslatableBoolean
15+
{
16+
/**
17+
* @ORM\Id
18+
* @ORM\GeneratedValue
19+
* @ORM\Column(type="integer")
20+
*
21+
* @var int
22+
*/
23+
private $id;
24+
25+
/**
26+
* @Gedmo\Translatable
27+
* @ORM\Column(type="string", nullable=true)
28+
*
29+
* @var string|null
30+
*/
31+
private $title;
32+
33+
/**
34+
* @Gedmo\Translatable
35+
* @ORM\Column(type="string", nullable=true)
36+
*
37+
* @var string|null
38+
*/
39+
private $isOperating;
40+
41+
/**
42+
* @var string
43+
*
44+
* @Gedmo\Locale()
45+
*/
46+
private $locale;
47+
48+
public function __construct(string $title, string $isOperating = '0')
49+
{
50+
$this->translateInLocale('en', $title, $isOperating);
51+
}
52+
53+
public function translateInLocale(string $locale, ?string $title, ?string $isOperating): void
54+
{
55+
$this->title = $title;
56+
$this->isOperating = $isOperating;
57+
$this->locale = $locale;
58+
}
59+
60+
public function getId(): ?int
61+
{
62+
return $this->id;
63+
}
64+
65+
public function getTitle(): ?string
66+
{
67+
return $this->title;
68+
}
69+
70+
public function isOperating(): ?string
71+
{
72+
return $this->isOperating;
73+
}
74+
75+
public function getLocale(): string
76+
{
77+
return $this->locale;
78+
}
79+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Gedmo\Translatable\Issue;
6+
7+
use Doctrine\Common\EventManager;
8+
use Gedmo\Translatable\TranslatableListener;
9+
use Tool\BaseTestCaseORM;
10+
use Gedmo\Translatable\Entity\Translation;
11+
use Translatable\Fixture\Issue2152\EntityWithTranslatableBoolean;
12+
13+
class Issue2152Test extends BaseTestCaseORM
14+
{
15+
private const TRANSLATION = Translation::class;
16+
private const ENTITY = EntityWithTranslatableBoolean::class;
17+
18+
/**
19+
* @var TranslatableListener
20+
*/
21+
private $translatableListener;
22+
23+
protected function setUp(): void
24+
{
25+
parent::setUp();
26+
27+
$evm = new EventManager();
28+
29+
$this->translatableListener = new TranslatableListener();
30+
$this->translatableListener->setTranslatableLocale('en');
31+
$this->translatableListener->setDefaultLocale('en');
32+
$this->translatableListener->setTranslationFallback(true);
33+
$evm->addEventSubscriber($this->translatableListener);
34+
35+
$this->getMockSqliteEntityManager($evm);
36+
}
37+
38+
/**
39+
* @test
40+
*/
41+
public function shouldFindInheritedClassTranslations(): void
42+
{
43+
//Arrange
44+
//by default we have English
45+
$title = 'Hello World';
46+
$isOperating = '1';
47+
48+
//operating in germany
49+
$deTitle = 'Hallo Welt';
50+
$isOperatingInGermany = '0';
51+
52+
//but in Ukraine not operating, should fallback to default one
53+
$uaTitle = null;
54+
$isOperatingInUkraine = null;
55+
56+
$entity = new EntityWithTranslatableBoolean($title, $isOperating);
57+
$this->em->persist($entity);
58+
$this->em->flush();
59+
60+
$entity->translateInLocale('de', $deTitle, $isOperatingInGermany);
61+
62+
$this->em->persist($entity);
63+
$this->em->flush();
64+
65+
$entity->translateInLocale('ua', $uaTitle, $isOperatingInUkraine);
66+
67+
$this->em->persist($entity);
68+
$this->em->flush();
69+
70+
//Act
71+
$entityInDe = $this->findUsingQueryBuilder('de');
72+
$entityInUa = $this->findUsingQueryBuilder('ua');
73+
74+
//Assert
75+
76+
$this->assertSame($deTitle, $entityInDe->getTitle());
77+
$this->assertEquals($isOperatingInGermany, $entityInDe->isOperating());
78+
79+
$this->assertSame($title, $entityInUa->getTitle(), 'should fallback to default title if null');
80+
$this->assertEquals($isOperating, $entityInUa->isOperating(), ' should fallback to default operating if null');
81+
}
82+
83+
protected function getUsedEntityFixtures()
84+
{
85+
return [
86+
self::TRANSLATION,
87+
self::ENTITY,
88+
];
89+
}
90+
91+
private function findUsingQueryBuilder(string $locale): ?EntityWithTranslatableBoolean
92+
{
93+
$this->em->clear();
94+
$this->translatableListener->setTranslatableLocale($locale);
95+
96+
$qb = $this->em->createQueryBuilder()->select('e')->from(self::ENTITY, 'e');
97+
98+
return $qb->getQuery()->getSingleResult();
99+
}
100+
}

tests/Gedmo/Translatable/TranslatableWithEmbeddedTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,6 @@ public function testQueryWalker()
109109
$this->assertSame('facebook-de', $result[0]['link.facebook']);
110110
}
111111

112-
public function testTranslationOfNullValuesFallbacksToDefault(): void
113-
{
114-
$this->markTestIncomplete('TODO');
115-
}
116-
117-
public function testTranslationOfFalseValuesDoesNotFallback(): void
118-
{
119-
$this->markTestIncomplete('TODO');
120-
}
121-
122112
protected function getUsedEntityFixtures()
123113
{
124114
return array(

0 commit comments

Comments
 (0)