2017 © Pedro Peláez
 

symfony-bundle translate-bundle

Bundle provides translation mechanism for Doctrine entities

image

nkt/translate-bundle

Bundle provides translation mechanism for Doctrine entities

  • Friday, September 5, 2014
  • by nkt
  • Repository
  • 1 Watchers
  • 0 Stars
  • 16 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Translate bundle

Bundle provides translation mechanism for Doctrine entities., (*1)

Installation

Add to your composer.json:, (*2)

{
    "require": {
        "nkt/translate-bundle": "~0.1"
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}

Enable the bundle in the kernel:, (*3)

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Nkt\TranslateBundle\NktTranslateBundle(),
    );
}

Usage

Let's create entities for simple blog., (*4)

<?php

namespace Acme\BlogBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Nkt\TranslateBundle\Behavior as TranslateBehavior;

class Post implements TranslateBehavior\LocaleAware
{
    use TranslateBehavior\Translatable;

    /**
     * @var int
     */
    private $id;

    public function __construct()
    {
        $this->translations = new ArrayCollection();
    }

    public function getId()
    {
        return $this->id;
    }
}

Now we have to create entity for translations., (*5)

<?php

namespace Acme\BlogBundle\Entity;

use Nkt\TranslateBundle\Behavior as TranslateBehavior;

class PostTranslation
{
    use TranslateBehavior\Translation;

    private $title;
    private $content;

    public function getTitle()
    {
        return $this->title;
    }

    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    public function getContent()
    {
        return $this->content;
    }

    public function setContent($content)
    {
        $this->content = $content;

        return $this;
    }
}

Now you can use it., (*6)

public function newPostAction(Request $request)
{
    $post = new Post();
    foreach ($request->request->get('translations') as $translation) {
        $post->translate($translation['locale'])
             ->setTitle($translation['title'])
             ->setContent($translation['content']);
    }
    ...
}

The translate method look for translation for given $locale and creates it if not found., (*7)

The getTranslation method look for translation for given $locale and if not found, look for currentLocale translation., (*8)

LocaleAware interface

If you want inject application locale in your translatable entities, you should implement Nkt\TranslateBundle\Behavior\LocaleAware interface. By default currentLocale equals to en in every translatable entity., (*9)

License

MIT, (*10)

The Versions

05/09 2014

dev-master

9999999-dev

Bundle provides translation mechanism for Doctrine entities

  Sources   Download

MIT

The Requires

 

by Nikita Gusakov

entity translation translate