Skip to content

Commit 92e35ca

Browse files
Introduce StaticMessage
1 parent 2f180d4 commit 92e35ca

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+
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy