From 937fa6b1836ffce9e614deb0f69edb3bb1d1bbeb Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Wed, 7 Oct 2015 10:13:50 -0300 Subject: [PATCH] Create documentation for message localization Also define the `translator` param to the related rules in `AbstractNestedException`. --- docs/README.md | 18 +++++++++++ .../Exceptions/AbstractNestedException.php | 13 ++++++++ tests/integration/translator-assert.phpt | 30 +++++++++++++++++++ tests/integration/translator-check.phpt | 26 ++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 tests/integration/translator-assert.phpt create mode 100644 tests/integration/translator-check.phpt diff --git a/docs/README.md b/docs/README.md index d99f0dbe..b9d712ea 100644 --- a/docs/README.md +++ b/docs/README.md @@ -193,6 +193,24 @@ $errors = $exception->findMessages(array( For all messages, the `{{name}}` and `{{input}}` variable is available for templates. +## Message localization + +You're also able to translate your message to another language with Validation. +The only thing one must do is to define the param `translator` as a callable that +will handle the translation: + +```php +$exception->setParam('translator', 'gettext'); +``` + +The example above uses `gettext()` but you can use any other callable value, like +`array($translator, 'trans')` or `you_custom_function()`. + +After that, if you call `getMainMessage()` or `getFullMessage()` (for nested), +the message will be translated. + +Note that `getMessage()` will keep the original message. + ## Custom Rules You also can use your own rules: diff --git a/library/Exceptions/AbstractNestedException.php b/library/Exceptions/AbstractNestedException.php index 7fbc5b7d..b88e8de1 100644 --- a/library/Exceptions/AbstractNestedException.php +++ b/library/Exceptions/AbstractNestedException.php @@ -108,6 +108,19 @@ class AbstractNestedException extends ValidationException implements NestedValid } } + public function setParam($name, $value) + { + parent::setParam($name, $value); + + if ('translator' === $name) { + foreach ($this->getRelated(true) as $related) { + $related->setParam($name, $value); + } + } + + return $this; + } + public function getRelatedByName($name) { foreach ($this->getIterator(true) as $e) { diff --git a/tests/integration/translator-assert.phpt b/tests/integration/translator-assert.phpt new file mode 100644 index 00000000..f4eb9936 --- /dev/null +++ b/tests/integration/translator-assert.phpt @@ -0,0 +1,30 @@ +--FILE-- + 'Todas as regras requeridas devem passar para {{name}}', + '{{name}} must be a string' => '{{name}} deve ser uma string', + '{{name}} must have a length between {{minValue}} and {{maxValue}}' => '{{name}} deve possuir de {{minValue}} a {{maxValue}} caracteres', + ); + + return $messages[$message]; +} + +try { + Validator::string()->length(2, 15)->assert(0); +} catch (NestedValidationExceptionInterface $exception) { + $exception->setParam('translator', 'translatorCallback'); + + echo $exception->getFullMessage(); +} +?> +--EXPECTF-- +\-Todas as regras requeridas devem passar para 0 + |-0 deve ser uma string + \-0 deve possuir de 2 a 15 caracteres diff --git a/tests/integration/translator-check.phpt b/tests/integration/translator-check.phpt new file mode 100644 index 00000000..3c3cc17f --- /dev/null +++ b/tests/integration/translator-check.phpt @@ -0,0 +1,26 @@ +--FILE-- + '{{name}} deve ser uma string', + ); + + return $messages[$message]; +} + +try { + Validator::string()->length(2, 15)->check(0); +} catch (ValidationExceptionInterface $exception) { + $exception->setParam('translator', 'translatorCallback'); + + echo $exception->getMainMessage(); +} +?> +--EXPECTF-- +0 deve ser uma string