Skip to content

Respect Validation in Webman framework returns messages in Chinese instead of English #1468

@coding-enjoyer

Description

@coding-enjoyer

Description
I'm using the Respect Validation library within the Webman framework, and the validation error messages are being returned in Chinese by default. I would like to change the error messages to English.

Steps to Reproduce

  1. Install and configure Webman with Respect Validation. Or you can use my minimal reproduction : https://github.com/coding-enjoyer/Repro-Webman-Respect-Validation
  2. Run composer install, and start the server using : php start.php start
  3. Send POST request to http://localhost:8787/auth/login with empty JSON to trigger validation error.
  4. Observe the json response that the validation message is returned in Chinese.

Expected Behavior
Validation error messages should be returned in English by default, or there should be an easy way to configure the default language.

Current Behavior
The validation messages are returned in Chinese, and I haven't found a way to switch them to English.

trait HasValidation
{
    public array $validationResult = [];

    public function check(): ?array
    {
        if (count($this->validationResult) > 0) {
            return $this->validationResult;
        }

        return null;
    }
}

Example of name validation :

class UserEntity extends BaseEntity
{
    use HasValidation;

    public function __construct(
        public mixed $name,
        public mixed $password,
        public mixed $email,
    ) {}

    public function validateName()
    {
        try {
            (new Rules\AllOf(
                new Rules\Alnum(),
                new Rules\NoWhitespace(),
                new Rules\Length(1, 255)
            ))->assert($this->name);
        } catch (ValidationException $e) {
            $this->validationResult['name'] = $e->getMessage();
        }

        return $this;
    }
class AuthController
{
    public function index(Request $request)
    {
        Factory::setDefaultInstance(
            (new Factory())->withTranslator('gettext')
        );

        $validation = (new UserEntity(
            name: $request->input('name'),
            password: $request->input('password'),
            email: $request->input('email')
        ))->validateName()
            ->validatePassword()
            ->validateEmail()
            ->check();

        return json(['code' => 0, 'msg' => 'ok', 'data' => $validation]);
    }
}

image

Environment
PHP version: 8.3.12
Webman version: 1.5.0
Workerman/Respect Validation version : 3.1
Respect/Validation version: X.X.X

Additional Context
I've changed the default locale to "en" but no result.
I've checked the documentation but couldn't find a clear way to set the language for validation messages in Webman.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions