Skip to content

Commit 53592e8

Browse files
committed
refactor: remove legacy code
1 parent b500fc0 commit 53592e8

24 files changed

+34
-377
lines changed

Resources/doc/usage.rst

+6-7
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ we use the source message as key:
3333
.. code-block :: jinja
3434
3535
{# index.html.twig #}
36-
{{ "{0} There is no apples|{1} There is one apple|]1,Inf] There are %count% apples"|transchoice(count) }}
36+
{{ "{0} There is no apples|{1} There is one apple|]1,Inf] There are %count% apples"|trans({'%count%': count}) }}
3737
3838
If we translate this to use an abstract key instead, we would get something like
3939
the following:
4040

4141
.. code-block :: jinja
4242
4343
{# index.html.twig #}
44-
{{ "text.apples_remaining"|transchoice(count) }}
44+
{{ "text.apples_remaining"|trans({'%count%': count}) }}
4545
4646
If a translator now sees this abstract key, s/he does not really know what the
4747
expected translation should look like. Fortunately, there is a solution for
@@ -51,7 +51,7 @@ via the ``desc`` filter:
5151
.. code-block :: jinja
5252
5353
{# index.html.twig #}
54-
{{ "text.apples_remaining"|transchoice(count)
54+
{{ "text.apples_remaining"|trans({'%count%': count})
5555
|desc("{0} There is no apples|{1} There is one apple|]1,Inf] There are %count% apples") }}
5656
5757
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:
7373
7474
// Controller.php
7575
/** @Desc("{0} There is no apples|{1} There is one apple|]1,Inf] There are %count% apples") */
76-
$this->translator->transChoice('text_apples_remaining', $count)
76+
$this->translator->trans('text_apples_remaining', ['%count%' => $count])
7777
7878
You can place the doc comment anywhere in the method call chain or directly
7979
before the key.
@@ -83,11 +83,10 @@ Extracting Translation Messages
8383
This bundle automatically supports extracting messages from the following
8484
sources:
8585

86-
- Twig: ``trans``, and ``transchoice`` filters as well as ``trans``,
87-
and ``transchoice`` blocks
86+
- Twig: ``trans`` filters as well as ``trans`` blocks
8887
- PHP:
8988

90-
- all calls to the ``trans``, or ``transChoice`` method
89+
- all calls to the ``trans`` method
9190
- all classes implementing the ``TranslationContainerInterface``
9291
- all form labels that are defined as options to the ->add() method of the FormBuilder
9392
- messages declared in validation constraints

Tests/Functional/BaseTestCase.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,12 @@ class BaseTestCase extends WebTestCase
2828
{
2929
protected static function createKernel(array $options = []): KernelInterface
3030
{
31-
$isSf5 = version_compare(Kernel::VERSION, '5.0.0') >= 0;
32-
33-
$default = $isSf5 ? 'default_sf5.yml' : 'default.yml';
34-
3531
if (version_compare(Kernel::VERSION, '6.0.0') >= 0) {
3632
$conf = 'framework_sf6.yml';
37-
} elseif (version_compare(Kernel::VERSION, '5.0.0') >= 0) {
38-
$conf = 'framework.yml';
3933
} else {
4034
$conf = 'framework.yml';
4135
}
4236

43-
return new AppKernel($conf, $options['config'] ?? $default);
37+
return new AppKernel($conf, $options['config'] ?? 'default.yml');
4438
}
4539
}

Tests/Functional/Controller/ApiControllerTest.php

+3-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace JMS\TranslationBundle\Tests\Functional\Controller;
66

77
use JMS\TranslationBundle\Tests\Functional\BaseTestCase;
8-
use Symfony\Component\HttpKernel\Kernel;
98
use Symfony\Component\Yaml\Yaml;
109

1110
/**
@@ -19,10 +18,8 @@ public function testUpdateAction(): void
1918
$client = static::createClient(['config' => 'test_updating_translations.yml']);
2019
$outputDir = $client->getContainer()->getParameter('translation_output_dir');
2120

22-
$isSf4 = version_compare(Kernel::VERSION, '4.0.0') >= 0;
23-
2421
// Add a file
25-
$file = $isSf4 ? $outputDir . '/navigation.en.yaml' : $outputDir . '/navigation.en.yml';
22+
$file = $outputDir . '/navigation.en.yaml';
2623
$written = file_put_contents($file, 'main.home: Home');
2724
$this->assertTrue($written !== false && $written > 0);
2825

@@ -35,13 +32,7 @@ public function testUpdateAction(): void
3532
// Verify that the file has new content
3633
$array = Yaml::parse($fileContent);
3734

38-
if ($isSf4) {
39-
$this->assertTrue(isset($array['main.home']), print_r($array, true));
40-
$this->assertEquals('Away', $array['main.home']);
41-
} else {
42-
$this->assertTrue(isset($array['main']));
43-
$this->assertTrue(isset($array['main']['home']));
44-
$this->assertEquals('Away', $array['main']['home']);
45-
}
35+
$this->assertTrue(isset($array['main.home']), print_r($array, true));
36+
$this->assertEquals('Away', $array['main.home']);
4637
}
4738
}

Tests/Functional/Fixture/TestBundle/Controller/AppleController.php

-6
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,4 @@ public function viewAction(): Response
2121
{
2222
return $this->render('@Test/Apple/view.html.twig', ['nbApples' => 5]);
2323
}
24-
25-
#[Route('/view_sf5')]
26-
public function viewsf5Action(): Response
27-
{
28-
return $this->render('@Test/Apple/view_sf5.html.twig', ['nbApples' => 5]);
29-
}
3024
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
1-
{# This translation does not exist in our messages.en.yml file #}
2-
{{ "text.apples_remaining_does_not_exist"|transchoice(nbApples)
3-
|desc("{0} There is no apple|{1} There is one apple|]1,Inf] There are %count% apples") }}
4-
5-
{# This translation does exist in our messages.en.yml file #}
6-
{{ "text.apples_remaining"|transchoice(nbApples)
1+
{{ "text.apples_remaining"|trans({'%count%': nbApples})
72
|desc("Some default message which should never be applied because it instead is coming from the messages file.") }}

Tests/Functional/Fixture/TestBundle/Resources/views/Apple/view_sf5.html.twig

-2
This file was deleted.

Tests/Functional/TranslationTest.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,15 @@
44

55
namespace JMS\TranslationBundle\Tests\Functional;
66

7-
use Symfony\Component\HttpKernel\Kernel;
8-
97
class TranslationTest extends BaseTestCase
108
{
119
public function testTranschoiceWhenTranslationNotYetExtracted(): void
1210
{
13-
$isSf5 = version_compare(Kernel::VERSION, '5.0.0') >= 0;
14-
15-
$url = $isSf5 ? '/apples/view_sf5' : '/apples/view';
1611
$client = $this->createClient();
17-
$client->request('GET', $url);
12+
$client->request('GET', '/apples/view');
1813
$response = $client->getResponse();
1914

2015
$this->assertEquals(200, $response->getStatusCode(), $response->getContent());
21-
$expected = $isSf5 ? "There are 5 apples\n" : "There are 5 apples\n\nThere are 5 apples\n";
22-
$this->assertEquals($expected, $response->getContent());
16+
$this->assertEquals("There are 5 apples\n", $response->getContent());
2317
}
2418
}

Tests/Functional/config/default.yml

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
imports:
2-
- { resource: framework.yml }
32
- { resource: twig.yml }
43
- { resource: bundle.yml }

Tests/Translation/Extractor/File/Fixture/simple_template.html.twig

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88

99
{% trans with {'%name%': 'Johannes'} from "app" %}text.name{% endtrans %}
1010

11-
{% transchoice count with {'%name%': 'Johannes'} from "app" %}text.apple_choice{% endtranschoice %}
12-
1311
{{ "foo.bar" | trans }}
1412

15-
{{ "foo.bar2" | transchoice(5) }}
13+
{{ "foo.bar2" | trans({'%count%': 5}) }}
1614

1715
{{ "foo.bar3" | trans({'%name%': 'Johannes'}, "app") }}
1816

19-
{{ "foo.bar4" | transchoice(5, {'%name%': 'Johannes'}, 'app') }}
17+
{{ "foo.bar4" | trans({'%count%': 5, '%name%': 'Johannes'}, 'app') }}
2018

21-
{% trans %}text.default_domain{% endtrans %}
19+
{% trans %}text.default_domain{% endtrans %}

Tests/Translation/Extractor/File/Fixture/simple_template_sf5.html.twig

-19
This file was deleted.

Tests/Translation/Extractor/File/TwigFileExtractorTest.php

+3-73
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
use Symfony\Bridge\Twig\Extension\FormExtension;
3333
use Symfony\Bridge\Twig\Extension\RoutingExtension;
3434
use Symfony\Bridge\Twig\Extension\TranslationExtension as SymfonyTranslationExtension;
35-
use Symfony\Component\HttpKernel\Kernel;
3635
use Symfony\Component\Routing\Generator\UrlGenerator;
3736
use Symfony\Component\Routing\RequestContext;
3837
use Symfony\Component\Routing\RouteCollection;
@@ -43,17 +42,11 @@
4342

4443
class TwigFileExtractorTest extends TestCase
4544
{
46-
public function testExtractSimpleTemplateInSF5(): void
45+
public function testExtractSimpleTemplate(): void
4746
{
48-
$isSF5 = version_compare(Kernel::VERSION, '5.0.0') >= 0;
49-
50-
if (! $isSF5) {
51-
$this->markTestSkipped('Test only available with Symfony 5+');
52-
}
53-
5447
$expected = new MessageCatalogue();
5548
$fileSourceFactory = $this->getFileSourceFactory();
56-
$fixtureSplInfo = new \SplFileInfo(__DIR__ . '/Fixture/simple_template_sf5.html.twig');
49+
$fixtureSplInfo = new \SplFileInfo(__DIR__ . '/Fixture/simple_template.html.twig');
5750

5851
$message = new Message('text.foo');
5952
$message->setDesc('Foo Bar');
@@ -99,69 +92,6 @@ public function testExtractSimpleTemplateInSF5(): void
9992
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 19));
10093
$expected->add($message);
10194

102-
$this->assertEquals($expected, $this->extract('simple_template_sf5.html.twig'));
103-
}
104-
105-
public function testExtractSimpleTemplate(): void
106-
{
107-
$isSF5 = version_compare(Kernel::VERSION, '5.0.0') >= 0;
108-
109-
if ($isSF5) {
110-
$this->markTestSkipped('Test only available with Symfony < 5');
111-
}
112-
113-
$expected = new MessageCatalogue();
114-
$fileSourceFactory = $this->getFileSourceFactory();
115-
$fixtureSplInfo = new \SplFileInfo(__DIR__ . '/Fixture/simple_template.html.twig');
116-
117-
$message = new Message('text.foo');
118-
$message->setDesc('Foo Bar');
119-
$message->setMeaning('Some Meaning');
120-
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 1));
121-
$expected->add($message);
122-
123-
$message = new Message('text.bar');
124-
$message->setDesc('Foo');
125-
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 3));
126-
$expected->add($message);
127-
128-
$message = new Message('text.baz');
129-
$message->setMeaning('Bar');
130-
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 5));
131-
$expected->add($message);
132-
133-
$message = new Message('text.foo_bar', 'foo');
134-
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 7));
135-
$expected->add($message);
136-
137-
$message = new Message('text.name', 'app');
138-
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 9));
139-
$expected->add($message);
140-
141-
$message = new Message('text.apple_choice', 'app');
142-
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 11));
143-
$expected->add($message);
144-
145-
$message = new Message('foo.bar');
146-
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 13));
147-
$expected->add($message);
148-
149-
$message = new Message('foo.bar2');
150-
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 15));
151-
$expected->add($message);
152-
153-
$message = new Message('foo.bar3', 'app');
154-
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 17));
155-
$expected->add($message);
156-
157-
$message = new Message('foo.bar4', 'app');
158-
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 19));
159-
$expected->add($message);
160-
161-
$message = new Message('text.default_domain');
162-
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 21));
163-
$expected->add($message);
164-
16595
$this->assertEquals($expected, $this->extract('simple_template.html.twig'));
16696
}
16797

@@ -215,7 +145,7 @@ private function extract($file, ?TwigFileExtractor $extractor = null): MessageCa
215145

216146
$env = new Environment(new ArrayLoader([]));
217147
$env->addExtension(new SymfonyTranslationExtension($translator = new IdentityTranslator()));
218-
$env->addExtension(new TranslationExtension($translator, true));
148+
$env->addExtension(new TranslationExtension(null, true));
219149
$env->addExtension(new RoutingExtension(new UrlGenerator(new RouteCollection(), new RequestContext())));
220150
$env->addExtension(new FormExtension());
221151

Tests/Translation/Extractor/FileExtractorTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private function extract($directory): MessageCatalogue
105105
{
106106
$twig = new Environment(new ArrayLoader([]));
107107
$twig->addExtension(new SymfonyTranslationExtension($translator = new IdentityTranslator()));
108-
$twig->addExtension(new TranslationExtension($translator));
108+
$twig->addExtension(new TranslationExtension(null));
109109
$loader = new FilesystemLoader(realpath(__DIR__ . '/Fixture/SimpleTest/Resources/views/'));
110110
$twig->setLoader($loader);
111111

Tests/Twig/BaseTwigTestCase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ final protected function parse($file, $debug = false): string
3636

3737
$env = new Environment(new ArrayLoader([]));
3838
$env->addExtension(new SymfonyTranslationExtension($translator = new IdentityTranslator()));
39-
$env->addExtension(new TranslationExtension($translator, $debug));
39+
$env->addExtension(new TranslationExtension(null, $debug));
4040

4141
return $env->compile($env->parse($env->tokenize(new Source($content, 'whatever')))->getNode('body'));
4242
}

Tests/Twig/Fixture/simple_template.html.twig

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88

99
{% trans with {'%name%': 'Johannes'} from "app" %}text.name{% endtrans %}
1010

11-
{% transchoice count with {'%name%': 'Johannes'} from "app" %}text.apple_choice{% endtranschoice %}
12-
1311
{{ "foo.bar" | trans }}
1412

15-
{{ "foo.bar2" | transchoice(5) }}
13+
{{ "foo.bar2" | trans({'%count%': 5}) }}
1614

1715
{{ "foo.bar3" | trans({'%name%': 'Johannes'}, "app") }}
1816

19-
{{ "foo.bar4" | transchoice(5, {'%name%': 'Johannes'}, 'app') }}
17+
{{ "foo.bar4" | trans({'%count%': 5, '%name%': 'Johannes'}, 'app') }}
2018

2119
{% trans %}text.default_domain{% endtrans %}

Tests/Twig/Fixture/simple_template_compiled.html.twig

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88

99
{% trans with {'%name%': 'Johannes'} from "app" %}text.name{% endtrans %}
1010

11-
{% transchoice count with {'%name%': 'Johannes'} from "app" %}text.apple_choice{% endtranschoice %}
12-
1311
{{ "foo.bar" | trans }}
1412

15-
{{ "foo.bar2" | transchoice(5) }}
13+
{{ "foo.bar2" | trans({'%count%': 5}) }}
1614

1715
{{ "foo.bar3" | trans({'%name%': 'Johannes'}, "app") }}
1816

19-
{{ "foo.bar4" | transchoice(5, {'%name%': 'Johannes'}, 'app') }}
17+
{{ "foo.bar4" | trans({'%count%': 5, '%name%': 'Johannes'}, 'app') }}
2018

2119
{% trans %}text.default_domain{% endtrans %}

Tests/Twig/Fixture/simple_template_compiled_sf5.html.twig

-19
This file was deleted.

Tests/Twig/Fixture/simple_template_sf5.html.twig

-19
This file was deleted.

0 commit comments

Comments
 (0)