Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Laravel event to catch locale change in your own application #61

Merged
merged 4 commits into from Jan 9, 2024

Conversation

michaelklopf
Copy link

The package is good to change the language in the browser, but we want to save the users locale in the database. Currently that's not possible.

We introduce a Laravel event that gets dispatched when the locale is changed.

In your own application you can listen for this event and add your own listener that uses the locale string to save it in the database like this:

In the EventServiceProvider

    protected $listen = [
        LocaleChanged::class => [
            UserChangedLocale::class,
        ],
    ];

and in the listener

    public function handle(object $event): void
    {
        Filament::auth()->user()->update(['locale' => $event->locale]);
    }

To load the users locale in your application you can add your own middleware to the panel providers where you set the locale from the database

    public function handle(Request $request, Closure $next): Response
    {
        if (filament()->auth()->check()) {
            app()->setLocale(filament()->auth()->user()->locale);
        }

        return $next($request);
    }

@bezhanSalleh bezhanSalleh linked an issue Dec 30, 2023 that may be closed by this pull request
@bezhanSalleh bezhanSalleh merged commit 86a0825 into bezhanSalleh:main Jan 9, 2024
9 checks passed
@bezhanSalleh
Copy link
Owner

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Save user locale in database
3 participants