Skip to content

Commit 29f9ea2

Browse files
ashatrovMarc Cámara
authored and
Marc Cámara
committed
[#293] Cant switch to default language if hiding default locale in URL (#439)
* [#293] Cant switch to default language if hiding default locale in URL. #293 * [#293] Cant switch to default language if hiding default locale in URL. #293
1 parent 03d0a04 commit 29f9ea2

File tree

2 files changed

+96
-47
lines changed

2 files changed

+96
-47
lines changed

src/Mcamara/LaravelLocalization/LaravelLocalization.php

+12-8
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,14 @@ public function localizeURL($url = null, $locale = null)
211211
* @param string|bool $locale Locale to adapt, false to remove locale
212212
* @param string|false $url URL to adapt in the current language. If not passed, the current url would be taken.
213213
* @param array $attributes Attributes to add to the route, if empty, the system would try to extract them from the url.
214+
* @param bool $forceDefaultLocation Force to show default location even hideDefaultLocaleInURL set as TRUE
214215
*
215216
* @throws SupportedLocalesNotDefined
216217
* @throws UnsupportedLocaleException
217218
*
218219
* @return string|false URL translated, False if url does not exist
219220
*/
220-
public function getLocalizedURL($locale = null, $url = null, $attributes = [])
221+
public function getLocalizedURL($locale = null, $url = null, $attributes = [], $forceDefaultLocation = false)
221222
{
222223
if ($locale === null) {
223224
$locale = $this->getCurrentLocale();
@@ -233,7 +234,7 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = [])
233234

234235
if (empty($url)) {
235236
if (!empty($this->routeName)) {
236-
return $this->getURLFromRouteNameTranslated($locale, $this->routeName, $attributes);
237+
return $this->getURLFromRouteNameTranslated($locale, $this->routeName, $attributes, $forceDefaultLocation);
237238
}
238239

239240
$url = $this->request->fullUrl();
@@ -242,7 +243,7 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = [])
242243
}
243244

244245
if ($locale && $translatedRoute = $this->findTranslatedRouteByUrl($url, $attributes, $this->currentLocale)) {
245-
return $this->getURLFromRouteNameTranslated($locale, $translatedRoute, $attributes);
246+
return $this->getURLFromRouteNameTranslated($locale, $translatedRoute, $attributes, $forceDefaultLocation);
246247
}
247248

248249
$base_path = $this->request->getBaseUrl();
@@ -272,11 +273,13 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = [])
272273
$parsed_url['path'] = ltrim($parsed_url['path'], '/');
273274

274275
if ($translatedRoute = $this->findTranslatedRouteByPath($parsed_url['path'], $url_locale)) {
275-
return $this->getURLFromRouteNameTranslated($locale, $translatedRoute, $attributes);
276+
return $this->getURLFromRouteNameTranslated($locale, $translatedRoute, $attributes, $forceDefaultLocation);
276277
}
277278

278-
if (!empty($locale) && ($locale != $this->defaultLocale || !$this->hideDefaultLocaleInURL())) {
279-
$parsed_url['path'] = $locale.'/'.ltrim($parsed_url['path'], '/');
279+
if (!empty($locale)) {
280+
if ($locale != $this->getDefaultLocale() || !$this->hideDefaultLocaleInURL() || $forceDefaultLocation) {
281+
$parsed_url['path'] = $locale.'/'.ltrim($parsed_url['path'], '/');
282+
}
280283
}
281284
$parsed_url['path'] = ltrim(ltrim($base_path, '/').'/'.$parsed_url['path'], '/');
282285

@@ -302,13 +305,14 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = [])
302305
* @param string|bool $locale Locale to adapt
303306
* @param string $transKeyName Translation key name of the url to adapt
304307
* @param array $attributes Attributes for the route (only needed if transKeyName needs them)
308+
* @param bool $forceDefaultLocation Force to show default location even hideDefaultLocaleInURL set as TRUE
305309
*
306310
* @throws SupportedLocalesNotDefined
307311
* @throws UnsupportedLocaleException
308312
*
309313
* @return string|false URL translated
310314
*/
311-
public function getURLFromRouteNameTranslated($locale, $transKeyName, $attributes = [])
315+
public function getURLFromRouteNameTranslated($locale, $transKeyName, $attributes = [], $forceDefaultLocation = false)
312316
{
313317
if (!$this->checkLocaleInSupportedLocales($locale)) {
314318
throw new UnsupportedLocaleException('Locale \''.$locale.'\' is not in the list of supported locales.');
@@ -320,7 +324,7 @@ public function getURLFromRouteNameTranslated($locale, $transKeyName, $attribute
320324

321325
$route = '';
322326

323-
if (!($locale === $this->defaultLocale && $this->hideDefaultLocaleInURL())) {
327+
if ($forceDefaultLocation || !($locale === $this->defaultLocale && $this->hideDefaultLocaleInURL())) {
324328
$route = '/'.$locale;
325329
}
326330
if (is_string($locale) && $this->translator->has($transKeyName, $locale)) {

tests/LocalizerTests.php

+84-39
Original file line numberDiff line numberDiff line change
@@ -188,31 +188,6 @@ public function testLocalizeURL()
188188

189189
public function testGetLocalizedURL()
190190
{
191-
$this->assertEquals(
192-
$this->test_url.'es/acerca',
193-
app('laravellocalization')->getLocalizedURL('es', $this->test_url.'en/about')
194-
);
195-
196-
$this->assertEquals(
197-
$this->test_url.'es/ver/1',
198-
app('laravellocalization')->getLocalizedURL('es', $this->test_url.'view/1')
199-
);
200-
201-
$this->assertEquals(
202-
$this->test_url.'es/ver/1/proyecto',
203-
app('laravellocalization')->getLocalizedURL('es', $this->test_url.'view/1/project')
204-
);
205-
206-
$this->assertEquals(
207-
$this->test_url.'es/ver/1/proyecto/1',
208-
app('laravellocalization')->getLocalizedURL('es', $this->test_url.'view/1/project/1')
209-
);
210-
211-
$this->assertEquals(
212-
$this->test_url.'en/about',
213-
app('laravellocalization')->getLocalizedURL('en', $this->test_url.'about')
214-
);
215-
216191
$this->assertEquals(
217192
$this->test_url.app('laravellocalization')->getCurrentLocale(),
218193
app('laravellocalization')->getLocalizedURL()
@@ -221,20 +196,6 @@ public function testGetLocalizedURL()
221196
app('config')->set('laravellocalization.hideDefaultLocaleInURL', true);
222197
// testing default language hidden
223198

224-
$this->assertEquals(
225-
$this->test_url.'es/acerca',
226-
app('laravellocalization')->getLocalizedURL('es', $this->test_url.'about')
227-
);
228-
$this->assertEquals(
229-
$this->test_url.'about',
230-
app('laravellocalization')->getLocalizedURL('en', $this->test_url.'about')
231-
);
232-
233-
$this->assertEquals(
234-
$this->test_url,
235-
app('laravellocalization')->getLocalizedURL()
236-
);
237-
238199
$this->assertNotEquals(
239200
$this->test_url.app('laravellocalization')->getDefaultLocale(),
240201
app('laravellocalization')->getLocalizedURL()
@@ -311,6 +272,90 @@ public function testGetLocalizedURL()
311272
);
312273
}
313274

275+
/**
276+
* @param bool $hideDefaultLocaleInURL
277+
* @param bool $forceDefault
278+
* @param string $locale
279+
* @param string $path
280+
* @param string $expectedURL
281+
*
282+
* @dataProvider getLocalizedURLDataProvider
283+
*/
284+
public function testGetLocalizedURLFormat($hideDefaultLocaleInURL, $forceDefault, $locale, $path, $expectedURL)
285+
{
286+
app('config')->set('laravellocalization.hideDefaultLocaleInURL', $hideDefaultLocaleInURL);
287+
$this->assertEquals(
288+
$expectedURL,
289+
app('laravellocalization')->getLocalizedURL($locale, $path, [], $forceDefault)
290+
);
291+
292+
}
293+
294+
public function getLocalizedURLDataProvider()
295+
{
296+
return [
297+
// Do not hide default
298+
[false, false, 'es', $this->test_url, $this->test_url.'es'],
299+
[false, false, 'es', $this->test_url.'es', $this->test_url.'es'],
300+
[false, false, 'es', $this->test_url.'en/about', $this->test_url.'es/acerca'],
301+
[false, false, 'es', $this->test_url.'ver/1', $this->test_url.'es/ver/1'],
302+
[false, false, 'es', $this->test_url.'view/1/project', $this->test_url.'es/ver/1/proyecto'],
303+
[false, false, 'es', $this->test_url.'view/1/project/1', $this->test_url.'es/ver/1/proyecto/1'],
304+
305+
// Do not hide default
306+
[false, false, 'en', $this->test_url.'en', $this->test_url.'en'],
307+
[false, false, 'en', $this->test_url.'about', $this->test_url.'en/about'],
308+
[false, false, 'en', $this->test_url.'ver/1', $this->test_url.'en/ver/1'],
309+
[false, false, 'en', $this->test_url.'view/1/project', $this->test_url.'en/view/1/project'],
310+
[false, false, 'en', $this->test_url.'view/1/project/1', $this->test_url.'en/view/1/project/1'],
311+
312+
// Hide default
313+
[true, false, 'es', $this->test_url, $this->test_url.'es'],
314+
[true, false, 'es', $this->test_url.'es', $this->test_url.'es'],
315+
[true, false, 'es', $this->test_url.'en/about', $this->test_url.'es/acerca'],
316+
[true, false, 'es', $this->test_url.'ver/1', $this->test_url.'es/ver/1'],
317+
[true, false, 'es', $this->test_url.'view/1/project', $this->test_url.'es/ver/1/proyecto'],
318+
[true, false, 'es', $this->test_url.'view/1/project/1', $this->test_url.'es/ver/1/proyecto/1'],
319+
320+
// Hide default
321+
[true, false, 'en', $this->test_url.'en', $this->test_url.''],
322+
[true, false, 'en', $this->test_url.'about', $this->test_url.'about'],
323+
[true, false, 'en', $this->test_url.'ver/1', $this->test_url.'ver/1'],
324+
[true, false, 'en', $this->test_url.'view/1/project', $this->test_url.'view/1/project'],
325+
[true, false, 'en', $this->test_url.'view/1/project/1', $this->test_url.'view/1/project/1'],
326+
327+
// Do not hide default FORCE SHOWING
328+
[false, true, 'es', $this->test_url, $this->test_url.'es'],
329+
[false, true, 'es', $this->test_url.'es', $this->test_url.'es'],
330+
[false, true, 'es', $this->test_url.'en/about', $this->test_url.'es/acerca'],
331+
[false, true, 'es', $this->test_url.'ver/1', $this->test_url.'es/ver/1'],
332+
[false, true, 'es', $this->test_url.'view/1/project', $this->test_url.'es/ver/1/proyecto'],
333+
[false, true, 'es', $this->test_url.'view/1/project/1', $this->test_url.'es/ver/1/proyecto/1'],
334+
335+
// Do not hide default FORCE SHOWING
336+
[false, true, 'en', $this->test_url.'en', $this->test_url.'en'],
337+
[false, true, 'en', $this->test_url.'about', $this->test_url.'en/about'],
338+
[false, true, 'en', $this->test_url.'ver/1', $this->test_url.'en/ver/1'],
339+
[false, true, 'en', $this->test_url.'view/1/project', $this->test_url.'en/view/1/project'],
340+
[false, true, 'en', $this->test_url.'view/1/project/1', $this->test_url.'en/view/1/project/1'],
341+
342+
// Hide default FORCE SHOWING
343+
[true, true, 'es', $this->test_url, $this->test_url.'es'],
344+
[true, true, 'es', $this->test_url.'es', $this->test_url.'es'],
345+
[true, true, 'es', $this->test_url.'en/about', $this->test_url.'es/acerca'],
346+
[true, true, 'es', $this->test_url.'ver/1', $this->test_url.'es/ver/1'],
347+
[true, true, 'es', $this->test_url.'view/1/project', $this->test_url.'es/ver/1/proyecto'],
348+
[true, true, 'es', $this->test_url.'view/1/project/1', $this->test_url.'es/ver/1/proyecto/1'],
349+
350+
// Hide default FORCE SHOWING
351+
[true, true, 'en', $this->test_url.'en', $this->test_url.'en'],
352+
[true, true, 'en', $this->test_url.'about', $this->test_url.'en/about'],
353+
[true, true, 'en', $this->test_url.'ver/1', $this->test_url.'en/ver/1'],
354+
[true, true, 'en', $this->test_url.'view/1/project', $this->test_url.'en/view/1/project'],
355+
[true, true, 'en', $this->test_url.'view/1/project/1', $this->test_url.'en/view/1/project/1'],
356+
];
357+
}
358+
314359
public function testGetURLFromRouteNameTranslated()
315360
{
316361
$this->assertEquals(

0 commit comments

Comments
 (0)