0
votes

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 within Controller

 $validator = Validator::make($request->all(), [
      'email' => 'required|email|max:120',
 ]);

Blade

<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>

Validation Structure

resources/lang

     en

         validation.php

     fr

         validation.php

fr/validation.php

<?php

return [
    'email' => 'Veuillez insérer une adresse email valide.',
];

en/validation.php

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');
});

#ANSWER

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

1
Have you tried changing the locale in the config/app.php file directly, just to make sure that the fr language is loaded at all? - nakov
You are a genius @nakov! I tried that.. and low and behold, it changed to French. Now the question is, why does it not change on its own? What can I do to force the locale although it already knows that it should be FR based on the url? - Brad Ahrens
And how do you set the locale to be fr do you have a middleware where you check what is the locale in the url and you call App::setLocale(request('lang')); for example? Or do you use any package? - nakov
Also, good question. I have edited my original post above to include this information. To summarize, I get the language from the URI. (As the app is relatively simple, I am just taking it like this). Then, as a fallback, if there is no language selected, I set it to English. Note that for French, it is always myapp.dev/fr - Brad Ahrens
I saw your edit, so that will change the locale for just that route, but it won't persist it I believe. Take a look at this answer here might help you more. - nakov

1 Answers

4
votes

So based on the discussion in the comment below the question, the issue was that the locale was not persisted in the other routes. In order to do that you need middleware. here is a good answer for that.

Happy coding! :)