Symfony2实现数据库翻译的最佳方式

时间:2022-10-22 17:08:18

I'm refactoring a multilanguage website to symfony.

我正在为symfony重构一个多语言网站。

The site has plenty of language stuff stored in databases (product descriptions, product name...) and a language table. It looks pretty much like these: Table product: -id -price -stock -...

该网站有大量语言内容存储在数据库(产品描述,产品名称......)和语言表中。它看起来非常像:表产品:-id -price -stock -...

Table product_language: -id_product -id_language -name -description

表product_language:-id_product -id_language -name -description

Table language: -id -name -code

表语言:-id -name -code

So I'm thinking in the best way to migrate this to symfony and doctrine, I've been looking into the translatable extension but I don't know if it would fit here (I'm not sure about if its possible to ad the language table into it)

所以我想以最好的方式将其迁移到symfony和doctrine,我一直在研究可翻译的扩展,但我不知道它是否适合这里(我不确定它是否可以广告语言表进去吧)

Thanks!

2 个解决方案

#1


I built a multi language ecommerce plateform using the KNP Translatable behavior and it's totally awesome.

我使用KNP可翻译行为构建了一个多语言电子商务平台,它非常棒。

It would definatly fit into your needs. Here's a quick example of my "Product" entity and how it affects your database.

它绝对符合您的需求。这是我的“产品”实体的一个简单示例,以及它如何影响您的数据库。

//AcmeBundle/Entity/Product.php
use Knp\DoctrineBehaviors\Model as ORMBehaviors;

/**
 * @ORM\Table(name="product")
 */
class Product
{
    use ORMBehaviors\Translatable\Translatable;

    /**
     * @var integer
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     * @ORM\Column(name="sku", type="string", length=255)
     */
    protected $sku;

    // other properties

Then you put all your translatable properties into a new entity called ProductTranslation

然后,将所有可翻译属性放入名为ProductTranslation的新实体中

//AcmeBundle/Entity/ProductTranslation.php
use Knp\DoctrineBehaviors\Model as ORMBehaviors;

/**
 * @ORM\Table(name="product_translation")
 * @ORM\Entity
 */
class ProductTranslation
{
    use ORMBehaviors\Translatable\Translation,
        ORMBehaviors\Sluggable\Sluggable;

    /**
     * @ORM\Column(name="name", type="string", length=255)
     */
    protected $name;

    /**
     * @ORM\Column(name="description", type="text", nullable=true)
     */
    protected $description; 

This will give you two tables

这将为您提供两个表格

The first one with all your product information (in this example SKU). The second one will have a row for every translated instance using a 1-N relation.

第一个包含所有产品信息(在此示例中为SKU)。对于每个使用1-N关系的翻译实例,第二个将有一行。

Once you are done you can use either :

完成后,您可以使用以下任一方法:

$product->translate('en')->getName();
$product->translate('fr')->getName();

Or even better

甚至更好

$product->getName();

Will use the current user locale to output the good translation.

将使用当前用户区域设置输出良好的翻译。

#2


You could use VM5's Entity Translations bundle. It's pretty simple and lightweight. And it also uses native Doctrine ORM associations for translations.

您可以使用VM5的Entity Translations包。它非常简单轻巧。它还使用原生Doctrine ORM关联进行翻译。

#1


I built a multi language ecommerce plateform using the KNP Translatable behavior and it's totally awesome.

我使用KNP可翻译行为构建了一个多语言电子商务平台,它非常棒。

It would definatly fit into your needs. Here's a quick example of my "Product" entity and how it affects your database.

它绝对符合您的需求。这是我的“产品”实体的一个简单示例,以及它如何影响您的数据库。

//AcmeBundle/Entity/Product.php
use Knp\DoctrineBehaviors\Model as ORMBehaviors;

/**
 * @ORM\Table(name="product")
 */
class Product
{
    use ORMBehaviors\Translatable\Translatable;

    /**
     * @var integer
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     * @ORM\Column(name="sku", type="string", length=255)
     */
    protected $sku;

    // other properties

Then you put all your translatable properties into a new entity called ProductTranslation

然后,将所有可翻译属性放入名为ProductTranslation的新实体中

//AcmeBundle/Entity/ProductTranslation.php
use Knp\DoctrineBehaviors\Model as ORMBehaviors;

/**
 * @ORM\Table(name="product_translation")
 * @ORM\Entity
 */
class ProductTranslation
{
    use ORMBehaviors\Translatable\Translation,
        ORMBehaviors\Sluggable\Sluggable;

    /**
     * @ORM\Column(name="name", type="string", length=255)
     */
    protected $name;

    /**
     * @ORM\Column(name="description", type="text", nullable=true)
     */
    protected $description; 

This will give you two tables

这将为您提供两个表格

The first one with all your product information (in this example SKU). The second one will have a row for every translated instance using a 1-N relation.

第一个包含所有产品信息(在此示例中为SKU)。对于每个使用1-N关系的翻译实例,第二个将有一行。

Once you are done you can use either :

完成后,您可以使用以下任一方法:

$product->translate('en')->getName();
$product->translate('fr')->getName();

Or even better

甚至更好

$product->getName();

Will use the current user locale to output the good translation.

将使用当前用户区域设置输出良好的翻译。

#2


You could use VM5's Entity Translations bundle. It's pretty simple and lightweight. And it also uses native Doctrine ORM associations for translations.

您可以使用VM5的Entity Translations包。它非常简单轻巧。它还使用原生Doctrine ORM关联进行翻译。