Skip to content

Commit b2c0bb8

Browse files
author
Marc Cámara
committed
improvement of the readability of the code, some adjustments
1 parent 9ea9aca commit b2c0bb8

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"keywords": ["localization", "laravel", "php"],
55
"homepage": "https://github.com/mcamara/laravel-localization",
66
"license": "MIT",
7-
"version": "0.14.4",
7+
"version": "0.14.5",
88
"authors": [
99
{
1010
"name": "Marc Cámara",

src/Mcamara/LaravelLocalization/LaravelLocalization.php

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class LaravelLocalization
6565
/**
6666
* Name of the translation key of the current route, it is used for url translations
6767
*
68-
* @var array
68+
* @var array|string
6969
*/
7070
protected $routesNames = array();
7171

@@ -254,20 +254,24 @@ public function localizeURL($url = null, $locale = null)
254254
public function getLocalizedURL($locale = null, $url = null)
255255
{
256256
if ($locale !== false)
257-
{
258-
if (is_null($locale))
259-
{
260-
$locale = $this->getCurrentLocale();
261-
}
262-
else
263-
{
264-
$locales = $this->getSupportedLocales();
265-
if (empty($locales[$locale]))
266-
{
267-
throw new UnsupportedLocaleException('Locale \'' . $locale . '\' is not in the list of supported locales.');
268-
}
269-
}
270-
}
257+
{
258+
if (is_null($locale))
259+
{
260+
$locale = $this->getCurrentLocale();
261+
}
262+
else
263+
{
264+
$locales = $this->getSupportedLocales();
265+
if (empty($locales[$locale]))
266+
{
267+
throw new UnsupportedLocaleException('Locale \'' . $locale . '\' is not in the list of supported locales.');
268+
}
269+
}
270+
}
271+
else
272+
{
273+
$locale = $this->defaultLocale;
274+
}
271275

272276
if (is_null($url) || !is_string($url))
273277
{
@@ -280,20 +284,21 @@ public function getLocalizedURL($locale = null, $url = null)
280284
$urlTranslated = $this->getURLFromRouteNameTranslated($locale);
281285
if(!$urlTranslated)
282286
{
283-
return false;
287+
return False;
284288
}
285289

286290
$url = parse_url($url);
287291
$urlTranslated = parse_url($urlTranslated);
288-
$urlTranslated = array_merge($url,$urlTranslated);
292+
$urlTranslated = array_merge($url, $urlTranslated);
289293

290294
return $this->unparse_url($urlTranslated);
291295
}
292296
}
293297

294298
$base_path = Request::getBaseUrl();
295299
$parsed_url = parse_url($url);
296-
if (empty($parsed_url['path']))
300+
301+
if ( !$parsed_url || empty($parsed_url['path']) )
297302
{
298303
$path = $parsed_url['path'] = "";
299304
}
@@ -385,7 +390,7 @@ public function getURLFromRouteNameTranslated($locale, $transKeysNames = array()
385390
{
386391
if ($this->translator->has($transKeyName,$locale))
387392
{
388-
$translation = $this->translator->trans($transKeyName,array(),array(),$locale);
393+
$translation = $this->translator->trans($transKeyName, [], "", $locale);
389394
$route = $route."/".$translation;
390395

391396
if (is_array($attributes))
@@ -746,9 +751,9 @@ public function transRoute($routeName)
746751
/**
747752
* Returns the translation key for a given path
748753
*
749-
* @param string $path [description]
754+
* @param string $path Path to get the key translated
750755
*
751-
* @return string [description]
756+
* @return string|boolean Key for translation
752757
*/
753758
public function getRouteNameFromAPath($path)
754759
{
@@ -759,18 +764,18 @@ public function getRouteNameFromAPath($path)
759764
}
760765
$path = str_replace('/' . $this->currentLocale . '/', '', $path);
761766
$path = trim($path,"/");
762-
$routesNames = array();
767+
$routesNames = [];
763768

764769

765770
foreach ($this->translatedRoutes as $route)
766771
{
767772
if ($this->translator->trans($route) == $path)
768773
{
769-
$routesNames[] = $route;
774+
return $route;
770775
}
771776
}
772777

773-
return $routesNames;
778+
return False;
774779
}
775780

776781

src/Mcamara/LaravelLocalization/LaravelLocalizationServiceProvider.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,18 @@ public function register()
4444
$localeCode = $params[0];
4545
$locales = $app['laravellocalization']->getSupportedLocales();
4646

47+
// Save any flashed data for redirect
48+
Session::reflash();
49+
4750
if (!empty($locales[$localeCode]))
4851
{
4952
if ($localeCode === $defaultLocale && $app['laravellocalization']->hideDefaultLocaleInURL())
5053
{
51-
// Save any flashed data for redirect
52-
Session::reflash();
53-
5454
return Redirect::to($app['laravellocalization']->getNonLocalizedURL(), 307)->header('Vary','Accept-Language');
5555
}
5656
}
5757
else if ($currentLocale !== $defaultLocale || !$app['laravellocalization']->hideDefaultLocaleInURL())
5858
{
59-
// Save any flashed data for redirect
60-
Session::reflash();
61-
6259
// If the current url does not contain any locale
6360
// The system redirect the user to the very same url "localized"
6461
// we use the current locale to redirect him

0 commit comments

Comments
 (0)