From: Timo Tijhof Date: Mon, 1 Jun 2015 23:04:26 +0000 (+0100) Subject: language: Use item 'fallbackSequence' instead of duplicating logic X-Git-Tag: 1.31.0-rc.0~11178^2 X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/exercices/modifier.php?a=commitdiff_plain;h=dd587fb99cb24f0e2db4ca8a4107e1da5ba6ba3a;p=lhc%2Fweb%2Fwiklou.git language: Use item 'fallbackSequence' instead of duplicating logic The 'fallbackSequence' is exactly generated for this purpose. It is equal to the value of 'fallback' after splitting, trimming and ensuring 'en' is present. See LocalisationCache::recache(). Also simplify returning of the first array index by returning it directly instead of modifying the array first. Due to an inconsistency between how LocalisationCache and Language classes treat the fallback sequence differently, we have to manually fallback to 'en' in case of unknown language codes. This is because otherwise Language::factory() will throw an exception causing tests to fail. We should investigate whether this is desirable or not, but keeping existing behaviour for now and documenting it. Change-Id: I9c1d51b59aabebf5a31f38205304bb8cc22dcd8c --- diff --git a/languages/Language.php b/languages/Language.php index d19dc25373..0634d9fc0f 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -4396,8 +4396,7 @@ class Language { return false; } else { $fallbacks = self::getFallbacksFor( $code ); - $first = array_shift( $fallbacks ); - return $first; + return $fallbacks[0]; } } @@ -4406,19 +4405,15 @@ class Language { * * @since 1.19 * @param string $code Language code - * @return array + * @return array Non-empty array, ending in "en" */ public static function getFallbacksFor( $code ) { if ( $code === 'en' || !Language::isValidBuiltInCode( $code ) ) { return array(); - } else { - $v = self::getLocalisationCache()->getItem( $code, 'fallback' ); - $v = array_map( 'trim', explode( ',', $v ) ); - if ( $v[count( $v ) - 1] !== 'en' ) { - $v[] = 'en'; - } - return $v; } + // For unknown languages, fallbackSequence returns an empty array, + // hardcode fallback to 'en' in that case. + return self::getLocalisationCache()->getItem( $code, 'fallbackSequence' ) ?: array( 'en' ); } /**