Skip to content

Commit

Permalink
fix current method+ add URL method
Browse files Browse the repository at this point in the history
  • Loading branch information
MoamenEltouny committed Mar 15, 2022
1 parent 695237e commit 4d0d1c4
Showing 1 changed file with 49 additions and 13 deletions.
62 changes: 49 additions & 13 deletions src/Classes/Localization.php
Expand Up @@ -11,7 +11,7 @@
* Laravel URL Localization Manager
* ccTLDs, sub-domains, sub-directories
*
* @version 1.2.4
* @version 1.3.3
* @author Moamen Eltouny (Raggi) <raggi@raggitech.com>
*/
class Localization
Expand Down Expand Up @@ -389,22 +389,21 @@ public function route(string $key, $params = [])

if (!is_array($params)) $params = [$params];

if ($this->hideDefault && $this->current == $this->default && $this->type == 'sub-directory') {
if ($locale == $this->current)
return route($key, $params);
// Default Locale
if ($locale == $this->default && $this->hideDefault)
return route($key, $params);

// Depends on Type
if ($this->type == 'sub-directory') {
$route = Route::getRoutes()->getByName($key);

if (substr($route->getPrefix(), 0, 9) != '{locale?}')
$route->prefix('/{locale?}');

return app('url')->toRoute($route, array_merge($params, ['locale' => $locale]), true);
} else {
return route($key, array_merge(['locale' => $locale . ($this->type == 'sub-domain' ? '.' : null)], $params));
}

if ($this->type != 'sub-directory' && $locale == $this->default && $this->hideDefault)
return route($key, $params);

return route($key, array_merge(['locale' => $locale . ($this->type == 'sub-domain' ? '.' : null)], $params));
}

/**
Expand All @@ -414,21 +413,58 @@ public function route(string $key, $params = [])
* @param array $params
* @return string
*/
public function unLocalizedRoute(string $key, array $params = [])
public function unLocalizedRoute(string $key, $params = [])
{
return route($key, $params);
}

/**
* Get localized URL
*
* @param string $uri
* @param mixed $params
* @return string
*/
public function url(string $uri, $params = [])
{
$locale = $this->tmp ?? $this->current;

if ($this->type == 'sub-directory') {
if ($locale == $this->default && $this->hideDefault) {
return urL($uri, $params);
} else {
return urL($locale . '/' . $uri, $params);
}
} else {
// TASK : NOT SUB-DIRECTORY
}
}

/**
* Get Localized route.
*
* @return string
*/
public function current()
{
if (!($name = Route::current()->getName()))
throw new Exception('You should set name for that route.');
if ($params = request()->getQueryString())
parse_str($params, $params);

// Has Route Name
if (($route = Route::current()) && ($name = $route->getName()))
return $this->route($name, $route->parameters());

// Has Not Route Name
if ($this->type == 'sub-directory') {
$uri = ltrim(request()->getRequestUri(), '/');
$uri = explode('/', $uri);

return $this->route($name, Route::current()->parameters());
if (isset($uri[0]) && in_array($uri[0], $this->supported))
array_shift($uri);

return $this->url(implode('/', $uri), $params);
} else {
// TASK : NOT SUB-DIRECTORY
}
}
}

0 comments on commit 4d0d1c4

Please sign in to comment.