php - 检查 DoctrineBehaviors 中的字段是否存在翻译

标签 php symfony doctrine-extensions

我对 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/

相关文章:

php - 在 CentOS 上为 PHP 安装 pthreads

php - Symfony2.3 在 Controller 中获取 EntityManager 的更好方法

PHP:如何防止序列化命中某个成员

Symfony2/学说 : Get the field(s) that changed after "Loggable" entity changed

mongodb - 在 symfony2 中使用 doctrine 扩展在 doctrine2 odm 和 orm 之间共享对象

php curl 设置ssl版本

php在每个foreach之后获取数组中的一些元素

php - 在最后出现的空格上拆分字符串

php - 迁移数据库模式时出现 MySQL 错误,Doctrine with Symfony

symfony - 如何在参数转换器中禁用原则过滤器