diff --git a/Civi/RemoteTools/JsonSchema/Validation/Translation/CiviValidationTranslator.php b/Civi/RemoteTools/JsonSchema/Validation/Translation/CiviValidationTranslator.php new file mode 100644 index 0000000..234d9bc --- /dev/null +++ b/Civi/RemoteTools/JsonSchema/Validation/Translation/CiviValidationTranslator.php @@ -0,0 +1,54 @@ +. + */ + +declare(strict_types = 1); + +namespace Civi\RemoteTools\JsonSchema\Validation\Translation; + +use Opis\JsonSchema\Errors\ValidationError; +use Systopia\JsonSchema\Translation\TranslatorFactory; +use Systopia\JsonSchema\Translation\TranslatorInterface; + +/** + * @codeCoverageIgnore + */ +final class CiviValidationTranslator implements TranslatorInterface { + + private string $lastLocale = ''; + + /** + * @phpstan-ignore-next-line Not initialized in constructor. + */ + private TranslatorInterface $translator; + + /** + * @inheritDoc + */ + public function trans(string $id, array $parameters, ValidationError $error): string { + return $this->getTranslator()->trans($id, $parameters, $error); + } + + private function getTranslator(): TranslatorInterface { + if (\CRM_Core_I18n::getLocale() !== $this->lastLocale) { + $this->lastLocale = \CRM_Core_I18n::getLocale(); + $this->translator = TranslatorFactory::createTranslator($this->lastLocale); + } + + return $this->translator; + } + +} diff --git a/Civi/RemoteTools/JsonSchema/Validation/ValidationResult.php b/Civi/RemoteTools/JsonSchema/Validation/ValidationResult.php index 8a7fb66..472900d 100644 --- a/Civi/RemoteTools/JsonSchema/Validation/ValidationResult.php +++ b/Civi/RemoteTools/JsonSchema/Validation/ValidationResult.php @@ -19,9 +19,11 @@ namespace Civi\RemoteTools\JsonSchema\Validation; -use Opis\JsonSchema\Errors\ErrorFormatter; use Opis\JsonSchema\Errors\ValidationError; use Systopia\JsonSchema\Errors\ErrorCollector; +use Systopia\JsonSchema\Translation\ErrorTranslator; +use Systopia\JsonSchema\Translation\NullTranslator; +use Systopia\JsonSchema\Translation\TranslatorInterface; final class ValidationResult { @@ -32,20 +34,16 @@ final class ValidationResult { private ErrorCollector $errorCollector; - private static function getErrorFormatter(): ErrorFormatter { - static $errorFormatter; - $errorFormatter ??= new ErrorFormatter(); - - return $errorFormatter; - } + private ErrorTranslator $errorTranslator; /** * @param array $data * @param \Systopia\JsonSchema\Errors\ErrorCollector $errorCollector */ - public function __construct(array $data, ErrorCollector $errorCollector) { + public function __construct(array $data, ErrorCollector $errorCollector, ?TranslatorInterface $translator = NULL) { $this->data = $data; $this->errorCollector = $errorCollector; + $this->errorTranslator = new ErrorTranslator($translator ?? new NullTranslator()); } /** @@ -59,14 +57,14 @@ public function getData(): array { * @return array> */ public function getErrorMessages(): array { - return static::mapErrorsToMessages($this->errorCollector->getErrors()); + return $this->mapErrorsToMessages($this->errorCollector->getErrors()); } /** * @return array> */ public function getLeafErrorMessages(): array { - return static::mapErrorsToMessages($this->errorCollector->getLeafErrors()); + return $this->mapErrorsToMessages($this->errorCollector->getLeafErrors()); } public function hasErrors(): bool { @@ -82,11 +80,10 @@ public function isValid(): bool { * * @return array> */ - private static function mapErrorsToMessages(array $errors): array { - $errorFormatter = static::getErrorFormatter(); + private function mapErrorsToMessages(array $errors): array { return array_map( fn (array $errors): array => array_map( - fn (ValidationError $error): string => $errorFormatter->formatErrorMessage($error), + fn (ValidationError $error): string => $this->errorTranslator->trans($error), $errors ), $errors ); diff --git a/Civi/RemoteTools/JsonSchema/Validation/Validator.php b/Civi/RemoteTools/JsonSchema/Validation/Validator.php index 226f16b..f543dc0 100644 --- a/Civi/RemoteTools/JsonSchema/Validation/Validator.php +++ b/Civi/RemoteTools/JsonSchema/Validation/Validator.php @@ -23,12 +23,16 @@ use Civi\RemoteTools\Util\JsonConverter; use Opis\JsonSchema\Validator as OpisValidator; use Systopia\JsonSchema\Errors\ErrorCollector; +use Systopia\JsonSchema\Translation\TranslatorInterface; final class Validator implements ValidatorInterface { + private TranslatorInterface $translator; + private OpisValidator $validator; - public function __construct(OpisValidator $validator) { + public function __construct(TranslatorInterface $translator, OpisValidator $validator) { + $this->translator = $translator; $this->validator = $validator; } @@ -48,7 +52,7 @@ public function validate(JsonSchema $jsonSchema, array $data, int $maxErrors = 1 $this->validator->setMaxErrors($prevMaxErrors); } - return new ValidationResult(JsonConverter::toArray($validationData), $errorCollector); + return new ValidationResult(JsonConverter::toArray($validationData), $errorCollector, $this->translator); } } diff --git a/composer.json b/composer.json index 5e45bcd..4741e25 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "require": { "webmozart/assert": "^1", "systopia/expression-language-ext": "~0.1", - "systopia/opis-json-schema-ext": "~0.1", + "systopia/opis-json-schema-ext": "~0.2", "psr/log": "<3" }, "scripts": { diff --git a/services/json-forms.php b/services/json-forms.php index b4234b7..36e717c 100644 --- a/services/json-forms.php +++ b/services/json-forms.php @@ -30,14 +30,18 @@ use Civi\RemoteTools\JsonSchema\FormSpec\JsonSchemaFactory; use Civi\RemoteTools\JsonSchema\FormSpec\JsonSchemaFactoryInterface; use Civi\RemoteTools\JsonSchema\Validation\OpisValidatorFactory; +use Civi\RemoteTools\JsonSchema\Validation\Translation\CiviValidationTranslator; use Civi\RemoteTools\JsonSchema\Validation\Validator; use Civi\RemoteTools\JsonSchema\Validation\ValidatorInterface; use Opis\JsonSchema\Validator as OpisValidator; use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; +use Systopia\JsonSchema\Translation\TranslatorInterface; $container->register(OpisValidator::class)->setFactory([OpisValidatorFactory::class, 'getValidator']); $container->autowire(ValidatorInterface::class, Validator::class); +$container->autowire(TranslatorInterface::class, CiviValidationTranslator::class); + $container->autowire(JsonSchemaFactoryInterface::class, JsonSchemaFactory::class) ->addArgument(new TaggedIteratorArgument( FieldJsonSchemaFactoryInterface::SERVICE_TAG,