Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 \App\Mail\Test();
  $test->locale('de');
  \Mail::to('[email protected]')->send($test);

Then the mail is not using my locale file from resources/lang/de/list.php

<?php 

   return [ 'test' => 'Dies ist ein Test'];

Why is that?

like image 751
Adam Avatar asked Mar 06 '23 21:03

Adam


2 Answers

Use locale with Mail Facade.

$test = new \App\Mail\Test();
\Mail::to('[email protected]')->locale('de')->send($test);

Mail Facade and Mailable refers to different classes. for using locale() with Mailable try this.

 $test = new \App\Mail\Test();
 $test->locale('de')->send();
like image 131
Romantic Dev Avatar answered Mar 15 '23 01:03

Romantic Dev


Try passing the locale in to the constructor and setting then setting it in the build function:

public $locale;

public function __construct(string $locale = 'de')
{
    $this->locale = $locale;
}

public function build()
{
    return $this->locale($this->locale)
                ->from('[email protected]')
                ->view('emails.example');
}
like image 32
Brian Lee Avatar answered Mar 15 '23 00:03

Brian Lee