如何从翻译中获取表单字段标签?

时间:2016-04-20 08:49:50

标签: twig translation symfony

我正在尝试为我的表单实现翻译。我已经翻译了错误消息,但我似乎无法翻译表单字段标签。 这就是我所拥有的:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add("title", TextType::class, array(
            "label" => ""vacancy.label.title"",
            "constraints" => array(
                new Length(array(
                    "min" => 4,
                    "max" => 100,
                    "minMessage" => "vacancy.title.min_message",
                    "maxMessage" => "vacancy.title.max_message"),
                new NotBlank(array("message" => "vacancy.not_blank"))
        ))))
        ->add("description", TextareaType::class, array(
            "label" => "Omschrijving",
            "constraints" => array(
                new Length(array(
                    "min" => 20,
                    "max" => 2000,
                    "minMessage" => "vacancy.description.min_message",
                    "maxMessage" => "vacancy.description.max_message"),
                new NotBlank(array("message" => "vacancy.not_blank"))
        ))))
        ->add("startdate", DateType::class, array(
            "label" => "Begindatum",
            "widget" => "single_text",
            "constraints" => array(
                new Date(array(
                    "message" => "vacancy.date.message"
        )))))
        ->add("enddate", DateType::class, array(
            "label" => "Einddatum",
            "widget" => "single_text",
            "constraints" => array(
                new Date(array(
                    "message" => "vacancy.date.message"
        )))))
        ->add("submit", SubmitType::class, array(
            "label" => "Opslaan"
        ));
}

翻译:

vacancy:
not_blank: Gelieve een waarde op te geven.
title:
    min_message: Gelieve minimum {{ limit }} tekens in te geven.
    max_message: Gelieve maximum {{ limit }} tekens in te geven.
description:
    min_message: Gelieve minimum {{ limit }} tekens in te geven.
    max_message: Gelieve maximum {{ limit }} tekens in te geven.
date:
    message: Gelieve een geldige datum op te geven.
    name: Opslaan
label:
    title: Titel
    description: Omschrijving
    startdate: Begindatum
    enddate: Einddatum
    submit: Opslaan

从我到目前为止收集到的内容,我可以推断出这应该像枝条一样完成:

{{ form_start(form) }}
    {{ form_errors(form) }}

    <div>
    {{ form_label("vacancy.label.title"|trans }}
    {{ form_errors(form.title) }}
    {{ form_widget(form.title) }}
    </div>
    <div>
    {{ form_label(form.description) }}
    {{ form_errors(form.description) }}
    {{ form_widget(form.description) }}
    </div>
    <div>
    {{ form_label(form.startdate) }}
    {{ form_errors(form.startdate) }}
    {{ form_widget(form.startdate) }}
    </div>
    <div>
    {{ form_label(form.enddate) }}
    {{ form_errors(form.enddate) }}
    {{ form_widget(form.enddate) }}
    </div>
    <div>
    {{ form_widget(form.submit) }}
    </div>
{{ form_end(form) }}

但这不起作用,我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

发现它,我需要告诉它从这样的翻译中获取:

->add("enddate", DateType::class, array(
            "label" => "vacancy.label.enddate",
            "translation_domain" => "validators", // tell it to look in translations
            "widget" => "single_text",
            // stuff ...