8000 Dropdown to change locale · SofHad/symfony@307236b · GitHub
[go: up one dir, main page]

Skip to content

Commit 307236b

Browse files
JulienItardjaviereguiluz
authored andcommitted
Dropdown to change locale
1 parent b8acef7 commit 307236b

File tree

8 files changed

+88
-6
lines changed

8 files changed

+88
-6
lines changed

app/Resources/assets/scss/main.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ header {
5959
ul.nav li {
6060
margin-bottom: 0;
6161
}
62+
63+
.locales a {
64+
padding-right: 10px;
65+
}
6266
}
6367

6468
// Body contents

app/Resources/views/base.html.twig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@
4949
</div>
5050
<div class="navbar-collapse collapse">
5151
<ul class="nav navbar-nav navbar-right">
52+
5253
{% block header_navigation_links %}
54+
5355
<li>
5456
<a href="{{ path('blog_index') }}">
5557
<i class="fa fa-home"></i> {{ 'menu.homepage'|trans }}
@@ -77,6 +79,16 @@
7779
</a>
7880
</li>
7981
{% endif %}
82+
83+
<li class="dropdown">
84+
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-globe"></i> <span class="caret"></span></a>
85+
<ul class="dropdown-menu locales" role="menu">
86+
{% for locale in locales() %}
87+
<li {% if app.request.locale == locale.code %}class="active"{% endif %}><a href="{{ path('blog_index', {_locale:locale.code}) }}">{{ locale.name|capitalize }}</a></li>
88+
{% endfor %}
89+
</ul>
90+
</li>
91+
8092
</ul>
8193
</div>
8294
</div>

app/config/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ imports:
99
- { resource: security.yml }
1010
- { resource: services.yml }
1111

12+
parameters:
13+
app_locales: en|fr|de|es|cs|ru|uk|ro|pt_BR
14+
1215
# Basic configuration for the Symfony framework features
1316
framework:
1417
# Uncomment the 'ide' option to turn all of the file paths in an exception

app/config/routing.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
app:
88
resource: @AppBundle/Controller/
99
type: annotation
10+
prefix: /{_locale}
11+
requirements:
12+
_locale: %app_locales%
13+
defaults:
14+
_locale: %locale%
1015

1116
# These lines define a route using YAML configuration. The controller used by
1217
# the route (FrameworkBundle:Template:template) is a convenient shortcut when

app/config/services.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ services:
1818
tags:
1919
- { name: twig.extension }
2020

21+
app.twig.locale_extension:
22+
class: AppBundle\Twig\LocaleExtension
23+
arguments: ["%app_locales%"]
24+
tags:
25+
- { name: twig.extension }
26+
2127
app.twig.source_code_extension:
2228
class: AppBundle\Twig\SourceCodeExtension
2329
arguments: ["@twig.loader", "%kernel.root_dir%"]

src/AppBundle/Controller/BlogController.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@
1111

1212
namespace AppBundle\Controller;
1313

14-
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
14+
use AppBundle\Entity\Comment;
15+
use AppBundle\Entity\Post;
16+
use AppBundle\Form\CommentType;
1517
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
18+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
1619
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
1720
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
18-
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
21+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
1922
use Symfony\Component\HttpFoundation\Request;
2023
use Symfony\Component\HttpFoundation\Response;
21-
use AppBundle\Entity\Post;
22-
use AppBundle\Entity\Comment;
23-
use AppBundle\Form\CommentType;
24+
use Symfony\Component\Intl\Intl;
2425

2526
/**
2627
* Controller used to manage blog contents in the public part of the site.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace AppBundle\Twig;
13+
14+
use Symfony\Component\Intl\Intl;
15+
16+
/**
17+
* @author Julien ITARD <julienitard@gmail.com>
18+
*/
19+
class LocaleExtension extends \Twig_Extension
20+
{
21+
private $locales;
22+
23+
public function __construct( $locales)
24+
{
25+
$this->locales = $locales;
26+
}
27+
28+
public function getFunctions()
29+
{
30+
return array(
31+
new \Twig_SimpleFunction('locales', array($this, 'getLocales')),
32+
);
33+
}
34+
35+
public function getLocales()
36+
{
37+
$array = explode('|', $this->locales);
38+
39+
foreach ($array as $locale) {
40+
$locales[] = array('code' => $locale, 'name' => Intl::getLocaleBundle()->getLocaleName($locale, $locale));
41+
}
42+
43+
return $locales;
44+
}
45+
46+
// the name of the Twig extension must be unique in the application
47+
public function getName()
48+
{
49+
return 'app.locale_extension';
50+
}
51+
}

web/css/app.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
0