如何使用带有A2lix翻译形式的doctrine knp可翻译扩展来翻译未翻译的字段?

时间:2015-04-07 11:58:10

标签: forms symfony internationalization a2lix-translation

我正在寻找一种在symfony2表单中编辑翻译字段的简单方法。我正在使用doctrine knp translatable extentions来翻译实体。表单混合未按特殊顺序翻译为已翻译的属性。应仅以活动语言显示表单(结束编辑)。例如:

 $builder
 ->add('key')
 ->add('translate1','text',array(
    'property_path' => 'translations[de].translate1',
 ))
 ->add('mynumber')
 ->add('translate2','text',array(
    'property_path' => 'translations[de].translate2',
))

如果语言翻译[de]不存在,我会收到错误:“无法从数组中读取属性”translate1“......”

A2LiX翻译表不是解决方案,因为它在单个列表中显示所有translatabe字段。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

如果您需要按特定顺序显示表单字段(使用A2lix),您可以像在此示例中那样执行:

$builder
     ->add('key')
     ->add('mynumber')
     ->add('translations', 'a2lix_translations', [
         'required_locales' => ['de'], <-- your current locale
             'fields'           => [
                 'translate1' => [
                     # your field options
                 ],
                 'translate2' => [
                     # your field options
                 ],
    ))

然后在视图中:

{% import "A2lixTranslationFormBundle::macros.html.twig" as a2lixTranslations %}

{{ form_errors(form_edit) }}
{{ form_start(form_edit) }}
{{ form_row(form_edit.key) }}    
{{ a2lixTranslations.partialTranslations(form_edit.translations, ['translate1']) }}
{{ form_row(form_edit.mynumber) }}
{{ a2lixTranslations.partialTranslations(form_edit.translations, ['translate2']) }}
{{ form_end(form_edit) }}

答案 1 :(得分:0)

实际上我用自定义类型解决了我的问题,其中有两个事件监听器用于设置和获取翻译值:

  /**
 * build fields
 * @param \Symfony\Component\Form\FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{

    /**
     * merge new translation
     */ 

    $builder->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) use ($options) {

        //test
        $form = $event->getForm();//form
        $data = $event->getData();//products

        $newTranslation = $event->getData();

        $key=$options['translation_property'];

        $lang=$this->getLocale($options);

        $entity=$form->getParent()->getData();

        $setter='set'.strtoupper($key);
        $getter='get'.strtoupper($key);

        $entity->translate($lang)->$setter($newTranslation);
        $entity->mergeNewTranslations();
     });

    /**
     * Populate with data
     */ 
    $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($options) {

        $data = $event->getData();//products
        $form = $event->getForm();//form

        $key=$options['translation_property'];
        $lang=$this->getLocale($options);

        $entity=$form->getParent()->getData();

        $setter='set'.strtoupper($key);
        $getter='get'.strtoupper($key);

        $oldValue=$entity->translate($lang,false)->$getter();//no fallback

        $event->setData($oldValue);

    });
}

字段未映射。翻译属性名称和语言可以直接提供。

 /**
 * Defauls
 * @param \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'translation_path'=>'translations',
        'translation_property' => null,
        'translation_lang'=>null,//getLocale or set in form type
        'mapped'=>false     
    ));
}

并输入继承表单基本文本。

public function getParent()
{
    return 'text';
}

将其定义为枝条扩展服务(translation_text)后,您可以非常轻松地使用它:

 $builder
        ->add('name')           
        ->add('key')            
        ->add('translate1', 'translation_text',
            array(
                    'translation_property'=>'translate1',
                    'translation_lang'=>null
            ))