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

[Translator] Mark the translator as nullable #2061

Open
wants to merge 7 commits into
base: 2.x
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/Translator/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
4 changes: 4 additions & 0 deletions src/Translator/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://webpack.js.org/guides/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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
10 changes: 9 additions & 1 deletion src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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,
) {
}

Expand All @@ -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()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
4 changes: 4 additions & 0 deletions src/Translator/tests/Kernel/FrameworkAppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/Translator/tests/UxTranslatorBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
}

/**
Expand Down