How to use the translator service inside an Entity?

前端 未结 4 974
渐次进展
渐次进展 2021-01-12 22:07

Let\'s say I have a User Entity :

$user = new User(007);
echo $user->getName(); // display Bond
echo $user->getGender(); // display \"Male         


        
4条回答
  •  醉话见心
    2021-01-12 22:14

    I ran into the similar problem and finally found this solution. This is not a direct answer to your problem because I'm also aware that an entity should have nothing to do with a service, like translator. So you should leave the getDesignation function untouched. Instead, in the presentation layer, twig for example, you translate that French designation.

    {% trans %}{{ entity.designation }}{% endtrans %} {{ entity.name }}

    And in your messages.en.yml

    Monsieur: Mr.
    Madame: Mrs.
    

提交回复
热议问题