-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Translation] Improve LocaleSwitcher a bit #46045
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,6 @@ | |
|
||
use Psr\Container\ContainerInterface; | ||
use Symfony\Bundle\FrameworkBundle\CacheWarmer\TranslationsCacheWarmer; | ||
use Symfony\Bundle\FrameworkBundle\Translation\LocaleAwareRequestContext; | ||
use Symfony\Bundle\FrameworkBundle\Translation\Translator; | ||
use Symfony\Component\Translation\Dumper\CsvFileDumper; | ||
use Symfony\Component\Translation\Dumper\IcuResFileDumper; | ||
|
@@ -46,6 +45,7 @@ | |
use Symfony\Component\Translation\Reader\TranslationReaderInterface; | ||
use Symfony\Component\Translation\Writer\TranslationWriter; | ||
use Symfony\Component\Translation\Writer\TranslationWriterInterface; | ||
use Symfony\Contracts\Translation\LocaleAwareInterface; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
return static function (ContainerConfigurator $container) { | ||
|
@@ -164,22 +164,15 @@ | |
->args([service(ContainerInterface::class)]) | ||
->tag('container.service_subscriber', ['id' => 'translator']) | ||
->tag('kernel.cache_warmer') | ||
; | ||
|
||
if (class_exists(LocaleSwitcher::class)) { | ||
$container->services() | ||
->set('translation.locale_switcher', LocaleSwitcher::class) | ||
->args([ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed wrong indentation also |
||
param('kernel.default_locale'), | ||
abstract_arg('LocaleAware services'), | ||
]) | ||
->alias(LocaleSwitcher::class, 'translation.locale_switcher') | ||
|
||
->set('translation.locale_aware_request_context', LocaleAwareRequestContext::class) | ||
->set('translation.locale_switcher', LocaleSwitcher::class) | ||
->args([ | ||
service('router.request_context'), | ||
param('kernel.default_locale'), | ||
tagged_iterator('kernel.locale_aware'), | ||
service('router.request_context')->ignoreOnInvalid(), | ||
]) | ||
; | ||
} | ||
->tag('kernel.reset', ['method' => 'reset']) | ||
->alias(LocaleAwareInterface::class, 'translation.locale_switcher') | ||
->alias(LocaleSwitcher::class, 'translation.locale_switcher') | ||
; | ||
}; |
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,23 +11,31 @@ | |
|
||
namespace Symfony\Component\Translation; | ||
|
||
use Symfony\Component\Routing\RequestContext; | ||
use Symfony\Contracts\Translation\LocaleAwareInterface; | ||
|
||
/** | ||
* @author Kevin Bond <kevinbond@gmail.com> | ||
*/ | ||
final class LocaleSwitcher implements LocaleAwareInterface | ||
class LocaleSwitcher implements LocaleAwareInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the class is registered as an autowiring alias, it means we're telling ppl they can type-hint for it. Type-hinting for a final class breaks many SOLID principles, so I removed the keyword. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Imho, we should register There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That works for get/setLocale, but runWithLocale isn't in any interfaces, and I'm not sure we want to add one for it. |
||
{ | ||
private string $defaultLocale; | ||
|
||
/** | ||
* @param LocaleAwareInterface[] $localeAwareServices | ||
*/ | ||
public function __construct(private string $locale, private iterable $localeAwareServices) | ||
{ | ||
public function __construct( | ||
private string $locale, | ||
private iterable $localeAwareServices, | ||
private ?RequestContext $requestContext = null, | ||
fabpot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) { | ||
$this->defaultLocale = $locale; | ||
} | ||
|
||
public function setLocale(string $locale): void | ||
{ | ||
\Locale::setDefault($this->locale = $locale); | ||
$this->requestContext?->setParameter('_locale', $locale); | ||
|
||
foreach ($this->localeAwareServices as $service) { | ||
$service->setLocale($locale); | ||
|
@@ -42,17 +50,26 @@ public function getLocale(): string | |
/** | ||
* Switch to a new locale, execute a callback, then switch back to the original. | ||
* | ||
* @param callable():void $callback | ||
* @template T | ||
* | ||
* @param callable():T $callback | ||
* | ||
* @return T | ||
*/ | ||
public function runWithLocale(string $locale, callable $callback): void | ||
public function runWithLocale(string $locale, callable $callback): mixed | ||
{ | ||
$original = $this->getLocale(); | ||
$this->setLocale($locale); | ||
|
||
try { | ||
$callback(); | ||
return $callback(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's return the callback's result. |
||
} finally { | ||
$this->setLocale($original); | ||
} | ||
} | ||
|
||
public function reset(): void | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Statefull services must at least reset their state. Although the default locale is already set back in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to implement There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not mandatory: |
||
{ | ||
$this->setLocale($this->defaultLocale); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be careful NOT doing conditional definition in the config files please. This logic is for DI extensions. Config files should remain purely declarative. /cc @symfony/mergers for future reviews.