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

Disable cache warmup when translator is identity translator #1965

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
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` to be enabled in your Symfony application. If you don't use the `translator` service, the warmup command will not generate any translations.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This package requires the translator to be enabled

Is translator a package, a service of something else? In the next sentence it is written that it's a service, I think it should be explicit in the first sentence too.

Copy link
Contributor

@alexislefebvre alexislefebvre Aug 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code should have double quotes, otherwise it is displayed like this:

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated this note in #2061


Configuring the default locale
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Symfony\UX\Translator\DependencyInjection;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Translation\TranslatorBagInterface;

class TranslatorCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (!$this->hasValidTranslator($container)) {
$container->removeDefinition('ux.translator.cache_warmer.translations_cache_warmer');
}
}

private function hasValidTranslator(ContainerBuilder $containerBuilder): bool
kbond marked this conversation as resolved.
Show resolved Hide resolved
{
if (!$containerBuilder->hasDefinition('translator')) {
return false;
}

$translator = $containerBuilder->getDefinition('translator');
if (!is_subclass_of($translator->getClass(), TranslatorBagInterface::class)) {
return false;
}
return true;
}
}
7 changes: 7 additions & 0 deletions src/Translator/src/UxTranslatorBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

namespace Symfony\UX\Translator;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\UX\Translator\DependencyInjection\TranslatorCompilerPass;

/**
* @author Hugo Alliaume <[email protected]>
Expand All @@ -26,4 +28,9 @@ public function getPath(): string
{
return \dirname(__DIR__);
}

public function build(ContainerBuilder $container): void
{
$container->addCompilerPass(new TranslatorCompilerPass());
}
}
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