karser / KarserRecaptcha3Bundle

Google ReCAPTCHA v3 for Symfony
MIT License
158 stars 21 forks source link

Translation messages #43

Closed critcat closed 3 years ago

critcat commented 3 years ago

How can I set the captcha language for different locales? Is there some option for this?

karser commented 3 years ago

You should install the Symfony Translation component. Then replace the validation text with the translation keys for the message and messageMissingValue options:

#config/validator/validation.yaml
App\Entity\YourEntity:
    properties:
        captcha:
            - Karser\Recaptcha3Bundle\Validator\Constraints\Recaptcha3:
                message: 'karser_recaptcha3.message'
                messageMissingValue: 'karser_recaptcha3.message_missing_value'

Add English, Spanish, or any other translation:

# translations/validators/validators.en.yaml
karser_recaptcha3.message: 'Your computer or network may be sending automated queries'
karser_recaptcha3.message_missing_value: 'The captcha value is missing'

# translations/validators/validators.es.yaml
karser_recaptcha3.message: 'Es posible que su computadora o red esté enviando consultas automatizadas'
karser_recaptcha3.message_missing_value: 'Falta el valor de captcha'

Please let me know if it worked for you.

critcat commented 3 years ago

Thank you for your quick response!

I tried using the settings in config/validator/validation.yaml file, but this throws a 500 error

Property "captcha" does not exist in class "App\Entity\User"

Instead, I changed the code in the form class:

->add('captcha', Recaptcha3Type::class, [
     'constraints' => new Recaptcha3 ([
         'message' => 'karser_recaptcha3.message',
         'messageMissingValue' => 'karser_recaptcha3.message_missing_value',
     ]),
     'action_name' => 'registration',
])

And it worked great.

karser commented 3 years ago

I'm glad it worked for you :+1: