laravel-localization: getLocalizedURL doesn't translate routes
Hello.
Laravel-localization works well on my application. When I am on the homepage if I switch language and navigate on the application, all is running well, routes are translated as expected in all links, menus etc.
The problem is when I am on a page, if I click on the switch languages this is what I have :
- http://app.dev/fr/titres/mon-titre
- http://app.dev/en/titres/mon-titre
- http://app.dev/es/titres/mon-titre
This is what I should have :
- http://app.dev/fr/titres/mon-titre
- http://app.dev/en/titles/mon-titre
- http://app.dev/es/titulos/mon-titre
On my header.blade.php, this is what I have :
@foreach(Localization::getSupportedLocales() as $lang => $properties)
<a
href="{{Localization::getLocalizedURL($lang)}}"
@if(Localization::getCurrentLocale() == $lang) class="active" @endif
>
{{$lang}}
</a>
@endforeach
Insofar as all is working as expected for the language when we are on a good URL … what is the issue with that problem ? I saw many posts about similar problems with the getLocalizedURL() method, I tried many “issues” without success … 😕
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 8
- Comments: 28 (2 by maintainers)
The workaround I found does not work with routes defined as resources. A small variation is needed:
@mcamara Could we find a definitive solution to this problem?
Uhm… Then the package will need a breakpoint between Laravel 5.3 and 5.4.
Unfortunately it is too minimal a solution, I have more specific needs that currently only mcamara can satisfy.
I found this other project, which looks like an improved mcamara clone:
https://github.com/ARCANEDEV/Localization
I have the same issue in laravel 5.4
Yes, I have the same problem. When I made some deeper debug into the code I found that function getLocalizedURL returns wrong results when routes (in lang routes.php) are declared with parameters. When routes are clean (without parameters) everything is fine and working as desired. I found problem is really in function extractAttributes - it returns wrong results when routes are declared with parameters. WORKAROUND 😃:
public function handle($request, Closure $next) { // If the URL of the request is in exceptions. if ($this->shouldIgnore($request)) { return $next($request); } $app = app(); if (app()->router->currentRouteName() !== ‘’) { $routeName = ‘routes.’.app()->router->currentRouteName(); } else { $routeName = $app[‘laravellocalization’]->getRouteNameFromAPath($request->getUri()); } $app[‘laravellocalization’]->setRouteName($routeName); return $next($request); }
With these changes getLocalizedURL should be working. I think this will help you! Good Luck!