Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Dahdoul's avatar

get current locale in app service provider

Hi,

I'm using Laravel 5.6 with with Laravel localization package, what am trying to is to get current selected language in app service provider App::getLocale() but I keep getting the default language.

I tried App::getLocale() in blade working without any problem.

Thanks,

0 likes
1 reply
Dahdoul's avatar
Dahdoul
OP
Best Answer
Level 9

Find a solution, but I don't know if it's good practice

public function boot() {

//did not work here <-------

 view()->composer('*', function (View $view) {

            $locale = App::getLocale();

            Carbon::setUtf8(true);
            Carbon::setLocale($locale);

            if ($locale === 'en') {
                $cities = City::pluck('name_en', 'id');
                $shopTypes = ShopType::pluck('name_en', 'id');
                $categories = Category::pluck('name_en', 'id');
            } else {
                $cities = City::pluck('name_ar', 'id');
                $shopTypes = ShopType::pluck('name_ar', 'id');
                $categories = Category::pluck('name_ar', 'id');
            }

            $view->with('cities', $cities);
            $view->with('shopTypes', $shopTypes);
            $view->with('categories', $categories);

        });
}

Please or to participate in this conversation.