From: Michael <heluecht@pirati.ca>
Date: Sun, 3 Sep 2023 17:44:44 +0000 (+0000)
Subject: New functions for the language library
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=595508a91fb90113e4764707dab09f19648e384e;p=friendica.git

New functions for the language library
---

diff --git a/src/Core/L10n.php b/src/Core/L10n.php
index 362d6d7a4a..9d1fa70167 100644
--- a/src/Core/L10n.php
+++ b/src/Core/L10n.php
@@ -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.
 	 *
diff --git a/src/Model/User.php b/src/Model/User.php
index 43e2c067fc..b4df99d82c 100644
--- a/src/Model/User.php
+++ b/src/Model/User.php
@@ -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 {
diff --git a/src/Module/Conversation/Channel.php b/src/Module/Conversation/Channel.php
index dfe8871f16..eacbb2201e 100644
--- a/src/Module/Conversation/Channel.php
+++ b/src/Module/Conversation/Channel.php
@@ -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) . ")";
diff --git a/src/Module/Settings/Display.php b/src/Module/Settings/Display.php
index 95dd4e78ff..fa1496da6d 100644
--- a/src/Module/Settings/Display.php
+++ b/src/Module/Settings/Display.php
@@ -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);