I have a web app that is frequently updated, so we use php arisan down
with the --render
option to display a helpful blade php view to let users know that we will be back soon.
The page is displayed in English, but a significant portion of my users are Francophone. I am unable to get the right locale app()->getLocale()
or relevant session session()->get('Accept-Language')
or request request()->getPreferredLanguage()
data on the rendered maintenance page. The Accept-Language
key isn't even present in the $_SERVER
variable. I suspect that is because that a significant portion of the app isn't booted up in maintenance mode.
How do I get or set the right locale information for the rendered maintenance page so that French users can see it translated?
The pre-rendered view is generated ONCE during execution of the down
command in console, NOT during an actual HTTP request from user. So, when view is rendered, there is NO access to an HTTP request object or its headers, like Accept-Language
. Finally, the resulting file contains ONLY static content that is served as-is for all requests when maintenance mode is active.
Instead of relying on a pre-rendered view, you can use Laravel's default maintenance mode behavior without --render
option.
If maintenance mode is detected, a MaintenanceModeException
is thrown, and exception handler renders errors/503.blade.php view dynamically for that request.
So you can implement your stuff in your resources/views/errors/503.blade.php view:
@php
$_supportedLanguages = ['en', 'fa'];
$_preferredLanguage = request()->getPreferredLanguage($_supportedLanguages );
app()->setLocale($_preferredLanguage );
@endphp
<h1>@lang('Maintenance_Mode')</h1><br/>
@lang('we_are_currently_performing_maintenance')
Don't forget to fill translation files in resources/lang/