respect-validation/tests/feature/TranslatorTest.php
Alexandre Gomes Gaigalas 91ceaafd2d Update translation docs
The documents on translation were updated to feature symfony with
an array provider. Duplicated container notes were extracted to
a single configuration.md file.

An API for accessing the messages, so users don't have to copy
and paste them from the source or docs, was provided and
TemplateResolver was refactored to use it.
2026-02-06 17:11:34 +00:00

76 lines
3.5 KiB
PHP

<?php
/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
*/
declare(strict_types=1);
use Respect\Validation\ContainerRegistry;
use Respect\Validation\Message\TemplateRegistry;
use Respect\Validation\Validators as vs;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\Translator;
use Symfony\Contracts\Translation\TranslatorInterface;
$templates = new TemplateRegistry();
$translator = new Translator('pt_BR');
$translator->addLoader('array', new ArrayLoader());
$translator->addResource(
'array',
[
// Directly translating validator messages
$templates->get(vs\AllOf::class, vs\AllOf::TEMPLATE_ALL)->default => 'Todas as regras requeridas devem passar para {{subject}}',
$templates->get(vs\Length::class)->default => 'O comprimento de',
$templates->get(vs\StringVal::class)->default => '{{subject}} deve ser uma string',
$templates->get(vs\Between::class)->default => '{{subject}} deve possuir de {{minValue}} a {{maxValue}} caracteres',
$templates->get(vs\Phone::class, vs\Phone::TEMPLATE_FOR_COUNTRY)->default => '{{subject}} deve ser um número de telefone válido para o país {{countryName|trans}}',
$templates->get(vs\DateTimeDiff::class)->default => 'O número de {{type|trans}} entre agora e',
$templates->get(vs\Equals::class)->default => '{{subject}} deve ser igual a {{compareTo}}',
// Custom templates set during runtime
'Your name must be {{haystack|list:or}}' => 'Seu nome deve ser {{haystack|list:or}}',
'{{haystack|list:and}} are the only possible names' => '{{haystack|list:and}} são os únicos nomes possíveis',
// Miscellaneous translations
'United States' => 'Estados Unidos',
'years' => 'anos',
'or' => 'ou',
'and' => 'e',
],
'pt_BR',
);
$container = ContainerRegistry::createContainer();
$container->set(TranslatorInterface::class, $translator);
beforeAll(fn() => ContainerRegistry::setContainer($container));
afterAll(fn() => ContainerRegistry::setContainer(ContainerRegistry::createContainer()));
test('Various translations', catchFullMessage(
fn() => v::stringType()->lengthBetween(2, 15)->phone('US')->assert([]),
fn(string $fullMessage) => expect($fullMessage)->toBe(<<<'FULL_MESSAGE'
- Todas as regras requeridas devem passar para `[]`
- `[]` deve ser uma string
- O comprimento de `[]` deve possuir de 2 a 15 caracteres
- `[]` deve ser um número de telefone válido para o país Estados Unidos
FULL_MESSAGE),
));
test('DateTimeDiff', catchMessage(
fn() => v::dateTimeDiff('years', v::equals(2))->assert('1972-02-09'),
fn(string $message) => expect($message)->toBe('O número de anos entre agora e "1972-02-09" deve ser igual a 2'),
));
test('Using "list:or"', catchMessage(
fn() => v::templated('Your name must be {{haystack|list:or}}', v::in(['Respect', 'Validation']))->assert(''),
fn(string $message) => expect($message)->toBe('Seu nome deve ser "Respect" ou "Validation"'),
));
test('Using "list:and"', catchMessage(
fn() => v::templated('{{haystack|list:and}} are the only possible names', v::in(['Respect', 'Validation']))->assert(''),
fn(string $message) => expect($message)->toBe('"Respect" e "Validation" são os únicos nomes possíveis'),
));