jprud67/translator-bundle

This set offers you the possibility to add a content translation to your symfony website very easily.

Installs: 5

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 2

Forks: 1

Open Issues: 0

Type:symfony-bundle

v0.1.1 2019-10-15 09:22 UTC

This package is not auto-updated.

Last update: 2024-04-10 18:27:55 UTC


README

This set offers you the possibility to add a content translation to your symfony website very easily.

TranslatorBundle installation

1: Add the dependancy to your composer

composer require jprud67/translator-bundle

2: Register TranslatorBundle in the Symfony kernel

<?php

// config/bundle.php

return [
    //...
   Jprud67\TranslatorBundle\Jprud67TranslatorBundle::class => ['all' => true],
];

3: Add routes routes

# config/routes.yaml

jprud67_translation_routing:
    resource: "@Jprud67TranslatorBundle/Resources/config/routing.yaml"

4: Bundle configuration

# config/packages/jprud67_translator.yaml

doctrine:
    orm:
        mappings:
            Jprud67_translator:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/vendor/Jprud67/translator-bundle/Entity'
                prefix: 'Jprud67\TranslatorBundle\Entity'
                alias: Jprud67_translator
jprud67_translator:
    locales: ['en','es']

5: Update the database

 php bin/console doctrine:schema:update --force

6: Install the styles

 php bin/console asset:install

Using TranslatorBundle

1: Add annotations to your entity

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Jprud67\TranslatorBundle\Annotation\Translatable;
use Jprud67\TranslatorBundle\Annotation\TranslatableField;

/**
 * @ORM\Entity(repositoryClass="App\Repository\PostRepository")
 * @Translatable()
 */
class Post
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=190)
     * @TranslatableField()
     */
    private $title;

    // ...
}

2: Go to url mysite.xyz/translation/webui to edit your translations

3: Show your translation in twig

 {% for post in posts %}
     <li>{{ jp_trans(post,'title') }}</li>
 {% endfor %}