Merge pull request #333 from 1day2die/dynamic_language_selection

Dynamic language depending on users browserlanguage - Ability to change Language manually
This commit is contained in:
Dennis 2021-12-27 13:59:41 +01:00 committed by GitHub
commit 70b261f2ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 370 additions and 31 deletions

View file

@ -5,9 +5,20 @@ APP_DEBUG=false
APP_URL=http://localhost
#list with timezones https://www.php.net/manual/en/timezones.php
APP_TIMEZONE=UTC
# If set to true, Language is chosen automatically depending on the users browserlanguage.
DYNAMIC_LOCALE = false
# The language of the Dashboard. This is also the fallback if dynamic_locale is true but no translation is found
LOCALE=en
# You can grab the Language-Codes for the Datatables from this Website https://datatables.net/plug-ins/i18n/
DATATABLE_LOCALE=en-gb
#The languages you DO NOT want to support on your Controlpanel split by comma.
#Remove the language to make it available
UNSUPPORTED_LOCALES=german,italian,chinese
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306

View file

@ -0,0 +1,21 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
class TranslationController extends Controller
{
/**
*
* Change session locale
* @param Request $request
* @return Response
*/
public function changeLocale(Request $request)
{
Session::put('locale', $request->inputLocale);
return redirect()->back();
}
}

View file

@ -4,7 +4,7 @@ namespace App\Http;
use App\Http\Middleware\ApiAuthToken;
use App\Http\Middleware\CheckSuspended;
use App\Http\Middleware\CreditsDisplayName;
use App\Http\Middleware\GlobalNames;
use App\Http\Middleware\isAdmin;
use App\Http\Middleware\LastSeen;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
@ -43,13 +43,14 @@ class Kernel extends HttpKernel
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
LastSeen::class,
CreditsDisplayName::class,
GlobalNames::class,
\App\Http\Middleware\SetLocale::class,
],
'api' => [
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
CreditsDisplayName::class
GlobalNames::class
],
];

View file

@ -6,7 +6,7 @@ use App\Models\Configuration;
use Closure;
use Illuminate\Http\Request;
class CreditsDisplayName
class GlobalNames
{
/**
* Handle an incoming request.
@ -18,6 +18,11 @@ class CreditsDisplayName
public function handle(Request $request, Closure $next)
{
define('CREDITS_DISPLAY_NAME' , Configuration::getValueByKey('CREDITS_DISPLAY_NAME' , 'Credits'));
$unsupported_lang_array = explode(',', config("app.unsupported_locales"));
$unsupported_lang_array = array_map( 'strtolower', $unsupported_lang_array );
define('UNSUPPORTED_LANGS', $unsupported_lang_array);
return $next($request);
}
}

View file

@ -0,0 +1,231 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Session;
class SetLocale
{
function getLocaleCodeForDisplayLanguage($name){
$languageCodes = array(
"aa" => "Afar",
"ab" => "Abkhazian",
"ae" => "Avestan",
"af" => "Afrikaans",
"ak" => "Akan",
"am" => "Amharic",
"an" => "Aragonese",
"ar" => "Arabic",
"as" => "Assamese",
"av" => "Avaric",
"ay" => "Aymara",
"az" => "Azerbaijani",
"ba" => "Bashkir",
"be" => "Belarusian",
"bg" => "Bulgarian",
"bh" => "Bihari",
"bi" => "Bislama",
"bm" => "Bambara",
"bn" => "Bengali",
"bo" => "Tibetan",
"br" => "Breton",
"bs" => "Bosnian",
"ca" => "Catalan",
"ce" => "Chechen",
"ch" => "Chamorro",
"co" => "Corsican",
"cr" => "Cree",
"cs" => "Czech",
"cu" => "Church Slavic",
"cv" => "Chuvash",
"cy" => "Welsh",
"da" => "Danish",
"de" => "German",
"dv" => "Divehi",
"dz" => "Dzongkha",
"ee" => "Ewe",
"el" => "Greek",
"en" => "English",
"eo" => "Esperanto",
"es" => "Spanish",
"et" => "Estonian",
"eu" => "Basque",
"fa" => "Persian",
"ff" => "Fulah",
"fi" => "Finnish",
"fj" => "Fijian",
"fo" => "Faroese",
"fr" => "French",
"fy" => "Western Frisian",
"ga" => "Irish",
"gd" => "Scottish Gaelic",
"gl" => "Galician",
"gn" => "Guarani",
"gu" => "Gujarati",
"gv" => "Manx",
"ha" => "Hausa",
"he" => "Hebrew",
"hi" => "Hindi",
"ho" => "Hiri Motu",
"hr" => "Croatian",
"ht" => "Haitian",
"hu" => "Hungarian",
"hy" => "Armenian",
"hz" => "Herero",
"ia" => "Interlingua (International Auxiliary Language Association)",
"id" => "Indonesian",
"ie" => "Interlingue",
"ig" => "Igbo",
"ii" => "Sichuan Yi",
"ik" => "Inupiaq",
"io" => "Ido",
"is" => "Icelandic",
"it" => "Italian",
"iu" => "Inuktitut",
"ja" => "Japanese",
"jv" => "Javanese",
"ka" => "Georgian",
"kg" => "Kongo",
"ki" => "Kikuyu",
"kj" => "Kwanyama",
"kk" => "Kazakh",
"kl" => "Kalaallisut",
"km" => "Khmer",
"kn" => "Kannada",
"ko" => "Korean",
"kr" => "Kanuri",
"ks" => "Kashmiri",
"ku" => "Kurdish",
"kv" => "Komi",
"kw" => "Cornish",
"ky" => "Kirghiz",
"la" => "Latin",
"lb" => "Luxembourgish",
"lg" => "Ganda",
"li" => "Limburgish",
"ln" => "Lingala",
"lo" => "Lao",
"lt" => "Lithuanian",
"lu" => "Luba-Katanga",
"lv" => "Latvian",
"mg" => "Malagasy",
"mh" => "Marshallese",
"mi" => "Maori",
"mk" => "Macedonian",
"ml" => "Malayalam",
"mn" => "Mongolian",
"mr" => "Marathi",
"ms" => "Malay",
"mt" => "Maltese",
"my" => "Burmese",
"na" => "Nauru",
"nb" => "Norwegian Bokmal",
"nd" => "North Ndebele",
"ne" => "Nepali",
"ng" => "Ndonga",
"nl" => "Dutch",
"nn" => "Norwegian Nynorsk",
"no" => "Norwegian",
"nr" => "South Ndebele",
"nv" => "Navajo",
"ny" => "Chichewa",
"oc" => "Occitan",
"oj" => "Ojibwa",
"om" => "Oromo",
"or" => "Oriya",
"os" => "Ossetian",
"pa" => "Panjabi",
"pi" => "Pali",
"pl" => "Polish",
"ps" => "Pashto",
"pt" => "Portuguese",
"qu" => "Quechua",
"rm" => "Raeto-Romance",
"rn" => "Kirundi",
"ro" => "Romanian",
"ru" => "Russian",
"rw" => "Kinyarwanda",
"sa" => "Sanskrit",
"sc" => "Sardinian",
"sd" => "Sindhi",
"se" => "Northern Sami",
"sg" => "Sango",
"si" => "Sinhala",
"sk" => "Slovak",
"sl" => "Slovenian",
"sm" => "Samoan",
"so" => "Somali",
"sq" => "Albanian",
"sr" => "Serbian",
"ss" => "Swati",
"st" => "Southern Sotho",
"su" => "Sundanese",
"sv" => "Swedish",
"sw" => "Swahili",
"ta" => "Tamil",
"te" => "Telugu",
"tg" => "Tajik",
"th" => "Thai",
"ti" => "Tigrinya",
"tk" => "Turkmen",
"tl" => "Tagalog",
"tn" => "Tswana",
"to" => "Tonga",
"tr" => "Turkish",
"ts" => "Tsonga",
"tt" => "Tatar",
"tw" => "Twi",
"ty" => "Tahitian",
"ug" => "Uighur",
"uk" => "Ukrainian",
"ur" => "Urdu",
"uz" => "Uzbek",
"ve" => "Venda",
"vi" => "Vietnamese",
"vo" => "Volapuk",
"wa" => "Walloon",
"wo" => "Wolof",
"xh" => "Xhosa",
"yi" => "Yiddish",
"yo" => "Yoruba",
"za" => "Zhuang",
"zh" => "Chinese",
"zu" => "Zulu"
);
return array_search($name, array_flip($languageCodes));
}
/**
*
* Handle an incoming request.
*
* @param Request $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (Session::has('locale')) {
$locale = Session::get('locale', config('app.locale'));
} else {
if (!config('app.dynamic_locale')) {
$locale = config('app.locale');
}else{
$locale = substr($request->server('HTTP_ACCEPT_LANGUAGE'), 0, 2);
if (!in_array($locale, config('app.available_locales'))
|| in_array(strtolower($this->getLocaleCodeForDisplayLanguage($locale)), UNSUPPORTED_LANGS)) {
$locale = config('app.locale');
}
}
}
App::setLocale($locale);
return $next($request);
}
}

View file

@ -70,6 +70,17 @@ return [
'timezone' => env('APP_TIMEZONE', 'UTC'),
/*
|--------------------------------------------------------------------------
| Dyamic Locales
|--------------------------------------------------------------------------
|
| Change the Locale depending on the Users Browserlanguage
| Can either be true or false
|
*/
'dynamic_locale' => env('DYNAMIC_LOCALE', false),
/*
|--------------------------------------------------------------------------
| Application Locale Configuration
@ -84,6 +95,31 @@ return [
'locale' => env('LOCALE', 'en'),
/*
|--------------------------------------------------------------------------
| Available Locales
|--------------------------------------------------------------------------
|
| You should not change this
| If the dashboard is 100% translated in a certain language, it will be added here
|
*/
'available_locales' => array('English'=>'en','German'=>'de','Italian'=>'it','Chinese'=>'zh'),
/*
|--------------------------------------------------------------------------
| Unsupported Locales
|--------------------------------------------------------------------------
|
| Locales the Owner of the Dashboard does not want to support
|
|
*/
'unsupported_locales' => env("UNSUPPORTED_LOCALES", ""),
/*
|--------------------------------------------------------------------------
| Datatable Language Setting

View file

@ -41,11 +41,37 @@
<a class="nav-link" data-widget="pushmenu" href="#" role="button"><i class="fas fa-bars"></i></a>
</li>
<li class="nav-item d-none d-sm-inline-block">
<a href="{{route('home')}}" class="nav-link">{{__('Home')}}</a>
<a href="{{route('home')}}" class="nav-link"><i class="fas fa-home mr-2"></i>{{__('Home')}}</a>
</li>
<li class="nav-item d-none d-sm-inline-block">
<a href="{{env('DISCORD_INVITE_URL')}}" class="nav-link" target="__blank">{{__('Discord')}}</a>
<a href="{{env('DISCORD_INVITE_URL')}}" class="nav-link" target="__blank"><i
class="fab fa-discord mr-2"></i>{{__('Discord')}}</a>
</li>
<!-- Language Selection -->
<li class="nav-item dropdown">
<a class="nav-link" href="#" id="languageDropdown" role="button" data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false">
<span class="mr-1 d-lg-inline text-gray-600">
<small><i class="fa fa-language mr-2"></i></small>{{__("Languages")}}
</span>
</a>
<div class="dropdown-menu dropdown-menu-right shadow animated--grow-in" aria-labelledby="changeLocale">
<form method="post" action="{{route('changeLocale')}}" class="nav-item text-center">
@csrf
@foreach (config("app.available_locales") as $key=>$value)
@if(!in_array(strtolower($key),UNSUPPORTED_LANGS))
<button class="dropdown-item" name="inputLocale" value="{{$value}}">
{{$key}}
</button>
@endif
@endforeach
</form>
</div>
</li>
<!-- End Language Selection -->
</ul>
<!-- Right navbar links -->
@ -60,7 +86,8 @@
@endif
</a>
<div class="dropdown-menu dropdown-menu-lg dropdown-menu-right">
<span class="dropdown-item dropdown-header">{{Auth::user()->unreadNotifications->count()}} {{__('Notifications')}}</span>
<span
class="dropdown-item dropdown-header">{{Auth::user()->unreadNotifications->count()}} {{__('Notifications')}}</span>
<div class="dropdown-divider"></div>
@foreach(Auth::user()->unreadNotifications->sortBy('created_at')->take(5) as $notification)
@ -73,7 +100,8 @@
@endforeach
<div class="dropdown-divider"></div>
<a href="{{route('notifications.index')}}" class="dropdown-item dropdown-footer">{{__('See all Notifications')}}</a>
<a href="{{route('notifications.index')}}"
class="dropdown-item dropdown-footer">{{__('See all Notifications')}}</a>
</div>
</li>
@ -263,23 +291,23 @@
</a>
</li>
{{-- <li class="nav-header">Pterodactyl</li>--}}
{{-- <li class="nav-header">Pterodactyl</li>--}}
{{-- <li class="nav-item">--}}
{{-- <a href="{{route('admin.nodes.index')}}"--}}
{{-- class="nav-link @if(Request::routeIs('admin.nodes.*')) active @endif">--}}
{{-- <i class="nav-icon fas fa-sitemap"></i>--}}
{{-- <p>Nodes</p>--}}
{{-- </a>--}}
{{-- </li>--}}
{{-- <li class="nav-item">--}}
{{-- <a href="{{route('admin.nodes.index')}}"--}}
{{-- class="nav-link @if(Request::routeIs('admin.nodes.*')) active @endif">--}}
{{-- <i class="nav-icon fas fa-sitemap"></i>--}}
{{-- <p>Nodes</p>--}}
{{-- </a>--}}
{{-- </li>--}}
{{-- <li class="nav-item">--}}
{{-- <a href="{{route('admin.nests.index')}}"--}}
{{-- class="nav-link @if(Request::routeIs('admin.nests.*')) active @endif">--}}
{{-- <i class="nav-icon fas fa-th-large"></i>--}}
{{-- <p>Nests</p>--}}
{{-- </a>--}}
{{-- </li>--}}
{{-- <li class="nav-item">--}}
{{-- <a href="{{route('admin.nests.index')}}"--}}
{{-- class="nav-link @if(Request::routeIs('admin.nests.*')) active @endif">--}}
{{-- <i class="nav-icon fas fa-th-large"></i>--}}
{{-- <p>Nests</p>--}}
{{-- </a>--}}
{{-- </li>--}}
<li class="nav-header">{{__('Other')}}</li>
@ -329,7 +357,9 @@
@if(Auth::user()->created_at->diffInHours(now(), false) > 1)
<div class="alert alert-warning p-2 m-2">
<h5><i class="icon fas fa-exclamation-circle"></i> {{__('Warning!')}}</h5>
{{__('You have not yet verified your email address')}} <a class="text-primary" href="{{route('verification.send')}}">{{__('Click here to resend verification email')}}</a> <br>
{{__('You have not yet verified your email address')}} <a class="text-primary"
href="{{route('verification.send')}}">{{__('Click here to resend verification email')}}</a>
<br>
{{__('Please contact support If you didnt receive your verification email.')}}
</div>
@endif
@ -343,7 +373,8 @@
<footer class="main-footer">
<strong>Copyright &copy; 2021-{{date('Y')}} <a href="{{url('/')}}">{{env('APP_NAME' , 'Laravel')}}</a>.</strong>
All rights
reserved. Powered by <a href="https://controlpanel.gg">ControlPanel</a>. Version <b>{{config('app')['version']}}</b>
reserved. Powered by <a href="https://controlpanel.gg">ControlPanel</a>. Version
<b>{{config('app')['version']}}</b>
</footer>
<!-- Control Sidebar -->
@ -376,15 +407,15 @@
<!-- Select2 -->
<script src={{asset('plugins/select2/js/select2.min.js')}}>
<script>
$(document).ready(function () {
<script>
$(document).ready(function () {
$('[data-toggle="popover"]').popover();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
});
</script>
<script>

View file

@ -19,6 +19,7 @@ use App\Http\Controllers\ProductController as FrontProductController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\ServerController;
use App\Http\Controllers\StoreController;
use App\Http\Controllers\TranslationController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
@ -82,6 +83,8 @@ Route::middleware(['auth', 'checkSuspended'])->group(function () {
#voucher redeem
Route::post('/voucher/redeem', [VoucherController::class, 'redeem'])->middleware('throttle:5,1')->name('voucher.redeem');
#switch language
Route::post('changelocale', [TranslationController::class, 'changeLocale'])->name('changeLocale');
#admin
Route::prefix('admin')->name('admin.')->middleware('admin')->group(function () {