Symfony 无法从表单中提取标签以进行翻译

Symfony 无法从表单中提取标签以进行翻译,symfony,Symfony,Im使用JMS翻译包进行提取,下面是我的设置: jms_i18n_路由: default_locale: %locale% locales: [en] strategy: prefix_except_default jms_translation: configs: app: dirs: [%kernel.root_dir%, %kernel.root_dir%/../src] output_dir

Im使用JMS翻译包进行提取,下面是我的设置:

jms_i18n_路由:

    default_locale: %locale%
    locales: [en]
    strategy: prefix_except_default

jms_translation:
    configs:
        app:
            dirs: [%kernel.root_dir%, %kernel.root_dir%/../src]
            output_dir: %kernel.root_dir%/Resources/translations
            ignored_domains: [routes]
            excluded_names: [*TestCase.php, *Test.php]
            excluded_dirs: [cache, data, logs]
#            extractors: [jms_translation.file_visitor]
但是,在运行时,它似乎无法从我的捆绑包中拾取和形成标签:

php应用程序/控制台翻译:extract de--dir=./src/ --输出目录=./app/Resources/translations

我的表格是这样的

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', 'text', array(
            'label' => 'nilead.form.discount.name'
        ))

            ->add('value', 'number', array(
                'label' => 'nilead.form.discount.number',
                'constraints' => array(
                    new GreaterThan(array('value' => 0))
                )
            ))

            ->add('percentage', 'percent', array(
                'label' => 'nilead.form.discount.percentage',
            ))

            ->add('code', 'text', array(
                'label' => 'nilead.form.discount.code',
            ))

            ->add('throughDate', 'date', array(
                'label' => 'nilead.form.discount.throughDate',
                'widget' => 'single_text',
                'format' => 'yyyy-MM-dd',
            ))

            ->add('fromDate', 'date', array(
                'label' => 'nilead.form.discount.fromDate',
                'widget' => 'single_text',
                'format' => 'yyyy-MM-dd',
            ))

            ->add('usageRemaining', 'text', array(
                'label' => 'nilead.form.discount.usageRemaining',
            ))

            //->add('fromDate', 'text')

//            ->add('throughDate', 'text')

            ->add('calculator', 'nilead_discount_calculator_choice', array(
                'empty_value' => '----',
                'label' => 'nilead.form.discount.calculator',
            ))
            ->addEventSubscriber(new BuildDiscountFormListener($this->calculator, $builder->getFormFactory()));

        $prototypes = array();
        $prototypes['calculators'] = array();

        foreach ($this->calculator->getCalculators() as $calculator) {
            $calculatorObj = $this->calculator->get($calculator);
            $prototype = $builder->create('settings', $calculatorObj->getConfigurationFormType())->getForm();

            $prototypes['calculators'][$calculator] = $prototype;
        }
        $builder->setAttribute('prototypes', $prototypes);
    }

您必须使用翻译服务,如下所示:

$translator = $this->get('translator');
$translator->trans('nilead.form.discount.number', array(), 'SomeBundle');
在此之前-使用DI将转换器注入表单。

您可以使用将标签等从表单提取到翻译文件:

示例:

$ bin/console translation:extract --output-dir translations/ -d src/Form en

你清除缓存了吗?:)您已经配置了一个名为“app”的配置-您应该使用
php-app/console-translation:extract-de--config=app
来提取您的翻译。谢谢,它现在似乎可以工作了。它似乎不会自动将trans放入MyBundle.en.xliff,而是放入messages.en.xliff。我是否可以让它自动检测名称空间并放入正确的名称空间?这些不是名称空间,而是所谓的。。。您可以在配置中将消息域设置为
domain:[messagedomain]
(注意,您可以有多个配置),或者根据我的记忆,将
--domain=domainname
附加到控制台提取命令中:)。。。我很确定,如果你在twig中也指定了这个域,那么这个域就会被提取出来。非常感谢nifr。你能把你的答案写成“答案”吗?这样我就可以接受了。你可以在对象创建过程中将翻译服务传递给构造函数