Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the translator service inside an Entity?

Let's say I have a User Entity :

$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 the translator service should be used only inside a Controller, but I think it's appropriate in that case to use it inside this Entity.

like image 311
lepix Avatar asked Nov 23 '11 14:11

lepix


3 Answers

The translator service is, like you say, a "service" you can use a service inside any class (i.e. defining it as a service too and using the dependency injector container). So, you can use the translator almost wherever you want.

But the entities like aldo said shouldn't have that responsability. In the worst scenario if you really want to translate things inside the entity, you could pass the translator to the entity with a set method, i.e.

$entity->setTranslator($translator);

but I recommend you too to create a class that handles the problem outside the entity, i.e. using the twig template

{{ entity.property|trans }}).
like image 134
Silence Avatar answered Sep 30 '22 11:09

Silence


You shouldn't and in general it is not possible. According to the Single Responsibility Principle the entity have already their purpose, which is representing data on a database. Moreover the translation is a matter of representation, so it is unlikely that you want to address such a problem in the entity layer (unless you want to provide entities translated in different languages, which totally a different problem and shouldn't even be solved using the translator).

Rethink to your logic and try something different for this. Are you sure that you don't want to do this translation on the view layer? That would be the best thing probably. Otherwise (if your logic really need to have translation at a model level) you could create a wrapper class for entities and a factory to generate this "wrapped entities"; in that factory you could inject the translator service.

like image 26
Aldo Stracquadanio Avatar answered Sep 30 '22 11:09

Aldo Stracquadanio


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.

<div>{% trans %}{{ entity.designation }}{% endtrans %} {{ entity.name }}</div>

And in your messages.en.yml

Monsieur: Mr.
Madame: Mrs.
like image 42
Anh Nguyen Avatar answered Sep 30 '22 11:09

Anh Nguyen