Symfony2 Best Way to Implement Database Translations

I am refactoring a multilingual website in symfony.

The site has a lot of language material stored in databases (product descriptions, product name ...) and language table. It looks something like this: Desktop product: -I would -price -shares -...

Product_language table: -id_product -id_language -name -description

Table language: -I would -code-name

So I think it is best to port this to symfony and doctrine, I was looking for a translatable extension, but I don’t know if that would match (I’m not sure to put the language table in it)

Thank!

+3


source to share


2 answers


I've built a multi-language e-commerce bar using KNP's Translitable behavior and it's absolutely awesome.

It would definitely suit your needs. Here's a quick example of my Product object 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 object called 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 the information about your product (in this example SKU). The second will have a string for each translated instance using a 1-N relationship.

Once you're done, you can use either:

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

      

Or even better

$product->getName();

      

Will use the user's current language to display a good translation.

+4


source


You can use the VMP VM5 binding package . It's pretty simple and lightweight. And it also uses Doctrine's own ORM associations for translations.



0


source







All Articles