Skip to content

Commit c633dee

Browse files
[Contracts] move Translatable to symfony/translation-contracts
1 parent 6349a1b commit c633dee

File tree

11 files changed

+85
-120
lines changed

11 files changed

+85
-120
lines changed

src/Symfony/Bridge/Twig/Extension/TranslationExtension.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Bridge\Twig\NodeVisitor\TranslationNodeVisitor;
1616
use Symfony\Bridge\Twig\TokenParser\TransDefaultDomainTokenParser;
1717
use Symfony\Bridge\Twig\TokenParser\TransTokenParser;
18-
use Symfony\Component\Translation\Translatable;
18+
use Symfony\Contracts\Translation\Translatable;
1919
use Symfony\Contracts\Translation\TranslatorInterface;
2020
use Symfony\Contracts\Translation\TranslatorTrait;
2121
use Twig\Extension\AbstractExtension;
@@ -104,7 +104,7 @@ public function getTranslationNodeVisitor(): TranslationNodeVisitor
104104
}
105105

106106
/**
107-
* @param ?string|Translatable $message The message id (may also be an object that can be cast to string)
107+
* @param string|\Stringable|Translatable|null $message
108108
*/
109109
public function trans($message, array $arguments = [], string $domain = null, string $locale = null, int $count = null): string
110110
{
@@ -114,7 +114,7 @@ public function trans($message, array $arguments = [], string $domain = null, st
114114
$message = $message->getMessage();
115115
}
116116

117-
if (null === $message || '' === $message) {
117+
if ('' === $message = (string) $message) {
118118
return '';
119119
}
120120

@@ -125,7 +125,7 @@ public function trans($message, array $arguments = [], string $domain = null, st
125125
return $this->getTranslator()->trans($message, $arguments, $domain, $locale);
126126
}
127127

128-
public function createTranslatable(string $message, array $parameters = [], string $domain = 'messages'): Translatable
128+
public function createTranslatable(string $message, array $parameters = [], string $domain = null): Translatable
129129
{
130130
return new Translatable($message, $parameters, $domain);
131131
}

src/Symfony/Bridge/Twig/composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": ">=7.2.5",
2020
"symfony/polyfill-php80": "^1.15",
21-
"symfony/translation-contracts": "^1.1|^2",
21+
"symfony/translation-contracts": "^2.2",
2222
"twig/twig": "^2.10|^3.0"
2323
},
2424
"require-dev": {
@@ -34,7 +34,7 @@
3434
"symfony/polyfill-intl-icu": "~1.0",
3535
"symfony/property-info": "^4.4|^5.1",
3636
"symfony/routing": "^4.4|^5.0",
37-
"symfony/translation": "^5.2",
37+
"symfony/translation": "^5.0",
3838
"symfony/yaml": "^4.4|^5.0",
3939
"symfony/security-acl": "^2.8|^3.0",
4040
"symfony/security-core": "^4.4|^5.0",
@@ -55,7 +55,7 @@
5555
"symfony/form": "<5.1",
5656
"symfony/http-foundation": "<4.4",
5757
"symfony/http-kernel": "<4.4",
58-
"symfony/translation": "<5.2",
58+
"symfony/translation": "<5.0",
5959
"symfony/workflow": "<5.2"
6060
},
6161
"suggest": {

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ CHANGELOG
66

77
* added support for calling `trans` with ICU formatted messages
88
* added `PseudoLocalizationTranslator`
9-
* added `Translatable` objects that represent a message that can be translated
109
* added the `t()` function to easily create `Translatable` objects
1110
* Added support for extracting messages from `Translatable` objects
1211

src/Symfony/Component/Translation/Extractor/PhpExtractor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
7575
'\\',
7676
'Symfony',
7777
'\\',
78-
'Component',
78+
'Contracts',
7979
'\\',
8080
'Translation',
8181
'\\',
@@ -89,7 +89,7 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
8989
],
9090
[
9191
'new',
92-
'\Symfony\Component\Translation\Translatable',
92+
'\Symfony\Contracts\Translation\Translatable',
9393
'(',
9494
self::MESSAGE_TOKEN,
9595
',',
@@ -102,7 +102,7 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
102102
'\\',
103103
'Symfony',
104104
'\\',
105-
'Component',
105+
'Contracts',
106106
'\\',
107107
'Translation',
108108
'\\',
@@ -112,7 +112,7 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
112112
],
113113
[
114114
'new',
115-
'\Symfony\Component\Translation\Translatable',
115+
'\Symfony\Contracts\Translation\Translatable',
116116
'(',
117117
self::MESSAGE_TOKEN,
118118
],

src/Symfony/Component/Translation/Resources/functions/translatable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
use Symfony\Component\Translation\Translatable;
12+
use Symfony\Contracts\Translation\Translatable;
1313

1414
if (!function_exists('t')) {
1515
/**
1616
* @author Nate Wiebe <nate@northern.co>
1717
*/
18-
function t(string $message, array $parameters = [], string $domain = 'messages'): Translatable
18+
function t(string $message, array $parameters = [], string $domain = null): Translatable
1919
{
2020
return new Translatable($message, $parameters, $domain);
2121
}

src/Symfony/Component/Translation/Tests/TranslatableTest.php

Lines changed: 0 additions & 80 deletions
This file was deleted.
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
11
This template is used for translation message extraction tests
2-
<?php new \Symfony\Component\Translation\Translatable('translatable-fqn single-quoted key'); ?>
3-
<?php new \Symfony\Component\Translation\Translatable('translatable-fqn double-quoted key'); ?>
4-
<?php new \Symfony\Component\Translation\Translatable(<<<EOF
2+
<?php new \Symfony\Contracts\Translation\Translatable('translatable-fqn single-quoted key'); ?>
3+
<?php new \Symfony\Contracts\Translation\Translatable('translatable-fqn double-quoted key'); ?>
4+
<?php new \Symfony\Contracts\Translation\Translatable(<<<EOF
55
translatable-fqn heredoc key
66
EOF
77
); ?>
8-
<?php new \Symfony\Component\Translation\Translatable(<<<'EOF'
8+
<?php new \Symfony\Contracts\Translation\Translatable(<<<'EOF'
99
translatable-fqn nowdoc key
1010
EOF
1111
); ?>
12-
<?php new \Symfony\Component\Translation\Translatable(
12+
<?php new \Symfony\Contracts\Translation\Translatable(
1313
"translatable-fqn double-quoted key with whitespace and escaped \$\n\" sequences"
1414
); ?>
15-
<?php new \Symfony\Component\Translation\Translatable(
15+
<?php new \Symfony\Contracts\Translation\Translatable(
1616
'translatable-fqn single-quoted key with whitespace and nonescaped \$\n\' sequences'
1717
); ?>
18-
<?php new \Symfony\Component\Translation\Translatable(<<<EOF
18+
<?php new \Symfony\Contracts\Translation\Translatable(<<<EOF
1919
translatable-fqn heredoc key with whitespace and escaped \$\n sequences
2020
EOF
2121
); ?>
22-
<?php new \Symfony\Component\Translation\Translatable(<<<'EOF'
22+
<?php new \Symfony\Contracts\Translation\Translatable(<<<'EOF'
2323
translatable-fqn nowdoc key with whitespace and nonescaped \$\n sequences
2424
EOF
2525
); ?>
2626

27-
<?php new \Symfony\Component\Translation\Translatable('translatable-fqn single-quoted key with "quote mark at the end"'); ?>
27+
<?php new \Symfony\Contracts\Translation\Translatable('translatable-fqn single-quoted key with "quote mark at the end"'); ?>
2828

29-
<?php new \Symfony\Component\Translation\Translatable('translatable-fqn concatenated'.' message'.<<<EOF
29+
<?php new \Symfony\Contracts\Translation\Translatable('translatable-fqn concatenated'.' message'.<<<EOF
3030
with heredoc
3131
EOF
3232
.<<<'EOF'
3333
and nowdoc
3434
EOF
3535
); ?>
3636

37-
<?php new \Symfony\Component\Translation\Translatable('translatable-fqn other-domain-test-no-params-short-array', [], 'not_messages'); ?>
37+
<?php new \Symfony\Contracts\Translation\Translatable('translatable-fqn other-domain-test-no-params-short-array', [], 'not_messages'); ?>
3838

39-
<?php new \Symfony\Component\Translation\Translatable('translatable-fqn other-domain-test-no-params-long-array', [], 'not_messages'); ?>
39+
<?php new \Symfony\Contracts\Translation\Translatable('translatable-fqn other-domain-test-no-params-long-array', [], 'not_messages'); ?>
4040

41-
<?php new \Symfony\Component\Translation\Translatable('translatable-fqn other-domain-test-params-short-array', ['foo' => 'bar'], 'not_messages'); ?>
41+
<?php new \Symfony\Contracts\Translation\Translatable('translatable-fqn other-domain-test-params-short-array', ['foo' => 'bar'], 'not_messages'); ?>
4242

43-
<?php new \Symfony\Component\Translation\Translatable('translatable-fqn other-domain-test-params-long-array', ['foo' => 'bar'], 'not_messages'); ?>
43+
<?php new \Symfony\Contracts\Translation\Translatable('translatable-fqn other-domain-test-params-long-array', ['foo' => 'bar'], 'not_messages'); ?>
4444

45-
<?php new \Symfony\Component\Translation\Translatable('translatable-fqn typecast', ['a' => (int) '123'], 'not_messages'); ?>
45+
<?php new \Symfony\Contracts\Translation\Translatable('translatable-fqn typecast', ['a' => (int) '123'], 'not_messages'); ?>
4646

47-
<?php new \Symfony\Component\Translation\Translatable('translatable-fqn default domain', [], null); ?>
47+
<?php new \Symfony\Contracts\Translation\Translatable('translatable-fqn default domain', [], null); ?>

src/Symfony/Component/Translation/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": ">=7.2.5",
2020
"symfony/polyfill-mbstring": "~1.0",
2121
"symfony/polyfill-php80": "^1.15",
22-
"symfony/translation-contracts": "^2"
22+
"symfony/translation-contracts": "^2.2"
2323
},
2424
"require-dev": {
2525
"symfony/config": "^4.4|^5.0",

src/Symfony/Contracts/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
CHANGELOG
22
=========
33

4+
2.2.0
5+
-----
6+
7+
* added `Service\Attribute\Required` attribute for PHP 8
8+
* added `Translation\Translatable` to represent a message that can be translated
9+
* made `Translation\TranslatorTrait::getLocale()` fallback to intl's `Locale::getDefault()` when available
10+
411
2.1.3
512
-----
613

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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\Contracts\Tests\Translation;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Contracts\Translation\Translatable;
16+
use Symfony\Contracts\Translation\TranslatorInterface;
17+
use Symfony\Contracts\Translation\TranslatorTrait;
18+
19+
class TranslatableTest extends TestCase
20+
{
21+
public function testTranslatable()
22+
{
23+
$translatable = new Translatable('Symfony is %what%!', ['%what%' => 'awesome'], 'domain');
24+
25+
$translator = new class() implements TranslatorInterface {
26+
use TranslatorTrait;
27+
};
28+
29+
$this->assertSame('Symfony is %what%!', $translatable->getMessage());
30+
$this->assertSame(['%what%' => 'awesome'], $translatable->getParameters());
31+
$this->assertSame('domain', $translatable->getDomain());
32+
$this->assertSame('Symfony is awesome!', $translatable->trans($translator));
33+
34+
$this->assertNull((new Translatable('Hello'))->getDomain());
35+
}
36+
37+
public function testToString()
38+
{
39+
$this->assertSame('Symfony is great!', (string) new Translatable('Symfony is great!'));
40+
}
41+
}

src/Symfony/Component/Translation/Translatable.php renamed to src/Symfony/Contracts/Translation/Translatable.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,18 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Component\Translation;
13-
14-
use Symfony\Contracts\Translation\TranslatorInterface;
12+
namespace Symfony\Contracts\Translation;
1513

1614
/**
1715
* @author Nate Wiebe <nate@northern.co>
1816
*/
19-
final class Translatable
17+
class Translatable
2018
{
2119
private $message;
2220
private $parameters;
2321
private $domain;
2422

25-
public function __construct(string $message, array $parameters = [], string $domain = 'messages')
23+
public function __construct(string $message, array $parameters = [], string $domain = null)
2624
{
2725
$this->message = $message;
2826
$this->parameters = $parameters;
@@ -31,7 +29,7 @@ public function __construct(string $message, array $parameters = [], string $dom
3129

3230
public function __toString(): string
3331
{
34-
return $this->message;
32+
return $this->getMessage();
3533
}
3634

3735
public function getMessage(): string
@@ -44,13 +42,13 @@ public function getParameters(): array
4442
return $this->parameters;
4543
}
4644

47-
public function getDomain(): string
45+
public function getDomain(): ?string
4846
{
4947
return $this->domain;
5048
}
5149

52-
public static function trans(TranslatorInterface $translator, self $translatable, ?string $locale = null): string
50+
public function trans(TranslatorInterface $translator, string $locale = null): string
5351
{
54-
return $translator->trans($translatable->getMessage(), $translatable->getParameters(), $translatable->getDomain(), $locale);
52+
return $translator->trans($this->getMessage(), $this->getParameters(), $this->getDomain(), $locale);
5553
}
5654
}

0 commit comments

Comments
 (0)