]> git.mxchange.org Git - friendica.git/commitdiff
Improved language detection
authorMichael <heluecht@pirati.ca>
Sun, 3 Sep 2023 08:44:17 +0000 (08:44 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 3 Sep 2023 08:44:17 +0000 (08:44 +0000)
src/Model/User.php
src/Module/Conversation/Channel.php
static/defaults.config.php

index af7a81a15dccc4a2ad22879d68beae7cb5eea192..774549d0a0d0a35f476e4f67b62a4972f1d19f3a 100644 (file)
@@ -183,7 +183,7 @@ class User
                $system['dob'] = '0000-00-00';
 
                // Ensure that the user contains data
-               $user = DBA::selectFirst('user', ['prvkey', 'guid'], ['uid' => 0]);
+               $user = DBA::selectFirst('user', ['prvkey', 'guid', 'language'], ['uid' => 0]);
                if (empty($user['prvkey']) || empty($user['guid'])) {
                        $fields = [
                                'username' => $system['name'],
@@ -203,7 +203,8 @@ class User
 
                        $system['guid'] = $fields['guid'];
                } else {
-                       $system['guid'] = $user['guid'];
+                       $system['guid']     = $user['guid'];
+                       $system['language'] = $user['language'];
                }
 
                return $system;
@@ -532,6 +533,28 @@ class User
                return $default_circle;
        }
 
+/**
+ * Fetch the language code from the given user. If the code is invalid, return the system language
+ *
+ * @param integer $uid   User-Id
+ * @param boolean $short If true, return the short form g.g. "en", otherwise the long form e.g. "en_UK"
+ * @return string
+ */
+       public static function getLanguageCode(int $uid, bool $short): string
+       {
+               $owner = self::getOwnerDataById($uid);
+               $languages = DI::l10n()->getAvailableLanguages();
+               if (in_array($owner['language'], array_keys($languages))) {
+                       $language = $owner['language'];
+               } else {
+                       $language = DI::config()->get('system', 'language');
+               }
+               if ($short) {
+                       return substr($language, 0, 2);
+               }
+               return $language;
+       }
+
        /**
         * Authenticate a user with a clear text password
         *
index 708764b455f99670148ac4f1b2bda04492c80f5b..c0948bdad7bd7382fd27c25a242161aa801a54f8 100644 (file)
@@ -180,27 +180,25 @@ class Channel extends BaseModule
                                'accesskey' => 'd'
                        ];
 
-                       $owner     = User::getOwnerDataById($this->session->getLocalUserId());
+                       $language  = User::getLanguageCode($this->session->getLocalUserId(), false);
                        $languages = $this->l10n->getAvailableLanguages();
-                       if (!empty($owner['language']) && !empty($languages[$owner['language']])) {
-                               $tabs[] = [
-                                       'label'     => $languages[$owner['language']],
-                                       'url'       => 'channel/' . self::LANGUAGE,
-                                       'sel'       => self::$content == self::LANGUAGE ? 'active' : '',
-                                       'title'     => $this->l10n->t('Posts in %s', $languages[$owner['language']]),
-                                       'id'        => 'channel-language-tab',
-                                       'accesskey' => 'g'
-                               ];
-
-                               $tabs[] = [
-                                       'label'     => $this->l10n->t('Whats Hot %s', $languages[$owner['language']]),
-                                       'url'       => 'channel/' . self::HOTLANG,
-                                       'sel'       => self::$content == self::HOTLANG ? 'active' : '',
-                                       'title'     => $this->l10n->t('Posts in %s with a lot of interactions', $languages[$owner['language']]),
-                                       'id'        => 'channel-hotlang-tab',
-                                       'accesskey' => 'o'
-                               ];
-                       }
+                       $tabs[] = [
+                               'label'     => $languages[$language],
+                               'url'       => 'channel/' . self::LANGUAGE,
+                               'sel'       => self::$content == self::LANGUAGE ? 'active' : '',
+                               'title'     => $this->l10n->t('Posts in %s', $languages[$language]),
+                               'id'        => 'channel-language-tab',
+                               'accesskey' => 'g'
+                       ];
+
+                       $tabs[] = [
+                               'label'     => $this->l10n->t('Whats Hot %s', $languages[$language]),
+                               'url'       => 'channel/' . self::HOTLANG,
+                               'sel'       => self::$content == self::HOTLANG ? 'active' : '',
+                               'title'     => $this->l10n->t('Posts in %s with a lot of interactions', $languages[$language]),
+                               'id'        => 'channel-hotlang-tab',
+                               'accesskey' => 'o'
+                       ];
 
                        $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
                        $o .= Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
@@ -354,14 +352,14 @@ class Channel extends BaseModule
                } elseif (self::$content == self::AUDIO) {
                        $condition = ["`media-type` & ?", 4];
                } elseif (self::$content == self::LANGUAGE) {
-                       $owner     = User::getOwnerDataById($this->session->getLocalUserId());
-                       $condition = ["JSON_EXTRACT(`language`, ?) > ?", '$.' . $owner['language'], $this->config->get('channel', 'language_threshold')];
+                       $condition = ["JSON_EXTRACT(JSON_KEYS(language), '$[0]') = ?", User::getLanguageCode($this->session->getLocalUserId(), true)];
                } elseif (self::$content == self::HOTLANG) {
-                       $owner = User::getOwnerDataById($this->session->getLocalUserId());
                        if (!is_null(self::$accountType)) {
-                               $condition = ["(`comments` >= ? OR `activities` >= ?) AND `contact-type` = ? AND JSON_EXTRACT(`language`, ?) > ?", $this->getMedianComments(4), $this->getMedianActivities(4), self::$accountType, '$.' . $owner['language'], $this->config->get('channel', 'language_threshold')];
+                               $condition = ["(`comments` >= ? OR `activities` >= ?) AND `contact-type` = ? AND JSON_EXTRACT(JSON_KEYS(language), '$[0]') = ?",
+                                       $this->getMedianComments(4), $this->getMedianActivities(4), self::$accountType, User::getLanguageCode($this->session->getLocalUserId(), true)];
                        } else {
-                               $condition = ["(`comments` >= ? OR `activities` >= ?) AND `contact-type` != ? AND JSON_EXTRACT(`language`, ?) > ?", $this->getMedianComments(4), $this->getMedianActivities(4), Contact::TYPE_COMMUNITY, '$.' . $owner['language'], $this->config->get('channel', 'language_threshold')];
+                               $condition = ["(`comments` >= ? OR `activities` >= ?) AND `contact-type` != ? AND JSON_EXTRACT(JSON_KEYS(language), '$[0]') = ?",
+                                       $this->getMedianComments(4), $this->getMedianActivities(4), Contact::TYPE_COMMUNITY, User::getLanguageCode($this->session->getLocalUserId(), true)];
                        }
                }
 
index d440f9c6a52976fcf4ebef25583d37de533612be..10324ed50486656effb0583d0a9c66e0d6049443 100644 (file)
@@ -804,9 +804,5 @@ return [
                // interaction_score_days (Integer)
                // Number of days that are used to calculate the interaction score.
                'interaction_score_days' => 30,
-
-               // language_threshold (Float)
-               // Treshold for the language detection.
-               'language_threshold' => 0.6,
        ],
 ];