From 130b4cda045d0bea721a753b3916dc09e538bbf4 Mon Sep 17 00:00:00 2001 From: 1Day Date: Thu, 16 Dec 2021 14:30:28 +0100 Subject: [PATCH] Updated: Enable/Disable dynamic localization. Dropdown to change language for users --- .env.example | 6 ++++-- app/Http/Middleware/SetLocale.php | 16 +++++++++------- config/app.php | 24 +++++++++++++++++++++++- resources/views/layouts/main.blade.php | 8 +++----- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/.env.example b/.env.example index 20aa7f1e..be3f7021 100644 --- a/.env.example +++ b/.env.example @@ -5,8 +5,10 @@ APP_DEBUG=false APP_URL=http://localhost #list with timezones https://www.php.net/manual/en/timezones.php APP_TIMEZONE=UTC -# Language is chosen automatically depending on the users browserlanguage. -# It will default to this value, if the translation is not available. Best would be to keep this "en" + +# 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 diff --git a/app/Http/Middleware/SetLocale.php b/app/Http/Middleware/SetLocale.php index 712e3858..71926b06 100644 --- a/app/Http/Middleware/SetLocale.php +++ b/app/Http/Middleware/SetLocale.php @@ -19,15 +19,17 @@ class SetLocale */ public function handle($request, Closure $next) { - if (Session::has('locale')) { - $locale = Session::get('locale', config('app.locale')); - } else { - $locale = substr($request->server('HTTP_ACCEPT_LANGUAGE'), 0, 2); + if (Session::has('locale')) { + $locale = Session::get('locale', config('app.locale')); + } else { + if (config('app.dynamic_locale')) { + $locale = substr($request->server('HTTP_ACCEPT_LANGUAGE'), 0, 2); - if (!in_array($locale, config('app.available_locales'))) { - $locale = config('app.locale'); + if (!in_array($locale, config('app.available_locales'))) { + $locale = config('app.locale'); + } + } } - } App::setLocale($locale); diff --git a/config/app.php b/config/app.php index 749e4f92..b5310c03 100644 --- a/config/app.php +++ b/config/app.php @@ -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 @@ -82,7 +93,18 @@ return [ */ 'locale' => env('LOCALE', 'en'), - 'available_locales' => array('en', 'de', 'it', 'zh'), + + + /* + |-------------------------------------------------------------------------- + | 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'), /* diff --git a/resources/views/layouts/main.blade.php b/resources/views/layouts/main.blade.php index 5266ff9d..c7207cf6 100644 --- a/resources/views/layouts/main.blade.php +++ b/resources/views/layouts/main.blade.php @@ -50,11 +50,9 @@
@csrf