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

Get the default local from the Laravel config file before the browser #70

Closed
wants to merge 2 commits into from

Conversation

alkoumi
Copy link

@alkoumi alkoumi commented Jan 10, 2024

To follow Laravel way If I want to start my app using the config('app.locale') by Editing the app file in config directory

'locale' => 'ar',

Then we must put this line

?? config('app.locale', 'en');

above this line

$this->getBrowserLocale($request)

then the final result in the SwitchLanguageLocale Middleware is

$locale = session()->get('locale')
				?? $request->get('locale')
				?? $request->cookie('filament_language_switch_locale')
				?? config('app.locale' , 'en')
				?? $this->getBrowserLocale($request);

@alkoumi
Copy link
Author

alkoumi commented Jan 10, 2024

I already test this in my application

@markosole
Copy link

markosole commented Jan 13, 2024

Hi @alkoumi I am looking for something similar, I would like to set language per user settings set in DB just as initial if cookie isn't set. I can see that language switcher is setting cookie filament_language_switch_locale but it's encrypted.

UPDATE:

I've made Middleware which is then cheching authenticated user's lang prefs. Maybe not ideal solution but works well for me and my use case.

<?php

// namespace BezhanSalleh\FilamentLanguageSwitch\Http\Middleware;
namespace App\Http\Middleware;

use BezhanSalleh\FilamentLanguageSwitch\LanguageSwitch;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


class DynamicUserLanguage
{
    public function handle(Request $request, Closure $next): \Illuminate\Http\Response | \Illuminate\Http\RedirectResponse
    {
        $locale = session()->get('locale')
            ?? $request->get('locale')
            ?? $request->cookie('filament_language_switch_locale')
            ?? $this->getUsersDefaultLanguage()
            ?? config('app.locale', 'en');
        
        if (in_array($locale, LanguageSwitch::make()->getLocales())) {
            app()->setLocale($locale);
        }
        return $next($request);
    }

    private function getBrowserLocale(Request $request): ?string
    {
        $appLocales = LanguageSwitch::make()->getLocales();
        $userLocales = preg_split('/[,;]/', $request->server('HTTP_ACCEPT_LANGUAGE'));
        foreach ($userLocales as $locale) {
            if (in_array($locale, $appLocales)) {
                return $locale;
            }
        }
        return null;
    }

    private function getUsersDefaultLanguage(){
        if(Auth::user()){
            return Auth::user()->user_language;
        }
        return null;
    }
}

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.

2 participants