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

Laravel Livewire - wrong redirect when using Livewire right before switching the language

Open renepardon opened this issue 4 years ago • 6 comments

Describe the bug I use Laravel Jetstream with Livewire and integrated a language Selector in the top bar.

Bildschirmfoto 2021-02-02 um 15 23 46
    @foreach (LaravelLocalization::getSupportedLocales() as $code => $info)
        <x-jet-dropdown-link href="{{ LaravelLocalization::getLocalizedURL($code, null, [], true) }}">
            {{ $info['native'] }}
        </x-jet-dropdown-link>
    @endforeach

To Reproduce Steps to reproduce the behavior:

  1. Install Laravel, Jetstream (with Livewire), laravel-localization
  2. Configure middlware and prefix like this:
    [
        'prefix'     => LaravelLocalization::setLocale(),
        'middleware' => ['web', 'localeCookieRedirect', 'localizationRedirect', 'localeViewPath'],
    ],
  1. Go to your user profile: http://127.0.0.1:8000/en/user/profile
  2. Hit the save button for profile information and immediately after hitting the save button switch the locale
  3. See 404 error because of a redirect to: http://127.0.0.1:8000/de/livewire/message/navigation-menu

Expected behavior An AJAX call/request to the application should not updated the last visited route. If I stay on http://127.0.0.1:8000/en/user/profile and want to switch the language to de, then I expect to land at http://127.0.0.1:8000/de/user/profile

More info:

  • Version of Laravel: ^8.12
  • Version of the Laravel-localization package: ^1.6
  • Copy of the config file ( supportedLocales: de,en | useAcceptLanguageHeader:true | hideDefaultLocaleInURL:false).

Additional context Have a nice day <3

renepardon avatar Feb 02 '21 14:02 renepardon

What version of livewire are you on? Livewire released an update which broke integration with this library (anything above 2.3.8). Test with 2.3.8 and check until the issue is resolved. https://github.com/livewire/livewire/issues/2445

usernotnull avatar Feb 03 '21 10:02 usernotnull

I'm on ^2.0 right now which resolves to an installed version of 2.3.6 on my project. So this might not be the problem.

If you click a save button for example, an AJAX request is made by livewire. This request (POST URL) will be used by mcamara/laravel-localization for the redirect and not the current page URL I'm on.

renepardon avatar Feb 03 '21 11:02 renepardon

This has been fixed and tagged in v2.3.12 of Livewire.

calebporzio avatar Feb 03 '21 13:02 calebporzio

I don't know if I'm wrong, but I've solved the issue changing RouteServiceProvider.php like so:

public function boot()
{
    LaravelLocalization::setLocale();
}

So, I changed routes/web.php like so:

Route::get('/', fn () => redirect('/'. LaravelLocalization::getCurrentLocale()));

Route::group(['prefix' => LaravelLocalization::getCurrentLocale(), 'middleware' => ['localize', 'localeSessionRedirect']], function() {
   // Your routes...
});

masterix21 avatar Mar 29 '21 08:03 masterix21

Thanks @masterix21. It's working for login. For the logout, I create my custom LogoutResponse.

Like this :

<?php

namespace App\Http\Responses;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use Laravel\Fortify\Contracts\LogoutResponse as LogoutResponseContract;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class LogoutResponse implements LogoutResponseContract
{
    /**
     * Create an HTTP response that represents the object.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function toResponse($request)
    {
        return $request->wantsJson()
                    ? new JsonResponse('', 204)
                    : redirect(LaravelLocalization::localizeUrl('/'));
    }
}

And inside FortifyServiceProvider.php :

public function boot()
    {
        $this->app->singleton(
            \Laravel\Fortify\Contracts\LogoutResponse::class,
            \App\Http\Responses\LogoutResponse::class
        ); 

gaetandezeiraud avatar Apr 29 '21 13:04 gaetandezeiraud

Hello, I'm preseting the same issue, I tried with @masterix21 solution but same result the url of menu is http://local.com/en/livewire/message/navigation-menu I've: "laravel/framework": "^9.19", "livewire/livewire": "^2.5", "laravel/jetstream": "^2.12",

any other idea how to solve it?

Regards

pixsolution avatar Jan 14 '24 18:01 pixsolution