我的默认语言环境是pl_PL。当我切换到en_US时,以下代码给出了预期的结果:
// locale set to en_US
$product = $em->getRepository('model\Product')->find(1);
$category = $em->getRepository('model\ProductCategory')->find(1);
echo $product->getName();
echo $category->getName();
// result
beach ball
summer
但是,当我想按型号\产品协会获取类别时,它未被翻译:
// locale set to en_US
$product = $em->getRepository('model\Product')->find(1);
echo $product->getName();
echo $product->getCategories()->first()->getName();
// result
beach ball
lato - pl_PL instead of en_US
它是可翻译的扩展错误还是我的代码中有错误?
转储:
select id, name from products; select object_id, locale, field, content from products_translations;
id | name
----+---------------
1 | pilka plazowa
object_id | locale | field | content
-----------+--------+-------+------------
1 | en_US | name | beach ball
select id, name from products_categories; select object_id, locale, field, content from products_categories_translations;
id | name
----+------
1 | lato
object_id | locale | field | content
-----------+--------+-------+---------
1 | en_US | name | summer
模型\产品
/**
* @ORM\Table("products")
* @ORM\Entity(repositoryClass="repository\TranslatableRepository")
* @Gedmo\TranslationEntity(class="model\ProductTranslation")
*/
class Product {
/**
* @Gedmo\Translatable
* @ORM\Column(type="string", length=255)
*/
protected $name;
/**
* @ORM\OneToMany(targetEntity="model\ProductCategory", mappedBy="product")
*/
protected $category_list;
}
模型\产品分类
/**
* @ORM\Table("products_categories")
* @Gedmo\TranslationEntity(class="model\ProductCategoryTranslation")
* @ORM\Entity(repositoryClass="repository\TranslatableRepository")
*/
class ProductCategory {
/**
* @Gedmo\Translatable
* @ORM\Column(type="string", length=255)
*/
protected $name;
}