Laravel: Cannot set locale in Mailable

Multi tool use
Laravel: Cannot set locale in Mailable
I updated to Laravel 5.6 and I want to use the new locale method from a Mailable class.
I created a mailable class with
php artisan make:mail Test --markdown="emails.test"
This is my blade file:
@component('mail::message')
@lang('list.test')
@endcomponent
If I send a mail
$test = new AppMailTest();
$test->locale('de');
Mail::to('myemail@test.com')->send($test);
Then the mail is not using my locale file from resources/lang/de/list.php
resources/lang/de/list.php
<?php
return [ 'test' => 'Dies ist ein Test'];
Why is that?
2 Answers
2
Use locale with Mail Facade.
$test = new AppMailTest();
Mail::to('myemail@test.com')->locale('de')->send($test);
Mail Facade and Mailable refers to different classes. for using locale()
with Mailable try this.
locale()
$test = new AppMailTest();
$test->locale('de')->send();
Mailable
@Adam have a look at my updated answer
– Adnan Mumtaz
Jul 3 at 7:49
Try passing the locale in to the constructor and setting then setting it in the build
function:
build
public $locale;
public function __construct(string $locale = 'de')
{
$this->locale = $locale;
}
public function build()
{
return $this->locale($this->locale)
->from('example@example.com')
->view('emails.example');
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
This works. But isn't locale a method on the
Mailable
object?– Adam
Jul 3 at 7:42