You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `Template` class now offers a static façade to quickly obtain
the template of a message: `Template::from($class, $mode)`.
This replaces the old way of referencing messages in 2.x
(`FooException::$defaultTemplates`), making existing translation
setups easier to migrate.
The documents on translation were updated to feature symfony with
an array provider. Duplicated container notes were extracted to
a single configuration.md file.
The `ContainerRegistry::createContainer()` method returns a [PHP-DI](https://php-di.org/) container. The definitions array follows the [PHP-DI definitions format](https://php-di.org/doc/php-definitions.html).
12
+
13
+
If you prefer to use a different container, `ContainerRegistry::setContainer()` accepts any [PSR-11](https://www.php-fig.org/psr/psr-11/) compatible container:
See [PlaceholderFormatter][] documentation for more information on creating custom modifiers.
39
-
40
-
## Container configuration
41
-
42
-
The `ContainerRegistry::createContainer()` method returns a [PHP-DI](https://php-di.org/) container. The definitions array follows the [PHP-DI definitions format](https://php-di.org/doc/php-definitions.html).
43
-
44
-
If you prefer to use a different container, `ContainerRegistry::setContainer()` accepts any [PSR-11](https://www.php-fig.org/psr/psr-11/) compatible container:
See [PlaceholderFormatter][] documentation for more information on creating custom modifiers and the [configuration](../configuration.md) section for more details on container setup.
Validation uses [symfony/translation](https://symfony.com/doc/current/translation.html) for message translation, providing interoperability with the Symfony ecosystem and other PHP projects.
10
+
Validation provides full translation capabilities, but they are not enabled by default nor
11
+
do we provide official translations for our messages other than English.
10
12
11
-
By default, validation messages are not translated. To enable translation, provide a `Symfony\Contracts\Translation\TranslatorInterface` implementation to `ContainerRegistry::createContainer()`:
13
+
Therefore, if you want to use it with translation, you must provide the translations yourself
14
+
using a compatible [translation contract](https://github.com/symfony/translation-contracts).
15
+
16
+
Here's a quick setup using [symfony/translation](https://symfony.com/doc/current/translation.html):
12
17
13
18
```php
14
19
use Respect\Validation\ContainerRegistry;
20
+
use Respect\Validation\Message\TemplateRegistry;
21
+
use Respect\Validation\Validators as vs;
22
+
use Symfony\Component\Translation\Loader\ArrayLoader;
15
23
use Symfony\Component\Translation\Translator;
16
24
use Symfony\Contracts\Translation\TranslatorInterface;
$translator->addLoader('array', new ArrayLoader()); // Choose the loader of your preference
29
+
$translator->addResource('array', [
30
+
// Reference standard template by class (StringVal, Intval, ...) and mode (default or inverted)
31
+
$templates->get(vs\IntVal::class)->default => '{{subject}} DEVE ser um inteiro.',
32
+
$templates->get(vs\IntVal::class)->inverted => '{{subject}} NÃO DEVE ser um inteiro.',
33
+
34
+
// Reference alternative templates by their id (second argument)
35
+
$templates->get(vs\AllOf::class, vs\AllOf::TEMPLATE_ALL)->default => 'Todas as regras requeridas DEVEM passar para {{subject}}',
36
+
37
+
// You can also just translate messages directly
38
+
'{{subject}} must be a URL' => '{{subject}} DEVE ser uma URL'
39
+
]);
22
40
23
41
$container = ContainerRegistry::createContainer([
24
42
TranslatorInterface::class => $translator,
43
+
TemplateRegistry::class => $templates
25
44
]);
26
-
27
45
ContainerRegistry::setContainer($container);
28
46
```
29
47
30
-
After setting up the container, all messages produced by Validation will your translator.
48
+
You only need to do this once before you perform any validation, and messages will start
49
+
being produced with your translation setup. If you're using a framework, you can configure
50
+
this in the service provider of your choice.
51
+
52
+
Check out the documentation for each validator for its available modes and existing messages
53
+
and the [configuration](../configuration.md) section.
31
54
32
55
## Translating dynamic values
33
56
34
57
Validation messages contain placeholders like `{{subject}}` and `{{minValue}}` that are replaced with actual values. Some of these values may also need translation.
35
58
36
-
Use the `|trans` modifier to translate parameter values:
59
+
You will encounter several messages with `|trans` in different validators. Those enable the
60
+
translation of such dynamic values automatically.
37
61
38
62
```php
39
63
// Message template
@@ -44,6 +68,8 @@ Use the `|trans` modifier to translate parameter values:
44
68
'Palestine' => 'Palestina',
45
69
```
46
70
71
+
The `|trans` modifier will also work with custom templates defined by [Templated](../validators/Templated.md) or provided by [`assert`](../handling-exceptions.md).
72
+
47
73
## Translating lists
48
74
49
75
When using validators that display lists of values, use the `|list:or` or `|list:and` modifiers. These modifiers also require translating the conjunctions:
@@ -63,15 +89,3 @@ When using validators that display lists of values, use the `|list:or` or `|list
63
89
'{{haystack|list:and}} are the only possible names' => '{{haystack|list:and}} são os únicos nomes possíveis',
64
90
'and' => 'e',
65
91
```
66
-
67
-
## Container configuration
68
-
69
-
The `ContainerRegistry::createContainer()` method returns a [PHP-DI](https://php-di.org/) container. The definitions array follows the [PHP-DI definitions format](https://php-di.org/doc/php-definitions.html).
70
-
71
-
If you prefer to use a different container, `ContainerRegistry::setContainer()` accepts any [PSR-11](https://www.php-fig.org/psr/psr-11/) compatible container:
'{{subject}} must pass all the rules' => 'Todas as regras requeridas devem passar para {{subject}}',
23
-
'The length of' => 'O comprimento de',
24
-
'{{subject}} must be a string' => '{{subject}} deve ser uma string',
25
-
'{{subject}} must be between {{minValue}} and {{maxValue}}' => '{{subject}} deve possuir de {{minValue}} a {{maxValue}} caracteres',
26
-
'{{subject}} must be a phone number for country {{countryName|trans}}' => '{{subject}} deve ser um número de telefone válido para o país {{countryName|trans}}',
25
+
// Directly translating validator messages
26
+
$templates->get(vs\AllOf::class, vs\AllOf::TEMPLATE_ALL)->default => 'Todas as regras requeridas devem passar para {{subject}}',
27
+
$templates->get(vs\Length::class)->default => 'O comprimento de',
28
+
$templates->get(vs\StringVal::class)->default => '{{subject}} deve ser uma string',
29
+
$templates->get(vs\Between::class)->default => '{{subject}} deve possuir de {{minValue}} a {{maxValue}} caracteres',
30
+
$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}}',
31
+
$templates->get(vs\DateTimeDiff::class)->default => 'O número de {{type|trans}} entre agora e',
32
+
$templates->get(vs\Equals::class)->default => '{{subject}} deve ser igual a {{compareTo}}',
33
+
34
+
// Custom templates set during runtime
35
+
'Your name must be {{haystack|list:or}}' => 'Seu nome deve ser {{haystack|list:or}}',
36
+
'{{haystack|list:and}} are the only possible names' => '{{haystack|list:and}} são os únicos nomes possíveis',
37
+
38
+
// Miscellaneous translations
27
39
'United States' => 'Estados Unidos',
28
40
'years' => 'anos',
29
-
'The number of {{type|trans}} between now and' => 'O número de {{type|trans}} entre agora e',
30
-
'{{subject}} must be equal to {{compareTo}}' => '{{subject}} deve ser igual a {{compareTo}}',
31
-
'Your name must be {{haystack|list:or}}' => 'Seu nome deve ser {{haystack|list:or}}',
32
41
'or' => 'ou',
33
-
'{{haystack|list:and}} are the only possible names' => '{{haystack|list:and}} são os únicos nomes possíveis',
0 commit comments