Ben
Ben

Reputation: 11198

Localization in emails with Laravel 5

I'm new to Laravel and trying to send an e-mail after a user signed up via a form. I'm using the default authentication files shipped with Laravel and modified them. For example: I want only to require an unique emailadress for registration, then send the credentials and automaticly login the new user.

For maybe future purposes I'm also working with the language files where I store my translations.

I've customized the AuthController to also accept a MailerContract and within the postRegistration function I'm sending the e-mail like below:

/**
     * Handle a registration request for the application.
     *
     * @param  \Illuminate\Foundation\Http\FormRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function postRegister(Request $request)
    {
        $validator = $this->registrar->validator($request->all());

        if ($validator->fails())
        {
            $this->throwValidationException(
                $request, $validator
            );
        }

        $input = $request->all();
        $input['password'] = str_random(8);

        $this->auth->login($this->registrar->create($input));

        $this->mailer->send('emails.welcome', ['email' => $input['email'], 'password' => $input['password']], function($message) use($input) {
            $message->to($input['email'])->subject(trans('emails.welcome:subject'));
        });

        return redirect($this->redirectPath());
    }

I've noticed that my subject-value is not translated. How to use language files in the Controllers/Traits? Doesn't Laravel pick this up from the view?

As asked, a part of the language file: (/resources/lang/nl/emails.php)

<?php

return [
    'welcome:subject' => 'Uw inloggegevens' // Dutch for Your login credentials
];

Upvotes: 1

Views: 5824

Answers (2)

Ben
Ben

Reputation: 11198

I ran into another thing with localization and asked a question about that on SO. Seems like using a middleware is the solution to this, because that sets the correct language in the app.

With thanks to @lukasgeiter, the answer can be found here: Localization with Laravel5 without domain.com/language/

Upvotes: 0

Bj&#246;rn
Bj&#246;rn

Reputation: 5876

You need to use multidimensional arrays in /resources/lang/nl/emails.php:

return [
    'welcome' => [
        'subject' => 'Uw inloggegevens',
    ],
];

And use dot notation instead of colon in the trans function:

$message->to($input['email'])->subject(trans('emails.welcome.subject'));

Upvotes: 1

Related Questions