From 9efe6091ea83798dc1532c32596914cc8e343960 Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Thu, 14 Nov 2024 12:55:36 +0100 Subject: [PATCH 1/4] chore: add validation translations --- extensions/package-manager/locale/en.yml | 20 ++++++++++++++++++++ extensions/suspend/locale/en.yml | 4 ++++ extensions/tags/locale/en.yml | 10 ++++++++++ framework/core/locale/validation.yml | 1 + 4 files changed, 35 insertions(+) diff --git a/extensions/package-manager/locale/en.yml b/extensions/package-manager/locale/en.yml index 54d93e731f..2299616681 100755 --- a/extensions/package-manager/locale/en.yml +++ b/extensions/package-manager/locale/en.yml @@ -161,3 +161,23 @@ flarum-extension-manager: why_not_modal: title: Why Won't it Update + + validation: + attributes: + minimum_stability: minimum stability + repositories: repositories + repositories.*: repositories + repositories.*.type: repository type + repositories.*.url: repository URL + extension_id: extension ID + update_mode: update mode + package: package + version: version + github_oauth: GitHub OAuth + github_oauth.*: GitHub OAuth + gitlab_oauth: GitLab OAuth + gitlab_oauth.*: GitLab OAuth + gitlab_token: GitLab Token + gitlab_token.*: GitLab Token + bearer: HTTP Bearer + bearer.*: HTTP Bearer diff --git a/extensions/suspend/locale/en.yml b/extensions/suspend/locale/en.yml index 495c28d5f3..e33c835861 100644 --- a/extensions/suspend/locale/en.yml +++ b/extensions/suspend/locale/en.yml @@ -69,3 +69,7 @@ flarum-suspend: You have been unsuspended. You can head back to the forum by clicking on the following link: {forum_url} + + validation: + attributes: + suspended_until: suspended until diff --git a/extensions/tags/locale/en.yml b/extensions/tags/locale/en.yml index 3890539d2d..0bca66ead7 100644 --- a/extensions/tags/locale/en.yml +++ b/extensions/tags/locale/en.yml @@ -126,3 +126,13 @@ flarum-tags: choose_tags_placeholder: "{count, plural, one {Choose 1 more tag} other {Choose # more tags}}" name: Name tags: Tags + + validation: + attributes: + name: name + slug: slug + is_hidden: hidden + description: description + color: color + tag_count_primary: => validation.attributes.tag_count_primary + tag_count_secondary: => validation.attributes.tag_count_secondary diff --git a/framework/core/locale/validation.yml b/framework/core/locale/validation.yml index 934d1739d2..b1737c455e 100644 --- a/framework/core/locale/validation.yml +++ b/framework/core/locale/validation.yml @@ -79,6 +79,7 @@ validation: present: "The :attribute field must be present." regex: "The :attribute format is invalid." required: "The :attribute field is required." + required_array_keys: "The :attribute array must contain entries for: :values." required_if: "The :attribute field is required when :other is :value." required_unless: "The :attribute field is required unless :other is in :values." required_with: "The :attribute field is required when :values is present." From f1f96fb8983369dc002c2c93b21e1349c91fdf8f Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Thu, 14 Nov 2024 15:48:52 +0100 Subject: [PATCH 2/4] feat: implement ability to translate validation attributes --- framework/core/src/Extension/Extension.php | 13 ++++++++ .../core/src/Foundation/AbstractValidator.php | 20 ++++++++++++ .../core/src/Foundation/ExtensionIdTrait.php | 31 +++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 framework/core/src/Foundation/ExtensionIdTrait.php diff --git a/framework/core/src/Extension/Extension.php b/framework/core/src/Extension/Extension.php index 8cccbd3007..f3cbb51db6 100644 --- a/framework/core/src/Extension/Extension.php +++ b/framework/core/src/Extension/Extension.php @@ -333,6 +333,19 @@ public function getTitle() return $this->composerJsonAttribute('extra.flarum-extension.title'); } + /** + * @return string|null + */ + public function getNamespace(): ?string + { + return Collection::make($this->composerJsonAttribute('autoload.psr-4')) + ->filter(function ($source) { + return $source === 'src/'; + }) + ->keys() + ->first(); + } + /** * @return string */ diff --git a/framework/core/src/Foundation/AbstractValidator.php b/framework/core/src/Foundation/AbstractValidator.php index 5629213aa8..855cc3ef35 100644 --- a/framework/core/src/Foundation/AbstractValidator.php +++ b/framework/core/src/Foundation/AbstractValidator.php @@ -9,6 +9,7 @@ namespace Flarum\Foundation; +use Flarum\Foundation\ExtensionIdTrait; use Illuminate\Support\Arr; use Illuminate\Validation\Factory; use Illuminate\Validation\ValidationException; @@ -16,6 +17,8 @@ abstract class AbstractValidator { + use ExtensionIdTrait; + /** * @var array */ @@ -81,6 +84,22 @@ protected function getMessages() return []; } + /** + * @return array + */ + protected function getAttributeNames() + { + $extId = $this->getClassExtensionId(); + $attributeNames = []; + + foreach(array_keys($this->rules) as $attribute) { + $key = $extId ? "$extId.validation.attributes.$attribute" : "validation.attributes.$attribute"; + $attributeNames[$attribute] = $this->translator->trans($key); + } + + return $attributeNames; + } + /** * Make a new validator instance for this model. * @@ -92,6 +111,7 @@ protected function makeValidator(array $attributes) $rules = Arr::only($this->getRules(), array_keys($attributes)); $validator = $this->validator->make($attributes, $rules, $this->getMessages()); + $validator->setAttributeNames($this->getAttributeNames()); foreach ($this->configuration as $callable) { $callable($this, $validator); diff --git a/framework/core/src/Foundation/ExtensionIdTrait.php b/framework/core/src/Foundation/ExtensionIdTrait.php new file mode 100644 index 0000000000..49654454b0 --- /dev/null +++ b/framework/core/src/Foundation/ExtensionIdTrait.php @@ -0,0 +1,31 @@ +getExtensions() + ->mapWithKeys(function (Extension $extension) { + return [$extension->getId() => $extension->getNamespace()]; + }) + ->filter(function ($namespace) { + return $namespace && str_starts_with(static::class, $namespace); + }) + ->keys() + ->first(); + } +} From 356624b026b6387c55085461b5fca696db3e1da9 Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Thu, 14 Nov 2024 15:50:06 +0100 Subject: [PATCH 3/4] chore: change translation key --- extensions/suspend/locale/en.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/suspend/locale/en.yml b/extensions/suspend/locale/en.yml index e33c835861..d9c9254a48 100644 --- a/extensions/suspend/locale/en.yml +++ b/extensions/suspend/locale/en.yml @@ -72,4 +72,4 @@ flarum-suspend: validation: attributes: - suspended_until: suspended until + suspendedUntil: suspended until From e761327a0be21acf5cb5ca861dbf81719c016a1e Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Thu, 14 Nov 2024 15:51:35 +0100 Subject: [PATCH 4/4] style: formatting --- framework/core/src/Foundation/AbstractValidator.php | 3 +-- framework/core/src/Foundation/ExtensionIdTrait.php | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/framework/core/src/Foundation/AbstractValidator.php b/framework/core/src/Foundation/AbstractValidator.php index 855cc3ef35..e05a38bffe 100644 --- a/framework/core/src/Foundation/AbstractValidator.php +++ b/framework/core/src/Foundation/AbstractValidator.php @@ -9,7 +9,6 @@ namespace Flarum\Foundation; -use Flarum\Foundation\ExtensionIdTrait; use Illuminate\Support\Arr; use Illuminate\Validation\Factory; use Illuminate\Validation\ValidationException; @@ -92,7 +91,7 @@ protected function getAttributeNames() $extId = $this->getClassExtensionId(); $attributeNames = []; - foreach(array_keys($this->rules) as $attribute) { + foreach (array_keys($this->rules) as $attribute) { $key = $extId ? "$extId.validation.attributes.$attribute" : "validation.attributes.$attribute"; $attributeNames[$attribute] = $this->translator->trans($key); } diff --git a/framework/core/src/Foundation/ExtensionIdTrait.php b/framework/core/src/Foundation/ExtensionIdTrait.php index 49654454b0..7e2bde4e5d 100644 --- a/framework/core/src/Foundation/ExtensionIdTrait.php +++ b/framework/core/src/Foundation/ExtensionIdTrait.php @@ -7,10 +7,10 @@ * LICENSE file that was distributed with this source code. */ - namespace Flarum\Foundation; +namespace Flarum\Foundation; -use Flarum\Extension\ExtensionManager; use Flarum\Extension\Extension; +use Flarum\Extension\ExtensionManager; trait ExtensionIdTrait {