diff --git a/src/Translator/config/services.php b/src/Translator/config/services.php index e5eb6bab50b..363081459dd 100644 --- a/src/Translator/config/services.php +++ b/src/Translator/config/services.php @@ -24,8 +24,9 @@ $container->services() ->set('ux.translator.cache_warmer.translations_cache_warmer', TranslationsCacheWarmer::class) ->args([ - service('translator'), + service('translator')->nullOnInvalid(), service('ux.translator.translations_dumper'), + service('logger')->ignoreOnInvalid(), ]) ->tag('kernel.cache_warmer') diff --git a/src/Translator/doc/index.rst b/src/Translator/doc/index.rst index 614f47991f7..a61eeaaa693 100644 --- a/src/Translator/doc/index.rst +++ b/src/Translator/doc/index.rst @@ -71,6 +71,10 @@ For a better developer experience, TypeScript types definitions are also generat Then, you will be able to import those JavaScript translations in your assets. Don't worry about your final bundle size, only the translations you use will be included in your final bundle, thanks to the `tree shaking `_. +.. note:: + + This package requires the ``translator`` package to be enabled in your Symfony application. If you don't use the ``translator`` package, the warmup command will not generate any translations. + Configuring the dumped translations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php b/src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php index 2411772375c..f58630bf667 100644 --- a/src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php +++ b/src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php @@ -11,8 +11,10 @@ namespace Symfony\UX\Translator\CacheWarmer; +use Psr\Log\LoggerInterface; use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; use Symfony\Component\Translation\TranslatorBagInterface; +use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\UX\Translator\TranslationsDumper; /** @@ -23,8 +25,9 @@ class TranslationsCacheWarmer implements CacheWarmerInterface { public function __construct( - private TranslatorBagInterface $translatorBag, + private TranslatorInterface|TranslatorBagInterface|null $translatorBag, private TranslationsDumper $translationsDumper, + private readonly ?LoggerInterface $logger = null, ) { } @@ -35,6 +38,11 @@ public function isOptional(): bool public function warmUp(string $cacheDir, ?string $buildDir = null): array { + if (!$this->translatorBag instanceof TranslatorBagInterface) { + $this->logger?->warning('Translator bag not available'); + + return []; + } $this->translationsDumper->dump( ...$this->translatorBag->getCatalogues() ); diff --git a/src/Translator/tests/CacheWarmer/TranslationsCacheWarmerTest.php b/src/Translator/tests/CacheWarmer/TranslationsCacheWarmerTest.php index d97e49e0967..a573e6930ad 100644 --- a/src/Translator/tests/CacheWarmer/TranslationsCacheWarmerTest.php +++ b/src/Translator/tests/CacheWarmer/TranslationsCacheWarmerTest.php @@ -12,7 +12,9 @@ namespace Symfony\UX\Translator\Tests\CacheWarmer; use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerInterface; use Symfony\Component\Translation\MessageCatalogue; +use Symfony\Component\Translation\Translator; use Symfony\Component\Translation\TranslatorBag; use Symfony\UX\Translator\CacheWarmer\TranslationsCacheWarmer; use Symfony\UX\Translator\TranslationsDumper; @@ -55,4 +57,26 @@ public function test() $translationsCacheWarmer->warmUp(self::$cacheDir); } + + public function testWithoutTranslator() + { + $translationsDumperMock = $this->createMock(TranslationsDumper::class); + $translationsDumperMock + ->expects($this->never()) + ->method('dump'); + + $loggerMock = $this->createMock(LoggerInterface::class); + $loggerMock + ->expects($this->once()) + ->method('warning') + ->with('Translator bag not available'); + + $translationsCacheWarmer = new TranslationsCacheWarmer( + null, + $translationsDumperMock, + $loggerMock, + ); + + $translationsCacheWarmer->warmUp(self::$cacheDir); + } } diff --git a/src/Translator/tests/Kernel/FrameworkAppKernel.php b/src/Translator/tests/Kernel/FrameworkAppKernel.php index 98c0ca6a703..2686ab7e54d 100644 --- a/src/Translator/tests/Kernel/FrameworkAppKernel.php +++ b/src/Translator/tests/Kernel/FrameworkAppKernel.php @@ -38,6 +38,10 @@ public function registerContainerConfiguration(LoaderInterface $loader) 'secret' => '$ecret', 'test' => true, 'translator' => [ + 'enabled' => match ($this->environment) { + 'test_without_translator' => false, + default => true, + }, 'fallbacks' => ['en'], ], 'http_method_override' => false, diff --git a/src/Translator/tests/UxTranslatorBundleTest.php b/src/Translator/tests/UxTranslatorBundleTest.php index 8f244289558..fe17709d8b0 100644 --- a/src/Translator/tests/UxTranslatorBundleTest.php +++ b/src/Translator/tests/UxTranslatorBundleTest.php @@ -22,6 +22,7 @@ public static function provideKernels() { yield 'empty' => [new EmptyAppKernel('test', true)]; yield 'framework' => [new FrameworkAppKernel('test', true)]; + yield 'framework without translator' => [new FrameworkAppKernel('test_without_translator', true)]; } /**