Open nikuscs opened 6 years ago
Hello there,
I have the same "issue" using the latest (1.3.9
) version.
In the (simplified) routes/web.php
file I have:
<?php
Route::group([
'prefix' => LaravelLocalization::setLocale(),
'middleware' => [
'localize',
'localizationRedirect',
'localeSessionRedirect',
'localeViewPath'
]], function() {
// fr/tableau-de-bord
// en/dashboard
Route::prefix(LaravelLocalization::transRoute('routes.dashboard'))
->group(function () {
// fr/tableau-de-bord/compte
// en/dashboard/account
Route::get(LaravelLocalization::transRoute('routes.dashboard_account', 'DashboardController@account')
->name('dashboard.account');
});
});
and in the lang files:
<?php
// resources/lang/en/routes.php
return [
'dashboard' => 'dashboard',
'dashboard_account' => 'account',
];
// resources/lang/fr/routes.php
return [
'dashboard' => 'tableau-de-bord',
'dashboard_account' => 'compte',
];
The default language used is fr
.
http://localhost/fr/tableau-de-bord/compte
and http://localhost/en/dashboard/account
both works IF I access them directly in the browser.
But if I try to change the language using the language switcher the route is not translated correctly:
fr
, the links will be http://localhost/fr/tableau-de-bord/compte
and http://localhost/en/tableau-de-bord/compte
en
the links will be http://localhost/fr/dashboard/account
and http://localhost/en/dashboard/account
This issue seems to be a duplicate of #290, where other users points to a limitation in the getLocalizedURL()
method that can't translate routes that are in a group using a translated prefix.
Is this a real issue or just limitation of the package?
Edit If it's a limitation it could be nice to have a little note on the README next to the language switcher example code to alert the devs of this behaviour :)
My workaround is to add the slash directly into the translation, I know it might confuse you if you are organized but its a temporary workaround for this issue
<?php
return [
# Hosting
'hosting-web' => 'web-hosting-linux-hosting',
# Services
'services-websites' => 'services/web-design-create-websites',
]
This is an example of how you can bypass this by adding the slashes directly into the translation.
I have created also a new Middleware as an alternative to Language Switcher
<?php
# Get the Localized URL
$redirect = LaravelLocalization::getLocalizedURL($lang,$url,[],true);
# Check if we have found a route by the URL Name
$isTranslatedRoute = LaravelLocalization::getRouteNameFromAPath($redirect) !== false;
# Route is not a translated one, so we we will just redirect back since the language is set
# we also here make sure to ignore the home url or trailing slashes compare
$nonLocalizedUrl = rtrim(LaravelLocalization::getNonLocalizedURL($redirect),'/');
if (!$isTranslatedRoute && $nonLocalizedUrl !== url()->to('/')) {
return redirect()->back();
}
# Redirect to the localized URI
return redirect()->to($redirect);
This will make sure if a NON-Translated URL is not available for this language it will redirect back to his page and not throw a 404. This used to provide a translation option at all pages even if they are not in the localized group. The previous solution was still trying to build a URL even if the URL is not in the translations group.
I'm having the same issue, Laravel 5.8, Laravel Localization 1.4.7. When placed directly in the LaravelLocalization route group, the routes get translated correctly, however, creating another route group within LL's will stop translating the route.
The workaround above works, but it makes it impossible to properly organise and group routes in larger projects.
@mcamara Is there plan to try and address this issue ? I admit I do not have the time to check the code and submit a pull request...
Me too, looking for solution. Switcher does not creating links. site.com/en/products/random-product becoms site/de/produkt/etwas
browsing site from root is fine all the way. slugs are in separate database. using astrotomic/laravel-translatable
If you a group inside the Localize Group ( Nested Group ) and the group prefix is translated the GetLocalizedURL will fail
Lets say you have "services/something"
If you translate "services" and then "something" the script will fail to translate, a workaround was adding the slash directly into the translated route ( like your example /ver/{id} ) , if you add a group with "ver" it will fail when you switch languages.
Once again thanks for this awsome package, but seems like there is still a lot of issues to deal with.