]> git.mxchange.org Git - friendica.git/commitdiff
New function to change language codes for the library
authorMichael <heluecht@pirati.ca>
Sun, 3 Sep 2023 16:42:17 +0000 (16:42 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 3 Sep 2023 16:42:17 +0000 (16:42 +0000)
src/Core/L10n.php
src/Model/Item.php
src/Model/User.php
src/Module/Settings/Display.php

index 1d8cfa705bae359df110a42d826b28e5e11712a4..362d6d7a4a095962f4a84896a6450acffe18d342 100644 (file)
@@ -378,7 +378,7 @@ class L10n
         *
         * @return array
         */
-       public static function getAvailableLanguages(): array
+       public function getAvailableLanguages(bool $additional = false): array
        {
                $langs              = [];
                $strings_file_paths = glob('view/lang/*/strings.php');
@@ -392,10 +392,45 @@ class L10n
                                $path_array            = explode('/', $strings_file_path);
                                $langs[$path_array[2]] = self::LANG_NAMES[$path_array[2]] ?? $path_array[2];
                        }
+
+                       if ($additional) {
+                               // 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);
+                               ksort($langs);
+                       }
                }
                return $langs;
        }
 
+       /**
+        * The language detection routine uses some slightly different language codes.
+        * This function changes the language language codes accordingly.
+        *
+        * @param array $languages
+        * @return array
+        */
+       public function convertForLanguageDetection(array $languages): array
+       {
+               $languages['fi'] = $languages['fi-fi'];
+               unset($languages['fi-fi']);
+               $languages['da'] = $languages['da-dk'];
+               unset($languages['da-dk']);
+               $languages['nb'] = $languages['nb-no'];
+               unset($languages['nb-no']);
+               $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);
+
+               return $languages;
+       }
+
        /**
         * Translate days and months names.
         *
index 1e16f85d9b39d1147a82a325ac9a80828fc492e0..e48bae77a619b2bced6a68f311e5b5f4d8bee861 100644 (file)
@@ -2015,11 +2015,8 @@ class Item
 
                $naked_body = self::getDominantLanguage($naked_body);
 
-               $availableLanguages = DI::l10n()->getAvailableLanguages();
-               // 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.
-               $availableLanguages['fa'] = 'fa';
+               $availableLanguages = DI::l10n()->getAvailableLanguages(true);
+               $availableLanguages = DI::l10n()->convertForLanguageDetection($availableLanguages);
 
                $ld = new Language(array_keys($availableLanguages));
                return $ld->detect($naked_body)->limit(0, $count)->close() ?: [];
index d6dfa3525361800e5174fdd40a44c139b2b35e22..43e2c067fce2a4cd2669ab9062c203a1b349f03f 100644 (file)
@@ -543,15 +543,12 @@ class User
        public static function getLanguageCode(int $uid, bool $short): string
        {
                $owner = self::getOwnerDataById($uid);
-               $languages = DI::l10n()->getAvailableLanguages();
+               $languages = DI::l10n()->getAvailableLanguages($short);
                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;
        }
 
index b870c91ac0b5ba92d75c41a53dfd1c1c682fd349..95dd4e78ffab776ea5f3fec5388ab3ad03f26df9 100644 (file)
@@ -219,7 +219,7 @@ class Display extends BaseSettings
                ];
 
                $channel_languages = $this->pConfig->get($uid, 'channel', 'languages', [User::getLanguageCode($uid, false)]);
-               $languages         = $this->l10n->getAvailableLanguages();
+               $languages         = $this->l10n->getAvailableLanguages(true);
 
                $first_day_of_week = $this->pConfig->get($uid, 'calendar', 'first_day_of_week', 0);
                $weekdays          = [