From 8f49c64a041cfd3348b7ef6446a4c564786e431b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jib=C3=A9=20Barth?= Date: Sat, 4 Nov 2023 19:32:01 +0100 Subject: [PATCH 1/2] [Translation] add bundles trans dirs into commands --- .../FrameworkExtension.php | 2 +- .../Resources/translations/messages.en.yaml | 0 .../FrameworkExtensionTestCase.php | 52 ++++++++++++++++++- 3 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/TestBundle/Resources/translations/messages.en.yaml diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index da60eeabb41c9..44cf1b99134b8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -1342,7 +1342,7 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder $defaultDir = $container->getParameterBag()->resolveValue($config['default_path']); foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) { if ($container->fileExists($dir = $bundle['path'].'/Resources/translations') || $container->fileExists($dir = $bundle['path'].'/translations')) { - $dirs[] = $dir; + $dirs[] = $transPaths[] = $dir; } else { $nonExistingDirs[] = $dir; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/TestBundle/Resources/translations/messages.en.yaml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/TestBundle/Resources/translations/messages.en.yaml new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php index 52a6ad6a4840f..2626a089fcfb6 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php @@ -1072,7 +1072,19 @@ public function testMessengerWithDisabledResetOnMessage() public function testTranslator() { - $container = $this->createContainerFromFile('full'); + require_once __DIR__.'/Fixtures/TestBundle/TestBundle.php'; + + $container = $this->createContainerFromFile('full', [ + 'kernel.bundles' => [ + 'FrameworkBundle' => 'Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle', + 'TestBundle' => 'Symfony\\Bundle\\FrameworkBundle\\Tests\\TestBundle', + ], + 'kernel.bundles_metadata' => [ + 'FrameworkBundle' => ['namespace' => 'Symfony\\Bundle\\FrameworkBundle', 'path' => __DIR__.'/../..'], + 'TestBundle' => ['namespace' => 'Symfony\\Bundle\\FrameworkBundle\\Tests', 'path' => __DIR__.'/Fixtures/TestBundle'], + ], + ]); + $this->assertTrue($container->hasDefinition('translator.default'), '->registerTranslatorConfiguration() loads translation.php'); $this->assertEquals('translator.default', (string) $container->getAlias('translator'), '->registerTranslatorConfiguration() redefines translator service from identity to real translator'); $options = $container->getDefinition('translator.default')->getArgument(4); @@ -1115,6 +1127,11 @@ public function testTranslator() '->registerTranslatorConfiguration() finds translation resources with dots in domain' ); $this->assertContains(strtr(__DIR__.'/translations/security.en.yaml', '/', \DIRECTORY_SEPARATOR), $files); + $this->assertContains( + strtr(__DIR__.'/Fixtures/TestBundle/Resources/translations/messages.en.yaml', '/', \DIRECTORY_SEPARATOR), + $files, + '->registerTranslatorConfiguration() finds translation resources with in bundles' + ); $positionOverridingTranslationFile = array_search(strtr(realpath(__DIR__.'/translations/security.en.yaml'), '/', \DIRECTORY_SEPARATOR), $files); @@ -1140,7 +1157,38 @@ function ($directory) { $this->assertNotEmpty($nonExistingDirectories, 'FrameworkBundle should pass non existing directories to Translator'); - $this->assertSame('Fixtures/translations', $options['cache_vary']['scanned_directories'][3]); + $this->assertSame('Fixtures/translations', $options['cache_vary']['scanned_directories'][4]); + + $transPaths = $container->getDefinition('console.command.translation_extract')->getArgument(6); + + $ref = new \ReflectionClass(Validation::class); + $this->assertContains( + strtr(\dirname($ref->getFileName()).'/Resources/translations', '/', \DIRECTORY_SEPARATOR), + $transPaths, + '->registerTranslatorConfiguration() finds Validator translation resources to extract' + ); + $ref = new \ReflectionClass(Form::class); + $this->assertContains( + strtr(\dirname($ref->getFileName()).'/Resources/translations', '/', \DIRECTORY_SEPARATOR), + $transPaths, + '->registerTranslatorConfiguration() finds Form translation resources to extract' + ); + $ref = new \ReflectionClass(Security::class); + $this->assertContains( + strtr(\dirname($ref->getFileName()).'/Resources/translations', '/', \DIRECTORY_SEPARATOR), + $transPaths, + '->registerTranslatorConfiguration() finds Security translation resources to extract' + ); + $this->assertContains( + strtr(__DIR__.'/Fixtures/translations', '/', \DIRECTORY_SEPARATOR), + $transPaths, + '->registerTranslatorConfiguration() finds translation resources in custom paths to extract' + ); + $this->assertContains( + strtr(__DIR__.'/Fixtures/TestBundle/Resources/translations', '/', \DIRECTORY_SEPARATOR), + $transPaths, + '->registerTranslatorConfiguration() finds translation resources with in bundles to extract' + ); } public function testTranslatorMultipleFallbacks() From 9d7c1bd4e30bcd8497c6f76d2e0d5b454f76a177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jib=C3=A9=20Barth?= Date: Sat, 4 Nov 2023 21:19:42 +0100 Subject: [PATCH 2/2] Update src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php Co-authored-by: Oskar Stark --- .../Tests/DependencyInjection/FrameworkExtensionTestCase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php index 2626a089fcfb6..215b318d5e44d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php @@ -1130,7 +1130,7 @@ public function testTranslator() $this->assertContains( strtr(__DIR__.'/Fixtures/TestBundle/Resources/translations/messages.en.yaml', '/', \DIRECTORY_SEPARATOR), $files, - '->registerTranslatorConfiguration() finds translation resources with in bundles' + '->registerTranslatorConfiguration() finds translation resources within bundles' ); $positionOverridingTranslationFile = array_search(strtr(realpath(__DIR__.'/translations/security.en.yaml'), '/', \DIRECTORY_SEPARATOR), $files);