访问表单标签中的翻译

access to translation in form label

目前,当我使用 FormBuilder 创建表单时,Symfony (3.3.5) 在分析器中向我显示一条警告,告诉我没有针对给定语言环境的翻译消息。

我按照 symfony 文档中的说明进行操作,但问题仍然存在。

我的翻译文件位于:app/Resources/translations/properties.fr.yml 我的 config.yml 文件如下所示:

parameters: locale: fr framework: translator: fallbacks: ['%locale%'] paths: - '%kernel.project_dir%/app/Resources/translations'

该文件仅包含以下内容:

properties:
    zipcode:
        label: 'Code postal du bien'

最终表单创建如下:

$property = new Properties();
$form = $this->createFormBuilder($property)
  ->add('zipCode', Type\IntegerType::class, array(
    'attr' => array(
      'min' => '10000',
      'max' => '99999'
    ),
    'label' => 'properties.zipcode.label'
  ))
  ->getForm();

为什么我的翻译文件没有被使用?

您应该像这样向您的字段添加一个属性:

$property = new Properties();
$form = $this->createFormBuilder($property)
  ->add('zipCode', Type\IntegerType::class, array(
    'translation_domain' => 'yourDomain',
    'attr' => array(
      'min' => '10000',
      'max' => '99999'
    ),
    'label' => 'properties.zipcode.label'
  ))
  ->getForm();

将 "yourDomain" 替换为您的域名(文件名)。希望这会有所帮助。

默认翻译域是'messages'。因此,您可以将 properties.fr.yml 更改为 messages.fr.yml。另一种可能性是像这样更改翻译域:

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
       'data_class'         => 'Acme\Entity\DemoEntity',
       'translation_domain' => 'properties'
    ]);
}