语言切换

This commit is contained in:
TL 2022-07-28 16:37:27 +08:00
parent 28d92d8b40
commit 5b5630cd95
4 changed files with 14 additions and 5 deletions

View File

@ -18,7 +18,7 @@ class Language
*/
public function handle(Request $request, Closure $next)
{
if (Session()->has('locale') AND array_key_exists(Session()->get('locale'), LanguageRepo::all()->where('status', true)->pluck('code'))) {
if (Session()->has('locale') AND array_key_exists(Session()->get('locale'), languages())) {
App::setLocale(Session()->get('locale'));
}
else { // This is optional as Laravel will automatically set the fallback language if there is none specified

View File

@ -2,6 +2,7 @@
use Beike\Models\Customer;
use Beike\Models\AdminUser;
use Beike\Repositories\LanguageRepo;
use Illuminate\Support\Str;
use TorMorten\Eventy\Facades\Eventy;
use Illuminate\Support\Facades\Route;
@ -231,12 +232,11 @@ function image_origin($image)
/**
* 获取当前所有语言列表
*
* @return array|false
* @return \Illuminate\Support\Collection
*/
function languages()
{
$languageDir = resource_path('lang');
return array_diff(scandir($languageDir), array('..', '.'));
return LanguageRepo::enabled()->pluck('code');
}
/**

View File

@ -74,4 +74,13 @@ class LanguageRepo
{
return Language::query()->get();
}
/**
* 获取所有启用的语言
* @return Builder[]|Collection
*/
public static function enabled()
{
return Language::query()->where('status', true)->get();
}
}

View File

@ -17,7 +17,7 @@ class LanguageController extends Controller
{
public function index($lang)
{
if (array_key_exists($lang, LanguageRepo::all()->where('status', true)->pluck('code'))) {
if (array_key_exists($lang, languages())) {
Session::put('locale', $lang);
}
return Redirect::back();