Skip to content

Commit d3170c4

Browse files
committed
Merge pull request #1141 from azogheb/constraintTranslation
How-to add constraint translation
2 parents f486214 + fcc6c08 commit d3170c4

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

book/translation.rst

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,115 @@ The translation of database content should be handled by Doctrine through
821821
the `Translatable Extension`_. For more information, see the documentation
822822
for that library.
823823

824+
Translating Constraint Messages
825+
-------------------------------
826+
827+
The best way to understand constraint translation is to see it in action. To start, suppose
828+
you've created a plain-old-PHP object that you need to use somewhere in
829+
your application:
830+
831+
.. code-block:: php
832+
833+
// src/Acme/BlogBundle/Entity/Author.php
834+
namespace Acme\BlogBundle\Entity;
835+
836+
class Author
837+
{
838+
public $name;
839+
}
840+
841+
Add constraints though any of the supported methods. Set the message option to the translation source text. For example, to guarantee that the $name property is not empty, add the following:
842+
843+
.. configuration-block::
844+
845+
.. code-block:: yaml
846+
847+
# src/Acme/BlogBundle/Resources/config/validation.yml
848+
Acme\BlogBundle\Entity\Author:
849+
properties:
850+
name:
851+
- NotBlank: { message: "author.name.not_blank" }
852+
853+
.. code-block:: php-annotations
854+
855+
// src/Acme/BlogBundle/Entity/Author.php
856+
use Symfony\Component\Validator\Constraints as Assert;
857+
858+
class Author
859+
{
860+
/**
861+
* @Assert\NotBlank(message = "author.name.not_blank")
862+
*/
863+
public $name;
864+
}
865+
866+
.. code-block:: xml
867+
868+
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
869+
<?xml version="1.0" encoding="UTF-8" ?>
870+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
871+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
872+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
873+
874+
<class name="Acme\BlogBundle\Entity\Author">
875+
<property name="name">
876+
<constraint name="NotBlank">
877+
<option name="message">author.name.not_blank</option>
878+
</constraint>
879+
</property>
880+
</class>
881+
</constraint-mapping>
882+
883+
.. code-block:: php
884+
885+
// src/Acme/BlogBundle/Entity/Author.php
886+
887+
use Symfony\Component\Validator\Mapping\ClassMetadata;
888+
use Symfony\Component\Validator\Constraints\NotBlank;
889+
890+
class Author
891+
{
892+
public $name;
893+
894+
public static function loadValidatorMetadata(ClassMetadata $metadata)
895+
{
896+
$metadata->addPropertyConstraint('name', new NotBlank(array(
897+
'message' => 'author.name.not_blank'
898+
)));
899+
}
900+
}
901+
902+
Create a translation file under the ``validators`` catalog for the constraint messages, typically in the ``Resources/translations/`` directory of the bundle. See `Message Catalogues`_ for more details.
903+
904+
.. configuration-block::
905+
906+
.. code-block:: xml
907+
908+
<!-- validators.fr.xliff -->
909+
<?xml version="1.0"?>
910+
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
911+
<file source-language="en" datatype="plaintext" original="file.ext">
912+
<body>
913+
<trans-unit id="1">
914+
<source>author.name.not_blank</source>
915+
<target>Please enter an author name.</target>
916+
</trans-unit>
917+
</body>
918+
</file>
919+
</xliff>
920+
921+
.. code-block:: php
922+
923+
// validators.fr.php
924+
return array(
925+
'author.name.not_blank' => 'Please enter an author name.',
926+
);
927+
928+
.. code-block:: yaml
929+
930+
# validators.fr.yml
931+
author.name.not_blank: Please enter an author name.
932+
824933
Summary
825934
-------
826935

0 commit comments

Comments
 (0)