Skip to content
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

Remove legacy code #598

Merged
merged 1 commit into from
Feb 8, 2025
Merged
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
13 changes: 6 additions & 7 deletions Resources/doc/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ 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:

.. 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
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand Down
8 changes: 1 addition & 7 deletions Tests/Functional/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
15 changes: 3 additions & 12 deletions Tests/Functional/Controller/ApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);

Expand All @@ -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']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
Original file line number Diff line number Diff line change
@@ -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.") }}

This file was deleted.

10 changes: 2 additions & 8 deletions Tests/Functional/TranslationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
1 change: 0 additions & 1 deletion Tests/Functional/config/default.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
imports:
- { resource: framework.yml }
- { resource: twig.yml }
- { resource: bundle.yml }
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
{% trans %}text.default_domain{% endtrans %}

This file was deleted.

76 changes: 3 additions & 73 deletions Tests/Translation/Extractor/File/TwigFileExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
Expand Down Expand Up @@ -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'));
}

Expand Down Expand Up @@ -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());

Expand Down
2 changes: 1 addition & 1 deletion Tests/Translation/Extractor/FileExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion Tests/Twig/BaseTwigTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down
6 changes: 2 additions & 4 deletions Tests/Twig/Fixture/simple_template.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
6 changes: 2 additions & 4 deletions Tests/Twig/Fixture/simple_template_compiled.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
19 changes: 0 additions & 19 deletions Tests/Twig/Fixture/simple_template_compiled_sf5.html.twig

This file was deleted.

19 changes: 0 additions & 19 deletions Tests/Twig/Fixture/simple_template_sf5.html.twig

This file was deleted.

Loading
Loading