laravel-graphql icon indicating copy to clipboard operation
laravel-graphql copied to clipboard

Localization

Open SonarSoftware opened this issue 7 years ago • 2 comments

For all built in errors (e.g. 'In field "x": Expected type "String", found "y"), can these be localized? Laravel now offers a way for packages to present localized errors in a custom folder.

SonarSoftware avatar Sep 26 '17 00:09 SonarSoftware

The way I am handling it is but having all of them set to "String!" and using validation rules with custom messages, like:

    public function args()
    {
        return [
            'id' => [
                // Do not verify type here because we want to have custom validation messages.
                'type' => Type::string(),
                'rules' => ['required', 'integer'],
            ],
            'token' => [
                'type' => Type::string(),
                'rules' => ['required'],
            ],
        ];
    }

    public function validationErrorMessages($root, $args, $context)
    {
        return [
            'id.required' => \Lang::get('validation.required', ['attribute' => 'ID']),
            'id.integer' => \Lang::get('validation.integer', ['attribute' => 'ID']),
            'token.required' => \Lang::get('validation.required', ['attribute' => 'token']),
        ];
    }

I am not sure if it's the best way, but it is what I've found.

Note that for Query you also need to add:

class UserQuery extends Query
{
    use ShouldValidate;
    ...
}

deivamagalhaes avatar Jul 19 '18 00:07 deivamagalhaes

@SonarSoftware, You can see issues #266 and #261 for more info.

mohammad-fouladgar avatar Jul 23 '18 09:07 mohammad-fouladgar