具有自定义域的 Symfony 3.1 翻译显示已翻译的消息

Symfony 3.1 translation with custom domain shows already translated messages

简介

我正在使用 Symfony v3.1.1,在翻译表单提交按钮标签时发现了奇怪的行为。

我正在使用 domain_name.locale.xliff 个文件。

描述

标签翻译正确,但在翻译消息缺失部分中出现了一个额外的翻译!

See screenshot 1, See screenshot 2

  1. 奇怪的是漏掉的翻译只是一个翻译的词,但我用的是generally.approximately.precisely表示法,在模板中没有一个词。

  2. 此外,幻象翻译恰好出现在 messages 域中,但 messages 域未在该模板中使用...

我的树枝模板的相关部分:

{% set button_submit_label = "admin.button.chooseDate"|trans({}, "admin") %}
{{ form_row(form.submit, {'label': button_submit_label, 'attr': {'class': 'button-choose-date small success'}}) }}

调试工具栏中显示无效的翻译项。

See screenshot 1, See screenshot 2

代码

我在表单中使用的 ClickType

<?php

namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

class ClickType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('from_date', DateType::class,
                array(
                    'mapped' => false,
                    'label' => false,
                    'widget' => 'single_text',
                    'format' => 'yyyy-MM-dd'
                )
            )
            ->add('submit', SubmitType::class,
                array(
                    'label' => false
                )
            )
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Click',
        ));
    }

    public function getBlockPrefix()
    {
        return null;
    }
}

完整的树枝模板:

{% extends 'admin.html.twig' %}

{% block title %}{{ "admin.title.clicks"|trans({}, "admin") }}{% endblock %}

{% block stylesheets %}
    {{ parent() }}
{% endblock %}

{% block body %}
    <div class="content">
        {% block page %}

            {% block admin_menu %}
                {% include 'admin/admin_top_menu.html.twig' with {'admin_active': '6'} %}
            {% endblock %}

            {% block admin_content %}
                <div class="box-choose-date row clearfix text-center">
                    {% set from_date = app.request.get('from_date') %}
                    {{ form_start(form, {'attr': {'id': 'form-choose-date'}, 'method': 'GET'}) }}
                    <fieldset>
                        <p class="clicks-choose-date">{{ "admin.msg.clicksChooseDate"|trans({}, "admin") }}</p>
                        {% if (from_date != '') %}
                            <p class="clicks-you-chose">{{ "admin.msg.clicksSinceDate"|trans({'%from_date%': from_date|date("d.m.Y")}, "admin") }} {{ "admin.msg.foundClicks"|transchoice(click_count, {'%click_count%': click_count}, "admin") }}</p>
                        {% endif %}
                        {{ form_row(form.from_date, {'name': from_date, 'attr': {'class': 'input-date'}}) }}
                        {% if (from_date != '') %}
                            {% if (msg != '') %}
                                <p class="clicks-record-not-found">{{ msg }}</p>
                            {% endif %}
                        {% endif %}
                        {% set button_submit_label = "admin.button.chooseDate"|trans({}, "admin") %}
                        {{ form_row(form.submit, {'label': button_submit_label, 'attr': {'class': 'button-choose-date small success'}}) }}
                    </fieldset>
                    {{ form_end(form) }}
                </div>
                {% include 'admin/click_list.html.twig' with {'from_date': from_date, 'click_count': click_count} %}
            {% endblock %}

            {% block admin_footer %}
                {% include 'admin/admin_footer.html.twig' %}
            {% endblock %}

        {% endblock %}
    </div>
{% endblock %}

{% block javascripts %}
    {{ parent() }}
{% endblock %}

更新

在 Symfony 3.1.2 中测试,相同的幻像翻译字符串显示在 Symfony 分析器中。

结论

是 Symfony 的错误还是我做错了什么?

请指教

感谢您的时间和知识。

塞巴斯蒂安·斯托克回答了这个问题on Github

A label is always run trough the translator in the template, even if it's already translated (in the FormType or template calling form_row with a custom label). form_div_layout.html.twig line: 213.

可以通过将 translation_domain 设置为 false 来防止这种情况:

{% set button_submit_label = "admin.button.chooseDate"|trans({}, "admin") %}
{{ form_row(form.submit, {'label': button_submit_label, translation_domain: false, 'attr': {'class': 'button-choose-date small success'}}) }}