Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita71f328

Browse files
[Translation] Improve LocaleSwitcher a bit
1 parentf1ce841 commita71f328

File tree

11 files changed

+60
-205
lines changed

11 files changed

+60
-205
lines changed

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ConfigureLocaleSwitcherPass.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205
useSymfony\Component\Translation\Bridge\Loco\LocoProviderFactory;
206206
useSymfony\Component\Translation\Bridge\Lokalise\LokaliseProviderFactory;
207207
useSymfony\Component\Translation\Command\XliffLintCommandasBaseXliffLintCommand;
208+
useSymfony\Component\Translation\LocaleSwitcher;
208209
useSymfony\Component\Translation\PseudoLocalizationTranslator;
209210
useSymfony\Component\Translation\Translator;
210211
useSymfony\Component\Uid\Factory\UuidFactory;
@@ -1082,7 +1083,6 @@ private function registerRouterConfiguration(array $config, ContainerBuilder $co
10821083
$container->removeDefinition('console.command.router_debug');
10831084
$container->removeDefinition('console.command.router_match');
10841085
$container->removeDefinition('messenger.middleware.router_context');
1085-
$container->removeDefinition('translation.locale_aware_request_context');
10861086

10871087
return;
10881088
}
@@ -1295,6 +1295,11 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
12951295
}
12961296

12971297
$loader->load('translation.php');
1298+
1299+
if (!ContainerBuilder::willBeAvailable('symfony/translation', LocaleSwitcher::class, ['symfony/framework-bundle'])) {
1300+
$container->removeDefinition('translation.locale_switcher');
1301+
}
1302+
12981303
$loader->load('translation_providers.php');
12991304

13001305
// Use the "real" translator instead of the identity default

‎src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddDebugLogProcessorPass;
1616
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass;
1717
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AssetsContextPass;
18-
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ConfigureLocaleSwitcherPass;
1918
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ContainerBuilderDebugDumpPass;
2019
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\DataCollectorTranslatorPass;
2120
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggingTranslatorPass;
@@ -164,7 +163,6 @@ public function build(ContainerBuilder $container)
164163
$container->addCompilerPass(newRegisterReverseContainerPass(true));
165164
$container->addCompilerPass(newRegisterReverseContainerPass(false), PassConfig::TYPE_AFTER_REMOVING);
166165
$container->addCompilerPass(newRemoveUnusedSessionMarshallingHandlerPass());
167-
$container->addCompilerPass(newConfigureLocaleSwitcherPass());
168166

169167
if ($container->getParameter('kernel.debug')) {
170168
$container->addCompilerPass(newAddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION,2);

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
usePsr\Container\ContainerInterface;
1515
useSymfony\Bundle\FrameworkBundle\CacheWarmer\TranslationsCacheWarmer;
16-
useSymfony\Bundle\FrameworkBundle\Translation\LocaleAwareRequestContext;
1716
useSymfony\Bundle\FrameworkBundle\Translation\Translator;
1817
useSymfony\Component\Translation\Dumper\CsvFileDumper;
1918
useSymfony\Component\Translation\Dumper\IcuResFileDumper;
@@ -46,6 +45,7 @@
4645
useSymfony\Component\Translation\Reader\TranslationReaderInterface;
4746
useSymfony\Component\Translation\Writer\TranslationWriter;
4847
useSymfony\Component\Translation\Writer\TranslationWriterInterface;
48+
useSymfony\Contracts\Translation\LocaleAwareInterface;
4949
useSymfony\Contracts\Translation\TranslatorInterface;
5050

5151
returnstaticfunction (ContainerConfigurator$container) {
@@ -164,22 +164,15 @@
164164
->args([service(ContainerInterface::class)])
165165
->tag('container.service_subscriber', ['id' =>'translator'])
166166
->tag('kernel.cache_warmer')
167-
;
168-
169-
if (class_exists(LocaleSwitcher::class)) {
170-
$container->services()
171-
->set('translation.locale_switcher', LocaleSwitcher::class)
172-
->args([
173-
param('kernel.default_locale'),
174-
abstract_arg('LocaleAware services'),
175-
])
176-
->alias(LocaleSwitcher::class,'translation.locale_switcher')
177167

178-
->set('translation.locale_aware_request_context',LocaleAwareRequestContext::class)
168+
->set('translation.locale_switcher',LocaleSwitcher::class)
179169
->args([
180-
service('router.request_context'),
181170
param('kernel.default_locale'),
171+
tagged_iterator('kernel.locale_aware'),
172+
service('router.request_context')->ignoreOnInvalid(),
182173
])
183-
;
184-
}
174+
->tag('kernel.reset', ['method' =>'reset'])
175+
->alias(LocaleAwareInterface::class,'translation.locale_switcher')
176+
->alias(LocaleSwitcher::class,'translation.locale_switcher')
177+
;
185178
};

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/ConfigureLocaleSwitcherPassTest.php

Lines changed: 0 additions & 62 deletions
This file was deleted.

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
useSymfony\Component\Cache\Adapter\TagAwareAdapter;
3232
useSymfony\Component\Cache\DependencyInjection\CachePoolPass;
3333
useSymfony\Component\Config\Definition\Exception\InvalidConfigurationException;
34-
useSymfony\Component\DependencyInjection\Argument\AbstractArgument;
3534
useSymfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
3635
useSymfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
3736
useSymfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
@@ -2019,15 +2018,13 @@ public function testLocaleSwitcherServiceRegistered()
20192018
$container =$this->createContainerFromFile('full');
20202019

20212020
$this->assertTrue($container->has('translation.locale_switcher'));
2022-
$this->assertTrue($container->has('translation.locale_aware_request_context'));
20232021

20242022
$switcherDef =$container->getDefinition('translation.locale_switcher');
2025-
$localeAwareRequestContextDef =$container->getDefinition('translation.locale_aware_request_context');
20262023

20272024
$this->assertSame('%kernel.default_locale%',$switcherDef->getArgument(0));
2028-
$this->assertInstanceOf(AbstractArgument::class,$switcherDef->getArgument(1));
2029-
$this->assertEquals(newReference('router.request_context'),$localeAwareRequestContextDef->getArgument(0));
2030-
$this->assertSame('%kernel.default_locale%',$localeAwareRequestContextDef->getArgument(1));
2025+
$this->assertInstanceOf(TaggedIteratorArgument::class,$switcherDef->getArgument(1));
2026+
$this->assertSame('kernel.locale_aware',$switcherDef->getArgument(1)->getTag());
2027+
$this->assertEquals(newReference('router.request_context',ContainerBuilder::IGNORE_ON_INVALID_REFERENCE),$switcherDef->getArgument(2));
20312028
}
20322029

20332030
protectedfunctioncreateContainer(array$data = [])

‎src/Symfony/Bundle/FrameworkBundle/Tests/Translation/LocaleAwareRequestContextTest.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

‎src/Symfony/Bundle/FrameworkBundle/Translation/LocaleAwareRequestContext.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

‎src/Symfony/Component/Translation/LocaleSwitcher.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,31 @@
1111

1212
namespaceSymfony\Component\Translation;
1313

14+
useSymfony\Component\Routing\RequestContext;
1415
useSymfony\Contracts\Translation\LocaleAwareInterface;
1516

1617
/**
1718
* @author Kevin Bond <kevinbond@gmail.com>
1819
*/
19-
finalclass LocaleSwitcherimplements LocaleAwareInterface
20+
class LocaleSwitcherimplements LocaleAwareInterface
2021
{
22+
privatestring$defaultLocale;
23+
2124
/**
2225
* @param LocaleAwareInterface[] $localeAwareServices
2326
*/
24-
publicfunction__construct(privatestring$locale,privateiterable$localeAwareServices)
25-
{
27+
publicfunction__construct(
28+
privatestring$locale,
29+
privateiterable$localeAwareServices,
30+
private ?RequestContext$requestContext =null,
31+
) {
32+
$this->defaultLocale =$locale;
2633
}
2734

2835
publicfunctionsetLocale(string$locale):void
2936
{
3037
\Locale::setDefault($this->locale =$locale);
38+
$this->requestContext?->setParameter('_locale',$locale);
3139

3240
foreach ($this->localeAwareServicesas$service) {
3341
$service->setLocale($locale);
@@ -42,17 +50,26 @@ public function getLocale(): string
4250
/**
4351
* Switch to a new locale, execute a callback, then switch back to the original.
4452
*
45-
* @param callable():void $callback
53+
* @template T
54+
*
55+
* @param callable():T $callback
56+
*
57+
* @return T
4658
*/
47-
publicfunctionrunWithLocale(string$locale,callable$callback):void
59+
publicfunctionrunWithLocale(string$locale,callable$callback):mixed
4860
{
4961
$original =$this->getLocale();
5062
$this->setLocale($locale);
5163

5264
try {
53-
$callback();
65+
return$callback();
5466
}finally {
5567
$this->setLocale($original);
5668
}
5769
}
70+
71+
publicfunctionreset():void
72+
{
73+
$this->setLocale($this->defaultLocale);
74+
}
5875
}

‎src/Symfony/Component/Translation/Tests/LocaleSwitcherTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespaceSymfony\Component\Translation\Tests;
1313

1414
usePHPUnit\Framework\TestCase;
15+
useSymfony\Component\Routing\RequestContext;
1516
useSymfony\Component\Translation\LocaleSwitcher;
1617
useSymfony\Contracts\Translation\LocaleAwareInterface;
1718

@@ -75,6 +76,24 @@ public function testCanSwitchLocaleForCallback()
7576
$this->assertSame('en',$service->getLocale());
7677
$this->assertSame('en',$switcher->getLocale());
7778
}
79+
80+
publicfunctiontestWithRequestContext()
81+
{
82+
$context =newRequestContext();
83+
$service =newLocaleSwitcher('en', [],$context);
84+
85+
$this->assertSame('en',$service->getLocale());
86+
87+
$service->setLocale('fr');
88+
89+
$this->assertSame('fr',$service->getLocale());
90+
$this->assertSame('fr',$context->getParameter('_locale'));
91+
92+
$service->reset();
93+
94+
$this->assertSame('en',$service->getLocale());
95+
$this->assertSame('en',$context->getParameter('_locale'));
96+
}
7897
}
7998

8099
class DummyLocaleAwareimplements LocaleAwareInterface

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp