diff --git a/Resources/doc/usage.rst b/Resources/doc/usage.rst index 681e51a9..bf50aee8 100644 --- a/Resources/doc/usage.rst +++ b/Resources/doc/usage.rst @@ -33,7 +33,7 @@ we use the source message as key: .. code-block :: jinja {# index.html.twig #} - {{ "{0} There is no apples|{1} There is one apple|]1,Inf] There are %count% apples"|transchoice(count) }} + {{ "{0} There is no apples|{1} There is one apple|]1,Inf] There are %count% apples"|trans({'%count%': count}) }} If we translate this to use an abstract key instead, we would get something like the following: @@ -41,7 +41,7 @@ the following: .. code-block :: jinja {# index.html.twig #} - {{ "text.apples_remaining"|transchoice(count) }} + {{ "text.apples_remaining"|trans({'%count%': count}) }} If a translator now sees this abstract key, s/he does not really know what the expected translation should look like. Fortunately, there is a solution for @@ -51,7 +51,7 @@ via the ``desc`` filter: .. code-block :: jinja {# index.html.twig #} - {{ "text.apples_remaining"|transchoice(count) + {{ "text.apples_remaining"|trans({'%count%': count}) |desc("{0} There is no apples|{1} There is one apple|]1,Inf] There are %count% apples") }} As you can see we have basically moved the source translation to the ``desc`` filter. @@ -73,7 +73,7 @@ translations in PHP code, the ``@Desc`` annotation: // Controller.php /** @Desc("{0} There is no apples|{1} There is one apple|]1,Inf] There are %count% apples") */ - $this->translator->transChoice('text_apples_remaining', $count) + $this->translator->trans('text_apples_remaining', ['%count%' => $count]) You can place the doc comment anywhere in the method call chain or directly before the key. @@ -83,11 +83,10 @@ Extracting Translation Messages This bundle automatically supports extracting messages from the following sources: -- Twig: ``trans``, and ``transchoice`` filters as well as ``trans``, - and ``transchoice`` blocks +- Twig: ``trans`` filters as well as ``trans`` blocks - PHP: - - all calls to the ``trans``, or ``transChoice`` method + - all calls to the ``trans`` method - all classes implementing the ``TranslationContainerInterface`` - all form labels that are defined as options to the ->add() method of the FormBuilder - messages declared in validation constraints diff --git a/Tests/Functional/BaseTestCase.php b/Tests/Functional/BaseTestCase.php index 74cf0833..fd8228fc 100644 --- a/Tests/Functional/BaseTestCase.php +++ b/Tests/Functional/BaseTestCase.php @@ -28,20 +28,14 @@ class BaseTestCase extends WebTestCase { protected static function createKernel(array $options = []): KernelInterface { - $isSf5 = version_compare(Kernel::VERSION, '5.0.0') >= 0; - - $default = $isSf5 ? 'default_sf5.yml' : 'default.yml'; - if (version_compare(Kernel::VERSION, '7.0.0') >= 0) { $conf = 'framework_sf7.yaml'; } elseif (version_compare(Kernel::VERSION, '6.0.0') >= 0) { $conf = 'framework_sf6.yml'; - } elseif (version_compare(Kernel::VERSION, '5.0.0') >= 0) { - $conf = 'framework.yml'; } else { $conf = 'framework.yml'; } - return new AppKernel($conf, $options['config'] ?? $default); + return new AppKernel($conf, $options['config'] ?? 'default.yml'); } } diff --git a/Tests/Functional/Controller/ApiControllerTest.php b/Tests/Functional/Controller/ApiControllerTest.php index c3e51652..b58200ba 100644 --- a/Tests/Functional/Controller/ApiControllerTest.php +++ b/Tests/Functional/Controller/ApiControllerTest.php @@ -5,7 +5,6 @@ namespace JMS\TranslationBundle\Tests\Functional\Controller; use JMS\TranslationBundle\Tests\Functional\BaseTestCase; -use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Yaml\Yaml; /** @@ -19,10 +18,8 @@ public function testUpdateAction(): void $client = static::createClient(); $outputDir = $client->getContainer()->getParameter('translation_output_dir'); - $isSf4 = version_compare(Kernel::VERSION, '4.0.0') >= 0; - // Add a file - $file = $isSf4 ? $outputDir . '/navigation.en.yaml' : $outputDir . '/navigation.en.yml'; + $file = $outputDir . '/navigation.en.yaml'; $written = file_put_contents($file, 'main.home: Home'); $this->assertTrue($written !== false && $written > 0); @@ -35,13 +32,7 @@ public function testUpdateAction(): void // Verify that the file has new content $array = Yaml::parse($fileContent); - if ($isSf4) { - $this->assertTrue(isset($array['main.home']), print_r($array, true)); - $this->assertEquals('Away', $array['main.home']); - } else { - $this->assertTrue(isset($array['main'])); - $this->assertTrue(isset($array['main']['home'])); - $this->assertEquals('Away', $array['main']['home']); - } + $this->assertTrue(isset($array['main.home']), print_r($array, true)); + $this->assertEquals('Away', $array['main.home']); } } diff --git a/Tests/Functional/Fixture/TestBundle/Controller/AppleController.php b/Tests/Functional/Fixture/TestBundle/Controller/AppleController.php index b3c799e9..a64d02c7 100644 --- a/Tests/Functional/Fixture/TestBundle/Controller/AppleController.php +++ b/Tests/Functional/Fixture/TestBundle/Controller/AppleController.php @@ -21,10 +21,4 @@ public function viewAction(): Response { return $this->render('@Test/Apple/view.html.twig', ['nbApples' => 5]); } - - #[Route('/view_sf5')] - public function viewsf5Action(): Response - { - return $this->render('@Test/Apple/view_sf5.html.twig', ['nbApples' => 5]); - } } diff --git a/Tests/Functional/Fixture/TestBundle/Resources/views/Apple/view.html.twig b/Tests/Functional/Fixture/TestBundle/Resources/views/Apple/view.html.twig index 3eb6f30b..8d947968 100644 --- a/Tests/Functional/Fixture/TestBundle/Resources/views/Apple/view.html.twig +++ b/Tests/Functional/Fixture/TestBundle/Resources/views/Apple/view.html.twig @@ -1,7 +1,2 @@ -{# This translation does not exist in our messages.en.yml file #} -{{ "text.apples_remaining_does_not_exist"|transchoice(nbApples) - |desc("{0} There is no apple|{1} There is one apple|]1,Inf] There are %count% apples") }} - -{# This translation does exist in our messages.en.yml file #} -{{ "text.apples_remaining"|transchoice(nbApples) +{{ "text.apples_remaining"|trans({'%count%': nbApples}) |desc("Some default message which should never be applied because it instead is coming from the messages file.") }} diff --git a/Tests/Functional/Fixture/TestBundle/Resources/views/Apple/view_sf5.html.twig b/Tests/Functional/Fixture/TestBundle/Resources/views/Apple/view_sf5.html.twig deleted file mode 100644 index 8d947968..00000000 --- a/Tests/Functional/Fixture/TestBundle/Resources/views/Apple/view_sf5.html.twig +++ /dev/null @@ -1,2 +0,0 @@ -{{ "text.apples_remaining"|trans({'%count%': nbApples}) - |desc("Some default message which should never be applied because it instead is coming from the messages file.") }} diff --git a/Tests/Functional/TranslationTest.php b/Tests/Functional/TranslationTest.php index 2fe27d86..8384d037 100644 --- a/Tests/Functional/TranslationTest.php +++ b/Tests/Functional/TranslationTest.php @@ -4,21 +4,15 @@ namespace JMS\TranslationBundle\Tests\Functional; -use Symfony\Component\HttpKernel\Kernel; - class TranslationTest extends BaseTestCase { public function testTranschoiceWhenTranslationNotYetExtracted(): void { - $isSf5 = version_compare(Kernel::VERSION, '5.0.0') >= 0; - - $url = $isSf5 ? '/apples/view_sf5' : '/apples/view'; $client = $this->createClient(); - $client->request('GET', $url); + $client->request('GET', '/apples/view'); $response = $client->getResponse(); $this->assertEquals(200, $response->getStatusCode(), $response->getContent()); - $expected = $isSf5 ? "There are 5 apples\n" : "There are 5 apples\n\nThere are 5 apples\n"; - $this->assertEquals($expected, $response->getContent()); + $this->assertEquals("There are 5 apples\n", $response->getContent()); } } diff --git a/Tests/Functional/config/default.yml b/Tests/Functional/config/default.yml index 749a88e2..15cf2a1d 100644 --- a/Tests/Functional/config/default.yml +++ b/Tests/Functional/config/default.yml @@ -1,4 +1,3 @@ imports: - - { resource: framework.yml } - { resource: twig.yml } - { resource: bundle.yml } diff --git a/Tests/Translation/Extractor/File/Fixture/simple_template.html.twig b/Tests/Translation/Extractor/File/Fixture/simple_template.html.twig index 2d56ebef..09b09664 100644 --- a/Tests/Translation/Extractor/File/Fixture/simple_template.html.twig +++ b/Tests/Translation/Extractor/File/Fixture/simple_template.html.twig @@ -8,14 +8,12 @@ {% trans with {'%name%': 'Johannes'} from "app" %}text.name{% endtrans %} -{% transchoice count with {'%name%': 'Johannes'} from "app" %}text.apple_choice{% endtranschoice %} - {{ "foo.bar" | trans }} -{{ "foo.bar2" | transchoice(5) }} +{{ "foo.bar2" | trans({'%count%': 5}) }} {{ "foo.bar3" | trans({'%name%': 'Johannes'}, "app") }} -{{ "foo.bar4" | transchoice(5, {'%name%': 'Johannes'}, 'app') }} +{{ "foo.bar4" | trans({'%count%': 5, '%name%': 'Johannes'}, 'app') }} -{% trans %}text.default_domain{% endtrans %} \ No newline at end of file +{% trans %}text.default_domain{% endtrans %} diff --git a/Tests/Translation/Extractor/File/Fixture/simple_template_sf5.html.twig b/Tests/Translation/Extractor/File/Fixture/simple_template_sf5.html.twig deleted file mode 100644 index 09b09664..00000000 --- a/Tests/Translation/Extractor/File/Fixture/simple_template_sf5.html.twig +++ /dev/null @@ -1,19 +0,0 @@ -{{ "text.foo"|trans|desc("Foo Bar")|meaning("Some Meaning")}} - -{{ "text.bar"|trans|desc("Foo") }} - -{{ "text.baz"|trans|meaning("Bar") }} - -{{ "text.foo_bar"|trans({}, "foo") }} - -{% trans with {'%name%': 'Johannes'} from "app" %}text.name{% endtrans %} - -{{ "foo.bar" | trans }} - -{{ "foo.bar2" | trans({'%count%': 5}) }} - -{{ "foo.bar3" | trans({'%name%': 'Johannes'}, "app") }} - -{{ "foo.bar4" | trans({'%count%': 5, '%name%': 'Johannes'}, 'app') }} - -{% trans %}text.default_domain{% endtrans %} diff --git a/Tests/Translation/Extractor/File/TwigFileExtractorTest.php b/Tests/Translation/Extractor/File/TwigFileExtractorTest.php index 4f9aa564..3ad4f455 100644 --- a/Tests/Translation/Extractor/File/TwigFileExtractorTest.php +++ b/Tests/Translation/Extractor/File/TwigFileExtractorTest.php @@ -32,7 +32,6 @@ use Symfony\Bridge\Twig\Extension\FormExtension; use Symfony\Bridge\Twig\Extension\RoutingExtension; use Symfony\Bridge\Twig\Extension\TranslationExtension as SymfonyTranslationExtension; -use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Routing\Generator\UrlGenerator; use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\RouteCollection; @@ -43,17 +42,11 @@ class TwigFileExtractorTest extends TestCase { - public function testExtractSimpleTemplateInSF5(): void + public function testExtractSimpleTemplate(): void { - $isSF5 = version_compare(Kernel::VERSION, '5.0.0') >= 0; - - if (! $isSF5) { - $this->markTestSkipped('Test only available with Symfony 5+'); - } - $expected = new MessageCatalogue(); $fileSourceFactory = $this->getFileSourceFactory(); - $fixtureSplInfo = new \SplFileInfo(__DIR__ . '/Fixture/simple_template_sf5.html.twig'); + $fixtureSplInfo = new \SplFileInfo(__DIR__ . '/Fixture/simple_template.html.twig'); $message = new Message('text.foo'); $message->setDesc('Foo Bar'); @@ -99,69 +92,6 @@ public function testExtractSimpleTemplateInSF5(): void $message->addSource($fileSourceFactory->create($fixtureSplInfo, 19)); $expected->add($message); - $this->assertEquals($expected, $this->extract('simple_template_sf5.html.twig')); - } - - public function testExtractSimpleTemplate(): void - { - $isSF5 = version_compare(Kernel::VERSION, '5.0.0') >= 0; - - if ($isSF5) { - $this->markTestSkipped('Test only available with Symfony < 5'); - } - - $expected = new MessageCatalogue(); - $fileSourceFactory = $this->getFileSourceFactory(); - $fixtureSplInfo = new \SplFileInfo(__DIR__ . '/Fixture/simple_template.html.twig'); - - $message = new Message('text.foo'); - $message->setDesc('Foo Bar'); - $message->setMeaning('Some Meaning'); - $message->addSource($fileSourceFactory->create($fixtureSplInfo, 1)); - $expected->add($message); - - $message = new Message('text.bar'); - $message->setDesc('Foo'); - $message->addSource($fileSourceFactory->create($fixtureSplInfo, 3)); - $expected->add($message); - - $message = new Message('text.baz'); - $message->setMeaning('Bar'); - $message->addSource($fileSourceFactory->create($fixtureSplInfo, 5)); - $expected->add($message); - - $message = new Message('text.foo_bar', 'foo'); - $message->addSource($fileSourceFactory->create($fixtureSplInfo, 7)); - $expected->add($message); - - $message = new Message('text.name', 'app'); - $message->addSource($fileSourceFactory->create($fixtureSplInfo, 9)); - $expected->add($message); - - $message = new Message('text.apple_choice', 'app'); - $message->addSource($fileSourceFactory->create($fixtureSplInfo, 11)); - $expected->add($message); - - $message = new Message('foo.bar'); - $message->addSource($fileSourceFactory->create($fixtureSplInfo, 13)); - $expected->add($message); - - $message = new Message('foo.bar2'); - $message->addSource($fileSourceFactory->create($fixtureSplInfo, 15)); - $expected->add($message); - - $message = new Message('foo.bar3', 'app'); - $message->addSource($fileSourceFactory->create($fixtureSplInfo, 17)); - $expected->add($message); - - $message = new Message('foo.bar4', 'app'); - $message->addSource($fileSourceFactory->create($fixtureSplInfo, 19)); - $expected->add($message); - - $message = new Message('text.default_domain'); - $message->addSource($fileSourceFactory->create($fixtureSplInfo, 21)); - $expected->add($message); - $this->assertEquals($expected, $this->extract('simple_template.html.twig')); } @@ -215,7 +145,7 @@ private function extract($file, ?TwigFileExtractor $extractor = null): MessageCa $env = new Environment(new ArrayLoader([])); $env->addExtension(new SymfonyTranslationExtension($translator = new IdentityTranslator())); - $env->addExtension(new TranslationExtension($translator, true)); + $env->addExtension(new TranslationExtension(null, true)); $env->addExtension(new RoutingExtension(new UrlGenerator(new RouteCollection(), new RequestContext()))); $env->addExtension(new FormExtension()); diff --git a/Tests/Translation/Extractor/FileExtractorTest.php b/Tests/Translation/Extractor/FileExtractorTest.php index f30a9edc..a7fe6df0 100644 --- a/Tests/Translation/Extractor/FileExtractorTest.php +++ b/Tests/Translation/Extractor/FileExtractorTest.php @@ -105,7 +105,7 @@ private function extract($directory): MessageCatalogue { $twig = new Environment(new ArrayLoader([])); $twig->addExtension(new SymfonyTranslationExtension($translator = new IdentityTranslator())); - $twig->addExtension(new TranslationExtension($translator)); + $twig->addExtension(new TranslationExtension(null)); $loader = new FilesystemLoader(realpath(__DIR__ . '/Fixture/SimpleTest/Resources/views/')); $twig->setLoader($loader); diff --git a/Tests/Twig/BaseTwigTestCase.php b/Tests/Twig/BaseTwigTestCase.php index c6a84863..9539bb6c 100644 --- a/Tests/Twig/BaseTwigTestCase.php +++ b/Tests/Twig/BaseTwigTestCase.php @@ -36,7 +36,7 @@ final protected function parse($file, $debug = false): string $env = new Environment(new ArrayLoader([])); $env->addExtension(new SymfonyTranslationExtension($translator = new IdentityTranslator())); - $env->addExtension(new TranslationExtension($translator, $debug)); + $env->addExtension(new TranslationExtension(null, $debug)); return $env->compile($env->parse($env->tokenize(new Source($content, 'whatever')))->getNode('body')); } diff --git a/Tests/Twig/Fixture/simple_template.html.twig b/Tests/Twig/Fixture/simple_template.html.twig index ef0bf9e2..09b09664 100644 --- a/Tests/Twig/Fixture/simple_template.html.twig +++ b/Tests/Twig/Fixture/simple_template.html.twig @@ -8,14 +8,12 @@ {% trans with {'%name%': 'Johannes'} from "app" %}text.name{% endtrans %} -{% transchoice count with {'%name%': 'Johannes'} from "app" %}text.apple_choice{% endtranschoice %} - {{ "foo.bar" | trans }} -{{ "foo.bar2" | transchoice(5) }} +{{ "foo.bar2" | trans({'%count%': 5}) }} {{ "foo.bar3" | trans({'%name%': 'Johannes'}, "app") }} -{{ "foo.bar4" | transchoice(5, {'%name%': 'Johannes'}, 'app') }} +{{ "foo.bar4" | trans({'%count%': 5, '%name%': 'Johannes'}, 'app') }} {% trans %}text.default_domain{% endtrans %} diff --git a/Tests/Twig/Fixture/simple_template_compiled.html.twig b/Tests/Twig/Fixture/simple_template_compiled.html.twig index 4a36b580..ab6e9b94 100644 --- a/Tests/Twig/Fixture/simple_template_compiled.html.twig +++ b/Tests/Twig/Fixture/simple_template_compiled.html.twig @@ -8,14 +8,12 @@ {% trans with {'%name%': 'Johannes'} from "app" %}text.name{% endtrans %} -{% transchoice count with {'%name%': 'Johannes'} from "app" %}text.apple_choice{% endtranschoice %} - {{ "foo.bar" | trans }} -{{ "foo.bar2" | transchoice(5) }} +{{ "foo.bar2" | trans({'%count%': 5}) }} {{ "foo.bar3" | trans({'%name%': 'Johannes'}, "app") }} -{{ "foo.bar4" | transchoice(5, {'%name%': 'Johannes'}, 'app') }} +{{ "foo.bar4" | trans({'%count%': 5, '%name%': 'Johannes'}, 'app') }} {% trans %}text.default_domain{% endtrans %} diff --git a/Tests/Twig/Fixture/simple_template_compiled_sf5.html.twig b/Tests/Twig/Fixture/simple_template_compiled_sf5.html.twig deleted file mode 100644 index ab6e9b94..00000000 --- a/Tests/Twig/Fixture/simple_template_compiled_sf5.html.twig +++ /dev/null @@ -1,19 +0,0 @@ -{{ "text.foo"|trans }} - -{{ "text.bar"|trans }} - -{{ "text.baz"|trans }} - -{{ "text.foo_bar"|trans({}, "foo") }} - -{% trans with {'%name%': 'Johannes'} from "app" %}text.name{% endtrans %} - -{{ "foo.bar" | trans }} - -{{ "foo.bar2" | trans({'%count%': 5}) }} - -{{ "foo.bar3" | trans({'%name%': 'Johannes'}, "app") }} - -{{ "foo.bar4" | trans({'%count%': 5, '%name%': 'Johannes'}, 'app') }} - -{% trans %}text.default_domain{% endtrans %} diff --git a/Tests/Twig/Fixture/simple_template_sf5.html.twig b/Tests/Twig/Fixture/simple_template_sf5.html.twig deleted file mode 100644 index 09b09664..00000000 --- a/Tests/Twig/Fixture/simple_template_sf5.html.twig +++ /dev/null @@ -1,19 +0,0 @@ -{{ "text.foo"|trans|desc("Foo Bar")|meaning("Some Meaning")}} - -{{ "text.bar"|trans|desc("Foo") }} - -{{ "text.baz"|trans|meaning("Bar") }} - -{{ "text.foo_bar"|trans({}, "foo") }} - -{% trans with {'%name%': 'Johannes'} from "app" %}text.name{% endtrans %} - -{{ "foo.bar" | trans }} - -{{ "foo.bar2" | trans({'%count%': 5}) }} - -{{ "foo.bar3" | trans({'%name%': 'Johannes'}, "app") }} - -{{ "foo.bar4" | trans({'%count%': 5, '%name%': 'Johannes'}, 'app') }} - -{% trans %}text.default_domain{% endtrans %} diff --git a/Tests/Twig/RemovingNodeVisitorTest.php b/Tests/Twig/RemovingNodeVisitorTest.php index f44b3eb3..e4af8720 100644 --- a/Tests/Twig/RemovingNodeVisitorTest.php +++ b/Tests/Twig/RemovingNodeVisitorTest.php @@ -20,18 +20,12 @@ namespace JMS\TranslationBundle\Tests\Twig; -use Symfony\Component\HttpKernel\Kernel; - class RemovingNodeVisitorTest extends BaseTwigTestCase { public function testRemovalWithSimpleTemplate(): void { - $isSF5 = version_compare(Kernel::VERSION, '5.0.0') >= 0; - - $templateSuffix = $isSF5 ? '_sf5' : ''; - - $expected = $this->parse('simple_template_compiled' . $templateSuffix . '.html.twig'); - $actual = $this->parse('simple_template' . $templateSuffix . '.html.twig'); + $expected = $this->parse('simple_template_compiled.html.twig'); + $actual = $this->parse('simple_template.html.twig'); $this->assertEquals($expected, $actual); } diff --git a/Tests/Twig/TranslationExtensionTest.php b/Tests/Twig/TranslationExtensionTest.php deleted file mode 100644 index 7f02b3bb..00000000 --- a/Tests/Twig/TranslationExtensionTest.php +++ /dev/null @@ -1,34 +0,0 @@ - - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -namespace JMS\TranslationBundle\Tests\Twig; - -use JMS\TranslationBundle\Twig\TranslationExtension; -use PHPUnit\Framework\TestCase; - -final class TranslationExtensionTest extends TestCase -{ - public function testInvalidConstruction(): void - { - $this->expectException(\InvalidArgumentException::class); - - new TranslationExtension(new \StdClass()); - } -} diff --git a/Translation/Extractor/File/DefaultPhpFileExtractor.php b/Translation/Extractor/File/DefaultPhpFileExtractor.php index 4d9287c3..03aa635a 100644 --- a/Translation/Extractor/File/DefaultPhpFileExtractor.php +++ b/Translation/Extractor/File/DefaultPhpFileExtractor.php @@ -87,10 +87,7 @@ class DefaultPhpFileExtractor implements LoggerAwareInterface, FileVisitorInterf * * @var array method => position of the "domain" parameter */ - protected $methodsToExtractFrom = [ - 'trans' => 2, - 'transchoice' => 3, - ]; + protected $methodsToExtractFrom = ['trans' => 2]; public function __construct(DocParser $docParser, FileSourceFactory $fileSourceFactory) { diff --git a/Translation/Extractor/File/TwigFileExtractor.php b/Translation/Extractor/File/TwigFileExtractor.php index a34588e9..ccb3e931 100644 --- a/Translation/Extractor/File/TwigFileExtractor.php +++ b/Translation/Extractor/File/TwigFileExtractor.php @@ -84,7 +84,7 @@ public function enterNode(Node $node, Environment $env): Node } elseif ($node instanceof FilterExpression) { $name = $node->hasAttribute('name') ? $node->getAttribute('name') : $node->getNode('filter')->getAttribute('value'); - if ('trans' === $name || 'transchoice' === $name) { + if ('trans' === $name) { $idNode = $node->getNode('node'); if (!$idNode instanceof ConstantExpression) { return $node; @@ -94,7 +94,7 @@ public function enterNode(Node $node, Environment $env): Node } $id = $idNode->getAttribute('value'); - $index = $name === 'trans' ? 1 : 2; + $index = 1; $domain = 'messages'; $arguments = iterator_to_array($node->getNode('arguments')); if (isset($arguments[$index])) { diff --git a/Twig/DefaultApplyingNodeVisitor.php b/Twig/DefaultApplyingNodeVisitor.php index feddf431..ee9f72fa 100644 --- a/Twig/DefaultApplyingNodeVisitor.php +++ b/Twig/DefaultApplyingNodeVisitor.php @@ -21,7 +21,6 @@ namespace JMS\TranslationBundle\Twig; use JMS\TranslationBundle\Exception\RuntimeException; -use JMS\TranslationBundle\Twig\Node\Transchoice; use Twig\Environment; use Twig\Node\Expression\ArrayExpression; use Twig\Node\Expression\Binary\EqualBinary; @@ -41,10 +40,7 @@ */ class DefaultApplyingNodeVisitor implements NodeVisitorInterface { - /** - * @var bool - */ - private $enabled = true; + private bool $enabled = true; public function setEnabled($bool) { @@ -64,13 +60,13 @@ public function enterNode(Node $node, Environment $env): Node $transNode = $node->getNode('node'); while ( $transNode instanceof FilterExpression - && !in_array($transNode->hasAttribute('name') ? $transNode->getAttribute('name') : $transNode->getNode('filter')->getAttribute('value'), ['trans', 'transchoice'], true) + && !in_array($transNode->hasAttribute('name') ? $transNode->getAttribute('name') : $transNode->getNode('filter')->getAttribute('value'), ['trans'], true) ) { $transNode = $transNode->getNode('node'); } if (!$transNode instanceof FilterExpression) { - throw new RuntimeException(sprintf('The "desc" filter in "%s" line %d must be applied after a "trans", or "transchoice" filter.', $node->getTemplateName(), $node->getTemplateLine())); + throw new RuntimeException(sprintf('The "desc" filter in "%s" line %d must be applied after a "trans" filter.', $node->getTemplateName(), $node->getTemplateLine())); } $wrappingNode = $node->getNode('node'); @@ -79,23 +75,6 @@ public function enterNode(Node $node, Environment $env): Node $arguments = iterator_to_array($node->getNode('arguments')); $defaultNode = $arguments[0]; - // if the |transchoice filter is used, delegate the call to the TranslationExtension - // so that we can catch a possible exception when the default translation has not yet - // been extracted - if ('transchoice' === ($transNode->hasAttribute('name') ? $transNode->getAttribute('name') : $transNode->getNode('filter')->getAttribute('value'))) { - $transchoiceArguments = new ArrayExpression([], $transNode->getTemplateLine()); - $transchoiceArguments->addElement($wrappingNode->getNode('node')); - $transchoiceArguments->addElement($defaultNode); - foreach ($wrappingNode->getNode('arguments') as $arg) { - $transchoiceArguments->addElement($arg); - } - - $transchoiceNode = new Transchoice($transchoiceArguments, $transNode->getTemplateLine()); - $node->setNode('node', $transchoiceNode); - - return $node; - } - $wrappingNodeArguments = iterator_to_array($wrappingNode->getNode('arguments')); // if the |trans filter has replacements parameters diff --git a/Twig/Node/Transchoice.php b/Twig/Node/Transchoice.php deleted file mode 100644 index b83b5cf0..00000000 --- a/Twig/Node/Transchoice.php +++ /dev/null @@ -1,57 +0,0 @@ - - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -namespace JMS\TranslationBundle\Twig\Node; - -use JMS\TranslationBundle\Twig\TranslationExtension; -use Twig\Compiler; -use Twig\Node\Expression\AbstractExpression; -use Twig\Node\Expression\ArrayExpression; - -class Transchoice extends AbstractExpression -{ - public function __construct(ArrayExpression $arguments, $lineno) - { - parent::__construct(['arguments' => $arguments], [], $lineno); - } - - public function compile(Compiler $compiler) - { - $compiler->raw( - sprintf( - '$this->env->getExtension(\'%s\')->%s(', - TranslationExtension::class, - 'transchoiceWithDefault' - ) - ); - - $first = true; - foreach ($this->getNode('arguments')->getKeyValuePairs() as $pair) { - if (!$first) { - $compiler->raw(', '); - } - $first = false; - - $compiler->subcompile($pair['value']); - } - - $compiler->raw(')'); - } -} diff --git a/Twig/TranslationExtension.php b/Twig/TranslationExtension.php index 20ac34d1..c3e29769 100644 --- a/Twig/TranslationExtension.php +++ b/Twig/TranslationExtension.php @@ -25,39 +25,18 @@ * * @author Johannes M. Schmitt */ -use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface; -use Symfony\Contracts\Translation\TranslatorInterface; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; class TranslationExtension extends AbstractExtension { - /** - * @var TranslatorInterface|LegacyTranslatorInterface - */ - private $translator; - - /** - * @var bool - */ - private $debug; + private bool $debug; /** - * @param TranslatorInterface|LegacyTranslatorInterface $translator * @param bool $debug */ public function __construct($translator, $debug = false) { - if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) { - throw new \InvalidArgumentException(sprintf( - 'Argument 1 must be an instance of %s or %s, instance of %s given', - TranslatorInterface::class, - LegacyTranslatorInterface::class, - get_class($translator) - )); - } - - $this->translator = $translator; $this->debug = $debug; } @@ -89,38 +68,6 @@ public function getFilters() ]; } - /** - * @param string $message - * @param string $defaultMessage - * @param int $count - * @param array $arguments - * @param string|null $domain - * @param string|null $locale - * - * @return string - */ - public function transchoiceWithDefault($message, $defaultMessage, $count, array $arguments = [], $domain = null, $locale = null) - { - if (null === $domain) { - $domain = 'messages'; - } - - if (false === $this->translator->getCatalogue($locale)->defines($message, $domain)) { - return $this->doTransChoice($defaultMessage, $count, array_merge(['%count%' => $count], $arguments), $domain, $locale); - } - - return $this->doTransChoice($message, $count, array_merge(['%count%' => $count], $arguments), $domain, $locale); - } - - private function doTransChoice($message, $count, array $arguments, $domain, $locale) - { - if ($this->translator instanceof LegacyTranslatorInterface) { - return $this->translator->transChoice($message, $count, array_merge(['%count%' => $count], $arguments), $domain, $locale); - } - - return $this->translator->trans($message, array_merge(['%count%' => $count], $arguments), $domain, $locale); - } - /** * @param mixed $v *