Last active
August 29, 2015 14:06
-
-
Save flovntp/8c2a3052e10e0e774414 to your computer and use it in GitHub Desktop.
Language switcher
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
ezpublish: | |
siteaccess: | |
default_siteaccess: eng | |
list: | |
# Public site | |
- eng | |
- fre | |
# backoffice | |
- ezdemo_site_admin | |
groups: | |
public_access: | |
- eng | |
- fre | |
match: | |
URIElement: '1' | |
system: | |
public_access: | |
translation_siteaccesses: [eng,fre] |
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
<?php | |
namespace My\DesignBundle\Controller; | |
use eZ\Bundle\EzPublishCoreBundle\Controller; | |
use eZ\Publish\Core\MVC\Symfony\Routing\RouteReference; | |
class LanguageSwitcherController extends Controller | |
{ | |
public function languagesAction( RouteReference $routeReference ) | |
{ | |
// get a tab with info for existing siteaccess | |
$translationHelper = $this->container->get( 'ezpublish.translation_helper' ); | |
$availableLanguage = $translationHelper->getAvailableLanguages(); | |
// create an array for corresponding siteaccesses names depending on the lang | |
foreach ($availableLanguage as $lang) { | |
$siteaccess[ $lang ] = $translationHelper->getTranslationSiteAccess( $lang ); | |
} | |
// get cuurent eZ language | |
$currentSFLanguage = $this->getRequest()->get( '_locale'); | |
$currentEzLanguage = array_search( | |
$currentSFLanguage , | |
$this->container->getParameter( 'ezpublish.locale.conversion_map' ) | |
); | |
return $this->render( 'MyDesignBundle:Language:switcher.html.twig', | |
array( 'routeRef' => $routeReference, | |
'siteaccess' => $siteaccess, | |
'currentLanguage' => $currentEzLanguage ) ); | |
} | |
} |
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
<!-- Latest compiled and minified CSS --> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> | |
<!-- Optional theme --> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css"> | |
<!-- Latest compiled and minified JavaScript --> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> |
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
<div id="language-switcher" class="pull-right"> | |
{{ render( controller( 'MyDesignBundle:LanguageSwitcher:languages', {'routeReference': ez_route()} ) ) }} | |
</div> | |
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
{# Looping over all available languages to display the links #} | |
<ul id="" class="nav pull-left"> | |
<li class="dropdown menu-item"> | |
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"> | |
<img src="{{ asset( '/share/icons/flags/' ~ currentLanguage ~ '.gif' ) }}" | |
title="{{ currentLanguage }}"> | |
{{ siteaccess[currentLanguage] }} | |
<span class="caret"></span> | |
</button> | |
<ul class="dropdown-menu"> | |
{% for lang in ezpublish.availableLanguages %} | |
{# This time, we alter the "siteaccess" parameter directly. #} | |
{# We get the right siteaccess with the help of ezpublish.translationSiteAccess() helper #} | |
{% do routeRef.set( "siteaccess", ezpublish.translationSiteAccess( lang ) ) %} | |
{% if lang != currentLanguage %} | |
<li> | |
<a href="{{ url( routeRef ) }}"> | |
<img src="{{ asset( '/share/icons/flags/' ~ lang ~ '.gif' ) }}" title="{{ lang }}"> | |
{{ siteaccess[lang]}} | |
</a> | |
</li> | |
{% endif %} | |
{% endfor %} | |
</ul> | |
</li> | |
</ul> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment