From a9ef6c33f9f39e9e48a5a1546dcf958c6a8ecadb Mon Sep 17 00:00:00 2001 From: 3Dgoo Date: Wed, 10 Jan 2024 21:58:42 +1030 Subject: [PATCH] hCaptcha default language change (#9) --- README.md | 6 ++++-- src/Forms/HCaptchaField.php | 8 +++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index dffeda2..f09d024 100644 --- a/README.md +++ b/README.md @@ -42,8 +42,10 @@ X3dgoo\HCaptcha\Forms\HCaptchaField: We generate our site key and secret key at https://www.hcaptcha.com/ -The language of the captcha can be changed by setting the language_code variable to any of the -[available hCaptacha languages](https://docs.hcaptcha.com/languages/): +The default language of the captcha will be set to the site language. This can be changed by setting +the language_code variable to any of the +[available hCaptacha languages](https://docs.hcaptcha.com/languages/). If not set this will default +to the current site locale: ```yml X3dgoo\HCaptcha\Forms\HCaptchaField: diff --git a/src/Forms/HCaptchaField.php b/src/Forms/HCaptchaField.php index 3b6cfaf..6cc5bee 100644 --- a/src/Forms/HCaptchaField.php +++ b/src/Forms/HCaptchaField.php @@ -7,6 +7,7 @@ use SilverStripe\Core\Injector\Injector; use SilverStripe\Forms\FormField; use SilverStripe\Forms\Validator; +use SilverStripe\i18n\i18n; use SilverStripe\ORM\FieldType\DBHTMLText; use SilverStripe\View\Requirements; @@ -59,9 +60,8 @@ class HCaptchaField extends FormField /** * Captcha language code * @var string - * @default en */ - private static $language_code = 'en'; + private static $language_code; /** * Creates a new HCaptcha field. @@ -95,8 +95,10 @@ public function Field($properties = []) ); } + $languageCode = self::config()->language_code ?? i18n::getData()->langFromLocale(i18n::get_locale()); + Requirements::javascript( - 'https://hcaptcha.com/1/api.js?hl=' . self::config()->language_code + 'https://hcaptcha.com/1/api.js?hl=' . $languageCode ); return parent::Field($properties);