19

我需要从我的表单类型中翻译错误消息。这是我的表单类型代码:

class ReferFriendType extends AbstractType {

public function buildForm(FormBuilder $builder, array $options)
{
    $defaultSubject = "This is a default referral subject.";
    $defaultMessage = "This is a default referral message.";

    $builder->add('email1', 'email',array(
        'required' => true,
        'label' => 'Email 1* :',
        'attr' => array('class' => 'large_text'),
    ));
    $builder->add('email2', 'email',array(
        'label' => 'Email 2 :',
        'required' => false,
        'attr' => array('class' => 'large_text'),
    ));
    $builder->add('email3', 'email',array(
        'label' => 'Email 3 :',
        'required' => false,
        'attr' => array('class' => 'large_text'),
    ));
    $builder->add('email4', 'email',array(
        'label' => 'Email 4 :',
        'required' => false,
        'attr' => array('class' => 'large_text'),
    ));
    $builder->add('email5', 'email',array(
        'label' => 'Email 5 :',
        'required' => false,
        'attr' => array('class' => 'large_text'),
    ));
    $builder->add('subject', 'text', array(
        'data' => $defaultSubject,
        'required' => true,
        'label' => 'Subject* :',
        'attr' => array('class' => 'large_text'),
    ));
    $builder->add('message', 'textarea', array(
        'data' => $defaultMessage,
        'required' => true,
        'label' => 'Message* :',
        'attr' => array('rows' => '5', 'cols' => '40'),
    ));

}

public function getDefaultOptions(array $options)
{
    $collectionConstraint = new Collection( array(
        'fields' => array(
            'email1' => array(
                new Email(),
                new NotBlank(array(
                    'message' => 'You must enter atleast one email address for a valid submission',
                )),
            ),
            'subject' => new NotBlank(),
            'message' => new NotBlank(),
        ),
        'allowExtraFields' => true,
        'allowMissingFields' => true,
    ));

    return array(
        'validation_constraint' => $collectionConstraint,
        'csrf_protection' => false,
    );
}

public function getName()
{
    return 'referFriend';
}

}

我想将 getDefaultOptions() 方法中的“您必须输入至少一个电子邮件地址才能有效提交”翻译成法语。我在 messages.fr.yml 中添加了翻译。但它没有被翻译。任何想法如何做到这一点?

4

4 回答 4

38

验证翻译转到validators.LANG.yml文件——而不是文件messages.LANG.yml

于 2012-05-01T08:59:40.947 回答
3

替换不是在validation.yml 文件中设置的,而是由Validator 设置的。

验证器.en.yml

noFirstnameMinLimit: Please provide at least {{ limit }} characters

验证.yml

Acm\AddressBundle\Entity\Address:
    properties:
        firstname:
            - Length:
                min: 3 
                minMessage: "noFirstnameMinLimit"

这适用于 Symfony 2.4

于 2014-07-03T11:43:42.790 回答
1

文档中有一个示例。

于 2012-05-01T09:33:04.390 回答
1

这很容易,请参阅http://symfony.com/doc/current/book/translation.html#translating-constraint-messages 并在 /app/config/config.yml 中设置 default_locale 或使用 $this->get( 'request')->setLocale('ru');

于 2013-11-21T08:23:04.700 回答