表单占位符的翻译

时间:2013-02-02 13:03:09

标签: symfony translation

使用symfony2.1。

我在Form / RegisterUser.php中构建了一个表单

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder->add(
        'email',
        'email',
        array('attr' => array('placeholder' => 'email.placeholder')));
    $builder->add(
        'password',
        'repeated',
        array(
            'first_name' => 'password',
            'second_name' => 'confirm',
            'type' => 'password',
            'invalid_message' => 'register.password.repeat', ));
    $builder->add("t_and_c", "checkbox", array("mapped" => false, ));

    // "True" validator on the form for t&c
    $builder->addValidator(new CallbackValidator( function(FormInterface $form) {
        if (!$form["t_and_c"]->getData()) {
            $form->addError(new FormError('Please accept the terms and conditions in order to register'));
        }
    }));
}

/**
 * Returns the default options for this form type.
 * @param array $options
 * @return array The default options
 */
public function getDefaultOptions(array $options) {
    return array('data_class' => 'Frontend\AccountBundle\Entity\User');
}

在app / Ressources / translation / validators.LANG.yml:

<trans-unit id="6">
    <source>email.placeholder</source>
    <target>Enter email.</target>
</trans-unit>
<trans-unit id="12">
    <source>register.password.repeat</source>
    <target>Passwords don't match.</target>
</trans-unit>

将会翻译字段invalid_message,但不会翻译字段email.placeholder。有bug吗?我不会用树枝做正常的渲染。

1 个答案:

答案 0 :(得分:4)

翻译的错误消息会像你做的那样进入你的validators.LANG.yml。 但其他一切都需要进入你的 messages.LANG.yml

另外,我对你如何格式化你的yml感到有点困惑。通常你会这样写:

email:
    placeholder: Enter email.

register:
    password:
        repeat: Passwords don't match