2017 © Pedro PelĂĄez
 

symfony-bundle translate-bundle

Symfony2 Prims47TranslateBundle

image

prims47/translate-bundle

Symfony2 Prims47TranslateBundle

  • Wednesday, May 21, 2014
  • by prims47
  • Repository
  • 1 Watchers
  • 1 Stars
  • 53 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Prims47TranslateBundle

This bundle provides a translate databse solution for Symfony2., (*1)

First step: Installation

Use composer to manage your dependencies and download Prims47TranslateBundle:, (*2)

``` json "require": { "php": ">=5.3.3", "symfony/symfony": ">=2.2", "doctrine/orm": "~2.2,>=2.2.3", "doctrine/doctrine-bundle": "~1.2", // ... "prims47/translate-bundle": "dev-master" }, (*3)


## Enable Prims47TranslateBundle ``` php <?php // app/AppKernel.php public function registerBundles() { return array( // ... // Then add Prims47TranslateBundle new Prims47\Bundle\TranslateBundle\Prims47TranslateBundle(), // ... ); }

Configuration Prims47TranslateBundle

Configure your locale parameter

``` yml, (*4)

app/parameters.yml

parameters: # ... locale: en_FR # Or "de_DE"; "fr_FR" ... # ..., (*5)


### Mapping ``` yml # app/config/config.yml doctrine: dbal: # your dbal config here orm: auto_generate_proxy_classes: %kernel.debug% auto_mapping: true # only these lines are added additionally mappings: translatable: type: annotation alias: Gedmo prefix: Gedmo\Translatable\Entity # make sure vendor library location is correct dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity"

After that, running php app/console doctrine:mapping:info, (*6)

For more details, review the Gedmo doc, (*7)

Doctrine extension listener services

``` yml, (*8)

app/config/config.yml

services: extension.listener: class: Prims47\Bundle\TranslateBundle\Listener\DoctrineExtensionListener calls: - [ setContainer, [ @service_container ] ] tags: # translatable sets locale after router processing - { name: kernel.event_listener, event: kernel.request, method: onLateKernelRequest, priority: -10 }, (*9)

# Doctrine Extension listeners to handle behaviors
gedmo.listener.translatable:
    class: Gedmo\Translatable\TranslatableListener
    tags:
        - { name: doctrine.event_subscriber, connection: default }
    calls:
        - [ setAnnotationReader, [ @annotation_reader ] ]
        - [ setDefaultLocale, [ "%locale%" ] ]
        - [ setTranslationFallback, [ true ] ]

### Translatable Entity example ``` php id = $id; } public function getId() { return $this->id; } public function setTitle($title) { $this->title = $title; } public function getTitle() { return $this->title; } public function setContent($content) { $this->content = $content; } public function getContent() { return $this->content; } public function setTranslatableLocale($locale) { $this->locale = $locale; } } ``` ### Xml mapping example ``` xml <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> <entity repository-class="Acme\DemoBundle\Entity\PostRepository" name="Acme\DemoBundle\Entity\Post"> <!-- Don't forget xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping" --> <id name="id" type="integer" column="id"> <generator strategy="AUTO"/> </id> <field name="title" column="title" type="string" length="255"> <gedmo:translatable/> </field> <field name="content" column="content" type="text"> <gedmo:translatable/> </field> <gedmo:translation entity="Gedmo\Translatable\Entity\Translation" locale="locale"/> </entity> </doctrine-mapping>

After that, running php app/console doctrine:schema:update --force, (*10)

Basic usage examples

Currently a global locale used for translations in your parameters.yml (en_US for this exemple) which was set in DoctrineExtensionListener., (*11)

Step 0: Create your manager

Before start the translation, we must create a manager:, (*12)

``` php getRepository()->findAll(); } /** * Find Post by id. * * @param integer $id * * @return mixed|null|object */ public function find($id) { return $this->getRepository()->find($id); } } ``` After this, you must declare this manager has a service. ``` xml , (*13)

, (*14)

<services>
    <service id="acme.demo.post.manager" class="Acme\DemoBundle\Entity\PostManager">
        <argument type="service" id="doctrine.orm.default_entity_manager" />
        <argument>Acme\DemoBundle\Entity\Post</argument> <!-- Your entity -->
        <call method="setRepositoryLocale">
            <argument type="service" id="service_container" /> <!-- You must call this method for declare your locale dynamically -->
        </call>
    </service>
</services>

, (*15)


#### Step 1: Adapt your repository ``` php <?php namespace Acme\DemoBundle\Entity; use Prims47\Bundle\TranslateBundle\Doctrine\ORM\EntityRepository; /** * PostRepository * * This class was generated by the Doctrine ORM. Add your own custom * repository methods below. */ class PostRepository extends EntityRepository { /** * Find all post by locale. * * @return array */ public function findAll() { $qb = $this->createQueryBuilder('p'); return $this->getResult($qb); } /** * Find Post by id. * * @param integer $id * * @return mixed|null|object */ public function find($id) { $qb = $this->createQueryBuilder('p'); $qb->where('p.id = :id'); $qb->setParameter('id', $id); return $this->getOneOrNullResult($qb); } }

Step 1: Basic usage

To save post with its translations:, (*16)

``` php <?php, (*17)

/** @var PostManager $postManager */ $postManager = $this->get('acme.demo.post.manager');, (*18)

$post = new Post(); $post->setTitle('My first title in english'); $post->setContent('My first content in english');, (*19)

$postManager->save($post, true);, (*20)

$post = $postManager->find(1);, (*21)

Lets update our post in different locale:

``` php
<?php
/** @var PostManager $postManager */
$postManager = $this->get('acme.demo.post.manager');

$post = $postManager->find(1);

$post->setTitle('Mon premier titre en français');
$post->setContent('Mon premier contenu en français');
$post->setTranslatableLocale('fr_FR');

$postManager->save($post, true);

Now change your local parameter:, (*22)

They are two methods., (*23)

  • Change your local in parameters.yml
  • Or add in session the local value :

``` php <?php, (*24)

$session = $this->get('session'); $session->set('_locale', 'fr_FR');, (*25)

/** @var PostManager $postManager */ $postManager = $this->get('acme.demo.post.manager');, (*26)

$post = $postManager->find(1);, (*27)

```, (*28)

The Versions

21/05 2014

dev-master

9999999-dev

Symfony2 Prims47TranslateBundle

  Sources   Download

MIT

The Requires

 

symfony2 bundle translate