我正在尝试翻译自定义约束消息:
<?php
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Translation\TranslatorInterface;
/**
* @Annotation
*/
class YoutubeLink extends Constraint
{
public $message = '"{{ string }}" is not a valid Youtube link';
/**
* @var TranslatorInterface
*/
private $translator;
public function __construct($translator)
{
$this->translator = $translator;
}
}
我正在注入翻译器:
services:
App\Validator\Constraints\YoutubeLink:
arguments: [ "@translator" ]
tags:
- { name: validator.constraint_validator, alias: validator.youtube_link }
但是如何现在使用参数翻译消息?
答案 0 :(得分:0)
由于$message
是公开的,所以只需将翻译密钥放在实体中即可。
/**
* @var string
*
* @ORM\Column(type="string")
* @AppAssert\YoutubeLink(
* message = "link.invalid_youtube"
* )
*/
protected $url;