Kontaktformular in Laravel

Problem encountered at our web agency: The contact form on our website, built with the PHP framework Laravel, is available in two languages: German and English. When the German contact form is submitted, the administrator receives an email in German and can easily attach this email when replying to or forwarding it. However, when the English version of the contact form is submitted, the administrator also receives the email in German.

 

 

The assumption that the Blade file contains German text rather than translations leads us to the following file:

 

/resources/views/layouts/emails/contact/contact.blade.php

 

In this file, we replace all German text with

 

@lang('main.IDENTIFIER'),

 

where main can of course be replaced with any section, such as "layouts", etc.

 

We then add any missing texts to the corresponding language file (in this case, English):

 

/resources/lang/en/main.php

 

And does it work now? No. However, the German translations are now being loaded and displayed when the English contact form is submitted. So the @lang tags have been implemented correctly. Could the wrong language therefore be selected when the email is sent? Let's take a look at the following file:

 

/app/Http/Controllers/ContactController.php

 

If we output the current language here using

 

echo \App::getLocale();

 

we get 'de'. So, before sending the email, we add the following instructions:

 

$lang = substr(explode($request->header('host'), $request->header('referer'))[1], 1, 2) === 'en' ? 'en' : 'de';

\App::setLocale($lang);

 

And that solves the problem!

 

Tested at our web agency on a website running Laravel 6.0.4.