Use language setting of users when logging in.

This commit is contained in:
Jan Böhmer 2019-09-12 17:50:33 +02:00
parent 0dd20394cb
commit 0bfcec77fb
9 changed files with 97 additions and 8 deletions

View file

@ -28,6 +28,7 @@ security:
check_path: login check_path: login
csrf_token_generator: security.csrf.token_manager csrf_token_generator: security.csrf.token_manager
use_referer: true use_referer: true
default_target_path: '/'
logout: logout:
path: logout path: logout

View file

@ -1,3 +1,10 @@
#index: #index:
# path: / # path: /
# controller: App\Controller\DefaultController::index # controller: App\Controller\DefaultController::index
# Redirect every url without an locale to the locale of the user/the global base locale
redirector:
path: /{url}
requirements:
url: ".*"
controller: App\Controller\RedirectController:addLocalePart

View file

@ -2,7 +2,4 @@ controllers:
resource: ../../src/Controller/ resource: ../../src/Controller/
type: annotation type: annotation
prefix: prefix: '{_locale}'
en: '/en'
de: '/de'
'%locale%': '/'

View file

@ -49,6 +49,10 @@ services:
- name: doctrine.orm.entity_listener - name: doctrine.orm.entity_listener
App\Controller\RedirectController:
arguments:
$default_locale: '%locale%'
App\Command\UpdateExchangeRatesCommand: App\Command\UpdateExchangeRatesCommand:
arguments: arguments:
$base_current: '%default_currency%' $base_current: '%default_currency%'

View file

@ -0,0 +1,64 @@
<?php
/**
*
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 Jan Böhmer
* https://github.com/jbtronics
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
namespace App\Controller;
use App\Entity\UserSystem\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
class RedirectController extends AbstractController
{
protected $default_locale;
public function __construct(string $default_locale)
{
$this->default_locale = $default_locale;
}
public function addLocalePart(Request $request)
{
//By default we use the global default locale
$locale = $this->default_locale;
//Check if a user has set a preferred language setting:
$user = $this->getUser();
if ($user instanceof User) {
if(!empty($user->getLanguage())) {
$locale = $user->getLanguage();
}
}
$new_url = str_replace($request->getPathInfo(), '/' . $locale . $request->getPathInfo(), $request->getUri());
return $this->redirect($new_url);
}
}

View file

@ -141,7 +141,7 @@ class User extends NamedDBElement implements UserInterface, HasPermissionsInterf
/** /**
* @var string|null The language/locale the user prefers * @var string|null The language/locale the user prefers
* @ORM\Column(type="string", name="config_language", nullable=true) * @ORM\Column(type="string", name="config_language", nullable=true)
* @Assert\Locale(canonicalize=true) * @Assert\Language()
*/ */
protected $language = ''; protected $language = '';

View file

@ -6,6 +6,7 @@ use App\Entity\UserSystem\User;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\EmailType; use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\LanguageType;
use Symfony\Component\Form\Extension\Core\Type\LocaleType; use Symfony\Component\Form\Extension\Core\Type\LocaleType;
use Symfony\Component\Form\Extension\Core\Type\ResetType; use Symfony\Component\Form\Extension\Core\Type\ResetType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
@ -55,11 +56,12 @@ class UserSettingsType extends AbstractType
'label' => $this->trans->trans('user.email.label'), 'label' => $this->trans->trans('user.email.label'),
'disabled' => !$this->security->isGranted('edit_infos', $options['data']), 'disabled' => !$this->security->isGranted('edit_infos', $options['data']),
]) ])
->add('language', LocaleType::class, [ ->add('language', LanguageType::class, [
'required' => false, 'required' => false,
'attr' => ['class' => 'selectpicker', 'data-live-search' => true], 'attr' => ['class' => 'selectpicker', 'data-live-search' => true],
'placeholder' => $this->trans->trans('user_settings.language.placeholder'), 'placeholder' => $this->trans->trans('user_settings.language.placeholder'),
'label' => $this->trans->trans('user.language_select'), 'label' => $this->trans->trans('user.language_select'),
'preferred_choices' => ['en', 'de']
]) ])
->add('timezone', TimezoneType::class, [ ->add('timezone', TimezoneType::class, [
'required' => false, 'required' => false,

View file

@ -78,7 +78,8 @@ class AppExtension extends AbstractExtension
new TwigFilter('bbCode', [$this, 'parseBBCode'], ['pre_escape' => 'html', 'is_safe' => ['html']]), new TwigFilter('bbCode', [$this, 'parseBBCode'], ['pre_escape' => 'html', 'is_safe' => ['html']]),
new TwigFilter('moneyFormat', [$this, 'formatCurrency']), new TwigFilter('moneyFormat', [$this, 'formatCurrency']),
new TwigFilter('siFormat', [$this, 'siFormat']), new TwigFilter('siFormat', [$this, 'siFormat']),
new TwigFilter('amountFormat', [$this, 'amountFormat']) new TwigFilter('amountFormat', [$this, 'amountFormat']),
new TwigFilter('loginPath', [$this, 'loginPath'])
]; ];
} }
@ -104,6 +105,19 @@ class AppExtension extends AbstractExtension
return $this->serializer->serialize($tree, 'json', ['skip_null_values' => true]); return $this->serializer->serialize($tree, 'json', ['skip_null_values' => true]);
} }
/**
* This function/filter generates an path
* @param string $path
* @return string
*/
public function loginPath(string $path) : string
{
$parts = explode("/" ,$path);
//Remove the part with
unset($parts[1]);
return implode("/", $parts);
}
public function generateEntityURL(DBElement $entity, string $method = 'info'): string public function generateEntityURL(DBElement $entity, string $method = 'info'): string
{ {
return $this->entityURLGenerator->getURL($entity, $method); return $this->entityURLGenerator->getURL($entity, $method);

View file

@ -77,7 +77,7 @@
<li role="separator" class="dropdown-divider"></li> <li role="separator" class="dropdown-divider"></li>
<a class="dropdown-item" href="{{ path('logout') }}"><i class="fa fa-sign-out-alt fa-fw" aria-hidden="true"></i> {% trans %}user.logout{% endtrans %}</a> <a class="dropdown-item" href="{{ path('logout') }}"><i class="fa fa-sign-out-alt fa-fw" aria-hidden="true"></i> {% trans %}user.logout{% endtrans %}</a>
{% else %} {% else %}
<a class="dropdown-item" href="{{ path('login', {'_target_path':app.request.pathinfo}) }}" id="login-link"><i class="fa fa-sign-in-alt fa-fw" aria-hidden="true"></i> {% trans %}user.login{% endtrans %}</a> <a class="dropdown-item" href="{{ path('login', {'_target_path': app.request.pathinfo | loginPath}) }}" id="login-link"><i class="fa fa-sign-in-alt fa-fw" aria-hidden="true"></i> {% trans %}user.login{% endtrans %}</a>
{% endif %} {% endif %}
<li role="separator" class="dropdown-divider"></li> <li role="separator" class="dropdown-divider"></li>
<a class="dropdown-item disabled" href="#">{% trans %}user.language_select{% endtrans %}</a> <a class="dropdown-item disabled" href="#">{% trans %}user.language_select{% endtrans %}</a>