[英]symfony translation for custom assert
I created custom assert for check value, only digit and I need create translate message for this costume assert. 我创建了用于检查值的自定义断言(仅数字),并且需要为此服装断言创建转换消息。 I create all what need, but after validate I get trans key instead of trans message 我创建了所有需要的东西,但是在验证之后,我得到了trans键而不是trans消息
[
{
"property_path": "phone",
"message": "contains.numeric"
}
]
This what I have 这就是我所拥有的
\\AppBundle\Validator\Constraints\ContainsNumericValidator
class ContainsNumericValidator extends ConstraintValidator
{
private $repository;
public function __construct(UserRepository $repository)
{
$this->repository = $repository;
}
public function validate($value, Constraint $constraint)
{
if (!preg_match('/^[0-9]+$/', $value, $matches)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ string }}', $value)
->addViolation();
}
}
}
\\AppBundle\Validator\Constraints\ContainsNumeric
/**
* @Annotation
*/
class ContainsNumeric extends Constraint
{
public $message = 'contains.numeric';
}
and in my entity 在我的实体里
use AppBundle\Validator\Constraints as AppAssert;
/**
* @ORM\Table(name="users")
*/
class User extends AbstractUser implements UserInterface
{
/**
* @AppAssert\ContainsNumeric()
* @ORM\Column(name="phone", type="string", nullable=true)
*/
private $phone;
and my xlf app/Resources/translations/validators.en.xlf
和我的xlf app/Resources/translations/validators.en.xlf
/Resources/translations/validators.en.xlf
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="contains.numeric">
<source>contains.numeric</source>
<target>Please enter an valid data.</target>
</trans-unit>
</body>
</file>
</xliff>
In your translation file you need to attribute resname
like this: 在翻译文件中,您需要为resname
如下属性:
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="contains.numeric" resname="contains.numeric">
<source>contains.numeric</source>
<target>Please enter an valid data.</target>
</trans-unit>
</body>
</file>
</xliff>
In fact resname
value is the key used to choose translation. 实际上, resname
值是用于选择翻译的键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.