blessing-skin-server/app/Http/View/Composers/LanguagesMenuComposer.php

43 lines
1.1 KiB
PHP
Raw Normal View History

2019-09-17 23:10:44 +08:00
<?php
namespace App\Http\View\Composers;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
2019-12-14 11:10:37 +08:00
use Illuminate\View\View;
2019-09-17 23:10:44 +08:00
class LanguagesMenuComposer
{
/** @var Request */
protected $request;
public function __construct(Request $request)
{
$this->request = $request;
}
public function compose(View $view)
{
$query = $this->request->query();
2020-03-27 22:34:14 +08:00
$path = $this->request->path();
2019-09-17 23:10:44 +08:00
$langs = collect(config('locales'))
->reject(function ($locale) {
return Arr::has($locale, 'alias');
})
2020-03-27 22:34:14 +08:00
->map(function ($locale, $id) use ($query, $path) {
2019-09-17 23:10:44 +08:00
$query = array_merge($query, ['lang' => $id]);
2020-03-27 22:34:14 +08:00
$locale['url'] = url($path.'?'.http_build_query($query));
2019-09-17 23:10:44 +08:00
return $locale;
});
2020-02-01 12:44:39 +08:00
$current = 'locales.'.app()->getLocale();
2019-09-17 23:10:44 +08:00
$view->with([
2020-02-01 12:44:39 +08:00
'lang_short_name' => config($current.'.short_name'),
'lang_name' => config($current.'.name'),
2019-09-17 23:10:44 +08:00
'langs' => $langs,
]);
}
}