Skip to content

Commit 2424c56

Browse files
Use locale from kernel default locale (#1034)
| Q | A | --------------- | ----- | Bug fix? | kind of | New feature? | yes | BC breaks? | no | Deprecations? | no | Related tickets | fixes #1032 | License | MIT The idea is to ensure we will not break userland application translations when removing dependency to a locale parameter.
2 parents 85b879b + 4bd335c commit 2424c56

File tree

6 files changed

+141
-4
lines changed

6 files changed

+141
-4
lines changed

src/Bundle/SyliusResourceBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\WinzouStateMachinePass;
3131
use Sylius\Bundle\ResourceBundle\DependencyInjection\PagerfantaExtension;
3232
use Sylius\Resource\Symfony\DependencyInjection\Compiler\DisableMetadataCachePass;
33+
use Sylius\Resource\Symfony\DependencyInjection\Compiler\FallbackToKernelDefaultLocalePass;
3334
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
3435
use Symfony\Component\DependencyInjection\ContainerBuilder;
3536
use Symfony\Component\HttpKernel\Bundle\Bundle;
@@ -50,6 +51,7 @@ public function build(ContainerBuilder $container): void
5051

5152
$container->addCompilerPass(new CsrfTokenManagerPass());
5253
$container->addCompilerPass(new DisableMetadataCachePass());
54+
$container->addCompilerPass(new FallbackToKernelDefaultLocalePass());
5355
$container->addCompilerPass(new DoctrineContainerRepositoryFactoryPass());
5456
$container->addCompilerPass(new DoctrineTargetEntitiesResolverPass(new TargetEntitiesResolver()), PassConfig::TYPE_BEFORE_OPTIMIZATION, 1);
5557
$container->addCompilerPass(new RegisterFormBuilderPass());
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
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+
declare(strict_types=1);
13+
14+
namespace Sylius\Resource\Symfony\DependencyInjection\Compiler;
15+
16+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17+
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
19+
/**
20+
* @internal
21+
*/
22+
final class FallbackToKernelDefaultLocalePass implements CompilerPassInterface
23+
{
24+
public function process(ContainerBuilder $container): void
25+
{
26+
if (
27+
$container->hasParameter('locale') ||
28+
!$container->hasParameter('kernel.default_locale')
29+
) {
30+
return;
31+
}
32+
33+
/** @var string $locale */
34+
$locale = $container->getParameter('kernel.default_locale');
35+
36+
$this->replaceLocaleInFlashHelper($container, $locale);
37+
$this->replaceLocaleProvider($container, $locale);
38+
}
39+
40+
private function replaceLocaleInFlashHelper(ContainerBuilder $container, string $locale): void
41+
{
42+
if (!$container->hasDefinition('sylius.resource_controller.flash_helper')) {
43+
return;
44+
}
45+
46+
$flashHelper = $container->getDefinition('sylius.resource_controller.flash_helper');
47+
$flashHelper->replaceArgument(2, $locale);
48+
}
49+
50+
private function replaceLocaleProvider(ContainerBuilder $container, string $locale): void
51+
{
52+
if (!$container->hasDefinition('sylius.translation_locale_provider.immutable')) {
53+
return;
54+
}
55+
56+
$localeProvider = $container->getDefinition('sylius.translation_locale_provider.immutable');
57+
$localeProvider->replaceArgument(0, [$locale]);
58+
$localeProvider->replaceArgument(1, $locale);
59+
}
60+
}

src/Component/tests/Symfony/DependencyInjection/Compiler/DisableMetadataCachePassTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace Sylius\Component\Resource\tests\Symfony\DependencyInjection\Compiler;
14+
namespace Sylius\Resource\Tests\Symfony\DependencyInjection\Compiler;
1515

1616
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
1717
use Sylius\Resource\Symfony\DependencyInjection\Compiler\DisableMetadataCachePass;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
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+
declare(strict_types=1);
13+
14+
namespace Sylius\Resource\Tests\Symfony\DependencyInjection\Compiler;
15+
16+
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
17+
use PHPUnit\Framework\Attributes\Test;
18+
use Sylius\Bundle\ResourceBundle\Controller\FlashHelper;
19+
use Sylius\Resource\Symfony\DependencyInjection\Compiler\FallbackToKernelDefaultLocalePass;
20+
use Symfony\Component\DependencyInjection\ContainerBuilder;
21+
use Symfony\Component\DependencyInjection\Definition;
22+
23+
final class FallbackToKernelDefaultLocalePassTest extends AbstractCompilerPassTestCase
24+
{
25+
/** @test */
26+
public function it_does_nothing_if_locale_parameter_has_been_defined(): void
27+
{
28+
$this->setParameter('locale', 'en_US');
29+
$this->setDefinition('sylius.resource_controller.flash_helper', new Definition());
30+
31+
$this->compile();
32+
33+
$this->assertContainerBuilderHasService('sylius.resource_controller.flash_helper');
34+
}
35+
36+
/** @test */
37+
public function it_replaces_locale_argument_in_flash_helper(): void
38+
{
39+
$flashHelperDefinition = (new Definition(FlashHelper::class))->setArguments([
40+
null,
41+
null,
42+
null,
43+
]);
44+
45+
$this->setParameter('kernel.default_locale', 'en_US');
46+
$this->setDefinition('sylius.resource_controller.flash_helper', $flashHelperDefinition);
47+
48+
$this->compile();
49+
50+
$this->assertContainerBuilderHasService('sylius.resource_controller.flash_helper');
51+
$this->assertContainerBuilderHasServiceDefinitionWithArgument('sylius.resource_controller.flash_helper', 2, 'en_US');
52+
}
53+
54+
/** @test */
55+
public function it_replaces_locale_argument_in_translation_locale_provider(): void
56+
{
57+
$translationLocaleProviderDefinition = (new Definition(FlashHelper::class))->setArguments([
58+
[],
59+
null,
60+
]);
61+
62+
$this->setParameter('kernel.default_locale', 'en_US');
63+
$this->setDefinition('sylius.translation_locale_provider.immutable', $translationLocaleProviderDefinition);
64+
65+
$this->compile();
66+
67+
$this->assertContainerBuilderHasService('sylius.translation_locale_provider.immutable');
68+
$this->assertContainerBuilderHasServiceDefinitionWithArgument('sylius.translation_locale_provider.immutable', 0, ['en_US']);
69+
$this->assertContainerBuilderHasServiceDefinitionWithArgument('sylius.translation_locale_provider.immutable', 1, 'en_US');
70+
}
71+
72+
protected function registerCompilerPass(ContainerBuilder $container): void
73+
{
74+
$container->addCompilerPass(new FallbackToKernelDefaultLocalePass());
75+
}
76+
}

tests/Application/config/packages/framework.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ framework:
22
assets: false
33
translator:
44
default_path: '%kernel.project_dir%/translations'
5-
fallbacks: ["%locale%"]
5+
fallbacks: ["en_US"]
66
secret: "%secret%"
77
form: ~
88
csrf_protection: true
9-
default_locale: "%locale%"
9+
default_locale: "en_US"
1010
session:
1111
handler_id: ~
1212
storage_factory_id: session.storage.factory.mock_file

tests/Application/config/services.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ parameters:
55
database_driver: pdo_sqlite
66
database_path: "%kernel.project_dir%/config/db.sql"
77

8-
locale: en_US
98
secret: "Three can keep a secret, if two of them are dead."
109

1110
services:

0 commit comments

Comments
 (0)