Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel get route by locale

Tags:

php

laravel

Currently, I've got the following route in my web app.

Route::get(__('routes.account.dogs'), 
    [DogController::class, 'index'])->name('dog.index')->middleware('auth');

I specify the URL translation for the route in two different language files.

return [
    //Account
    'account' => [
        'dogs' => 'honden',
    ]
];

When I send a queued email with this link, I want to translate the link on the user's locale (saved in the database). So I want to get this route based on the user's locale, but app()->setLocale($user->locale); cannot be used because it's a queued job. How can I fix this?

In a perfect world it would be something like:

route('dog.index', [], $user->locale);
like image 511
Jamie Avatar asked Oct 25 '25 05:10

Jamie


1 Answers

laravel __() helper function also accepts the optional 3rd argument $locale for setting the locale to translate.

So you could use the $user->locale in the helper as follows.

url(__('account.dogs', [], $user->locale));
like image 135
Shabeer Ahammed Avatar answered Oct 27 '25 19:10

Shabeer Ahammed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!