5
votes

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?

2

2 Answers

7
votes

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();
1
votes

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');
}