Skip to content

Commit 3686363

Browse files
feature #60935 [Translation] Add StaticMessage (VincentLanglet)
This PR was squashed before being merged into the 7.4 branch. Discussion ---------- [Translation] Add `StaticMessage` | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix #... <!-- prefix each issue number with "Fix #"; no need to create an issue if none exists, explain below --> | License | MIT When working with translation, it's "classic" to have library with the following logic: ```php if ($message instanceof Translatable) { $message->trans($translator); } else { $translator->trans($message, domain: 'OpenSourceBundle'); } ``` Then if the user doesn't want the text to be translated, he cannot. A useful and simple way would be to support `new TranslatableMessage($text, domain: false)` but as recommended, it might be better to introduce a special class for this, the StaticMessage Commits ------- b329818 [Translation] Add `StaticMessage`
2 parents 9af1628 + b329818 commit 3686363

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)
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