Skip to content

Commit cdd516b

Browse files
Introduce UntranslatableMessage instead
1 parent 132a1f3 commit cdd516b

File tree

5 files changed

+79
-10
lines changed

5 files changed

+79
-10
lines changed

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CHANGELOG
44
7.4
55
---
66

7-
* Allow to use false as domain for `Symfony\Component\Translation\TranslatableMessage`
7+
* Introduce `Symfony\Component\Translation\UntranslatableMessage`
88

99
7.3
1010
---

src/Symfony/Component/Translation/Tests/TranslatableTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ public static function getTransTests()
5353
['Symfony est super !', new TranslatableMessage('Symfony is great!', [], ''), [
5454
'Symfony is great!' => 'Symfony est super !',
5555
], 'fr'],
56-
['Symfony is great!', new TranslatableMessage('Symfony is great!', [], false), [
57-
'Symfony is great!' => 'Symfony est super !',
58-
], 'fr'],
5956
['Symfony est awesome !', new TranslatableMessage('Symfony is %what%!', ['%what%' => 'awesome'], ''), [
6057
'Symfony is %what%!' => 'Symfony est %what% !',
6158
], 'fr'],
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Translation\Loader\ArrayLoader;
16+
use Symfony\Component\Translation\Translator;
17+
use Symfony\Component\Translation\UntranslatableMessage;
18+
19+
class UntranslatableTest extends TestCase
20+
{
21+
public function testTrans()
22+
{
23+
$translator = new Translator('en');
24+
$translator->addLoader('array', new ArrayLoader());
25+
$translator->addResource('array', [
26+
'Symfony is great!' => 'Symfony est super !',
27+
], 'fr', '');
28+
29+
$translatable = new UntranslatableMessage('Symfony is great!');
30+
31+
$this->assertSame('Symfony is great!', $translatable->trans($translator, 'fr'));
32+
}
33+
34+
public function testToString()
35+
{
36+
$this->assertSame('Symfony is great!', (string) new UntranslatableMessage('Symfony is great!'));
37+
}
38+
}

src/Symfony/Component/Translation/TranslatableMessage.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class TranslatableMessage implements TranslatableInterface
2222
public function __construct(
2323
private string $message,
2424
private array $parameters = [],
25-
private string|false|null $domain = null,
25+
private ?string $domain = null,
2626
) {
2727
}
2828

@@ -41,17 +41,13 @@ public function getParameters(): array
4141
return $this->parameters;
4242
}
4343

44-
public function getDomain(): string|false|null
44+
public function getDomain(): ?string
4545
{
4646
return $this->domain;
4747
}
4848

4949
public function trans(TranslatorInterface $translator, ?string $locale = null): string
5050
{
51-
if (false === $this->getDomain()) {
52-
return $this->getMessage();
53-
}
54-
5551
return $translator->trans($this->getMessage(), array_map(
5652
static fn ($parameter) => $parameter instanceof TranslatableInterface ? $parameter->trans($translator, $locale) : $parameter,
5753
$this->getParameters()
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation;
13+
14+
use Symfony\Contracts\Translation\TranslatableInterface;
15+
use Symfony\Contracts\Translation\TranslatorInterface;
16+
17+
class UntranslatableMessage implements TranslatableInterface
18+
{
19+
public function __construct(
20+
private string $message,
21+
) {
22+
}
23+
24+
public function __toString(): string
25+
{
26+
return $this->getMessage();
27+
}
28+
29+
public function getMessage(): string
30+
{
31+
return $this->message;
32+
}
33+
34+
public function trans(TranslatorInterface $translator, ?string $locale = null): string
35+
{
36+
return $this->getMessage();
37+
}
38+
}

0 commit comments

Comments
 (0)