From b329818eae4c2cc5e70d8cfe2a06050abe9174ef Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sat, 28 Jun 2025 09:12:00 +0200 Subject: [PATCH] [Translation] Add `StaticMessage` --- .../Component/Translation/CHANGELOG.md | 1 + .../Component/Translation/StaticMessage.php | 33 +++++++++++++++++++ .../Translation/Tests/StaticMessageTest.php | 33 +++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 src/Symfony/Component/Translation/StaticMessage.php create mode 100644 src/Symfony/Component/Translation/Tests/StaticMessageTest.php diff --git a/src/Symfony/Component/Translation/CHANGELOG.md b/src/Symfony/Component/Translation/CHANGELOG.md index 1670a537b09b..e913d5953e27 100644 --- a/src/Symfony/Component/Translation/CHANGELOG.md +++ b/src/Symfony/Component/Translation/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG --- * Deprecate `TranslatableMessage::__toString` + * Add `Symfony\Component\Translation\StaticMessage` 7.3 --- diff --git a/src/Symfony/Component/Translation/StaticMessage.php b/src/Symfony/Component/Translation/StaticMessage.php new file mode 100644 index 000000000000..ba1320562ddb --- /dev/null +++ b/src/Symfony/Component/Translation/StaticMessage.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation; + +use Symfony\Contracts\Translation\TranslatableInterface; +use Symfony\Contracts\Translation\TranslatorInterface; + +final class StaticMessage implements TranslatableInterface +{ + public function __construct( + private string $message, + ) { + } + + public function getMessage(): string + { + return $this->message; + } + + public function trans(TranslatorInterface $translator, ?string $locale = null): string + { + return $this->getMessage(); + } +} diff --git a/src/Symfony/Component/Translation/Tests/StaticMessageTest.php b/src/Symfony/Component/Translation/Tests/StaticMessageTest.php new file mode 100644 index 000000000000..aacb29a02440 --- /dev/null +++ b/src/Symfony/Component/Translation/Tests/StaticMessageTest.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Tests; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Translation\Loader\ArrayLoader; +use Symfony\Component\Translation\StaticMessage; +use Symfony\Component\Translation\Translator; + +class StaticMessageTest extends TestCase +{ + public function testTrans() + { + $translator = new Translator('en'); + $translator->addLoader('array', new ArrayLoader()); + $translator->addResource('array', [ + 'Symfony is great!' => 'Symfony est super !', + ], 'fr', ''); + + $translatable = new StaticMessage('Symfony is great!'); + + $this->assertSame('Symfony is great!', $translatable->trans($translator, 'fr')); + } +} 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