mirror of
https://github.com/HDInnovations/UNIT3D-Community-Edition.git
synced 2026-05-08 12:10:05 -05:00
fix: multilingual support
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
/**
|
||||
* NOTICE OF LICENSE.
|
||||
*
|
||||
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
|
||||
* The details is bundled with this project in the file LICENSE.txt.
|
||||
*
|
||||
* @project UNIT3D Community Edition
|
||||
*
|
||||
* @author HDVinnie <hdinnovations@protonmail.com>
|
||||
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
|
||||
*/
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Models\Language;
|
||||
use Carbon\Carbon;
|
||||
use Closure;
|
||||
use Illuminate\Support\Facades\App;
|
||||
|
||||
class SetLanguage
|
||||
{
|
||||
/**
|
||||
* This function checks if language to set is an allowed lang of config.
|
||||
*
|
||||
* @param string $locale
|
||||
**/
|
||||
private function setLocale($locale)
|
||||
{
|
||||
// Check if is allowed and set default locale if not
|
||||
if (! Language::allowed($locale)) {
|
||||
$locale = config('app.locale');
|
||||
}
|
||||
|
||||
// Set app language
|
||||
App::setLocale($locale);
|
||||
|
||||
// Set carbon language
|
||||
if (config('language.carbon')) {
|
||||
// Carbon uses only language code
|
||||
if (config('language.mode.code') == 'long') {
|
||||
$locale = explode('-', $locale)[0];
|
||||
}
|
||||
|
||||
Carbon::setLocale($locale);
|
||||
}
|
||||
|
||||
// Set date language
|
||||
if (config('language.date')) {
|
||||
// Date uses only language code
|
||||
if (config('language.mode.code') == 'long') {
|
||||
$locale = explode('-', $locale)[0];
|
||||
}
|
||||
|
||||
\Date::setLocale($locale);
|
||||
}
|
||||
}
|
||||
|
||||
public function setDefaultLocale()
|
||||
{
|
||||
$this->setLocale(config('app.locale'));
|
||||
}
|
||||
|
||||
public function setUserLocale()
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
if ($user->locale) {
|
||||
$this->setLocale($user->locale);
|
||||
} else {
|
||||
$this->setDefaultLocale();
|
||||
}
|
||||
}
|
||||
|
||||
public function setSystemLocale($request)
|
||||
{
|
||||
if ($request->session()->has('locale')) {
|
||||
$this->setLocale(session('locale'));
|
||||
} else {
|
||||
$this->setDefaultLocale();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
if ($request->has('lang')) {
|
||||
$this->setLocale($request->get('lang'));
|
||||
} elseif (auth()->check()) {
|
||||
$this->setUserLocale();
|
||||
} else {
|
||||
$this->setSystemLocale($request);
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@
|
||||
|
|
||||
*/
|
||||
|
||||
Route::group(['middleware' => 'language'], function () {
|
||||
/*
|
||||
|---------------------------------------------------------------------------------
|
||||
| Website (Not Authorized) (Alpha Ordered)
|
||||
@@ -875,3 +876,4 @@
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user