我对 DoctrineBehaviors
包有问题。我正在尝试为没有法语翻译的实体获取特定语言(法语)的翻译。它返回后备语言,这对前端来说是可以的,但我需要知道该语言是否有翻译,因为我需要填写我的后端。
我如何知道实体的字段是否已翻译成特定语言?
最佳答案
使用 Translatable
实体,您可以获得特定实体已知的所有翻译 (documentation)。
$product = $this->getDoctrine()
->getRepository('AppBundle:Product')
->find($productId);
$repository = $this->getDoctrine()->getRepository('Gedmo\Translatable\Entity\Translation');
$translations = $repository->findTranslations($product);
变量 $translations
现在包含一个键控数组,例如,
array(2) {
["en_US"]=>
array(1) {
["name"]=>
string(8) "Keyboard"
}
["fr_FR"]=>
array(1) {
["name"]=>
string(7) "Clavier"
}
}
查明实体是否被翻译现在可以简单地检查区域设置是否在数组键中。
if (!array_key_exists('fr_FR', $translations)) {
throw $this->createNotFoundException('product description not available in French');
}
请注意,Translatable
实体不包含默认语言环境(除非您已将 setPersistDefaultLocaleTranslation
设置为 true
).
关于php - 检查 DoctrineBehaviors 中的字段是否存在翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30593650/