语言切换
This commit is contained in:
parent
28d92d8b40
commit
5b5630cd95
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -74,4 +74,13 @@ class LanguageRepo
|
|||
{
|
||||
return Language::query()->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有启用的语言
|
||||
* @return Builder[]|Collection
|
||||
*/
|
||||
public static function enabled()
|
||||
{
|
||||
return Language::query()->where('status', true)->get();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Reference in New Issue