Say I have UserEntity:
User
$user = new User(007); echo $user->getName(); // display Bond echo $user->getGender(); // display "Male"; echo $user->getDesignation() // display "Monsieur Bond" or "Mister Bond"
With this function:
public function getDesignation() { if ($this->getGender() == 'Male') return "Monsieur ".$this->getName(); else return "Madame ".$this->getName(); }
How can I use the translator service inside this Entity to translate "Monsieur" and "Madame"?
It seems that the translation service should be used only inside the controller, but I think that in this case it is appropriate to use it inside this object.
, , "", (.. ). , , .
, aldo , . , - , , ..
$entity->setTranslator($translator);
, , ..
{{ entity.property|trans }}).
. , , . , , , ( , , , , ).
- . , ? , . ( ), - factory " "; factory .
, , . , , , , . getDesignation . , , , , .
<div>{% trans %}{{ entity.designation }}{% endtrans %} {{ entity.name }}</div>
.en.yml
Monsieur: Mr. Madame: Mrs.