Skip to content

Commit b329818

Browse files
VincentLangletnicolas-grekas
authored andcommitted
[Translation] Add StaticMessage
1 parent 2f180d4 commit b329818

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Deprecate `TranslatableMessage::__toString`
8+
* Add `Symfony\Component\Translation\StaticMessage`
89

910
7.3
1011
---
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
final class StaticMessage implements TranslatableInterface
18+
{
19+
public function __construct(
20+
private string $message,
21+
) {
22+
}
23+
24+
public function getMessage(): string
25+
{
26+
return $this->message;
27+
}
28+
29+
public function trans(TranslatorInterface $translator, ?string $locale = null): string
30+
{
31+
return $this->getMessage();
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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\StaticMessage;
17+
use Symfony\Component\Translation\Translator;
18+
19+
class StaticMessageTest 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 StaticMessage('Symfony is great!');
30+
31+
$this->assertSame('Symfony is great!', $translatable->trans($translator, 'fr'));
32+
}
33+
}

0 commit comments

Comments
 (0)