/**
* Localize current URL having a {parameter}.
*
* Awaiting a fix to the laravel-localization package
* this helper will translate any translated route
* that accept a mandatory slug, but no ?query.
*
* GitHub issue: https://github.com/mcamara/laravel-localization/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20getLocalizedURL
*
* Improvments:
* - redirect to parent category if empty slug;
* - handle ?query (will be done when needed);
* - findTheShorterNameForThisHelper;
* - make it PSR compliant.
*
* @param array $slugs
* @param string $loc
* @return bool $absolute
*/
function localizedUrlWithSlugParameter(array $slugs, string $loc, bool $absolute = false) {
$route_scheme = '/'.trans('routes.' . Route::currentRouteName() , [], $loc);
// Check if translation exists and replace {slug}
if(array_has($slugs, $loc) && strlen($slugs[$loc]) > 0)
{
if(preg_match('/{slug}/', $route_scheme))
{
$route_scheme = str_replace('{slug}', $slugs[$loc], $route_scheme);
}
}
else
{
$route_scheme = '';
}
// Generate route
$route = '';
if($absolute)
{
$route = request()->getScheme().':'.request()->getSchemeAndHttpHost();
}
return $route.'/'.$loc.$route_scheme;
}
Last active
April 1, 2024 18:20
-
-
Save meduzen/f65a9348a7dae319eda60758106d88fd to your computer and use it in GitHub Desktop.
A helper to allow named route with slug (named-route/{slug}) translation in mcamara/laravel-localization
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment