mirror of
https://github.com/Respect/Validation.git
synced 2026-03-15 06:45:44 +01:00
Currently, defining translations is quite cumbersome, and the translator callback is passed to the constructor of multiple classes, which makes it quite ugly and could make translations inconsistent. This commit completely changes how translations are done in Validation. Instead of using a callback, it uses a specific class, and `Validator` will pass that object through the objects that render the messages.
32 lines
684 B
PHP
32 lines
684 B
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Message\Translator;
|
|
|
|
use Respect\Validation\Message\Translator;
|
|
|
|
use function is_string;
|
|
|
|
final class ArrayTranslator implements Translator
|
|
{
|
|
/** @param array<string, string> $messages */
|
|
public function __construct(
|
|
private readonly array $messages
|
|
) {
|
|
}
|
|
|
|
public function translate(string $message): string
|
|
{
|
|
if (isset($this->messages[$message]) && is_string($this->messages[$message])) {
|
|
return $this->messages[$message];
|
|
}
|
|
|
|
return $message;
|
|
}
|
|
}
|