framework icon indicating copy to clipboard operation
framework copied to clipboard

[11.x] Fixing a missing translation error

Open andrey-helldar opened this issue 1 year ago • 0 comments

Laravel Version

11.0.5

PHP Version

8.3.3

Database Driver & Version

PostgreSQL 14

Description

After upgrading from Laravel 10 to Laravel 11, validation error translation started working incorrectly.

Laravel 10 could translate the phrases (and :count more error) and (and :count more errors), but Laravel 11 cannot. Because of this, English text appears in the response, which looks like an error.

Laravel 10:

[{
    "message": "Le champ foo est obligatoire. (et 3 erreurs en plus)",
    // ...
}]

Laravel 11:

[{
    "message": "Le champ foo est obligatoire. (and 3 more errors)",
    // ...
}].

I have not been able to add a test case to my PR because despite requiring a dependency to be installed for translation, I get an error:

config()->set('app.locale', 'fr');

// Illuminate\Contracts\Container\BindingResolutionException: Target class [config] does not exist.

Also the phrase The given data was invalid. is no longer translated.

The source of the problem is in this location:

https://github.com/laravel/framework/blob/6e1e7850a3e13a84d76d5b474a4d38a26924a22b/src/Illuminate/Validation/ValidationException.php#L86-L103

Steps To Reproduce

  1. Add the code to the routes/console.php file:
    use Illuminate\Support\Facades\Artisan;
    use Illuminate\Support\Facades\Validator;
    use Illuminate\Validation\ValidationException;
    
    Artisan::command('foo', function () {
        try {
            config()->set('app.locale', 'fr');
    
            app('translator')->setLoaded([
                '*' => [
                    '*' => [
                        'fr' => [
                            ' (and :count more error)' => ' (et :count erreur en plus)',
                            ' (and :count more errors)' => ' (et :count erreurs en plus)',
                            'validation.required' => 'Le champ :attribute est obligatoire.',
                        ]
                    ]
                ]
            ]);
    
            Validator::make([], [
                'foo' => ['required'],
                'bar' => ['required'],
                'baz' => ['required'],
            ])->validate();
        } catch (ValidationException $e) {
            dd($e->getMessage());
        }
    });
    
  2. Execute the console command php artisan foo
  3. See the error

andrey-helldar avatar Mar 13 '24 19:03 andrey-helldar