Closed francoisdusautoir closed 5 years ago
Any help ... ?
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 :):
change function handle(...) in file LaravelLocalizationRoutes.php like this:
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!
That workaround doesn't work for me, and I'm experiencing the problem without having parameterized routes.
@mrcbk Are you using laravel-translation-manager package at the time you found that fix? There was an issue in the other package.
Laravel trans function has 3 parameters, not 4:
public function trans($key, array $replace = [], $locale = null)
{
return $this->get($key, $replace, $locale);
}
So your pull request is invalid, same as #447.
Issue in vsch/laravel-translation-manager (fork of barryvdh/laravel-translation-manager, both can have this problem)
no, actually it's vanilla laravel 5.3: https://github.com/laravel/framework/blob/5.3/src/Illuminate/Translation/Translator.php#L235
So i guess for 5.4 it's invalid. Still if you are on 5.3.
Uhm.. Then the package will need a breakpoint between Laravel 5.3 and 5.4.
I have the same issue in laravel 5.4
Experiencing the same issue (5.4)
Same issue on Laravel 5.5.
It works with 1 parameter, not with more: only locale is changing.
en/routes.php
'post' => [
'index' => 'post',
'show' => 'post/{post}',
]
fr/routes.php
'post' => [
'index' => 'post-fr',
'show' => 'post-fr/{post}',
]
route/web.php
Route::name( 'post.' )
->group( function () {
Route::get( LaravelLocalization::transRoute( 'routes.post.index' ), 'PostController@dashboard' )->name( 'index' );
Route::get( LaravelLocalization::transRoute( 'routes.post.show' ), 'PostController@show' )->name( 'show' )->where( 'post', '[0-9]+' );
} );
(Note that the "post" ("post-fr" in french) segment in english version would normally be implemented as a Route group translated prefix, but it does not work as of #290 - prefix is so directly included in all routes translations values when needed).
getLocalizedURL()
is working great for "index" route, but not working for "show" route.
Index route output:
EN: http://domain.com/en/post
FR: http://domain.com/fr/post-fr
Show route output:
Locale is EN ->
EN: http://domain.com/en/post/1
FR: http://domain.com/fr/post/1
(should be http://domain.com/fr/post-fr/1
)
Locale is FR ->
EN: http://domain.com/en/post-fr/1
(should be http://domain.com/en/post/1
)
FR; http://domain.com/fr/post-fr/1
In the end, we can "tweak" it by setting same naming pattern on Route names and Route translated names. Being able to do something like :
LaravelLocalization::getURLFromRouteNameTranslated($localeCode, 'routes.' . Route::currentRouteName(), Route::current()->parameters, false);
as getURLFromRouteNameTranslated()
is working great if you pass the URL parameters to the method. In this case, just be careful with Laravel Route model binding. As the Parameter value will not be the ID (int/string) anymore, but the found model instance (ID based if not overided). I made a quick helper to send back the ID if the model as replaced the real URL parameter value.
This is not perfect and have limitations (translated route naming), but as getLocalizedURL()
is "not safe enough", it's a way to achieve a dynamic language switcher.
Related at ARCANEDEV/Localization#68, but not tested again this specific issue route name settings (2 translated parameter all from route translation file, here i have 1 variable wich is the ID).
Same here, both with Laravel 5.4 and 5.5.
More precisely, it doesn't work only for the last parameter of the route. If my route has two parameterers, only the first is localized. If it has three parameters only first two are translated, etc.
Anyone has found where is located that issue?
To @mtx-z, also LaravelLocalization::getURLFromRouteNameTranslated
doesn't work fine for me.
What's wrong with getUrlFromRouteTranslated()? You need to provide the route name from the translation file, not the one from laravel router.
Yes, I use the routes name from the translation file but both getLocalizedURL
and getURLFromRouteNameTranslated
work as described in my previous comment.
I found a working workaround:
$routeName = 'routes.' . Route::currentRouteName();
$translatedParameters = current(event('routes.translation', [$localeCode, Route::current()->parameters]));
$translatedUrl = LaravelLocalization::getURLFromRouteNameTranslated($localeCode, $routeName, $translatedParameters);
You also matched router route name and translated route name?
Hi, here’s my (temporary for the project I'm working on) solution.
Please carefully read the commented documentation, and note that my fellow back-end developer colleague still has to review it.
The workaround I found does not work with routes defined as resources. A small variation is needed:
$routeName = 'routes.' . Route::currentRouteName();
$translatedParameters = current(event('routes.translation', [$localeCode, Route::current()->parameters]));
if (!empty($translatedParameters)) {
$itemUrl = LaravelLocalization::getURLFromRouteNameTranslated($localeCode, $routeName, $translatedParameters);
} else {
$itemUrl = LaravelLocalization::getLocalizedURL($localeCode);
}
@mcamara Could we find a definitive solution to this problem?
The author apparently is no longer interested in further development of the package - it's time to look for something else.
@Grupa-Leonardo any suggestion?
@fede91it At this moment - nothing, but for next projects I'll look for somethink different. Now I must make it more 'statically' -.-
take a look at this: https://stackoverflow.com/questions/42527880/laravel-5-4-translate-routes I think it may be useful with some changes but idea is clear. You may have to use named routes in route definitions. Good luck!
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:
fede91it, this is great! Thank you!
Just out of curiosity, how would this package be able to fetch the translation of a slug from the database?
As far as I can tell this package only talks to the routes file and the files that hold the translations of named routes, for example resources/lang/fr/routes.php
Anyone?
@stefkes I think you should use another package.
I use myself mcamara/laravel-localization for route translations, and dimsav/laravel-translatable for model translations (slug, and all fields that you want translated).
@francoisdusautoir is this issue still present? getLocalizedURL
seems to return the corrected translated urls now, even if you use parameters.
Closing this as its seems to be fixed by now
Hi. Language switcher works with translated slug like this?
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 :
This is what I should have :
On my header.blade.php, this is what I have :
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 ... :/