-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from systopia/translate-validation-errors
Translate JSON schema validation errors
- Loading branch information
Showing
7 changed files
with
96 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,28 @@ | ||
# We cannot use things like autowiring or "!tagged_iterator": | ||
# https://www.drupal.org/project/drupal/issues/3111008 | ||
|
||
services: | ||
_defaults: | ||
autowire: true | ||
public: false # Controller classes and services directly fetched from container need to be public | ||
|
||
Drupal\json_forms\Form\FormArrayFactoryInterface: | ||
class: Drupal\json_forms\Form\FormArrayFactory | ||
public: true | ||
|
||
Drupal\json_forms\Form\Validation\FormValidationMapperInterface: | ||
class: Drupal\json_forms\Form\Validation\FormValidationMapper | ||
public: true | ||
|
||
Systopia\JsonSchema\Translation\TranslatorInterface: | ||
class: Drupal\json_forms\Form\Validation\Translation\DrupalJsonSchemaValidationTranslator | ||
arguments: | ||
- '@language_manager' | ||
|
||
Opis\JsonSchema\Validator: | ||
class: Opis\JsonSchema\Validator | ||
factory: [Drupal\json_forms\Form\Validation\OpisValidatorFactory, getValidator] | ||
|
||
Drupal\json_forms\Form\Validation\FormValidatorInterface: | ||
class: Drupal\json_forms\Form\Validation\FormValidator | ||
arguments: ['@Opis\JsonSchema\Validator'] | ||
public: true | ||
|
||
Drupal\json_forms\Form\Control\Rule\StatesArrayFactoryInterface: | ||
class: Drupal\json_forms\Form\Control\Rule\StatesArrayFactory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/Form/Validation/Translation/DrupalJsonSchemaValidationTranslator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (C) 2023 SYSTOPIA GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\json_forms\Form\Validation\Translation; | ||
|
||
use Drupal\Core\Language\LanguageManagerInterface; | ||
use Opis\JsonSchema\Errors\ValidationError; | ||
use Systopia\JsonSchema\Translation\TranslatorFactory; | ||
use Systopia\JsonSchema\Translation\TranslatorInterface; | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
final class DrupalJsonSchemaValidationTranslator implements TranslatorInterface { | ||
|
||
private LanguageManagerInterface $languageManager; | ||
|
||
private string $lastLanguageId = ''; | ||
|
||
|
||
/** | ||
* @phpstan-ignore-next-line Not initialized in constructor. | ||
*/ | ||
private TranslatorInterface $translator; | ||
|
||
public function __construct(LanguageManagerInterface $languageManager) { | ||
$this->languageManager = $languageManager; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function trans(string $id, array $parameters, ValidationError $error): string { | ||
return $this->getTranslator()->trans($id, $parameters, $error); | ||
} | ||
|
||
private function getTranslator(): TranslatorInterface { | ||
if ($this->languageManager->getCurrentLanguage()->getId() !== $this->lastLanguageId) { | ||
$this->lastLanguageId = $this->languageManager->getCurrentLanguage()->getId(); | ||
$this->translator = TranslatorFactory::createTranslator($this->lastLanguageId); | ||
} | ||
|
||
return $this->translator; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters