From c5ef0cfe20691ff33d36aaf5d6ba63ff27d698ed Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 3 Sep 2023 16:42:17 +0000 Subject: [PATCH] New function to change language codes for the library --- src/Core/L10n.php | 37 ++++++++++++++++++++++++++++++++- src/Model/Item.php | 7 ++----- src/Model/User.php | 5 +---- src/Module/Settings/Display.php | 2 +- 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/Core/L10n.php b/src/Core/L10n.php index 1d8cfa705b..362d6d7a4a 100644 --- a/src/Core/L10n.php +++ b/src/Core/L10n.php @@ -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. * diff --git a/src/Model/Item.php b/src/Model/Item.php index 1e16f85d9b..e48bae77a6 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -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() ?: []; diff --git a/src/Model/User.php b/src/Model/User.php index d6dfa35253..43e2c067fc 100644 --- a/src/Model/User.php +++ b/src/Model/User.php @@ -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; } diff --git a/src/Module/Settings/Display.php b/src/Module/Settings/Display.php index b870c91ac0..95dd4e78ff 100644 --- a/src/Module/Settings/Display.php +++ b/src/Module/Settings/Display.php @@ -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 = [ -- 2.39.5