8000 Added support for PHP files with translation in translation commands · symfony/symfony@9f9b828 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9f9b828

Browse files
committed
Added support for PHP files with translation in translation commands
1 parent c6a2c33 commit 9f9b828

20 files changed

+476
-4
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ CHANGELOG
1616
* Added php ini session options `sid_length` and `sid_bits_per_character`
1717
to the `session` section of the configuration
1818
* Added support for Translator paths, Twig paths in translation commands.
19+
* Added support for PHP files with translations in translation commands.
1920

2021
4.2.0
2122
-----

src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ private function extractMessages(string $locale, array $transPaths): MessageCata
346346
{
347347
$extractedCatalogue = new MessageCatalogue($locale);
348348
foreach ($transPaths as $path) {
349-
if (is_dir($path)) {
349+
if (is_dir($path) || is_file($path)) {
350350
$this->extractor->extract($path, $extractedCatalogue);
351351
}
352352
}

src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
207207
$errorIo->comment('Parsing templates...');
208208
$this->extractor->setPrefix($input->getOption('prefix'));
209209
foreach ($viewsPaths as $path) {
210-
if (is_dir($path)) {
210+
if (is_dir($path) || is_file($path)) {
211211
$this->extractor->extract($path, $extractedCatalogue);
212212
}
213213
F438 }

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
use Symfony\Component\Translation\DependencyInjection\TranslationDumperPass;
5252
use Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass;
5353
use Symfony\Component\Translation\DependencyInjection\TranslatorPass;
54+
use Symfony\Component\Translation\DependencyInjection\TranslatorPathsPass;
5455
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
5556
use Symfony\Component\Validator\DependencyInjection\AddValidatorInitializersPass;
5657
use Symfony\Component\Workflow\DependencyInjection\ValidateWorkflowsPass;
@@ -103,6 +104,7 @@ public function build(ContainerBuilder $container)
103104
// must be registered as late as possible to get access to all Twig paths registered in
104105
// twig.template_iterator definition
105106
$this->addCompilerPassIfExists($container, TranslatorPass::class, PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
107+
$this->addCompilerPassIfExists($container, TranslatorPathsPass::class, PassConfig::TYPE_AFTER_REMOVING);
106108
$container->addCompilerPass(new LoggingTranslatorPass());
107109
$container->addCompilerPass(new AddExpressionLanguageProvidersPass(false));
108110
$this->addCompilerPassIfExists($container, TranslationExtractorPass::class);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;
13+
14+
use Symfony\Contracts\Translation\TranslatorInterface;
15+
16+
class TransController
17+
{
18+
public function index(TranslatorInterface $translator)
19+
{
20+
$translator->trans('hello_from_controller');
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TransDebug;
13+
14+
use Symfony\Contracts\Translation\TranslatorInterface;
15+
16+
class TransConstructArgService
17+
{
18+
private $translator;
19+
20+
public function __construct(TranslatorInterface $translator)
21+
{
22+
$this->translator = $translator;
23+
}
24+
25+
public function hello(): string
26+
{
27+
return $this->translator->trans('hello_from_construct_arg_service');
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TransDebug;
13+
14+
use Symfony\Contracts\Translation\TranslatorInterface;
15+
16+
class TransMethodCallsService
17+
{
18+
private $translator;
19+
20+
public function setTranslator(TranslatorInterface $translator): void
21+
{
22+
$this->translator = $translator;
23+
}
24+
25+
public function hello(): string
26+
{
27+
return $this->translator->trans('hello_from_method_calls_service');
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TransDebug;
13+
14+
use Symfony\Contracts\Translation\TranslatorInterface;
15+
16+
class TransPropertyService
17+
{
18+
/** @var TranslatorInterface */
19+
public $translator;
20+
21+
public function hello(): string
22+
{
23+
return $this->translator->trans('hello_from_property_service');
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TransDebug;
13+
14+
use Psr\Container\ContainerInterface;
15+
use Symfony\Contracts\Service\ServiceSubscriberInterface;
16+
use Symfony\Contracts\Translation\TranslatorInterface;
17+
18+
class TransSubscriberService implements ServiceSubscriberInterface
19+
{
20+
private $container;
21+
22+
public function __construct(ContainerInterface $container)
23+
{
24+
$this->container = $container;
25+
}
26+
27+
public static function getSubscribedServices()
28+
{
29+
return ['translator' => TranslatorInterface::class];
30+
}
31+
32+
public function hello(): string
33+
{
34+
return $this->container->get('translator')->trans('hello_from_subscriber_service');
35+
}
36+
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/TranslationDebugCommandTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,13 @@ public function testDumpAllTrans()
3333
$ret = $tester->execute(['locale' => 'en']);
3434

3535
$this->assertSame(0, $ret, 'Returns 0 in case of success');
36-
$this->assertContains('unused validators This value should be blank.', $tester->getDisplay());
37-
$this->assertContains('unused security Invalid CSRF token.', $tester->getDisplay());
36+
$this->assertContains('missing messages hello_from_construct_arg_service', $tester->getDisplay());
37+
$this->assertContains('missing messages hello_from_subscriber_service', $tester->getDisplay());
38+
$this->assertContains('missing messages hello_from_property_service', $tester->getDisplay());
39+
$this->assertContains('missing messages hello_from_method_calls_service', $tester->getDisplay());
40+
$this->assertContains('missing messages hello_from_controller', $tester->getDisplay());
41+
$this->assertContains('unused validators This value should be blank.', $tester->getDisplay());
42+
$this->assertContains('unused security Invalid CSRF token.', $tester->getDisplay());
3843
}
3944

4045
private function createCommandTester(): CommandTester

0 commit comments

Comments
 (0)
0