How to pass locale parameter from job into controller?

224 Views Asked by At

On laravel site making Http Post request inside of job handle I need to pass locale parameter. And this locale must be used in controller.

I use astrotomic/laravel-translatable and defined in config/translatable.php with default 'en' :

'locales' => [
    'en',
    'fr',
...



'locale' => null,
...

My BannersCRUDTest job

<?php

namespace App\Jobs;

class BannersCRUDTest implements ShouldQueue
{
    public function handle()
    {
        $currentLocale = 'fr'; // I path French

        App::setLocale($currentLocale);
        session()->put('locale', $currentLocale); // That does not work...

        $response = Http::post(route('banners.filter' ));

But checking in related Controller with getLocale method I see English

class BannerController extends Controller
{

    public function filter(): array
    {
        \Log::info(' -1 BannerController app()->getLocale()::'); // I see 'en' value
        \Log::info(app()->getLocale();
        ...

How have I to pass locale from Job to Controller ? I prefer to use App::setLocale, not as parameter in url of request...

"laravel/framework": "^9.19",
"guzzlehttp/guzzle": "^7.2",
"astrotomic/laravel-translatable": "^11.11",
"mxl/laravel-job": "^1.3",

Thanks in advance!

1

There are 1 best solutions below

7
On

You use a job (BannersCRUDTest) to set locale from 'en' to 'fr'

This job of yours is set to be queued.

  • Make sure you have call / dispatch this job properly.
  • And also make sure the queue process is properly run.