Using the Translatable
entity, you can get all translations known to your specific entity (documentation).
$product = $this->getDoctrine()
->getRepository('AppBundle:Product')
->find($productId);
$repository = $this->getDoctrine()->getRepository('Gedmo\Translatable\Entity\Translation');
$translations = $repository->findTranslations($product);
The variable $translations
now contains a keyed array, e.g.,
array(2) {
["en_US"]=>
array(1) {
["name"]=>
string(8) "Keyboard"
}
["fr_FR"]=>
array(1) {
["name"]=>
string(7) "Clavier"
}
}
Finding out if the entity is translated can is now simply checking whether the locale is in the array keys.
if (!array_key_exists('fr_FR', $translations)) {
throw $this->createNotFoundException('product description not available in French');
}
Note that the Translatable
entity does not contain the default locale (unless you have set setPersistDefaultLocaleTranslation
to true
).