pishran / nova-rtl-theme

RTL layout for Laravel Nova.
MIT License
40 stars 10 forks source link

Service Provider boot run before app Locale change #17

Closed fh32000 closed 3 years ago

fh32000 commented 3 years ago

I have Middleware to change app Locale but ThemeServiceProvider boot run before my Middleware

`<?php

namespace App\Http\Middleware;

use Closure; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Log;


class Localization
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (session()->has('locale')) {
            app()->setLocale(session()->get('locale'));
        } elseif ($request->has('locale')) {
            app()->setLocale(request('locale'));
        } elseif (Auth::check()) {

            Log::debug('Auth::check');

            $user = request()->user();
            $prefix = $user->id;
            $locale_key = $prefix . ".locale";
            $locale_val = cache()->get($locale_key);
            if (!is_null($locale_val)) {
                app()->setLocale($locale_val);
            }

        }

        Log::info( app()->getLocale());
        return $next($request);
    }
}
`

` /**


<?php

namespace Pishran\NovaRtlTheme;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
use Laravel\Nova\Events\ServingNova;
use Laravel\Nova\Nova;

class ThemeServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Log::debug('boot');
        Log::debug(app()->getLocale());
        if (in_array(app()->getLocale(), config('nova-rtl-theme.locales', []))) {
            Nova::serving(function (ServingNova $event) {
                Nova::provideToScript([
                    'nova_rtl_theme' => [
                        'stylesheet' => config('nova-rtl-theme.stylesheet'),
                        'font_family' => config('nova-rtl-theme.font-family'),
                    ],
                ]);

                Nova::style('nova-rtl-theme', __DIR__ . '/../resources/css/theme.css');

                Nova::script('nova-rtl-theme', __DIR__ . '/../resources/js/theme.js');
            });
        }

        $this->publishes([
            __DIR__ . '/../config/nova-rtl-theme.php' => config_path('nova-rtl-theme.php'),
        ], 'nova-rtl-theme');
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->mergeConfigFrom(
            __DIR__ . '/../config/nova-rtl-theme.php', 'nova-rtl-theme'
        );
    }
}

Log:

[2021-02-22 15:10:21] local.DEBUG: boot  
[2021-02-22 15:10:21] local.DEBUG: en  
[2021-02-22 15:10:21] local.DEBUG: Auth::check  
fh32000 commented 3 years ago

@omarhen @sepehrr @FaridAghili @SadeghPM @

fh32000 commented 3 years ago

it work affer this change in your code

`        Nova::serving(function (ServingNova $event) {
            if (in_array(app()->getLocale(), config('nova-rtl-theme.locales', []))) {
                Nova::provideToScript([
                    'nova_rtl_theme' => [
                        'stylesheet' => config('nova-rtl-theme.stylesheet'),
                        'font_family' => config('nova-rtl-theme.font-family'),
                    ],
                ]);

                Nova::style('nova-rtl-theme', __DIR__ . '/../resources/css/theme.css');

                Nova::script('nova-rtl-theme', __DIR__ . '/../resources/js/theme.js');
            }
        });`
fh32000 commented 3 years ago

18

FaridAghili commented 3 years ago

Thanks Fixed in v1.5.9