Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feature #38424 [Translation] Rename Translatable class to Translatabl…
…eMessage (natewiebe13)

This PR was submitted for the master branch but it was merged into the 5.x branch instead.

Discussion
----------

[Translation] Rename Translatable class to TranslatableMessage

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #38342
| License       | MIT
| Doc PR        | TBD

This PR is in anticipation of #38342 passing. We'll need to also update the docs and the blog post here: https://symfony.com/blog/new-in-symfony-5-2-translatable-objects

One extra note not discussed in the issue thread is the increase in the class name length shouldn't really be a problem as `t()` is likely to be used more often.

Commits
-------

0d4e25f Rename Translatable class to TranslatableMessage
  • Loading branch information
fabpot committed Oct 7, 2020
2 parents ca220a1 + 0d4e25f commit b0ae228
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/CHANGELOG.md
Expand Up @@ -7,7 +7,7 @@ CHANGELOG
* added the `impersonation_exit_url()` and `impersonation_exit_path()` functions. They return a URL that allows to switch back to the original user.
* added the `workflow_transition()` function to easily retrieve a specific transition object
* added support for translating `TranslatableInterface` objects
* added the `t()` function to easily create `Translatable` objects
* added the `t()` function to easily create `TranslatableMessage` objects
* Added support for extracting messages from the `t()` function
* Added `field_*` Twig functions to access string values from Form fields

Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Bridge/Twig/Extension/TranslationExtension.php
Expand Up @@ -15,7 +15,7 @@
use Symfony\Bridge\Twig\NodeVisitor\TranslationNodeVisitor;
use Symfony\Bridge\Twig\TokenParser\TransDefaultDomainTokenParser;
use Symfony\Bridge\Twig\TokenParser\TransTokenParser;
use Symfony\Component\Translation\Translatable;
use Symfony\Component\Translation\TranslatableMessage;
use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Contracts\Translation\TranslatorTrait;
Expand Down Expand Up @@ -133,12 +133,12 @@ public function trans($message, $arguments = [], string $domain = null, string $
return $this->getTranslator()->trans($message, $arguments, $domain, $locale);
}

public function createTranslatable(string $message, array $parameters = [], string $domain = null): Translatable
public function createTranslatable(string $message, array $parameters = [], string $domain = null): TranslatableMessage
{
if (!class_exists(Translatable::class)) {
if (!class_exists(TranslatableMessage::class)) {
throw new \LogicException(sprintf('You cannot use the "%s" as the Translation Component is not installed. Try running "composer require symfony/translation".', __CLASS__));
}

return new Translatable($message, $parameters, $domain);
return new TranslatableMessage($message, $parameters, $domain);
}
}
6 changes: 3 additions & 3 deletions src/Symfony/Component/Translation/CHANGELOG.md
Expand Up @@ -6,9 +6,9 @@ CHANGELOG

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

5.1.0
-----
Expand Down
12 changes: 6 additions & 6 deletions src/Symfony/Component/Translation/Extractor/PhpExtractor.php
Expand Up @@ -56,7 +56,7 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
],
[
'new',
'Translatable',
'TranslatableMessage',
'(',
self::MESSAGE_TOKEN,
',',
Expand All @@ -66,7 +66,7 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
],
[
'new',
'Translatable',
'TranslatableMessage',
'(',
self::MESSAGE_TOKEN,
],
Expand All @@ -79,7 +79,7 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
'\\',
'Translation',
'\\',
'Translatable',
'TranslatableMessage',
'(',
self::MESSAGE_TOKEN,
',',
Expand All @@ -89,7 +89,7 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
],
[
'new',
'\Symfony\Component\Translation\Translatable',
'\Symfony\Component\Translation\TranslatableMessage',
'(',
self::MESSAGE_TOKEN,
',',
Expand All @@ -106,13 +106,13 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
'\\',
'Translation',
'\\',
'Translatable',
'TranslatableMessage',
'(',
self::MESSAGE_TOKEN,
],
[
'new',
'\Symfony\Component\Translation\Translatable',
'\Symfony\Component\Translation\TranslatableMessage',
'(',
self::MESSAGE_TOKEN,
],
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Translation/Resources/functions.php
Expand Up @@ -14,7 +14,7 @@
/**
* @author Nate Wiebe <nate@northern.co>
*/
function t(string $message, array $parameters = [], string $domain = null): Translatable
function t(string $message, array $parameters = [], string $domain = null): TranslatableMessage
{
return new Translatable($message, $parameters, $domain);
return new TranslatableMessage($message, $parameters, $domain);
}
14 changes: 7 additions & 7 deletions src/Symfony/Component/Translation/Tests/TranslatableTest.php
Expand Up @@ -13,7 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\Translatable;
use Symfony\Component\Translation\TranslatableMessage;
use Symfony\Component\Translation\Translator;

class TranslatableTest extends TestCase
Expand Down Expand Up @@ -44,14 +44,14 @@ public function testFlattenedTrans($expected, $messages, $translatable)

public function testToString()
{
$this->assertSame('Symfony is great!', (string) new Translatable('Symfony is great!'));
$this->assertSame('Symfony is great!', (string) new TranslatableMessage('Symfony is great!'));
}

public function getTransTests()
{
return [
['Symfony est super !', new Translatable('Symfony is great!', [], ''), 'Symfony est super !', 'fr'],
['Symfony est awesome !', new Translatable('Symfony is %what%!', ['%what%' => 'awesome'], ''), 'Symfony est %what% !', 'fr'],
['Symfony est super !', new TranslatableMessage('Symfony is great!', [], ''), 'Symfony est super !', 'fr'],
['Symfony est awesome !', new TranslatableMessage('Symfony is %what%!', ['%what%' => 'awesome'], ''), 'Symfony est %what% !', 'fr'],
];
}

Expand All @@ -72,9 +72,9 @@ public function getFlattenedTransTests()
];

return [
['Symfony est super!', $messages, new Translatable('symfony.is.great', [], '')],
['Foo Bar Baz', $messages, new Translatable('foo.bar.baz', [], '')],
['Foo Baz', $messages, new Translatable('foo.baz', [], '')],
['Symfony est super!', $messages, new TranslatableMessage('symfony.is.great', [], '')],
['Foo Bar Baz', $messages, new TranslatableMessage('foo.bar.baz', [], '')],
['Foo Baz', $messages, new TranslatableMessage('foo.baz', [], '')],
];
}
}
@@ -1,47 +1,47 @@
This template is used for translation message extraction tests
<?php new \Symfony\Component\Translation\Translatable('translatable-fqn single-quoted key'); ?>
<?php new \Symfony\Component\Translation\Translatable('translatable-fqn double-quoted key'); ?>
<?php new \Symfony\Component\Translation\Translatable(<<<EOF
<?php new \Symfony\Component\Translation\TranslatableMessage('translatable-fqn single-quoted key'); ?>
<?php new \Symfony\Component\Translation\TranslatableMessage('translatable-fqn double-quoted key'); ?>
<?php new \Symfony\Component\Translation\TranslatableMessage(<<<EOF
translatable-fqn heredoc key
EOF
); ?>
<?php new \Symfony\Component\Translation\Translatable(<<<'EOF'
<?php new \Symfony\Component\Translation\TranslatableMessage(<<<'EOF'
translatable-fqn nowdoc key
EOF
); ?>
<?php new \Symfony\Component\Translation\Translatable(
<?php new \Symfony\Component\Translation\TranslatableMessage(
"translatable-fqn double-quoted key with whitespace and escaped \$\n\" sequences"
); ?>
<?php new \Symfony\Component\Translation\Translatable(
<?php new \Symfony\Component\Translation\TranslatableMessage(
'translatable-fqn single-quoted key with whitespace and nonescaped \$\n\' sequences'
); ?>
<?php new \Symfony\Component\Translation\Translatable(<<<EOF
<?php new \Symfony\Component\Translation\TranslatableMessage(<<<EOF
translatable-fqn heredoc key with whitespace and escaped \$\n sequences
EOF
); ?>
<?php new \Symfony\Component\Translation\Translatable(<<<'EOF'
<?php new \Symfony\Component\Translation\TranslatableMessage(<<<'EOF'
translatable-fqn nowdoc key with whitespace and nonescaped \$\n sequences
EOF
); ?>

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

<?php new \Symfony\Component\Translation\Translatable('translatable-fqn concatenated'.' message'.<<<EOF
<?php new \Symfony\Component\Translation\TranslatableMessage('translatable-fqn concatenated'.' message'.<<<EOF
with heredoc
EOF
.<<<'EOF'
and nowdoc
EOF
); ?>

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

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

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

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

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

<?php new \Symfony\Component\Translation\Translatable('translatable-fqn default domain', [], null); ?>
<?php new \Symfony\Component\Translation\TranslatableMessage('translatable-fqn default domain', [], null); ?>
@@ -1,47 +1,47 @@
This template is used for translation message extraction tests
<?php new Translatable('translatable single-quoted key'); ?>
<?php new Translatable('translatable double-quoted key'); ?>
<?php new Translatable(<<<EOF
<?php new TranslatableMessage('translatable single-quoted key'); ?>
<?php new TranslatableMessage('translatable double-quoted key'); ?>
<?php new TranslatableMessage(<<<EOF
translatable heredoc key
EOF
); ?>
<?php new Translatable(<<<'EOF'
<?php new TranslatableMessage(<<<'EOF'
translatable nowdoc key
EOF
); ?>
<?php new Translatable(
<?php new TranslatableMessage(
"translatable double-quoted key with whitespace and escaped \$\n\" sequences"
); ?>
<?php new Translatable(
<?php new TranslatableMessage(
'translatable single-quoted key with whitespace and nonescaped \$\n\' sequences'
); ?>
<?php new Translatable(<<<EOF
<?php new TranslatableMessage(<<<EOF
translatable heredoc key with whitespace and escaped \$\n sequences
EOF
); ?>
<?php new Translatable(<<<'EOF'
<?php new TranslatableMessage(<<<'EOF'
translatable nowdoc key with whitespace and nonescaped \$\n sequences
EOF
); ?>

<?php new Translatable('translatable single-quoted key with "quote mark at the end"'); ?>
<?php new TranslatableMessage('translatable single-quoted key with "quote mark at the end"'); ?>

<?php new Translatable('translatable concatenated'.' message'.<<<EOF
<?php new TranslatableMessage('translatable concatenated'.' message'.<<<EOF
with heredoc
EOF
.<<<'EOF'
and nowdoc
EOF
); ?>

<?php new Translatable('translatable other-domain-test-no-params-short-array', [], 'not_messages'); ?>
<?php new TranslatableMessage('translatable other-domain-test-no-params-short-array', [], 'not_messages'); ?>

<?php new Translatable('translatable other-domain-test-no-params-long-array', [], 'not_messages'); ?>
<?php new TranslatableMessage('translatable other-domain-test-no-params-long-array', [], 'not_messages'); ?>

<?php new Translatable('translatable other-domain-test-params-short-array', ['foo' => 'bar'], 'not_messages'); ?>
<?php new TranslatableMessage('translatable other-domain-test-params-short-array', ['foo' => 'bar'], 'not_messages'); ?>

<?php new Translatable('translatable other-domain-test-params-long-array', ['foo' => 'bar'], 'not_messages'); ?>
<?php new TranslatableMessage('translatable other-domain-test-params-long-array', ['foo' => 'bar'], 'not_messages'); ?>

<?php new Translatable('translatable typecast', ['a' => (int) '123'], 'not_messages'); ?>
<?php new TranslatableMessage('translatable typecast', ['a' => (int) '123'], 'not_messages'); ?>

<?php new Translatable('translatable default domain', [], null); ?>
<?php new TranslatableMessage('translatable default domain', [], null); ?>
Expand Up @@ -17,7 +17,7 @@
/**
* @author Nate Wiebe <nate@northern.co>
*/
class Translatable implements TranslatableInterface
class TranslatableMessage implements TranslatableInterface
{
private $message;
private $parameters;
Expand Down

0 comments on commit b0ae228

Please sign in to comment.