Reputation: 10886
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);
Upvotes: 4
Views: 1359
Reputation: 355
When you use Mail::to(), you can use locale() to choose the translation
Mail::to($user->email)
->locale($user->locale)
->queue(new YourEmail());
https://laravel.com/docs/9.x/mail#localizing-mailables
Upvotes: 1
Reputation: 324
As @Vincent Decaux suggested, you need a library to make your life easier. You could use the one they use or the one I use arcanedev/localization.
Once you set a middleware, everything will be taken care of. But I am concerned regarding your comment:
Yes indeed! but then I still need to do something with the domain. For example .de or .com
Why not use the current domain (.com?) and simply add a directory /en or whatever locale you need instead? It seems inconvenient to me to change the entire domain.
Upvotes: 1
Reputation: 173
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));
Upvotes: 3