Build Status Scrutinizer Quality Score Latest Stable Version Total Downloads

SensioLabsInsight knpbundles.com

Overview

This bundle integrates Gedmo Doctrine Extensions for Sonata Admin.

It allows you to define multiple locales for your entities and provides a locale switcher in every forms.

PrestaSonataGedmoDoctrineExtensionsBundle-Translation

Installation

This bundles works with PrestaSonataAdminExtendedBundle so you should refer to its installation.

You should have a look at symfony-prestacms project for a ready to use installation of this bundles.

Getting started

First have a look at Gedmo translatable documentation

To show you how PrestaSonataGedmoDoctrineExtensionsBundle works we will take PrestaCMSFAQBundle as an example.

Make an entity translatable

If you want to make an entity translatable you have to :

  • extends AbstractTranslatable
  • implement TranslatableInterface
  • declare its TranslationEntity in Annotation
  • declare some of its fileds Translatable

FAQCategory code looks like this :

<?php
namespace Presta\CMSFAQBundle\Entity;

use Presta\SonataGedmoDoctrineExtensionsBundle\Entity\AbstractTranslatable;
use Gedmo\Mapping\Annotation as Gedmo;
use Presta\SonataGedmoDoctrineExtensionsBundle\Entity\TranslatableInterface;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @author Nicolas Bastien <nbastien@prestaconcept.net>
 *
 * @ORM\Table(name="presta_cms_faq_category")
 * @ORM\Entity(repositoryClass="Presta\CMSFAQBundle\Entity\FAQCategory\Repository")
 * @Gedmo\TranslationEntity(class="Presta\CMSFAQBundle\Entity\FAQCategory\Translation")
 */
class FAQCategory extends AbstractTranslatable implements TranslatableInterface
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string $title
     *
     * @Gedmo\Translatable
     * @ORM\Column(name="title", type="string", length=255, nullable=true)
     */
    protected $title;

    /**
     * @var boolean $enabled
     *
     * @ORM\Column(name="enabled", type="boolean", nullable=false)
     */
    protected $enabled = false;

    /**
     * @var integer $position
     *
     * @ORM\Column(name="position", type="integer", length=2, nullable=true)
     */
    protected $position;

    /**
     * @var ArrayCollection
     *
     * @ORM\OneToMany(
     *     targetEntity="Presta\CMSFAQBundle\Entity\FAQCategory\Translation",
     *     mappedBy="object",
     *     cascade={"persist", "remove"}
     * )
     */
    protected $translations;

    /* ... */
}

Associate a translation entity

Then create its translation class like this :

<?php

namespace Presta\CMSFAQBundle\Entity\FAQCategory;

use Doctrine\ORM\Mapping as ORM;
use Presta\SonataGedmoDoctrineExtensionsBundle\Entity\AbstractTranslation;

/**
 * @ORM\Entity
 * @ORM\Table(name="presta_cms_faq_category_translation",
 *     uniqueConstraints={@ORM\UniqueConstraint(name="lookup_unique_faq_category_translation_idx", columns={
 *         "locale", "object_id", "field"
 *     })}
 * )
 *
 * @author Nicolas Bastien <nbastien@prestaconcept.net>
 */
class Translation extends AbstractTranslation
{
    /**
     * @ORM\ManyToOne(targetEntity="Presta\CMSFAQBundle\Entity\FAQCategory", inversedBy="translations")
     * @ORM\JoinColumn(name="object_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $object;
}

Configuration

The last thing is to configure the locales to display in your locale switcher.

This bundle uses two parameters :

  • locales : array of available locales for translation; i.e., your frontend locales
  • default_locale : which locale to set by default ?

So your configuration can look like this :

parameters:
    default_locale:  en
    locales: [fr, en, it, nl, es]

Enjoy !


Want do contribute ? Just fork and edit it!