1
0
mirror of https://github.com/flarum/core.git synced 2025-08-05 07:57:46 +02:00

[1.x] Implement Support for Translatable Validation Attribute Errors (#4070)

* chore: add validation translations

* feat: implement ability to translate validation attributes

* chore: change translation key

* style: formatting

* perf: cache `getAttributeNames`

* perf: cache `getAttributeNames`

* chore

* style: formatting
This commit is contained in:
Davide Iadeluca
2024-11-20 09:51:17 +01:00
committed by GitHub
parent 79e17b3bde
commit 397642ab5a
7 changed files with 112 additions and 0 deletions

View File

@@ -161,3 +161,23 @@ flarum-extension-manager:
why_not_modal: why_not_modal:
title: Why Won't it Update 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

View File

@@ -69,3 +69,7 @@ flarum-suspend:
You have been unsuspended. You can head back to the forum by clicking on the following link: You have been unsuspended. You can head back to the forum by clicking on the following link:
{forum_url} {forum_url}
validation:
attributes:
suspendedUntil: suspended until

View File

@@ -127,3 +127,13 @@ flarum-tags:
choose_tags_placeholder: "{count, plural, one {Choose 1 more tag} other {Choose # more tags}}" choose_tags_placeholder: "{count, plural, one {Choose 1 more tag} other {Choose # more tags}}"
name: Name name: Name
tags: Tags 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

View File

@@ -79,6 +79,7 @@ validation:
present: "The :attribute field must be present." present: "The :attribute field must be present."
regex: "The :attribute format is invalid." regex: "The :attribute format is invalid."
required: "The :attribute field is required." 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_if: "The :attribute field is required when :other is :value."
required_unless: "The :attribute field is required unless :other is in :values." required_unless: "The :attribute field is required unless :other is in :values."
required_with: "The :attribute field is required when :values is present." required_with: "The :attribute field is required when :values is present."

View File

@@ -333,6 +333,19 @@ class Extension implements Arrayable
return $this->composerJsonAttribute('extra.flarum-extension.title'); 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 * @return string
*/ */

View File

@@ -9,6 +9,7 @@
namespace Flarum\Foundation; namespace Flarum\Foundation;
use Illuminate\Contracts\Cache\Store as Cache;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Validation\Factory; use Illuminate\Validation\Factory;
use Illuminate\Validation\ValidationException; use Illuminate\Validation\ValidationException;
@@ -16,6 +17,13 @@ use Symfony\Contracts\Translation\TranslatorInterface;
abstract class AbstractValidator abstract class AbstractValidator
{ {
use ExtensionIdTrait;
/**
* @var string
*/
public static $CORE_VALIDATION_CACHE_KEY = 'core.validation.extension_id_class_names';
/** /**
* @var array * @var array
*/ */
@@ -81,6 +89,30 @@ abstract class AbstractValidator
return []; return [];
} }
/**
* @return array
*/
protected function getAttributeNames()
{
$cache = resolve(Cache::class);
if ($cache->get(self::$CORE_VALIDATION_CACHE_KEY) !== null) {
return $cache->get(self::$CORE_VALIDATION_CACHE_KEY);
}
$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);
}
$cache->forever(self::$CORE_VALIDATION_CACHE_KEY, $attributeNames);
return $attributeNames;
}
/** /**
* Make a new validator instance for this model. * Make a new validator instance for this model.
* *
@@ -92,6 +124,7 @@ abstract class AbstractValidator
$rules = Arr::only($this->getRules(), array_keys($attributes)); $rules = Arr::only($this->getRules(), array_keys($attributes));
$validator = $this->validator->make($attributes, $rules, $this->getMessages()); $validator = $this->validator->make($attributes, $rules, $this->getMessages());
$validator->setAttributeNames($this->getAttributeNames());
foreach ($this->configuration as $callable) { foreach ($this->configuration as $callable) {
$callable($this, $validator); $callable($this, $validator);

View File

@@ -0,0 +1,31 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Foundation;
use Flarum\Extension\Extension;
use Flarum\Extension\ExtensionManager;
trait ExtensionIdTrait
{
protected function getClassExtensionId(): ?string
{
$extensions = resolve(ExtensionManager::class);
return $extensions->getExtensions()
->mapWithKeys(function (Extension $extension) {
return [$extension->getId() => $extension->getNamespace()];
})
->filter(function ($namespace) {
return $namespace && str_starts_with(static::class, $namespace);
})
->keys()
->first();
}
}