Skip to content

Commit

Permalink
feature #102 Dropdown to change locale (JulienItard)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the master branch (closes #102).

Discussion
----------

Dropdown to change locale

see #83

Someone should add comments in routing.yml because my english it not good enough :)

![capture du 2015-07-15 19 52 19](https://cloud.githubusercontent.com/assets/1370893/8705607/0b15ade0-2b2b-11e5-933a-5cafaad4c8ee.png)

Commits
-------

307236b Dropdown to change locale
  • Loading branch information
javiereguiluz committed Jul 21, 2015
2 parents be4e454 + 307236b commit de5d5ef
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 6 deletions.
4 changes: 4 additions & 0 deletions app/Resources/assets/scss/main.scss
Expand Up @@ -59,6 +59,10 @@ header {
ul.nav li {
margin-bottom: 0;
}

.locales a {
padding-right: 10px;
}
}

// Body contents
Expand Down
12 changes: 12 additions & 0 deletions app/Resources/views/base.html.twig
Expand Up @@ -49,7 +49,9 @@
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">

{% block header_navigation_links %}

<li>
<a href="{{ path('blog_index') }}">
<i class="fa fa-home"></i> {{ 'menu.homepage'|trans }}
Expand Down Expand Up @@ -77,6 +79,16 @@
</a>
</li>
{% endif %}

<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-globe"></i> <span class="caret"></span></a>
<ul class="dropdown-menu locales" role="menu">
{% for locale in locales() %}
<li {% if app.request.locale == locale.code %}class="active"{% endif %}><a href="{{ path('blog_index', {_locale:locale.code}) }}">{{ locale.name|capitalize }}</a></li>
{% endfor %}
</ul>
</li>

</ul>
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletions app/config/config.yml
Expand Up @@ -9,6 +9,9 @@ imports:
- { resource: security.yml }
- { resource: services.yml }

parameters:
app_locales: en|fr|de|es|cs|ru|uk|ro|pt_BR

# Basic configuration for the Symfony framework features
framework:
# Uncomment the 'ide' option to turn all of the file paths in an exception
Expand Down
5 changes: 5 additions & 0 deletions app/config/routing.yml
Expand Up @@ -7,6 +7,11 @@
app:
resource: @AppBundle/Controller/
type: annotation
prefix: /{_locale}
requirements:
_locale: %app_locales%
defaults:
_locale: %locale%

# These lines define a route using YAML configuration. The controller used by
# the route (FrameworkBundle:Template:template) is a convenient shortcut when
Expand Down
6 changes: 6 additions & 0 deletions app/config/services.yml
Expand Up @@ -18,6 +18,12 @@ services:
tags:
- { name: twig.extension }

app.twig.locale_extension:
class: AppBundle\Twig\LocaleExtension
arguments: ["%app_locales%"]
tags:
- { name: twig.extension }

app.twig.source_code_extension:
class: AppBundle\Twig\SourceCodeExtension
arguments: ["@twig.loader", "%kernel.root_dir%"]
Expand Down
11 changes: 6 additions & 5 deletions src/AppBundle/Controller/BlogController.php
Expand Up @@ -11,16 +11,17 @@

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use AppBundle\Entity\Comment;
use AppBundle\Entity\Post;
use AppBundle\Form\CommentType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use AppBundle\Entity\Post;
use AppBundle\Entity\Comment;
use AppBundle\Form\CommentType;
use Symfony\Component\Intl\Intl;

/**
* Controller used to manage blog contents in the public part of the site.
Expand Down
51 changes: 51 additions & 0 deletions src/AppBundle/Twig/LocaleExtension.php
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace AppBundle\Twig;

use Symfony\Component\Intl\Intl;

/**
* @author Julien ITARD <julienitard@gmail.com>
*/
class LocaleExtension extends \Twig_Extension
{
private $locales;

public function __construct( $locales)
{
$this->locales = $locales;
}

public function getFunctions()
{
return array(
new \Twig_SimpleFunction('locales', array($this, 'getLocales')),
);
}

public function getLocales()
{
$array = explode('|', $this->locales);

foreach ($array as $locale) {
$locales[] = array('code' => $locale, 'name' => Intl::getLocaleBundle()->getLocaleName($locale, $locale));
}

return $locales;
}

// the name of the Twig extension must be unique in the application
public function getName()
{
return 'app.locale_extension';
}
}
2 changes: 1 addition & 1 deletion web/css/app.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit de5d5ef

Please sign in to comment.