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

时间:2013-09-16 09:09:51

标签: symfony

我正在使用JMS Translation Bundle进行提取,以下是我的设置:

jms_i18n_routing:

    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 app / console translation:extract de --dir =。/ src /   --output-DIR =。/应用/资源/翻译

我的表单看起来像这样

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);
    }

2 个答案:

答案 0 :(得分:0)

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

$translator = $this->get('translator');
$translator->trans('nilead.form.discount.number', array(), 'SomeBundle');

在此之前 - 使用DI将翻译器注入您的表单。

答案 1 :(得分:0)

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

示例:

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