Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.6k
[FrameworkBundle][Translation] addLocaleSwitcher
service#45793
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
45 changes: 45 additions & 0 deletions...mfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ConfigureLocaleSwitcherPass.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; | ||
use Symfony\Component\DependencyInjection\Argument\IteratorArgument; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
/** | ||
* @author Kevin Bond <kevinbond@gmail.com> | ||
* | ||
* @internal | ||
*/ | ||
class ConfigureLocaleSwitcherPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
if (!$container->has('translation.locale_switcher')) { | ||
return; | ||
} | ||
$localeAwareServices = array_map( | ||
fn (string $id) => new Reference($id), | ||
array_keys($container->findTaggedServiceIds('kernel.locale_aware')) | ||
); | ||
if ($container->has('translation.locale_aware_request_context')) { | ||
$localeAwareServices[] = new Reference('translation.locale_aware_request_context'); | ||
} | ||
$container->getDefinition('translation.locale_switcher') | ||
->setArgument(1, new IteratorArgument($localeAwareServices)) | ||
; | ||
} | ||
} |
1 change: 1 addition & 0 deletionssrc/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletionssrc/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletionssrc/Symfony/Bundle/FrameworkBundle/Resources/config/translation.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions...le/FrameworkBundle/Tests/DependencyInjection/Compiler/ConfigureLocaleSwitcherPassTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ConfigureLocaleSwitcherPass; | ||
use Symfony\Component\DependencyInjection\Argument\IteratorArgument; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
class ConfigureLocaleSwitcherPassTest extends TestCase | ||
{ | ||
public function testProcess() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->register('translation.locale_switcher')->setArgument(0, 'en'); | ||
$container->register('locale_aware_service') | ||
->addTag('kernel.locale_aware') | ||
; | ||
$pass = new ConfigureLocaleSwitcherPass(); | ||
$pass->process($container); | ||
$switcherDef = $container->getDefinition('translation.locale_switcher'); | ||
$this->assertInstanceOf(IteratorArgument::class, $switcherDef->getArgument(1)); | ||
$this->assertEquals([new Reference('locale_aware_service')], $switcherDef->getArgument(1)->getValues()); | ||
} | ||
public function testProcessWithRequestContext() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->register('translation.locale_switcher'); | ||
$container->register('locale_aware_service') | ||
->addTag('kernel.locale_aware') | ||
; | ||
$container->register('translation.locale_aware_request_context'); | ||
$pass = new ConfigureLocaleSwitcherPass(); | ||
$pass->process($container); | ||
$switcherDef = $container->getDefinition('translation.locale_switcher'); | ||
$this->assertInstanceOf(IteratorArgument::class, $switcherDef->getArgument(1)); | ||
$this->assertEquals( | ||
[ | ||
new Reference('locale_aware_service'), | ||
new Reference('translation.locale_aware_request_context'), | ||
], | ||
$switcherDef->getArgument(1)->getValues() | ||
); | ||
} | ||
} |
22 changes: 22 additions & 0 deletionssrc/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletionssrc/Symfony/Bundle/FrameworkBundle/Tests/Translation/LocaleAwareRequestContextTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespaceSymfony\Bundle\FrameworkBundle\Tests\Translation; | ||
usePHPUnit\Framework\TestCase; | ||
useSymfony\Bundle\FrameworkBundle\Translation\LocaleAwareRequestContext; | ||
useSymfony\Component\Routing\RequestContext; | ||
class LocaleAwareRequestContextTestextends TestCase | ||
{ | ||
publicfunctiontestCanSwitchLocale() | ||
{ | ||
$context =newRequestContext(); | ||
$service =newLocaleAwareRequestContext($context,'en'); | ||
$this->assertSame('en',$service->getLocale()); | ||
$this->assertNull($context->getParameter('_locale')); | ||
$service->setLocale('fr'); | ||
$this->assertSame('fr',$service->getLocale()); | ||
$this->assertSame('fr',$context->getParameter('_locale')); | ||
} | ||
} |
35 changes: 35 additions & 0 deletionssrc/Symfony/Bundle/FrameworkBundle/Translation/LocaleAwareRequestContext.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace Symfony\Bundle\FrameworkBundle\Translation; | ||
use Symfony\Component\Routing\RequestContext; | ||
use Symfony\Contracts\Translation\LocaleAwareInterface; | ||
/** | ||
* @author Kevin Bond <kevinbond@gmail.com> | ||
*/ | ||
final class LocaleAwareRequestContext implements LocaleAwareInterface | ||
{ | ||
public function __construct(private RequestContext $requestContext, private string $defaultLocale) | ||
{ | ||
} | ||
public function setLocale(string $locale): void | ||
{ | ||
$this->requestContext->setParameter('_locale', $locale); | ||
} | ||
public function getLocale(): string | ||
{ | ||
return $this->requestContext->getParameter('_locale') ?? $this->defaultLocale; | ||
} | ||
} |
58 changes: 58 additions & 0 deletionssrc/Symfony/Component/Translation/LocaleSwitcher.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespaceSymfony\Component\Translation; | ||
useSymfony\Contracts\Translation\LocaleAwareInterface; | ||
/** | ||
* @author Kevin Bond <kevinbond@gmail.com> | ||
*/ | ||
finalclass LocaleSwitcherimplements LocaleAwareInterface | ||
{ | ||
/** | ||
* @param LocaleAwareInterface[] $localeAwareServices | ||
*/ | ||
publicfunction__construct(privatestring$locale,privateiterable$localeAwareServices) | ||
{ | ||
} | ||
publicfunctionsetLocale(string$locale):void | ||
{ | ||
\Locale::setDefault($this->locale =$locale); | ||
foreach ($this->localeAwareServicesas$service) { | ||
$service->setLocale($locale); | ||
} | ||
} | ||
publicfunctiongetLocale():string | ||
{ | ||
return$this->locale; | ||
} | ||
/** | ||
* Switch to a new locale, execute a callback, then switch back to the original. | ||
* | ||
* @param callable():void $callback | ||
*/ | ||
publicfunctionrunWithLocale(string$locale,callable$callback):void | ||
{ | ||
$original =$this->getLocale(); | ||
$this->setLocale($locale); | ||
try { | ||
$callback(); | ||
}finally { | ||
$this->setLocale($original); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.