Skip to content

Commit

Permalink
Merge fab249f into 3e91aa5
Browse files Browse the repository at this point in the history
  • Loading branch information
jo66 committed Nov 27, 2018
2 parents 3e91aa5 + fab249f commit 629c315
Show file tree
Hide file tree
Showing 16 changed files with 370 additions and 1 deletion.
Binary file added docs/images/locale_switcher.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion docs/index.rst
@@ -1,7 +1,8 @@
Translation Bundle
==================

SonataTranslationBundle allows you to easily translate your models in SonataAdmin in every locales you need on your frontend.
SonataTranslationBundle allows you to easily translate your models in SonataAdmin
in every locales you need on your frontend.

This bundle adds a locale switcher block in :

Expand All @@ -14,6 +15,8 @@ This bundle adds a locale switcher block in :
* **ORM** : integrates Doctrine ORM entities with Gedmo extensions
* **PHPCR**: integrates Doctrine PHPCR ODM

In addition, it provides a nice global locale switcher for your admin.

.. toctree::
:caption: Reference Guide
:name: reference-guide
Expand All @@ -25,3 +28,4 @@ This bundle adds a locale switcher block in :
reference/orm
reference/phpcr
reference/custom_action
reference/global_locale_switcher
62 changes: 62 additions & 0 deletions docs/reference/global_locale_switcher.rst
@@ -0,0 +1,62 @@
Global locale switcher
======================

``SonataTranslationBundle`` also provides a nice global locale switcher
for your admin.

Prerequisites
-------------

Install the following Sonata dependencies:

* `Sonata Intl Bundle`_

Configuration
-------------

Override standard layout template with the one from the bundle:

.. code-block:: yaml
# app/config/packages/sonata_admin.yaml
sonata_admin:
...
templates:
layout: '@SonataTranslation/standard_layout.html.twig'
Configure locale subscriber and user locale subscriber services:

.. code-block:: yaml
# app/config/services.yaml
services:
...
Sonata\TranslationBundle\EventSubscriber\LocaleSubscriber:
arguments: ['%kernel.default_locale%']
Sonata\TranslationBundle\EventSubscriber\UserLocaleSubscriber:
arguments: ~
Configure route:

.. code-block:: yaml
# app/config/routes.yaml
...
sonata_translation:
resource: '@SonataTranslationBundle/Resources/config/routes.yaml'
How it looks
------------

You are done and you probably want to know how this looks like in the admin
interface. Well, let's find out by going to /admin

.. image:: ../images/locale_switcher.png
:align: center
:alt: Sonata Dashboard with locale switcher
:width: 700px

.. _`Sonata Intl Bundle`: https://sonata-project.org/bundles/intl
35 changes: 35 additions & 0 deletions src/Action/LocaleAction.php
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\TranslationBundle\Action;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
* Locale action.
*
* @author Jonathan Vautrin <jonathan.vautrin@gmail.com>
*/
final class LocaleAction
{
/**
* Switch current locale.
*
* @return RedirectResponse
*/
public function index(Request $request, $locale)
{
$request->getSession()->set('_locale', $locale);
return new RedirectResponse($request->headers->get('referer', '/'));
}
}
68 changes: 68 additions & 0 deletions src/EventSubscriber/LocaleSubscriber.php
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\TranslationBundle\EventSubscriber;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Locale subscriber.
*
* @author Jonathan Vautrin <jonathan.vautrin@gmail.com>
*/
final class LocaleSubscriber implements EventSubscriberInterface
{
/**
* @var string
*/
private $defaultLocale;

/**
* @param string $defaultLocale
*/
public function __construct($defaultLocale = 'en')
{
$this->defaultLocale = $defaultLocale;
}

/**
* @param GetResponseEvent $event
*/
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if (!$request->hasPreviousSession()) {
return;
}

// try to see if the locale has been set as a _locale routing parameter
if ($locale = $request->attributes->get('_locale')) {
$request->getSession()->set('_locale', $locale);
return;
}

// if no explicit locale has been set on this request, use one from the session
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
}

/**
* @return array
*/
public static function getSubscribedEvents()
{
return array(
// must be registered before (i.e. with a higher priority than) the default Locale listener
KernelEvents::REQUEST => [['onKernelRequest', 20]],
);
}
}
61 changes: 61 additions & 0 deletions src/EventSubscriber/UserLocaleSubscriber.php
@@ -0,0 +1,61 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\TranslationBundle\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;

/**
* Stores the locale of the user in the session after the
* login. This can be used by the LocaleSubscriber afterwards.
*
* @author Jonathan Vautrin <jonathan.vautrin@gmail.com>
*/
final class UserLocaleSubscriber implements EventSubscriberInterface
{
/**
* @var SessionInterface
*/
private $session;

/**
* @param SessionInterface $session
*/
public function __construct(SessionInterface $session)
{
$this->session = $session;
}

/**
* @param InteractiveLoginEvent $event
*/
public function onInteractiveLogin(InteractiveLoginEvent $event)
{
$user = $event->getAuthenticationToken()->getUser();

if (null !== $user->getLocale()) {
$this->session->set('_locale', $user->getLocale());
}
}

/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
];
}
}
4 changes: 4 additions & 0 deletions src/Resources/config/routes.yaml
@@ -0,0 +1,4 @@
sonata.translation.locale:
path: /locale/{locale}
defaults:
_controller: Sonata\TranslationBundle\Action\LocaleAction::index
11 changes: 11 additions & 0 deletions src/Resources/translations/SonataAdminBundle.de.xlf
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="de" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="languages_title">
<source>languages_title</source>
<target>Sprachen</target>
</trans-unit>
</body>
</file>
</xliff>
11 changes: 11 additions & 0 deletions src/Resources/translations/SonataAdminBundle.en.xlf
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="languages_title">
<source>languages_title</source>
<target>Languages</target>
</trans-unit>
</body>
</file>
</xliff>
11 changes: 11 additions & 0 deletions src/Resources/translations/SonataAdminBundle.es.xlf
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="es" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="languages_title">
<source>languages_title</source>
<target>Idiomas</target>
</trans-unit>
</body>
</file>
</xliff>
11 changes: 11 additions & 0 deletions src/Resources/translations/SonataAdminBundle.fr.xlf
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="fr" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="languages_title">
<source>languages_title</source>
<target>Langues</target>
</trans-unit>
</body>
</file>
</xliff>
11 changes: 11 additions & 0 deletions src/Resources/translations/SonataAdminBundle.hu.xlf
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="hu" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="languages_title">
<source>languages_title</source>
<target>Nyelvek</target>
</trans-unit>
</body>
</file>
</xliff>
11 changes: 11 additions & 0 deletions src/Resources/translations/SonataAdminBundle.it.xlf
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="it" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="languages_title">
<source>languages_title</source>
<target>Le lingue</target>
</trans-unit>
</body>
</file>
</xliff>
11 changes: 11 additions & 0 deletions src/Resources/translations/SonataAdminBundle.pl.xlf
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="pl" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="languages_title">
<source>languages_title</source>
<target>Języki</target>
</trans-unit>
</body>
</file>
</xliff>
11 changes: 11 additions & 0 deletions src/Resources/translations/SonataAdminBundle.ru.xlf
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="ru" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="languages_title">
<source>languages_title</source>
<target>Языки</target>
</trans-unit>
</body>
</file>
</xliff>

0 comments on commit 629c315

Please sign in to comment.