mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 09:35:49 +02:00
Use language setting of users when logging in.
This commit is contained in:
parent
0dd20394cb
commit
0bfcec77fb
9 changed files with 97 additions and 8 deletions
|
@ -28,6 +28,7 @@ security:
|
|||
check_path: login
|
||||
csrf_token_generator: security.csrf.token_manager
|
||||
use_referer: true
|
||||
default_target_path: '/'
|
||||
|
||||
logout:
|
||||
path: logout
|
||||
|
|
|
@ -1,3 +1,10 @@
|
|||
#index:
|
||||
# path: /
|
||||
# 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
|
|
@ -2,7 +2,4 @@ controllers:
|
|||
resource: ../../src/Controller/
|
||||
type: annotation
|
||||
|
||||
prefix:
|
||||
en: '/en'
|
||||
de: '/de'
|
||||
'%locale%': '/'
|
||||
prefix: '{_locale}'
|
||||
|
|
|
@ -49,6 +49,10 @@ services:
|
|||
- name: doctrine.orm.entity_listener
|
||||
|
||||
|
||||
App\Controller\RedirectController:
|
||||
arguments:
|
||||
$default_locale: '%locale%'
|
||||
|
||||
App\Command\UpdateExchangeRatesCommand:
|
||||
arguments:
|
||||
$base_current: '%default_currency%'
|
||||
|
|
64
src/Controller/RedirectController.php
Normal file
64
src/Controller/RedirectController.php
Normal 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);
|
||||
}
|
||||
}
|
|
@ -141,7 +141,7 @@ class User extends NamedDBElement implements UserInterface, HasPermissionsInterf
|
|||
/**
|
||||
* @var string|null The language/locale the user prefers
|
||||
* @ORM\Column(type="string", name="config_language", nullable=true)
|
||||
* @Assert\Locale(canonicalize=true)
|
||||
* @Assert\Language()
|
||||
*/
|
||||
protected $language = '';
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ use App\Entity\UserSystem\User;
|
|||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
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\ResetType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
|
@ -55,11 +56,12 @@ class UserSettingsType extends AbstractType
|
|||
'label' => $this->trans->trans('user.email.label'),
|
||||
'disabled' => !$this->security->isGranted('edit_infos', $options['data']),
|
||||
])
|
||||
->add('language', LocaleType::class, [
|
||||
->add('language', LanguageType::class, [
|
||||
'required' => false,
|
||||
'attr' => ['class' => 'selectpicker', 'data-live-search' => true],
|
||||
'placeholder' => $this->trans->trans('user_settings.language.placeholder'),
|
||||
'label' => $this->trans->trans('user.language_select'),
|
||||
'preferred_choices' => ['en', 'de']
|
||||
])
|
||||
->add('timezone', TimezoneType::class, [
|
||||
'required' => false,
|
||||
|
|
|
@ -78,7 +78,8 @@ class AppExtension extends AbstractExtension
|
|||
new TwigFilter('bbCode', [$this, 'parseBBCode'], ['pre_escape' => 'html', 'is_safe' => ['html']]),
|
||||
new TwigFilter('moneyFormat', [$this, 'formatCurrency']),
|
||||
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]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
return $this->entityURLGenerator->getURL($entity, $method);
|
||||
|
|
|
@ -77,7 +77,7 @@
|
|||
<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>
|
||||
{% 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 %}
|
||||
<li role="separator" class="dropdown-divider"></li>
|
||||
<a class="dropdown-item disabled" href="#">{% trans %}user.language_select{% endtrans %}</a>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue