I am facing very difficult problem to handle my situation. Im saving title in database for en the name is title and for the translation column the name is bntitle.
now im use FosRest with jms serializer to provide the api response. but i can't find a way to send respond based on local. because it always calls the
getTitle()
method to get the title, my question is if local is en then the title key will be getTitle()
if local is bn title should call getBnTitle()
;
this the controller:
public function getAction(Content $entity, Request $request)
{
$locale = $request->getLocale();
$data = array();
$data['_embedded']['content'] = $entity;
eturn $data;
}
how can I do that?
This is not the best way to handle the translation for sure but if you just need this for the title attribute you can pass the local to the entity as an argument and then in getTitle()
method you do the test.
public class YourEntity{
private $locale ;
//......
public function setLocale($locale){
$this->locale = $locale
}
public function getTitle(){
// your test here
if ( $locale === 'bn' ) return $this->getBnTitle();
return $title ;
}
Then in your controller :
public function getAction(Content $entity, Request $request)
{
$locale = $request->getLocale();
// pass local to entity
$entity->setLocale($locale);
$data = array();
$data['_embedded']['content'] = $entity;
eturn $data;
}
Edit : If you want a better solution there is many doctrine extensions that do the translation one of them is this https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/translatable.md