Skip to content

Commit 844e330

Browse files
Amoifrjaviereguiluz
authored andcommitted
Fix filter labels not translated with correct domain
Filter labels were not being translated the same way as on other CRUD pages because the label was passed as a plain string while filters use translation_domain 'EasyAdminBundle'. Now CommonConfigurator: - Preserves TranslatableInterface labels from fields - Wraps string labels in t() with the application's translation domain Fix #7373
1 parent 8f4973d commit 844e330

2 files changed

Lines changed: 41 additions & 16 deletions

File tree

src/Filter/Configurator/CommonConfigurator.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
1010
use EasyCorp\Bundle\EasyAdminBundle\Dto\FilterDto;
1111
use EasyCorp\Bundle\EasyAdminBundle\Generator\LabelGenerator;
12-
use Symfony\Component\Translation\TranslatableMessage;
12+
use Symfony\Contracts\Translation\TranslatableInterface;
13+
use function Symfony\Component\Translation\t;
1314

1415
/**
1516
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
@@ -30,16 +31,16 @@ public function configure(FilterDto $filterDto, ?FieldDto $fieldDto, EntityDto $
3031
{
3132
if (null === $filterDto->getLabel()) {
3233
$fieldLabel = $fieldDto?->getLabel();
33-
if ($fieldLabel instanceof TranslatableMessage) {
34-
$fieldLabel = $fieldLabel->getMessage();
35-
}
34+
$translationDomain = $context->getI18n()->getTranslationDomain();
3635

37-
if (null !== $fieldLabel) {
36+
if ($fieldLabel instanceof TranslatableInterface) {
3837
$label = $fieldLabel;
38+
} elseif (null !== $fieldLabel) {
39+
$label = t($fieldLabel, [], $translationDomain);
3940
} elseif ($context->isUseEntityTranslations()) {
4041
$label = $this->entityTranslationIdGenerator->generateForProperty($entityDto->getFqcn(), $filterDto->getProperty());
4142
} else {
42-
$label = LabelGenerator::humanize($filterDto->getProperty());
43+
$label = t(LabelGenerator::humanize($filterDto->getProperty()), [], $translationDomain);
4344
}
4445

4546
$filterDto->setLabel($label);

tests/Unit/Filter/Configurator/CommonConfiguratorTest.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testSupportsAllFilters(): void
3838
$this->assertTrue($configurator->supports($filterDto, null, $this->entityDto, $adminContext));
3939
}
4040

41-
public function testConfigureUsesFieldLabelWhenAvailable(): void
41+
public function testConfigureUsesFieldLabelAndWrapsInTranslatable(): void
4242
{
4343
$configurator = $this->createConfigurator();
4444
$filter = TextFilter::new('name');
@@ -51,37 +51,61 @@ public function testConfigureUsesFieldLabelWhenAvailable(): void
5151

5252
$configurator->configure($filterDto, $fieldDto, $this->entityDto, $adminContext);
5353

54-
$this->assertSame('Product Name', $filterDto->getLabel());
54+
$label = $filterDto->getLabel();
55+
$this->assertInstanceOf(TranslatableMessage::class, $label);
56+
$this->assertSame('Product Name', $label->getMessage());
5557
}
5658

57-
public function testConfigureUsesFieldTranslatableMessageLabel(): void
59+
public function testConfigurePreservesTranslatableInterfaceFromField(): void
5860
{
5961
$configurator = $this->createConfigurator();
6062
$filter = TextFilter::new('name');
6163
$filterDto = $filter->getAsDto();
6264

65+
$translatableLabel = new TranslatableMessage('product.name', [], 'messages');
6366
$field = TextField::new('name');
6467
$fieldDto = $field->getAsDto();
65-
$fieldDto->setLabel(new TranslatableMessage('product.name'));
68+
$fieldDto->setLabel($translatableLabel);
6669

6770
$adminContext = $this->createAdminContext();
6871

6972
$configurator->configure($filterDto, $fieldDto, $this->entityDto, $adminContext);
7073

71-
$this->assertSame('product.name', $filterDto->getLabel());
74+
$this->assertSame($translatableLabel, $filterDto->getLabel());
7275
}
7376

74-
public function testConfigureHumanizesLabelWhenFieldIsNullAndEntityTranslationsDisabled(): void
77+
public function testConfigureHumanizesLabelAndWrapsInTranslatable(): void
7578
{
7679
$configurator = $this->createConfigurator();
7780
$filter = TextFilter::new('productName');
7881
$filterDto = $filter->getAsDto();
7982

80-
$adminContext = $this->createAdminContext(useEntityTranslations: false);
83+
$adminContext = $this->createAdminContext();
8184

8285
$configurator->configure($filterDto, null, $this->entityDto, $adminContext);
8386

84-
$this->assertSame('Product Name', $filterDto->getLabel());
87+
$label = $filterDto->getLabel();
88+
$this->assertInstanceOf(TranslatableMessage::class, $label);
89+
$this->assertSame('Product Name', $label->getMessage());
90+
}
91+
92+
public function testConfigureUsesCorrectTranslationDomain(): void
93+
{
94+
$configurator = $this->createConfigurator();
95+
$filter = TextFilter::new('name');
96+
$filterDto = $filter->getAsDto();
97+
98+
$field = TextField::new('name', 'custom.translation.key');
99+
$fieldDto = $field->getAsDto();
100+
101+
$adminContext = $this->createAdminContext(translationDomain: 'my_domain');
102+
103+
$configurator->configure($filterDto, $fieldDto, $this->entityDto, $adminContext);
104+
105+
$label = $filterDto->getLabel();
106+
$this->assertInstanceOf(TranslatableMessage::class, $label);
107+
$this->assertSame('custom.translation.key', $label->getMessage());
108+
$this->assertSame('my_domain', $label->getDomain());
85109
}
86110

87111
public function testConfigureUsesEntityTranslationWhenFieldIsNullAndEntityTranslationsEnabled(): void
@@ -125,7 +149,7 @@ private function createConfigurator(): CommonConfigurator
125149
return new CommonConfigurator($generator);
126150
}
127151

128-
private function createAdminContext(bool $useEntityTranslations = false): AdminContext
152+
private function createAdminContext(bool $useEntityTranslations = false, string $translationDomain = 'messages'): AdminContext
129153
{
130154
$dashboardDto = Dashboard::new()->getAsDto();
131155
if ($useEntityTranslations) {
@@ -136,7 +160,7 @@ private function createAdminContext(bool $useEntityTranslations = false): AdminC
136160
RequestContext::forTesting(new Request()),
137161
null,
138162
DashboardContext::forTesting($dashboardDto),
139-
I18nContext::forTesting('en', 'ltr')
163+
I18nContext::forTesting('en', 'ltr', $translationDomain)
140164
);
141165
}
142166
}

0 commit comments

Comments
 (0)