Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
370 additions
and
1 deletion.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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', '/')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
sonata.translation.locale: | ||
path: /locale/{locale} | ||
defaults: | ||
_controller: Sonata\TranslationBundle\Action\LocaleAction::index |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
Oops, something went wrong.