From d6167f7dd1f32b76ac9a45b27ad6fd81cedc6565 Mon Sep 17 00:00:00 2001 From: Christoph Reiter <reiter.christoph@gmail.com> Date: Wed, 9 Nov 2022 10:47:50 +0100 Subject: [PATCH] locale: expose method for setting a locale on a request object and not just the active request. In some cases this might be easier to understand if the user already is working with a request object. --- src/Locale/Locale.php | 5 ++++- tests/LocaleTest.php | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Locale/Locale.php b/src/Locale/Locale.php index c06bd95..b7d9a6d 100644 --- a/src/Locale/Locale.php +++ b/src/Locale/Locale.php @@ -74,7 +74,10 @@ class Locale return $locale; } - private static function setRequestLocaleFromQuery(Request $request, string $queryParam): void + /** + * Same as setCurrentRequestLocaleFromQuery(), but takes a request object. + */ + public static function setRequestLocaleFromQuery(Request $request, string $queryParam): void { if ($request->query->has($queryParam)) { $lang = $request->query->get($queryParam); diff --git a/tests/LocaleTest.php b/tests/LocaleTest.php index e9f924c..f5c0882 100644 --- a/tests/LocaleTest.php +++ b/tests/LocaleTest.php @@ -39,4 +39,20 @@ class LocaleTest extends TestCase $lang = $service->getCurrentPrimaryLanguage(); $this->assertSame('de', $lang); } + + public function testSetExplicit() + { + $stack = new RequestStack(); + $params = new ParameterBag(['kernel.default_locale' => \Locale::acceptFromHttp('en')]); + $service = new Locale($stack, $params); + $request = new Request(['foo' => 'fr']); + $service->setRequestLocaleFromQuery($request, 'foo'); + + $stack->push($request); + $lang = $service->getCurrentPrimaryLanguage(); + $this->assertSame('fr', $lang); + $stack->pop(); + $lang = $service->getCurrentPrimaryLanguage(); + $this->assertSame('en', $lang); + } } -- GitLab