App\Services\Locale\CurrentLocale::determine PHP Method

determine() public static method

public static determine ( ) : string
return string
    public static function determine() : string
    {
        if (request()->isBack()) {
            return config('app.backLocales')[0];
        }
        $urlLocale = app()->request->segment(1);
        if (static::isValidLocale($urlLocale)) {
            return $urlLocale;
        }
        try {
            $cookieLocale = app(Encrypter::class)->decrypt(request()->cookie('locale'));
            if (self::isValidLocale($cookieLocale)) {
                return $cookieLocale;
            }
        } catch (Exception $exception) {
        }
        $browserLocale = collect(request()->getLanguages())->first();
        if (self::isValidLocale($browserLocale)) {
            return $browserLocale;
        }
        return app()->getLocale();
    }

Usage Example

コード例 #1
0
 public function boot()
 {
     $this->app->setLocale(CurrentLocale::determine());
     $this->registerMacros(app(\Illuminate\Routing\Router::class));
     parent::boot();
 }