basically just a way to switch the session to a different language. maybe use LuneticsLocaleBundle
@lsmith77 the top right corner template can be tweaked so you can include the language switcher here.
indeed. got it to work like that.
can you post your solution here @lsmith77 ?
I followed @rande suggestion and created language switcher this way (using sf4 and autowiring):
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
final class LocaleController
{
/**
* @Route("/locale/{locale}", name="locale")
*/
public function index(Request $request, string $locale): RedirectResponse
{
$request->getSession()->set('_locale', $locale);
return new RedirectResponse($request->headers->get('referer', '/'));
}
}
{% extends '@!SonataAdmin/standard_layout.html.twig' %}
{% block sonata_top_nav_menu %}
{{ parent() }}
<div class="navbar-custom-menu">
<ul class="nav navbar-nav">
{% block sonata_top_nav_menu_locale_block %}
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
<i class="fa fa-flag fa-fw" aria-hidden="true"></i>
<i class="fa fa-caret-down" aria-hidden="true"></i>
</a>
<div class="dropdown-menu multi-column dropdown-add">
<div class="container-fluid">
<div class="row">
<ul class="dropdown-menu">
<li role="presentation" class="dropdown-header">
<i class="fa fa-language"></i>
{{ 'languages_title'|trans({}, 'SonataAdminBundle') }}
</li>
{% for locale in available_locales|split('|') %}
<li role="presentation" class="{{ app.request.locale == locale ? 'active' : '' }}">
<a role="menuitem" tabindex="-1" href="{{ path('locale', {'locale': locale}) }}">{{ locale|language|capitalize }}</a>
</li>
{% endfor %}
</ul>
</div>
</div>
</div>
</li>
{% endblock %}
</ul>
</div>
{% endblock %}
twig:
...
globals:
available_locales: '%app.locales%'
parameters:
...
app.locales: en|fr
composer require sonata-project/intl-bundle{{ locale|language|capitalize }} by {{ locale }} in template if you don't need locale switcher to display language.@jo66 would you like to provide a PR and add this to documentation?
@kunicmarko20 done! Here is the link.
Global locale switcher is now implemented in SonataTranslationBundle, see doc.
Thank you @jo66!
Most helpful comment
I followed @rande suggestion and created language switcher this way (using sf4 and autowiring):
composer require sonata-project/intl-bundleAlternatively, you can replace
{{ locale|language|capitalize }}by{{ locale }}in template if you don't need locale switcher to display language.