فهرست منبع

Updated: Enable/Disable dynamic localization. Dropdown to change language for users

1Day 3 سال پیش
والد
کامیت
130b4cda04
4فایلهای تغییر یافته به همراه39 افزوده شده و 15 حذف شده
  1. 4 2
      .env.example
  2. 9 7
      app/Http/Middleware/SetLocale.php
  3. 23 1
      config/app.php
  4. 3 5
      resources/views/layouts/main.blade.php

+ 4 - 2
.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

+ 9 - 7
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);
 

+ 23 - 1
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'),
 
 
     /*

+ 3 - 5
resources/views/layouts/main.blade.php

@@ -50,11 +50,9 @@
             <form method="post" action="{{route('changeLocale')}}">
                 @csrf
                 <select class="dropdown-item" id="inputLocale" name="inputLocale" onchange="this.form.submit()">
-                        <option value="de">DE</option>
-                        <option value="it">IT</option>
-                        <option value="en">EN</option>
-                        <option value="zh">ZH</option>
-                        <option value="fr">FR</option>
+                    @foreach (config("app.available_locales") as $key=>$value)
+                        <option @if(Session::get("locale") == $value) selected="selected" @endif value="{{$value}}">{{$key}}</option>
+                    @endforeach
                 </select>
             </form>
             <!-- End Language Selection -->