Why Changing Locale in Laravel Often Fails (And How to Fix It)
Attempting to switch application languages via a dropdown is a common requirement, yet it frequently breaks without warning. The code snippet provided illustrates a classic setup: a form that submits on change using onchange="this.form.submit()", a route handler that calls \App::setLocale(), and language-specific JSON files in the resources directory. Despite this seemingly complete architecture, the locale switch remains ineffective. The root cause isn't missing logic in your route; it's a fundamental misunderstanding of how Laravel loads translations at runtime versus how you are attempting to override them.
The Static Nature of Translated Views
The immediate issue lies in the structure of your resources folder and the nature of Blade templates. You have organized your files into /en/navigation.php and /du/navigation.php. This approach suggests a static file system architecture, but Laravel does not work this way out of the box. Unless you are using an older version of Laravel or have explicitly configured a custom view path that ignores the translation engine, your application is likely rendering views from resources/views.
When you call \App::setLocale('du'), Laravel updates the internal state used by the __('key') helper and the $t bag. However, if your Blade template contains hardcoded strings like 'Home-test-dutch' or 'Home', the locale switch has absolutely no effect on those specific elements. The translation engine is bypassed entirely because the view file itself contains the literal text rather than a translation key.
To make the locale change work, your views must be dynamic. You need to replace static strings with translation keys using the __('key') helper function or the {{ __('key') }} Blade syntax. For example, instead of returning 'Home' in your English file, the view should look like:
<div class="nav-item">{{ __('navigation.home') }}</div>
If you are using a JSON-based translation loader (which is standard for Laravel 5+), ensure that your resources/lang/du/navigation.php and resources/lang/en/navigation.php files are correctly keyed. The route handler sets the locale, but without corresponding translation keys in the view, the user will see no change.
Route Handling and Session Persistence
Your current route definition relies on \App::setLocale(Request::Input('locale')). While this technically sets the global locale for the request context, it resets on every subsequent request because Laravel's application instance is stateless by default in a web server environment.
If your navigation.php files are being loaded via require or include, setting the locale in the route won't help unless the view rendering happens immediately after. A more robust solution involves persisting the user's preference in the session. This ensures that if a user navigates to another page (e.g., from the navigation link back to the dashboard), the language preference is remembered without requiring them to select it again.
Here is how you should modify your route:
Route::post('/locale', function(Request $request) {
// Store the selected locale in the session
$user->locale = $request->input('locale');
// Or simply: Session::put('locale', $request->input('locale'));
return redirect()->back();
})->middleware('auth');
Additionally, ensure your AppServiceProvider boots up translations correctly. In modern Laravel versions (5.4+), you might find \App::setLocale() is deprecated in favor of the app()->setLocale() method or configuring it within the service provider's boot method.
File Structure and Translation Loading
Your source material mentions a path structure of resources/du/navigation.php. This implies a custom view path. If you are relying on this custom path, ensure that your .php files are actually being included by Laravel's autoloader or configured in config/view.php. However, the most reliable and standard approach is to keep views in resources/views and place translations strictly in resources/lang/{locale}/{group}.php.
If you stick with JSON translation files, ensure that your resources/lang/du/navigation.php looks like this:
<?php return [
'navigation.home' => 'Home-test-dutch',
];
And for English:
<?php return [
'navigation.home' => 'Home',
];
Conclusion
The failure of your locale switch is not due to a broken route but rather a disconnect between the static content in your views and the dynamic translation system. By replacing hardcoded strings with translation keys and ensuring that the locale state persists across requestsâeither via session or application configurationâyou can achieve a seamless multilingual experience. If you require deeper assistance with legacy view structures or custom localization strategies, LaravelCompany provides specialized resources for enterprise-grade localization implementations.