]> git.mxchange.org Git - friendica.git/commitdiff
New functions for the language library
authorMichael <heluecht@pirati.ca>
Sun, 3 Sep 2023 17:44:44 +0000 (17:44 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 3 Sep 2023 17:44:44 +0000 (17:44 +0000)
src/Core/L10n.php
src/Model/User.php
src/Module/Conversation/Channel.php
src/Module/Settings/Display.php

index 362d6d7a4a095962f4a84896a6450acffe18d342..9d1fa70167a7a60fe352281b7f6bb26525f0615a 100644 (file)
@@ -397,8 +397,8 @@ class L10n
                                // See https://github.com/friendica/friendica/issues/10511
                                // Persian is manually added to language detection until a persian translation is provided for the interface, at
                                // which point it will be automatically available through `getAvailableLanguages()` and this should be removed.
-                               // Additionally Portuguese, Ukrainian and Welsh are added to that list.
-                               $langs = array_merge(['cy' => 'Cymraeg', 'uk' => 'Українська', 'pt-PT' => 'Português', 'fa' => 'فارسی'], $langs);
+                               // Additionally Portuguese, Ukrainian, traditional Chinese and Welsh are added to that list.
+                               $langs = array_merge(['cy' => 'Cymraeg', 'uk' => 'Українська', 'pt-PT' => 'Português', 'zh-hant' => '繁體', 'fa' => 'فارسی'], $langs);
                                ksort($langs);
                        }
                }
@@ -407,7 +407,7 @@ class L10n
 
        /**
         * The language detection routine uses some slightly different language codes.
-        * This function changes the language language codes accordingly.
+        * This function changes the language array accordingly.
         *
         * @param array $languages
         * @return array
@@ -423,7 +423,6 @@ class L10n
                $languages['pt-BR'] = $languages['pt-br'];
                unset($languages['pt-br']);
                $languages['zh-Hans'] = $languages['zh-cn'];
-               $languages['zh-Hant'] = $languages['zh-cn'];
                unset($languages['zh-cn']);
 
                ksort($languages);
@@ -431,6 +430,34 @@ class L10n
                return $languages;
        }
 
+       /**
+        * The language detection routine uses some slightly different language codes.
+        * This function changes the language codes accordingly.
+        *
+        * @param string $language
+        * @return string
+        */
+       public function convertCodeForLanguageDetection(string $language): string
+       {
+               switch ($language) {
+                       case 'da-dk':
+                               return 'da';
+                       case 'en-us':
+                       case 'en-gb':
+                               return 'en';
+                       case 'fi-fi':
+                               return 'fi';
+                       case 'nb-no':
+                               return 'nb';
+                       case 'pt-br':
+                               return 'pt-BR';
+                       case 'zh-cn':
+                               return 'zh-Hans';
+                       default:
+                               return $language;
+               }
+       }
+
        /**
         * Translate days and months names.
         *
index 43e2c067fce2a4cd2669ab9062c203a1b349f03f..b4df99d82c38ae5d7984c88db4019a658a2ff492 100644 (file)
@@ -536,14 +536,13 @@ class User
 /**
  * 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-gb"
+ * @param integer $uid User-Id
  * @return string
  */
-       public static function getLanguageCode(int $uid, bool $short): string
+       public static function getLanguageCode(int $uid): string
        {
                $owner = self::getOwnerDataById($uid);
-               $languages = DI::l10n()->getAvailableLanguages($short);
+               $languages = DI::l10n()->getAvailableLanguages(true);
                if (in_array($owner['language'], array_keys($languages))) {
                        $language = $owner['language'];
                } else {
index dfe8871f162b4e0a707e2ca3e7bbb54739ad873f..eacbb2201e139ef1c3794c00fedd57c751d093e5 100644 (file)
@@ -144,8 +144,8 @@ class Channel extends BaseModule
                                'accesskey' => 'h'
                        ];
 
-                       $language  = User::getLanguageCode($this->session->getLocalUserId(), false);
-                       $languages = $this->l10n->getAvailableLanguages();
+                       $language  = User::getLanguageCode($this->session->getLocalUserId());
+                       $languages = $this->l10n->getAvailableLanguages(true);
 
                        $tabs[] = [
                                'label'     => $languages[$language],
@@ -344,7 +344,7 @@ class Channel extends BaseModule
                } elseif (self::$content == self::AUDIO) {
                        $condition = ["`media-type` & ?", 4];
                } elseif (self::$content == self::LANGUAGE) {
-                       $condition = ["JSON_EXTRACT(JSON_KEYS(language), '$[0]') = ?", User::getLanguageCode($this->session->getLocalUserId(), true)];
+                       $condition = ["JSON_EXTRACT(JSON_KEYS(language), '$[0]') = ?", $this->l10n->convertCodeForLanguageDetection(User::getLanguageCode($this->session->getLocalUserId()))];
                }
 
                if (self::$content != self::LANGUAGE) {
@@ -404,10 +404,11 @@ class Channel extends BaseModule
        private function addLanguageCondition(array $condition): array
        {
                $conditions = [];
-               $languages  = $this->pConfig->get($this->session->getLocalUserId(), 'channel', 'languages', [User::getLanguageCode($this->session->getLocalUserId(), false)]);
+               $languages  = $this->pConfig->get($this->session->getLocalUserId(), 'channel', 'languages', [User::getLanguageCode($this->session->getLocalUserId())]);
+               $languages  = $this->l10n->convertForLanguageDetection($languages);
                foreach ($languages as $language) {
                        $conditions[] = "JSON_EXTRACT(JSON_KEYS(language), '$[0]') = ?";
-                       $condition[]  = substr($language, 0, 2);
+                       $condition[]  = $language;
                }
                if (!empty($conditions)) {
                        $condition[0] .= " AND (`language` IS NULL OR " . implode(' OR ', $conditions) . ")";
index 95dd4e78ffab776ea5f3fec5388ab3ad03f26df9..fa1496da6dd988cc64d736c624a4640172133825 100644 (file)
@@ -218,7 +218,7 @@ class Display extends BaseSettings
                        BBCode::PREVIEW_LARGE    => $this->t('Large Image'),
                ];
 
-               $channel_languages = $this->pConfig->get($uid, 'channel', 'languages', [User::getLanguageCode($uid, false)]);
+               $channel_languages = $this->pConfig->get($uid, 'channel', 'languages', [User::getLanguageCode($uid)]);
                $languages         = $this->l10n->getAvailableLanguages(true);
 
                $first_day_of_week = $this->pConfig->get($uid, 'calendar', 'first_day_of_week', 0);