John Davidson

php - With a {locale} on each routes, my links (in my menu) for change the locale variable must have the rights parameters for each routes, so

0 comments
Message:


After hours of research, I ended up creating a solution myself, it works, but I think you'll laugh.


Does anyone have a better idea?


     @foreach(localization()->getSupportedLocales() as $key => $locale)
<li class="nav-item lang">
<a class="{{ (app()->getLocale() == $key) ? 'active' : '' }}" href=
"@php
$url = url()->full();
$url = explode("/", $url);
$newUrl = '';
$first = reset( $url );
foreach($url as $urlKey => $value)
{
$slash = '/';
if($urlKey == 0)
$slash = '/';
else
$slash = '/';
if($urlKey == 3)
$url[$urlKey] = $key;
if( !next( $url ) && $value != $first) {
$slash = '';
}
$newUrl .= $url[$urlKey].$slash;
}

echo($newUrl);

@endphp">{{ strtoupper($key) }}</a>
</li>
@endforeach

As I know that the $locale variable is always at the 3rd slash of my url, I had fun with an explode, and I have the expected result, a variable url with a random number of parameters depending on the current route.


But something tells me you're going to laugh... Am I right? ^^


I tried this code, but I need :


1 - Advice on where to create this function


2 - A better idea to achieve the same result?


Thanks by advance all !



My Answer:
>
@foreach(localization()->getLocales() as $locale)
{{ $locale }}
@endforeach
@endforeach

Rate this post

5 of 5 based on 9384 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us