29

私はSymfony2Beta3で次のようにクラスフォームを使用しています:

namespace Partners\FrontendBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class ConfigForm extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('no_containers', 'choice', array('choices' => array(1 => 'yes', 0 => 'no')));
        ...

「yes」と「no」のオプションを翻訳したいのですが、ここで翻訳者の使い方がわかりません。

4

4 に答える 4

88

翻訳リソースは通常どおり使用できます。これは私のために働いた:

    $builder->add('sex', 'choice', array( 
        'choices'   => array(
            1 => 'profile.show.sex.male', 
            2 => 'profile.show.sex.female',
        ),
        'required' => false,
        'label'     => 'profile.show.sex.label',
        'translation_domain' => 'AcmeUserBundle'
    ));

次に、バンドルのResources->translationsディレクトリに翻訳を追加します。

@CptSadfaceからの更新:

symfony 2.7では、choice_label引数を使用して、次のように翻訳ドメインを指定できます。

'choice_label' => 'typeName',
'choice_translation_domain' => 'messages',

ドメインを指定しないと、オプションは変換されません。

于 2013-01-04T02:00:05.610 に答える
4

しばらく検索して答えを見つけましたが、ついにSymfonyがフォームコンテンツをどのように翻訳するかを知りました。あなたの場合の最も簡単な方法は、YAMLまたはXLIFF翻訳ファイル(app / Resources / translations / messages.de.ymlなど)またはバンドルを追加して、「yes」と「no」の翻訳を追加することです。 。これはここで説明されています: http ://symfony.com/doc/current/book/translation.html

問題は、私の意見では、カスタム翻訳キーを使用できないように見えることです。FOSUserBundleのメンバーは、この(または同様の)問題を「フォームテーマ」(http://symfony.com/doc/2.0/cookbook/form/form_customization.html)で解決します。フォーム要素IDを翻訳キーとして使用するための2つの重要なコード行を次に示します。

https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/views/Registration/register_content.html.twig#L1 / https://github.com/FriendsOfSymfony/FOSUserBundle/blob/50ab4d8fdfd324c1e722cb982e685abdc111be0b/Resource form.html.twig#L4

フォームテーマを追加することで、テンプレート内のほとんどすべてのフォームを変更できます。これは、これを行う正しい方法のようです。

(申し訳ありませんが、2つ以上のリンクを投稿するのに十分な評判がないため、2つのリンクを分割する必要がありました。悲しいです。)

于 2011-08-18T18:14:48.287 に答える
3

symfony 2.7では、choice_label引数を使用して、次のように翻訳ドメインを指定できます。

'choice_label' => 'typeName',
'choice_translation_domain' => 'messages',

ドメインを指定しないと、オプションは変換されません。

于 2015-07-31T09:33:34.653 に答える
0

CptSadfaceの答えは、エンティティの選択を翻訳するのに役立ったものでした。

$builder
    ->add(
        'authorizationRoles',
        null,
        [
            'label' => 'app.user.fields.authorization_roles',
            'multiple' => true,
            'choice_label' => 'name', // entity field storing your translation key
            'choice_translation_domain' => 'messages',
        ]
    );
于 2015-09-08T11:46:37.577 に答える