laravel-localization icon indicating copy to clipboard operation
laravel-localization copied to clipboard

Route translation not working on prefixes

Open samfrjn11 opened this issue 9 years ago • 15 comments

Translation of regular routes works perfect, but as soon as I use an extra prefix, the route translation doesn't do anything.

`

Route::group(['prefix' => LaravelLocalization::setLocale(), 'middleware' => ['localeSessionRedirect', 'localizationRedirect', 'localize']], function () {
        Route::get(LaravelLocalization::transRoute('routes.home'), ['as' => 'home', function () { //this one works fine, even when I chang the language, it automatically goes from /en/home to /fr/accueil
            return view('pages.main.main');
        }]);
Route::group(['prefix' => LaravelLocalization::transRoute('routes.for-who')], function () {
Route::get(LaravelLocalization::transRoute('routes.other-businesses'), ['as' => 'other-businesses', function () { //if i try to change the language from en to fr the url goes from /en/for-who/other-businesses to /fr/for-who/other-businesses instead of /fr/pour-qui/autres-entreprises
                return view('pages.for-who.other-businesses');
            }]);
        //});
});

`

Any ideas?

samfrjn11 avatar Feb 09 '16 16:02 samfrjn11

It would be way easier to help you if you formatted your code correctly. Indent it by four spaces (or select it and press the <> button in the toolbar).

datashaman avatar Feb 24 '16 13:02 datashaman

Formated code:

Route::group([
    'prefix' => LaravelLocalization::setLocale(), 
    'middleware' => ['localeSessionRedirect', 'localizationRedirect', 'localize']
], function () {
    Route::get(LaravelLocalization::transRoute('routes.home'), [
        'as' => 'home', 
        function () { 
            // this one works fine, even when I chang the language,
            // it automatically goes from /en/home to /fr/accueil
            return view('pages.main.main'); 
        }
    ]);

    Route::group([
        'prefix' => LaravelLocalization::transRoute('routes.for-who')
    ], function () {
        Route::get(LaravelLocalization::transRoute('routes.other-businesses'), [
            'as' => 'other-businesses', 
            function () { 
                // if i try to change the language from en to fr the url goes
                // from /en/for-who/other-businesses to /fr/for-who/other-businesses
                // instead of /fr/pour-qui/autres-entreprises
                return view('pages.for-who.other-businesses'); 
            }
        ]);
    });
});

Can you show us the routes.php in your lang folder [en, fr] ?

arcanedev-maroc avatar Feb 24 '16 13:02 arcanedev-maroc

<?php
/**
 * Created by PhpStorm.
 * User: Sam Smekens
 * Date: 9/02/2016
 * Time: 9:57
 * routes.php [en]
 */
return [
    'home' => 'home',
    'for-who' => 'for-who',
    'advantages' => 'advantages',
    'features' => 'features',
    'software' => 'software',
    'hardware' => 'hardware',
    'contact' => 'contact/{ref?}',

    'territories' => 'territories',

    'cattle-slaughterhouses' => 'cattle-slaughterhouses',
    'food-production' => 'food-production',
    'food-trading' => 'food-trading',
    'fresh-meat-production' => 'fresh-meat-production',
    'meat-products' => 'meat-products',
    'pig-slaughterhouses' => 'pig-slaughterhouses',
    'poultry-slaughterhouses' => 'poultry-slaughterhouses',
    'other-businesses' => 'other-businesses',
];

samfrjn11 avatar Feb 24 '16 14:02 samfrjn11

Did you have a translated file for routes (resources/lang/fr/routes.php) ?

arcanedev-maroc avatar Feb 24 '16 14:02 arcanedev-maroc

yes, of course

<?php
/**
 * Created by PhpStorm.
 * User: Sam Smekens
 * Date: 9/02/2016
 * Time: 9:57
 */
return [
    'home' => 'accueil',
    'for-who' => 'pour-qui',
    'advantages' => 'advantages',
    'features' => 'applications',
    'software' => 'logiciel',
    'hardware' => 'materiels',
    'contact' => 'contact/{ref?}',

    'territories' => 'territoires',

    'cattle-slaughterhouses' => 'cattle-slaughterhouses',
    'food-production' => 'food-production',
    'food-trading' => 'food-trading',
    'fresh-meat-production' => 'fresh-meat-production',
    'meat-products' => 'meat-products',
    'pig-slaughterhouses' => 'pig-slaughterhouses',
    'poultry-slaughterhouses' => 'poultry-slaughterhouses',
    'other-businesses' => 'other-businesses',
];

It just doesn't redirect to the correct page. It discards the prefixes

samfrjn11 avatar Feb 24 '16 16:02 samfrjn11

I am experiencing a similar issue, lurking to see what happens. Also, English -> French. It looks like you are not a native speaker either. LOL. :P

datashaman avatar Feb 24 '16 16:02 datashaman

instead of using 'prefix' => LaravelLocalization::transRoute('routes.for-who'), why not simply using trans('routes.for-who').

And note the fr/routes.php is not fully translated, especially the other-businesses.

arcanedev-maroc avatar Feb 24 '16 16:02 arcanedev-maroc

From the source:

transRoute: Translate routes and save them to the translated routes array
(used in the localize route filter)

It does more than just translate.

datashaman avatar Feb 24 '16 16:02 datashaman

Yes, but did you see the usage of transRoute() inside of Route::group() ??

arcanedev-maroc avatar Feb 24 '16 16:02 arcanedev-maroc

I am indeed not a native speaker :P I'm from Belgium :) One of my customers needed a website with 12 different languages including translated routes. mcamara's laravel-localization does its job, but fails while redirecting to another language when using prefixes.

I am not using 'prefix' => trans('routes.for-who') because otherwise mcamara's laravel-localization won't recognize the 'for-who' prefix to be translated into 'pour-qui' or whatever translation it has to be.

I am aware that not everything was/is translated, but that's not the problem. Translations are doing fine, but if your are on /en/for-who/cattle-slaughterhouses and you change your language to french it redirects to /fr/for-who/cattle-slaughterhouses which then gets again redirected to '/fr' (main page) because the route supposedly does not exist since on 'fr' the routes are waiting for a request from /fr/pour-qui/...

samfrjn11 avatar Feb 24 '16 18:02 samfrjn11

@arcanedev-maroc i see, but I think @samfrjn11 has a point. I think examining the output of artisan route:list will be useful. The issue (I think) is the dynamic nature of the routing, only the current language's routes get created. I suspect this will work better (and be a lot easier to understand and debug) if you loop through each language and create static routes, so all languages routes exist (to be redirected to). This will also allow you to use route caching.

Disclaimer: I'm a noob to this package.

datashaman avatar Feb 24 '16 21:02 datashaman

Hmm, transRoute uses the app translator which is set to the current locale, it doesn't look easy to parameterize that so you can translate for another language besides the current. Perhaps something like this would help:

$locale = LaravelLocalization::getCurrentLocale();

foreach (LaravelLocalization::getSupportedLanguagesKeys() as $lang) {
    LaravelLocalization::setLocale($lang);

    Route::group([
        'prefix' => $lang,
        'middleware' => ['localeSessionRedirect', 'localizationRedirect', 'localize']
    ], function () {
        Route::get(LaravelLocalization::transRoute('routes.home'), [
            'as' => 'home', 
            function () { 
                // this one works fine, even when I chang the language,
                // it automatically goes from /en/home to /fr/accueil
                return view('pages.main.main'); 
            }
        ]);

        Route::group([
            'prefix' => trans('routes.for-who')
        ], function () {
            Route::get(LaravelLocalization::transRoute('routes.other-businesses'), [
                'as' => 'other-businesses', 
                function () { 
                    // if i try to change the language from en to fr the url goes
                    // from /en/for-who/other-businesses to /fr/for-who/other-businesses
                    // instead of /fr/pour-qui/autres-entreprises
                    return view('pages.for-who.other-businesses'); 
                }
            ]);
        });
    });
}

LaravelLocalization::setLocale($locale);

I don't like the context switching but it is necessary to get each language's routes defined. This should help, I think, since all the routes (in every language) will now exist. You probably want to add another layer for the routes which do not have a lang prefix (but I'd imagine the package does this already). I have not tested it.

I suspect you may have to manually add translated route names to LaravelLocalization::compiledRoutes collection as well.

datashaman avatar Feb 24 '16 21:02 datashaman

Anybody found something about this ?

mtx-z avatar Nov 27 '16 01:11 mtx-z

For anyone having similar issues as I have, I think this might be a fix:

Thanks to some kind of repost from @mtx-z (https://github.com/ARCANEDEV/Localization/issues/68) it seems that the probleem is using translated routes in a translated route prefix. If you remove the translated prefix and hardcode the translated route name, it should work.

samfrjn11 avatar Jun 09 '17 14:06 samfrjn11

Re-confirmed.

Tested with Laravel 5.5 and latest laravel-localization.

Same issue, cannot use translated prefix. The full URL need to be provided in the translated value, including the prefix. So, you still cannot group your routes by translated prefix (not tried with non-translated prefix, hardcoded one).

mtx-z avatar Sep 21 '17 17:09 mtx-z