Skip to content

Commit 3cb8c56

Browse files
niels-numbersMarc Cámara
authored andcommitted
Maintain query string when hidedDefaultLocaleInURL is true (#667)
Also added a test case for this scenario. Fixes #634
1 parent 14b5ab8 commit 3cb8c56

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

src/Mcamara/LaravelLocalization/LaravelLocalization.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,15 +268,18 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = [], $
268268
if (empty($attributes)) {
269269
$attributes = $this->extractAttributes($url, $locale);
270270
}
271+
271272
$urlQuery = parse_url($url, PHP_URL_QUERY);
272273
$urlQuery = $urlQuery ? '?'.$urlQuery : '';
273274

274275
if (empty($url)) {
276+
$url = $this->request->fullUrl();
277+
$urlQuery = parse_url($url, PHP_URL_QUERY);
278+
$urlQuery = $urlQuery ? '?'.$urlQuery : '';
279+
275280
if (!empty($this->routeName)) {
276-
return $this->getURLFromRouteNameTranslated($locale, $this->routeName, $attributes, $forceDefaultLocation);
281+
return $this->getURLFromRouteNameTranslated($locale, $this->routeName, $attributes, $forceDefaultLocation) . $urlQuery;
277282
}
278-
279-
$url = $this->request->fullUrl();
280283
} else {
281284
$url = $this->url->to($url);
282285
$url = preg_replace('/'. preg_quote($urlQuery, '/') . '$/', '', $url);

tests/LocalizerTests.php

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ protected function getPackageAliases($app)
2525
];
2626
}
2727

28+
public function setUp(): void
29+
{
30+
parent::setUp();
31+
}
32+
2833
/**
2934
* Set routes for testing.
3035
*
@@ -109,10 +114,10 @@ protected function createRequest(
109114
return $request->createFromBase(
110115
\Symfony\Component\HttpFoundation\Request::create(
111116
$uri,
112-
$method,
113-
$parameters,
114-
$cookies,
115-
$files,
117+
'GET',
118+
[],
119+
[],
120+
[],
116121
$server,
117122
$content
118123
)
@@ -168,22 +173,6 @@ public function testSetLocale()
168173
$this->assertEquals('en', app('laravellocalization')->getCurrentLocale());
169174
}
170175

171-
// LaravelLocalization setLocale method should return the locale of
172-
// the request uri (if any). This behavior should be independet
173-
// of the `hideDefaultLocaleInURL` setting
174-
public function testHideDefaultLocaleInUrlShouldNotChangeSetLocaleBehaviour()
175-
{
176-
app('config')->set('laravellocalization.hideDefaultLocaleInURL', true);
177-
178-
app()['request'] = $this->createRequest(
179-
$uri = '/en/test'
180-
);
181-
182-
$laravelLocalization = new \Mcamara\LaravelLocalization\LaravelLocalization();
183-
184-
$this->assertEquals('en', $laravelLocalization->setLocale());
185-
}
186-
187176
public function testLocalizeURL()
188177
{
189178
$this->assertEquals(
@@ -336,6 +325,22 @@ public function testGetLocalizedURL()
336325
);
337326
}
338327

328+
public function testGetLocalizedURLWithQueryStringAndhideDefaultLocaleInURL()
329+
{
330+
app('config')->set('laravellocalization.hideDefaultLocaleInURL', true);
331+
app()['request'] = $this->createRequest(
332+
$uri = 'en/about?q=2'
333+
);
334+
$laravelLocalization = new \Mcamara\LaravelLocalization\LaravelLocalization();
335+
$laravelLocalization->transRoute('LaravelLocalization::routes.about');
336+
337+
$this->assertEquals(
338+
$this->test_url . 'about?q=2',
339+
$laravelLocalization->getLocalizedURL()
340+
);
341+
}
342+
343+
339344
/**
340345
* @param string $path
341346
* @param string|bool $expectedRouteName

0 commit comments

Comments
 (0)