Skip to content

[Contracts] move Translatable to symfony/translation-contracts #38328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
10 changes: 5 additions & 5 deletions src/Symfony/Bridge/Twig/Extension/TranslationExtension.php
Original file line number Diff line number Diff line change
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\Contracts\Translation\Translatable;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Contracts\Translation\TranslatorTrait;
use Twig\Extension\AbstractExtension;
Expand Down Expand Up @@ -104,17 +104,17 @@ public function getTranslationNodeVisitor(): TranslationNodeVisitor
}

/**
* @param ?string|Translatable $message The message id (may also be an object that can be cast to string)
* @param string|\Stringable|Translatable|null $message
*/
public function trans($message, array $arguments = [], string $domain = null, string $locale = null, int $count = null): string
{
if ($message instanceof Translatable) {
$arguments += $message->getParameters();
$domain = $message->getDomain();
$domain = $message->getDomain() ?? $domain;
$message = $message->getMessage();
}

if (null === $message || '' === $message) {
if ('' === $message = (string) $message) {
return '';
}

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

public function createTranslatable(string $message, array $parameters = [], string $domain = 'messages'): Translatable
public function createTranslatable(string $message, array $parameters = [], string $domain = null): Translatable
{
return new Translatable($message, $parameters, $domain);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Bridge/Twig/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=7.2.5",
"symfony/polyfill-php80": "^1.15",
"symfony/translation-contracts": "^1.1|^2",
"symfony/translation-contracts": "^2.3",
"twig/twig": "^2.10|^3.0"
},
"require-dev": {
Expand All @@ -34,7 +34,7 @@
"symfony/polyfill-intl-icu": "~1.0",
"symfony/property-info": "^4.4|^5.1",
"symfony/routing": "^4.4|^5.0",
"symfony/translation": "^5.2",
"symfony/translation": "^5.0",
"symfony/yaml": "^4.4|^5.0",
"symfony/security-acl": "^2.8|^3.0",
"symfony/security-core": "^4.4|^5.0",
Expand All @@ -55,7 +55,7 @@
"symfony/form": "<5.1",
"symfony/http-foundation": "<4.4",
"symfony/http-kernel": "<4.4",
"symfony/translation": "<5.2",
"symfony/translation": "<5.0",
"symfony/workflow": "<5.2"
},
"suggest": {
Expand Down
1 change: 0 additions & 1 deletion src/Symfony/Component/Translation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ 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

Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Translation/Extractor/PhpExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
'\\',
'Symfony',
'\\',
'Component',
'Contracts',
'\\',
'Translation',
'\\',
Expand All @@ -89,7 +89,7 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
],
[
'new',
'\Symfony\Component\Translation\Translatable',
'\Symfony\Contracts\Translation\Translatable',
'(',
self::MESSAGE_TOKEN,
',',
Expand All @@ -102,7 +102,7 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
'\\',
'Symfony',
'\\',
'Component',
'Contracts',
'\\',
'Translation',
'\\',
Expand All @@ -112,7 +112,7 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
],
[
'new',
'\Symfony\Component\Translation\Translatable',
'\Symfony\Contracts\Translation\Translatable',
'(',
self::MESSAGE_TOKEN,
],
Expand Down
22 changes: 22 additions & 0 deletions src/Symfony/Component/Translation/Resources/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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\Translatable;

/**
* @author Nate Wiebe <nate@northern.co>
*/
function t(string $message, array $parameters = [], string $domain = null): Translatable
{
return new Translatable($message, $parameters, $domain);
}

This file was deleted.

80 changes: 0 additions & 80 deletions src/Symfony/Component/Translation/Tests/TranslatableTest.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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\Contracts\Translation\Translatable('translatable-fqn single-quoted key'); ?>
<?php new \Symfony\Contracts\Translation\Translatable('translatable-fqn double-quoted key'); ?>
<?php new \Symfony\Contracts\Translation\Translatable(<<<EOF
translatable-fqn heredoc key
EOF
); ?>
<?php new \Symfony\Component\Translation\Translatable(<<<'EOF'
<?php new \Symfony\Contracts\Translation\Translatable(<<<'EOF'
translatable-fqn nowdoc key
EOF
); ?>
<?php new \Symfony\Component\Translation\Translatable(
<?php new \Symfony\Contracts\Translation\Translatable(
"translatable-fqn double-quoted key with whitespace and escaped \$\n\" sequences"
); ?>
<?php new \Symfony\Component\Translation\Translatable(
<?php new \Symfony\Contracts\Translation\Translatable(
'translatable-fqn single-quoted key with whitespace and nonescaped \$\n\' sequences'
); ?>
<?php new \Symfony\Component\Translation\Translatable(<<<EOF
<?php new \Symfony\Contracts\Translation\Translatable(<<<EOF
translatable-fqn heredoc key with whitespace and escaped \$\n sequences
EOF
); ?>
<?php new \Symfony\Component\Translation\Translatable(<<<'EOF'
<?php new \Symfony\Contracts\Translation\Translatable(<<<'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\Contracts\Translation\Translatable('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\Contracts\Translation\Translatable('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\Contracts\Translation\Translatable('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\Contracts\Translation\Translatable('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\Contracts\Translation\Translatable('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\Contracts\Translation\Translatable('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\Contracts\Translation\Translatable('translatable-fqn typecast', ['a' => (int) '123'], 'not_messages'); ?>

<?php new \Symfony\Component\Translation\Translatable('translatable-fqn default domain', [], null); ?>
<?php new \Symfony\Contracts\Translation\Translatable('translatable-fqn default domain', [], null); ?>
4 changes: 2 additions & 2 deletions src/Symfony/Component/Translation/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"php": ">=7.2.5",
"symfony/polyfill-mbstring": "~1.0",
"symfony/polyfill-php80": "^1.15",
"symfony/translation-contracts": "^2"
"symfony/translation-contracts": "^2.3"
},
"require-dev": {
"symfony/config": "^4.4|^5.0",
Expand Down Expand Up @@ -48,7 +48,7 @@
"psr/log-implementation": "To use logging capability in translator"
},
"autoload": {
"files": [ "Resources/functions/translatable.php" ],
"files": [ "Resources/functions.php" ],
"psr-4": { "Symfony\\Component\\Translation\\": "" },
"exclude-from-classmap": [
"/Tests/"
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Contracts/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
CHANGELOG
=========

2.3.0
-----

* added `Translation\Translatable` to represent a message that can be translated
* made `Translation\TranslatorTrait::getLocale()` fallback to intl's `Locale::getDefault()` when available

2.2.0
-----

* added `Service\Attribute\Required` attribute for PHP 8

2.1.3
-----

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Contracts/Cache/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-master": "2.2-dev"
"dev-master": "2.3-dev"
},
"thanks": {
"name": "symfony/contracts",
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Contracts/Deprecation/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-master": "2.2-dev"
"dev-master": "2.3-dev"
},
"thanks": {
"name": "symfony/contracts",
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Contracts/EventDispatcher/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-master": "2.2-dev"
"dev-master": "2.3-dev"
},
"thanks": {
"name": "symfony/contracts",
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Contracts/HttpClient/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-master": "2.2-dev"
"dev-master": "2.3-dev"
},
"thanks": {
"name": "symfony/contracts",
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Contracts/Service/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-master": "2.2-dev"
"dev-master": "2.3-dev"
},
"thanks": {
"name": "symfony/contracts",
Expand Down
Loading