Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Translation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Deprecate `TranslatableMessage::__toString`
* Add `Symfony\Component\Translation\StaticMessage`

7.3
---
Expand Down
33 changes: 33 additions & 0 deletions src/Symfony/Component/Translation/StaticMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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();
}
}
33 changes: 33 additions & 0 deletions src/Symfony/Component/Translation/Tests/StaticMessageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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'));
}
}