Reputation: 5168
I have a problem that should be relatively simple, but I've read through the documentation and multiple posts here and I still can't figure it out. :(
I am creating a Laravel application with localisation (EN & FR). Everything works fine on the page in terms of localisation. However, when it comes to validation, I cannot seem to get the errors to translate.
Here is some sample code:
Note that I have removed mostly irrelevant code to make this example cleaner.
$validator = Validator::make($request->all(), [
'email' => 'required|email|max:120',
]);
<div class="form-group">
<label for="input-email">@error('email') {{ $errors->first('email') }} @else @lang('index.email') @enderror</label>
<input type="email" name="email" placeholder="@lang('index.email')" value="{{ old('email') }}">
</div>
resources/lang
en
validation.php
fr
validation.php
<?php
return [
'email' => 'Veuillez insérer une adresse email valide.',
];
php
return [
'email' => 'Please insert a valid email address.',
];
I have tried: - to clean the cache:
php artisan cache:clear
One thing that I have noticed is that it seems to always return with the validation in English. It seems to ignore the fr/validation.php file.
Does anyone know why? Is there someway to force the locale within the validator? Or, am I missing something here? I greatly appreciate your comments, suggestions, and thoughts!
Thanks @nakov for the suggestion. I am able to force the language withint he config/app.php file.
'locale' => 'fr',
Here, it will change the locale for the validation, which is awesome. Now, the question is, why is the "link" missing / broken within the controller? I.e., knowing that the applocale has been set to FR, why is it bringing back the EN version of validation.php? Can I force this within the controller?
Here is how I have the Routing setup to set the locale:
Route::get('/{lang?}', function($lang = 'en') {
App::setLocale($lang);
return view('welcome');
});
Thanks to @nakov for all of your help and suggestions!
So, the problem was that the locale does not persist and therefore was being lost upon refresh when validation failed.
How did I fix it?
Within the controller:
"use" the application
use Illuminate\Foundation\Application;
bring it into your function, in my case it is store:
public function store(Application $app, Request $request)
within Blade, set the locale within a hidden input. I named mine "language", but feel free to name it as you like. Then, again within the controller, set the locale to this language
$app->setLocale($request->language);
and bingo! Your problem is solved. :) Thanks again @nakov!
Thanks!
Brad
Upvotes: 1
Views: 7106