Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
coding-enjoyer opened this issue Oct 23, 2024 · 1 comment

Comments

@coding-enjoyer
Copy link

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.

@henriquemoody
Copy link
Member

Hi @coding-enjoyer!

You're setting a translator to gettext(), and if you're getting Chinese translations, it's because you have configured it to be in Chinese, and you also have the translations set for those messages. Why do you need to set up the translator if you need the messages to be in English?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants