-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Translate JSON schema validation errors
- Loading branch information
Dominic Tubach
committed
Nov 6, 2023
1 parent
023a027
commit a6dc256
Showing
5 changed files
with
75 additions
and
16 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
Civi/RemoteTools/JsonSchema/Validation/Translation/CiviValidationTranslator.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,54 @@ | ||
<?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 Affero General Public License as published by | ||
* the Free Software Foundation in version 3. | ||
* | ||
* 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 Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
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; | ||
} | ||
|
||
} |
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
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