From e7bd6e7663515a318c153e839d6a01f59113e767 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 15 Apr 2025 02:03:35 +0000 Subject: [PATCH 1/2] Issue 14881: Fixed language detection for danish --- src/Core/L10n.php | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Core/L10n.php b/src/Core/L10n.php index 79d83950a9..42949fa1fe 100644 --- a/src/Core/L10n.php +++ b/src/Core/L10n.php @@ -51,6 +51,11 @@ class L10n 'zh-cn' => '简体中文', ]; + CONST LANG_PARENTS = [ + 'en-gb' => 'en', 'da-dk' => 'da', 'fi-fi' => 'fi', + 'nb-no' => 'nb', 'pt-br' => 'pt', 'zh-cn' => 'zh' + ]; + /** @var string Undetermined language */ const UNDETERMINED_LANGUAGE = 'un'; @@ -150,6 +155,11 @@ class L10n $a = new \stdClass(); $a->strings = []; + $child = array_search($lang, $this::LANG_PARENTS); + if ($child) { + $lang = $child; + } + // load enabled addons strings $addons = array_keys($this->config->get('addons') ?? []); foreach ($addons as $addon) { @@ -203,6 +213,8 @@ class L10n // start with quality zero (every guessed language is more acceptable ..) $current_q = 0; + $supported = self::getSupportedLanguages(); + foreach ($acceptedLanguages as $acceptedLanguage) { $res = preg_match( '/^([a-z]{1,8}(?:-[a-z]{1,8})*)(?:;\s*q=(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?$/i', @@ -230,8 +242,7 @@ class L10n while (count($lang_code)) { // try to mix them so we can get double-code parts too $match_lang = strtolower(join('-', $lang_code)); - if (file_exists(__DIR__ . "/../../view/lang/$match_lang") && - is_dir(__DIR__ . "/../../view/lang/$match_lang")) { + if (in_array($match_lang, $supported)) { if ($lang_quality > $current_q) { $current_lang = $match_lang; $current_q = $lang_quality; @@ -247,6 +258,20 @@ class L10n return $current_lang; } + private static function getSupportedLanguages(): array + { + $languages = []; + foreach (glob('view/lang/*/strings.php') as $language) { + $code = str_replace(['view/lang/', '/strings.php'], [], $language); + if (!empty(self::LANG_PARENTS[$code])) { + $languages[] = self::LANG_PARENTS[$code]; + } + $languages[] = $code; + } + + return $languages; + } + /** * Return the localized version of the provided string with optional string interpolation * From cc4ff9584ff1b6e32ac34ef8989d9b3686b5b799 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 15 Apr 2025 02:09:19 +0000 Subject: [PATCH 2/2] Fixed codestyle --- src/Core/L10n.php | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/Core/L10n.php b/src/Core/L10n.php index 42949fa1fe..d1af1c0e61 100644 --- a/src/Core/L10n.php +++ b/src/Core/L10n.php @@ -51,7 +51,7 @@ class L10n 'zh-cn' => '简体中文', ]; - CONST LANG_PARENTS = [ + const LANG_PARENTS = [ 'en-gb' => 'en', 'da-dk' => 'da', 'fi-fi' => 'fi', 'nb-no' => 'nb', 'pt-br' => 'pt', 'zh-cn' => 'zh' ]; @@ -427,8 +427,10 @@ class L10n ]; if (in_array('cld2', get_loaded_extensions())) { - $additional_langs = array_merge($additional_langs, - ['dv', 'kn', 'lo', 'ml', 'or', 'pa', 'sd', 'si', 'te', 'yi']); + $additional_langs = array_merge( + $additional_langs, + ['dv', 'kn', 'lo', 'ml', 'or', 'pa', 'sd', 'si', 'te', 'yi'] + ); } $langs = array_merge($additional_langs, array_keys($this->getAvailableLanguages())); @@ -444,7 +446,7 @@ class L10n */ public function getLanguageCodes(bool $international = false): array { - $iso639 = new \Matriphe\ISO639\ISO639; + $iso639 = new \Matriphe\ISO639\ISO639(); // In ISO 639-2 undetermined languages have got the code "und". // There is no official code for ISO 639-1, but "un" is not assigned to any language. @@ -502,13 +504,17 @@ class L10n */ public function getDay(string $s): string { - $ret = str_replace(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'], + $ret = str_replace( + ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'], [$this->t('Monday'), $this->t('Tuesday'), $this->t('Wednesday'), $this->t('Thursday'), $this->t('Friday'), $this->t('Saturday'), $this->t('Sunday')], - $s); + $s + ); - $ret = str_replace(['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], + $ret = str_replace( + ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], [$this->t('January'), $this->t('February'), $this->t('March'), $this->t('April'), $this->t('May'), $this->t('June'), $this->t('July'), $this->t('August'), $this->t('September'), $this->t('October'), $this->t('November'), $this->t('December')], - $ret); + $ret + ); return $ret; } @@ -521,13 +527,17 @@ class L10n */ public function getDayShort(string $s): string { - $ret = str_replace(['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], + $ret = str_replace( + ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], [$this->t('Mon'), $this->t('Tue'), $this->t('Wed'), $this->t('Thu'), $this->t('Fri'), $this->t('Sat'), $this->t('Sun')], - $s); + $s + ); - $ret = str_replace(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], + $ret = str_replace( + ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], [$this->t('Jan'), $this->t('Feb'), $this->t('Mar'), $this->t('Apr'), $this->t('May'), $this->t('Jun'), $this->t('Jul'), $this->t('Aug'), $this->t('Sep'), $this->t('Oct'), $this->t('Nov'), $this->t('Dec')], - $ret); + $ret + ); return $ret; }