Browse Source

Automatic language detection for guest

Visman 7 years ago
parent
commit
2b80012658
4 changed files with 30 additions and 5 deletions
  1. 3 2
      app/Core/Func.php
  2. 2 2
      app/Models/Pages/Install.php
  3. 24 0
      app/Models/User/Current.php
  4. 1 1
      app/Models/User/Model.php

+ 3 - 2
app/Core/Func.php

@@ -173,7 +173,7 @@ class Func
      *
      * @return array
      */
-    public function  langParse($str)
+    public function langParse($str)
     {
         $result = [];
 
@@ -196,7 +196,8 @@ class Func
 
             $result[$l] = $q;
         }
+        \arsort($result, \SORT_NUMERIC);
 
-        return \array_keys(\arsort($result, \SORT_NUMERIC));
+        return \array_keys($result);
     }
 }

+ 2 - 2
app/Models/Pages/Install.php

@@ -896,8 +896,8 @@ class Install extends Page
                 'dst'              => ['TINYINT(1)', false, 0],
                 'time_format'      => ['TINYINT(1)', false, 0],
                 'date_format'      => ['TINYINT(1)', false, 0],
-                'language'         => ['VARCHAR(25)', false, $v->defaultlang],
-                'style'            => ['VARCHAR(25)', false, $v->defaultstyle],
+                'language'         => ['VARCHAR(25)', false, ''],
+                'style'            => ['VARCHAR(25)', false, ''],
                 'num_posts'        => ['INT(10) UNSIGNED', false, 0],
                 'last_post'        => ['INT(10) UNSIGNED', true],
                 'last_search'      => ['INT(10) UNSIGNED', true],

+ 24 - 0
app/Models/User/Current.php

@@ -39,6 +39,7 @@ class Current extends Action
             $user->__dst = $this->c->config->o_default_dst;
 #            $user->language = $this->c->config->o_default_lang;
 #            $user->style = $this->c->config->o_default_style;
+            $user->__language = $this->getLangFromHTTP();
 
             // быстрое переключение языка - Visman
 /*            $language = $this->cookie->get('glang');
@@ -247,4 +248,27 @@ class Current extends Action
         }
         return $agent;
     }
+
+    /**
+     * Возвращает имеющийся в наличии язык из HTTP_ACCEPT_LANGUAGE
+     * или язык по умолчанию
+     *
+     * @return string
+     */
+    protected function getLangFromHTTP()
+    {
+        if (! empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
+            $langs = $this->c->Func->getLangs();
+            foreach ($this->c->Func->langParse($_SERVER['HTTP_ACCEPT_LANGUAGE']) as $entry) {
+                $arr = \explode('-', $entry);
+                if (isset($arr[1])) {
+                    $entry = $arr[0] . '_' . \strtoupper($arr[1]);
+                }
+                if (isset($langs[$entry])) {
+                    return $langs[$entry];
+                }
+            }
+        }
+        return $this->c->config->o_default_lang;
+    }
 }

+ 1 - 1
app/Models/User/Model.php

@@ -111,7 +111,7 @@ class Model extends DataModel
     {
         $langs = $this->c->Func->getLangs();
 
-        $lang = $this->isGuest || empty($this->a['language']) || ! isset($langs[$this->a['language']])
+        $lang = empty($this->a['language']) || ! isset($langs[$this->a['language']])
             ? $this->c->config->o_default_lang
             : $this->a['language'];