简体   繁体   English

Symfony:如何设置实体约束注释的翻译域

[英]Symfony : How to set translation domain of entity constraint annotation

In Symfony entity constraint can be specified via annotation like this : 在Symfony中,实体约束可以通过如下注释来指定:

 /**
 * @Assert\NotBlank(message="author.name.not_blank")
 */
protected $name;

The default translation domain is validators . 默认的翻译域是validators I would like to know how to specify an other translation domain than the default one only for a set of validations like this one. 我想知道如何仅为一组这样的验证指定默认翻译域以外的其他翻译域。

The short answer is that by default you can't , because there not exist a translation_domain option to be able to set it directly in the entity constraint Annotation and unless you have a very particular use case there is a lot of work to do to implement yourself this missed option. 简短的答案是, 默认情况下您不能这样做 ,因为不存在能够直接在实体约束注释中设置translation_domain选项,除非您有非常特殊的用例,否则要执行很多工作自己这个错过的选择。

If you take a look at the source code of the Symfony Validator Component you will see that the default translation domain was set by the ExecutionContextFactory . 如果查看一下Symfony Validator组件的源代码,您会看到默认翻译域是由ExecutionContextFactory设置的。 The context then is inherited by all constraint validator, so you can change it only inside the validator itself, and more precisely in the validate method, that you have to implement in each validator, like in the NotBlankValidator : 然后,上下文将由所有约束验证器继承,因此您只能在验证器本身内部更改它,更确切地说,可以在必须在每个验证器中实现的validate方法中进行NotBlankValidator ,例如NotBlankValidator

public function validate($value, Constraint $constraint)
{
    if (!$constraint instanceof NotBlank) {
        throw new UnexpectedTypeException($constraint, 
        __NAMESPACE__.'\NotBlank');
    }
    if (false === $value || (empty($value) && '0' != $value)) {
        $this->context->buildViolation($constraint->message)
            ->setParameter('{{ value }}', $this->formatValue($value))
            ->setCode(NotBlank::IS_BLANK_ERROR)
            ->setTranslationDomain('your_trans_domain') #<--- THIS LINE
            ->addViolation();
    }
}

So, the most simple option ( specially if you need this only for a few validators ) is to build your own validators to be able to set the translation domain like the previous example. 因此,最简单的选择( 特别是如果您仅需要几个验证器才需要的选项)是构建您自己的验证器 ,以便能够像前面的示例一样设置转换域。

If you need to change it for a third party validator just build your own validator and add a service alias to the third part ones: 如果您需要将其更改为第三方验证器,则只需构建自己的验证器,然后将服务别名添加到第三方验证器中即可:

App\NotBlankValidator:
    tags: [validator.constraint_validator]
    alias: The\Third\Part\Validator\Namespace

Anyway could be useful to know you can change the translation domain globally for all validators. 无论如何,知道您可以全局更改所有验证程序的翻译域可能会很有用。

Symfony <=3.3 (and 3.4 if you don't use Flex) Symfony <= 3.3(如果不使用Flex,则为3.4)

In the config.yml file(inside config folder): config.yml文件中(位于config文件夹中):

framework:
    validation: { translation_domain: your_custom_trans_domain_string }

Symfony >3.4 (and also 3.4 if you use Flex) Symfony> 3.4(如果使用Flex,则为3.4)

In the framework.yaml file (inside config/packages folder): framework.yaml文件中(位于config/packages文件夹中):

framework:
    validation: { translation_domain: your_custom_trans_domain_string }
Create a validators catalog file in the translations/ directory:


`<!-- 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="author.name.not_blank">
                <source>author.name.not_blank</source>
                <target>Please enter an author name.</target>
            </trans-unit>
        </body>
    </file>
</xliff>`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM