Admin Locale Middleware Added

This commit is contained in:
devansh bawari 2021-06-07 14:57:30 +05:30
parent 36a6ff7077
commit 520a796578
5 changed files with 110 additions and 10 deletions

View File

@ -0,0 +1,56 @@
<?php
namespace Webkul\Admin\Http\Middleware;
use Closure;
use Webkul\Core\Repositories\LocaleRepository;
class Locale
{
/**
* Locale repository.
*
* @var LocaleRepository
*/
protected $locale;
/**
* Create a new middleware instance.
*
* @param \Webkul\Core\Repositories\LocaleRepository $locale
*/
public function __construct(LocaleRepository $locale)
{
$this->locale = $locale;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$locale = request()->get('admin_locale');
if ($locale) {
if ($this->locale->findOneByField('code', $locale)) {
app()->setLocale($locale);
session()->put('admin_locale', $locale);
}
} else {
if ($locale = session()->get('admin_locale')) {
app()->setLocale($locale);
} else {
app()->setLocale(app()->getLocale());
}
}
unset($request['admin_locale']);
return $next($request);
}
}

View File

@ -1,6 +1,6 @@
<?php
Route::group(['middleware' => ['web']], function () {
Route::group(['middleware' => ['web', 'admin_locale']], function () {
Route::prefix(config('app.admin_url'))->group(function () {
Route::get('/', 'Webkul\Admin\Http\Controllers\Controller@redirectToLogin');

View File

@ -2,8 +2,10 @@
namespace Webkul\Admin\Providers;
use Illuminate\Support\ServiceProvider;
use Webkul\Core\Tree;
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;
use Webkul\Admin\Http\Middleware\Locale;
class AdminServiceProvider extends ServiceProvider
{
@ -12,12 +14,13 @@ class AdminServiceProvider extends ServiceProvider
*
* @return void
*/
public function boot()
public function boot(Router $router)
{
$this->loadRoutesFrom(__DIR__ . '/../Http/routes.php');
$this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'admin');
$this->publishes([__DIR__.'/../Resources/lang' => resource_path('lang/vendor/admin')]);
$this->publishes([__DIR__ . '/../Resources/lang' => resource_path('lang/vendor/admin')]);
$this->publishes([
__DIR__ . '/../../publishable/assets' => public_path('vendor/webkul/admin/assets'),
@ -29,6 +32,8 @@ class AdminServiceProvider extends ServiceProvider
$this->registerACL();
$router->aliasMiddleware('admin_locale', Locale::class);
$this->app->register(EventServiceProvider::class);
}
@ -43,7 +48,7 @@ class AdminServiceProvider extends ServiceProvider
}
/**
* Bind the the data to the views
* Bind the data to the views.
*
* @return void
*/
@ -118,7 +123,7 @@ class AdminServiceProvider extends ServiceProvider
}
/**
* Create acl tree
* Create ACL tree.
*
* @return mixed
*/
@ -149,15 +154,18 @@ class AdminServiceProvider extends ServiceProvider
protected function registerConfig()
{
$this->mergeConfigFrom(
dirname(__DIR__) . '/Config/menu.php', 'menu.admin'
dirname(__DIR__) . '/Config/menu.php',
'menu.admin'
);
$this->mergeConfigFrom(
dirname(__DIR__) . '/Config/acl.php', 'acl'
dirname(__DIR__) . '/Config/acl.php',
'acl'
);
$this->mergeConfigFrom(
dirname(__DIR__) . '/Config/system.php', 'core'
dirname(__DIR__) . '/Config/system.php',
'core'
);
}
}

View File

@ -16,6 +16,42 @@
<span class="avatar">
</span>
<div class="profile-info">
@php
$allLocales = core()->getAllLocales()->pluck('name', 'code');
$currentLocaleCode = request()->get('admin_locale') ?: app()->getLocale();
@endphp
<div class="dropdown-toggle">
<div style="display: inline-block; vertical-align: middle;">
<span class="name">
{{ __('admin::app.datagrid.locale') }}
</span>
<span class="role">
{{ $allLocales[$currentLocaleCode] }}
</span>
</div>
<i class="icon arrow-down-icon active"></i>
</div>
<div class="dropdown-list bottom-right">
<div class="dropdown-container">
<ul>
@foreach ($allLocales as $code => $name)
<li>
<a href="{{ url()->current() . '?' . http_build_query(array_merge(request()->all(), ['admin_locale' => $code])) }}">
{{ $name }}
</a>
</li>
@endforeach
</ul>
</div>
</div>
</div>
<div class="profile-info">
<div class="dropdown-toggle">
<div style="display: inline-block; vertical-align: middle;">

View File

@ -1,6 +1,6 @@
<?php
Route::group(['middleware' => ['web']], function () {
Route::group(['middleware' => ['web', 'admin_locale']], function () {
Route::prefix(config('app.admin_url') . '/velocity')->group(function () {
Route::group(['middleware' => ['admin']], function () {
Route::namespace('Webkul\Velocity\Http\Controllers\Admin')->group(function () {